/* * Copyright (c) 2016 Cisco and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.fd.honeycomb.translate.util.read; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verifyZeroInteractions; import com.google.common.collect.Lists; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import java.util.Collections; import java.util.List; import javax.annotation.Nonnull; import org.junit.Test; import org.mockito.Mock; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.DataContainer; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Identifiable; import org.opendaylight.yangtools.yang.binding.Identifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public class ReflexiveReaderCustomizerTest { @Mock private ReadContext ctx; private InstanceIdentifier id = InstanceIdentifier.create(TestingDataObject.class); @Test public void testReflexCustomizer() throws Exception { final ReflexiveReaderCustomizer reflexReaderCustomizer = new ReflexiveReaderCustomizer<>(TestingDataObject.class, TestingBuilder.class); assertEquals(TestingDataObject.class, reflexReaderCustomizer.getTypeClass()); assertEquals(TestingBuilder.class, reflexReaderCustomizer.getBuilderClass()); assertNotNull(reflexReaderCustomizer.getBuilder(id)); // NOOP final TestingBuilder builder = spy(new TestingBuilder()); reflexReaderCustomizer.readCurrentAttributes(id, builder, ctx); verifyZeroInteractions(builder); final TestingBuilderParent parentBuilder = new TestingBuilderParent(); final TestingDataObject readValue = new TestingDataObject(); reflexReaderCustomizer.merge(parentBuilder, readValue); assertEquals(readValue, parentBuilder.getTestingDataObject()); } @Test public void testReflexCustomizerAugment() throws Exception { final ReflexiveReaderCustomizer reflexReaderCustomizer = new ReflexiveReaderCustomizer<>(TestingAugmentation.class, TestingAugmentBuilder.class); final TestingBuilderParent parentBuilder = new TestingBuilderParent(); final TestingAugmentation readValue = new TestingAugmentation(); reflexReaderCustomizer.merge(parentBuilder, readValue); assertEquals(readValue, parentBuilder.getAugmentation()); } @Test public void testReflexCustomizerList() throws Exception { final ReflexiveListReaderCustomizer reflexReaderCustomizer = new ReflexiveListReaderCustomizer (TestingListObject.class, TestingListBuilder.class) { @Nonnull @Override public List getAllIds( @Nonnull final InstanceIdentifier id, @Nonnull final ReadContext context) throws ReadFailedException { return Lists.newArrayList(); } }; final TestingBuilderParent parentBuilder = new TestingBuilderParent(); final List readValue = Lists.newArrayList(new TestingListObject(), new TestingListObject()); reflexReaderCustomizer.merge(parentBuilder, readValue); assertEquals(readValue, parentBuilder.getTestingListObject()); final TestingListObject single = new TestingListObject(); reflexReaderCustomizer.merge(parentBuilder, single); assertEquals(Collections.singletonList(single), parentBuilder.getTestingListObject()); } static class TestingDataObject implements DataObject { @Override public Class getImplementedInterface() { return DataObject.class; } } static class TestingAugmentation implements DataObject, Augmentation { @Override public Class getImplementedInterface() { return DataObject.class; } } static class TestingListObject implements DataObject, Identifiable { @Override public Class getImplementedInterface() { return DataObject.class; } @Override public TestingListKey getKey() { return new TestingListKey(); } static class TestingListKey implements Identifier { } } static class TestingBuilder implements Builder { @Override public TestingDataObject build() { return new TestingDataObject(); } } static class TestingAugmentBuilder implements Builder { @Override public TestingAugmentation build() { return new TestingAugmentation(); } } static class TestingListBuilder implements Builder { @Override public TestingListObject build() { return new TestingListObject(); } } static class TestingBuilderParent implements Builder { private TestingDataObject object; private Augmentation augmentation; private List listObjects; public TestingBuilderParent setTestingDataObject(@Nonnull final TestingDataObject object) { this.object = object; return this; } public TestingDataObject getTestingDataObject() { return object; } public TestingBuilderParent setTestingListObject(@Nonnull final List listObjects) { this.listObjects = listObjects; return this; } public List getTestingListObject() { return listObjects; } public TestingBuilderParent addAugmentation(Class> augmentationType, Augmentation augmentation) { this.augmentation = augmentation; return this; } public Augmentation getAugmentation() { return augmentation; } @Override public TestingDataObject build() { return new TestingDataObject(); } } }