From 963a3601284b033ce8ae23a6c22789c8d1245d7f Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Tue, 13 Sep 2016 16:50:00 +0200 Subject: HONEYCOMB-194 Raise unit test coverage of translation-util to 80% Change-Id: I6689f6d1f6a4a1376465783dad2650f5137142d2 Signed-off-by: Maros Marsalek --- .../util/read/ReflexiveReaderCustomizerTest.java | 200 +++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java (limited to 'infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java') diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java new file mode 100644 index 000000000..7fb67be6e --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java @@ -0,0 +1,200 @@ +/* + * 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(); + } + } +} \ No newline at end of file -- cgit 1.2.3-korg