From 487f6c742ba4b23d3b738b89dd2f7efb45b2693a Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 9 Jan 2017 08:23:45 +0100 Subject: HONEYCOMB-331: unify initialization handling Due to incorrect instance of check, InitSubtreeReader was never created in CompositeReaderRegistryBuilder.getSubtreeHandler(). As a consequence, initializers registered by subtreeAdd*, were not invoked. The patch: - fixes mentioned check, - makes readers implement InitReader instead of directly implementing Initializer - includes missing unit tests for GenericReaders. Change-Id: I93be59fafddb60dce00191958b5c8c62e7c2d289 Signed-off-by: Marek Gradzki --- .../honeycomb/translate/read/InitListReader.java | 2 +- .../translate/impl/read/GenericInitReader.java | 4 +- .../impl/read/registry/CompositeReader.java | 3 +- .../registry/CompositeReaderRegistryBuilder.java | 5 +- .../impl/read/registry/InitSubtreeReader.java | 7 +- .../impl/read/AbstractListReaderTest.java | 140 +++++++++++++++++++++ .../translate/impl/read/AbstractReaderTest.java | 98 +++++++++++++++ .../impl/read/GenericInitListReaderTest.java | 89 +++++++++++++ .../translate/impl/read/GenericIntReaderTest.java | 86 +++++++++++++ .../translate/impl/read/GenericListReaderTest.java | 101 +-------------- .../translate/impl/read/GenericReaderTest.java | 58 +-------- .../impl/read/registry/InitSubtreeReaderTest.java | 94 ++++++++++++++ 12 files changed, 530 insertions(+), 157 deletions(-) create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractListReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericInitListReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericIntReaderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReaderTest.java diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java index a7d3ee2ac..f137aacaa 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java @@ -27,5 +27,5 @@ import org.opendaylight.yangtools.yang.binding.Identifier; */ @Beta public interface InitListReader, K extends Identifier, B extends Builder> - extends ListReader, Initializer { + extends ListReader, InitReader { } diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java index 024e0a815..e55bc8ab6 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java @@ -20,7 +20,7 @@ import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastor import com.google.common.base.Optional; import io.fd.honeycomb.translate.read.InitFailedException; -import io.fd.honeycomb.translate.read.Initializer; +import io.fd.honeycomb.translate.read.InitReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.Initialized; @@ -36,7 +36,7 @@ import org.slf4j.LoggerFactory; public final class GenericInitReader> extends GenericReader - implements Initializer { + implements InitReader { private static final Logger LOG = LoggerFactory.getLogger(GenericInitReader.class); diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java index ec82b3f7f..2eca8954c 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReader.java @@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import io.fd.honeycomb.translate.read.InitFailedException; import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.InitReader; import io.fd.honeycomb.translate.read.Initializer; import io.fd.honeycomb.translate.read.ListReader; import io.fd.honeycomb.translate.read.ReadContext; @@ -47,7 +48,7 @@ import org.slf4j.LoggerFactory; class CompositeReader> extends AbstractGenericReader - implements Initializer, DelegatingReader { + implements InitReader, DelegatingReader { private static final Logger LOG = LoggerFactory.getLogger(CompositeReader.class); diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java index 290c5d4aa..36ab90995 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/CompositeReaderRegistryBuilder.java @@ -19,6 +19,7 @@ package io.fd.honeycomb.translate.impl.read.registry; import com.google.common.collect.ImmutableMap; import io.fd.honeycomb.translate.impl.read.GenericReader; import io.fd.honeycomb.translate.read.InitReader; +import io.fd.honeycomb.translate.read.Initializer; import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; import io.fd.honeycomb.translate.read.registry.ReaderRegistry; @@ -46,8 +47,8 @@ public final class CompositeReaderRegistryBuilder @Override protected Reader> getSubtreeHandler(@Nonnull final Set> handledChildren, @Nonnull final Reader> reader) { - return reader instanceof InitReader - ? InitSubtreeReader.createForReader(handledChildren, reader) + return reader instanceof Initializer + ? InitSubtreeReader.createForReader(handledChildren, (InitReader)reader) : SubtreeReader.createForReader(handledChildren, reader); } diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java index 809cdb214..e7cef8107 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReader.java @@ -21,7 +21,6 @@ import io.fd.honeycomb.translate.read.InitListReader; import io.fd.honeycomb.translate.read.InitReader; import io.fd.honeycomb.translate.read.ListReader; import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; import java.util.Set; import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.binding.api.DataBroker; @@ -48,11 +47,11 @@ class InitSubtreeReader> /** * Wrap a Reader as an initializing subtree Reader. */ - static > Reader createForReader(@Nonnull final Set> handledChildren, - @Nonnull final Reader reader) { + static > InitReader createForReader(@Nonnull final Set> handledChildren, + @Nonnull final InitReader reader) { return (reader instanceof ListReader) ? new InitSubtreeListReader<>((InitListReader) reader, handledChildren) - : new InitSubtreeReader<>(((InitReader) reader), handledChildren); + : new InitSubtreeReader<>(reader, handledChildren); } private static class InitSubtreeListReader, B extends Builder, K extends Identifier> diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractListReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractListReaderTest.java new file mode 100644 index 000000000..5c08ce237 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractListReaderTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2017 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.impl.read; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.concepts.Builder; +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 abstract class AbstractListReaderTest { + + protected static final InstanceIdentifier + DATA_OBJECT_ID = InstanceIdentifier.create(TestingData.class); + + private final Class customizerClass; + + @Mock + protected Builder builder; + @Mock + protected TestingData data; + @Mock + protected ReadContext ctx; + private List keys = Lists.newArrayList(new TestingData.TestingKey(), + new TestingData.TestingKey()); + + private ListReaderCustomizer> customizer; + private GenericListReader> reader; + + protected AbstractListReaderTest(final Class customizerClass) { + this.customizerClass = customizerClass; + } + + @SuppressWarnings("unchecked") + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + customizer = mock(customizerClass); + when(customizer.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); + when(customizer.getAllIds(DATA_OBJECT_ID, ctx)).thenReturn(keys); + reader = initReader(); + when(builder.build()).thenReturn(data); + } + + protected abstract GenericListReader> initReader(); + + public GenericListReader> getReader() { + return reader; + } + + protected ListReaderCustomizer> getCustomizer() { + return customizer; + } + + @Test + public void testGetBuilder() throws Exception { + assertEquals(builder, reader.getBuilder(DATA_OBJECT_ID)); + verify(customizer).getBuilder(DATA_OBJECT_ID); + } + + @Test + public void testManagedType() throws Exception { + assertEquals(DATA_OBJECT_ID, reader.getManagedDataObjectType()); + } + + @Test + public void testMerge() throws Exception { + reader.merge(builder, data); + verify(customizer).merge(builder, data); + } + + @Test + public void testAllIds() throws Exception { + assertEquals(keys, reader.getAllIds(DATA_OBJECT_ID, ctx)); + verify(customizer).getAllIds(DATA_OBJECT_ID, ctx); + } + + @Test + public void testRead() throws Exception { + reader.read(DATA_OBJECT_ID, ctx); + + verify(customizer).getBuilder(DATA_OBJECT_ID); + verify(customizer).readCurrentAttributes(DATA_OBJECT_ID, builder, ctx); + } + + @SuppressWarnings("unchecked") + @Test + public void testReadList() throws Exception { + reader.readList(DATA_OBJECT_ID, ctx); + + verify(customizer, times(2)).getBuilder(any(InstanceIdentifier.class)); + verify(customizer, times(2)) + .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); + } + + static class TestingData implements DataObject, Identifiable { + + @Override + public Class getImplementedInterface() { + return DataObject.class; + } + + @Override + public TestingKey getKey() { + return new TestingKey(); + } + + static class TestingKey implements Identifier {} + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractReaderTest.java new file mode 100644 index 000000000..cac321764 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/AbstractReaderTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2017 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.impl.read; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public abstract class AbstractReaderTest { + + protected static final InstanceIdentifier + DATA_OBJECT_ID = InstanceIdentifier.create(DataObject.class); + private final Class customizerClass; + + @Mock + protected Builder builder; + @Mock + protected DataObject data; + @Mock + protected ReadContext ctx; + + private ReaderCustomizer> customizer; + private GenericReader> reader; + + protected AbstractReaderTest(final Class customizerClass) { + this.customizerClass = customizerClass; + } + + @SuppressWarnings("unchecked") + @Before + public final void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + customizer = mock(customizerClass); + when(customizer.getBuilder(DATA_OBJECT_ID)).thenReturn(builder); + reader = initReader(); + when(builder.build()).thenReturn(data); + } + + protected abstract GenericReader> initReader(); + + protected GenericReader> getReader() { + return reader; + } + + protected ReaderCustomizer> getCustomizer() { + return customizer; + } + + @Test + public void testGetBuilder() throws Exception { + assertEquals(builder, reader.getBuilder(DATA_OBJECT_ID)); + verify(customizer).getBuilder(DATA_OBJECT_ID); + } + + @Test + public void testManagedType() throws Exception { + assertEquals(DATA_OBJECT_ID, reader.getManagedDataObjectType()); + } + + @Test + public void testMerge() throws Exception { + reader.merge(builder, data); + verify(customizer).merge(builder, data); + } + + @Test + public void testRead() throws Exception { + reader.read(DATA_OBJECT_ID, ctx); + + verify(customizer).getBuilder(DATA_OBJECT_ID); + verify(customizer).readCurrentAttributes(DATA_OBJECT_ID, builder, ctx); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericInitListReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericInitListReaderTest.java new file mode 100644 index 000000000..f695e66d7 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericInitListReaderTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2017 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.impl.read; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yangtools.concepts.Builder; + +public class GenericInitListReaderTest extends AbstractListReaderTest { + + @Mock + private DataBroker broker; + @Mock + private WriteTransaction writeTx; + + public GenericInitListReaderTest() { + super(InitializingListReaderCustomizer.class); + } + + @Override + protected GenericListReader> initReader() { + return new GenericInitListReader<>(DATA_OBJECT_ID, getCustomizer()); + } + + @Override + protected InitializingListReaderCustomizer> getCustomizer() { + return (InitializingListReaderCustomizer>) super + .getCustomizer(); + } + + @Override + public GenericInitListReader> getReader() { + return (GenericInitListReader>) super + .getReader(); + } + + @SuppressWarnings("unchecked") + @Test + public void testInit() throws Exception { + final Initialized initialized = Initialized.create(DATA_OBJECT_ID, data); + when(getCustomizer().isPresent(any(), any(), any())).thenReturn(true); + doReturn(initialized).when(getCustomizer()).init(any(), any(), any()); + when(broker.newWriteOnlyTransaction()).thenReturn(writeTx); + + getReader().init(broker, DATA_OBJECT_ID, ctx); + + verify(writeTx, times(2)).merge(LogicalDatastoreType.CONFIGURATION, DATA_OBJECT_ID, data, true); + verify(writeTx, times(2)).submit(); + } + + @Test(expected = InitFailedException.class) + public void testInitFailed() throws Exception { + doThrow(new ReadFailedException(DATA_OBJECT_ID)).when(getCustomizer()) + .readCurrentAttributes(any(), any(), any()); + + getReader().init(broker, DATA_OBJECT_ID, ctx); + + verifyZeroInteractions(writeTx); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericIntReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericIntReaderTest.java new file mode 100644 index 000000000..ef3de457f --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericIntReaderTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017 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.impl.read; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; + +public class GenericIntReaderTest extends AbstractReaderTest { + + @Mock + private DataBroker broker; + @Mock + private WriteTransaction writeTx; + + public GenericIntReaderTest() { + super(InitializingReaderCustomizer.class); + } + + @Override + protected GenericReader> initReader() { + return new GenericInitReader<>(DATA_OBJECT_ID, getCustomizer()); + } + + @Override + public GenericInitReader> getReader() { + return (GenericInitReader>)super.getReader(); + } + + @Override + public InitializingReaderCustomizer> getCustomizer() { + return (InitializingReaderCustomizer>)super.getCustomizer(); + } + + @Test + public void testInit() throws Exception { + final Initialized initialized = Initialized.create(DATA_OBJECT_ID, data); + + when(getCustomizer().isPresent(DATA_OBJECT_ID, data, ctx)).thenReturn(true); + doReturn(initialized).when(getCustomizer()).init(DATA_OBJECT_ID, data, ctx); + when(broker.newWriteOnlyTransaction()).thenReturn(writeTx); + + getReader().init(broker, DATA_OBJECT_ID, ctx); + + verify(writeTx).merge(LogicalDatastoreType.CONFIGURATION, DATA_OBJECT_ID, data, true); + verify(writeTx).submit(); + } + + @Test(expected = InitFailedException.class) + public void testInitFailed() throws Exception { + doThrow(new ReadFailedException(DATA_OBJECT_ID)).when(getCustomizer()) + .readCurrentAttributes(DATA_OBJECT_ID, builder, ctx); + + getReader().init(broker, DATA_OBJECT_ID, ctx); + + verifyZeroInteractions(writeTx); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java index 3ed400dba..d3959ed3c 100644 --- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericListReaderTest.java @@ -16,106 +16,17 @@ package io.fd.honeycomb.translate.impl.read; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.collect.Lists; -import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.opendaylight.yangtools.concepts.Builder; -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 GenericListReaderTest { - - private static final InstanceIdentifier - DATA_OBJECT_ID = InstanceIdentifier.create(TestingData.class); - @Mock - private ListReaderCustomizer> customizer; - @Mock - private Builder builder; - @Mock - private TestingData data; - @Mock - private ReadContext ctx; - private GenericListReader> reader; - private List keys = Lists.newArrayList(new TestingData.TestingKey(), - new TestingData.TestingKey()); - - @SuppressWarnings("unchecked") - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - when(customizer.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); - when(customizer.getAllIds(DATA_OBJECT_ID, ctx)).thenReturn(keys); - reader = new GenericListReader<>(DATA_OBJECT_ID, customizer); - when(builder.build()).thenReturn(data); - } - @Test - public void testGetBuilder() throws Exception { - assertEquals(builder, reader.getBuilder(DATA_OBJECT_ID)); - verify(customizer).getBuilder(DATA_OBJECT_ID); - } - - @Test - public void testManagedType() throws Exception { - assertEquals(DATA_OBJECT_ID, reader.getManagedDataObjectType()); - } - - @Test - public void testMerge() throws Exception { - reader.merge(builder, data); - verify(customizer).merge(builder, data); - } +public class GenericListReaderTest extends AbstractListReaderTest { - @Test - public void testAllIds() throws Exception { - assertEquals(keys, reader.getAllIds(DATA_OBJECT_ID, ctx)); - verify(customizer).getAllIds(DATA_OBJECT_ID, ctx); + public GenericListReaderTest() { + super(ListReaderCustomizer.class); } - @Test - public void testRead() throws Exception { - reader.read(DATA_OBJECT_ID, ctx); - - verify(customizer).getBuilder(DATA_OBJECT_ID); - verify(customizer).readCurrentAttributes(DATA_OBJECT_ID, builder, ctx); - } - - @SuppressWarnings("unchecked") - @Test - public void testReadList() throws Exception { - reader.readList(DATA_OBJECT_ID, ctx); - - verify(customizer, times(2)).getBuilder(any(InstanceIdentifier.class)); - verify(customizer, times(2)) - .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); - } - - static class TestingData implements DataObject, Identifiable { - - @Override - public Class getImplementedInterface() { - return DataObject.class; - } - - @Override - public TestingKey getKey() { - return new TestingKey(); - } - - static class TestingKey implements Identifier {} + @Override + protected GenericListReader> initReader() { + return new GenericListReader<>(DATA_OBJECT_ID, getCustomizer()); } } \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericReaderTest.java index 02baab2eb..7b999bec0 100644 --- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericReaderTest.java +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/GenericReaderTest.java @@ -16,64 +16,18 @@ package io.fd.honeycomb.translate.impl.read; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class GenericReaderTest { - private static final InstanceIdentifier - DATA_OBJECT_ID = InstanceIdentifier.create(DataObject.class); - @Mock - private ReaderCustomizer> customizer; - @Mock - private Builder builder; - @Mock - private DataObject data; - @Mock - private ReadContext ctx; - private GenericReader> reader; +public class GenericReaderTest extends AbstractReaderTest { - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - when(customizer.getBuilder(DATA_OBJECT_ID)).thenReturn(builder); - reader = new GenericReader<>(DATA_OBJECT_ID, customizer); - when(builder.build()).thenReturn(data); + public GenericReaderTest() { + super(ReaderCustomizer.class); } - @Test - public void testGetBuilder() throws Exception { - assertEquals(builder, reader.getBuilder(DATA_OBJECT_ID)); - verify(customizer).getBuilder(DATA_OBJECT_ID); - } - - @Test - public void testManagedType() throws Exception { - assertEquals(DATA_OBJECT_ID, reader.getManagedDataObjectType()); - } - - @Test - public void testMerge() throws Exception { - reader.merge(builder, data); - verify(customizer).merge(builder, data); - } - - @Test - public void testRead() throws Exception { - reader.read(DATA_OBJECT_ID, ctx); - - verify(customizer).getBuilder(DATA_OBJECT_ID); - verify(customizer).readCurrentAttributes(DATA_OBJECT_ID, builder, ctx); + @Override + protected GenericReader> initReader() { + return new GenericReader<>(DATA_OBJECT_ID, getCustomizer()); } } \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReaderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReaderTest.java new file mode 100644 index 000000000..a9b15ad32 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/read/registry/InitSubtreeReaderTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2017 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.impl.read.registry; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.InitReader; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.ChildOf; +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 InitSubtreeReaderTest { + + @Mock + private DataBroker broker; + @Mock + private InitReader> delegate; + @Mock + private InitListReader> listDelegate; + @Mock + private ReadContext ctx; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(DataObject1.IID).when(delegate).getManagedDataObjectType(); + doReturn(DataObject2.IID).when(listDelegate).getManagedDataObjectType(); + } + + @Test + public void testInit() throws Exception { + final InitReader> initSubReader = + InitSubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegate); + assertFalse(initSubReader instanceof ListReader); + + initSubReader.init(broker, DataObject1.IID, ctx); + verify(delegate).init(broker, DataObject1.IID, ctx); + } + + @Test + public void testInitList() throws Exception { + final InitReader> initSubReader = + InitSubtreeReader.createForReader(Sets.newHashSet(DataObject2.DataObject22.IID), listDelegate); + assertTrue(initSubReader instanceof ListReader); + + initSubReader.init(broker, DataObject2.IID, ctx); + verify(listDelegate).init(broker, DataObject2.IID, ctx); + } + + private abstract static class DataObject1 implements DataObject { + private static InstanceIdentifier IID = InstanceIdentifier.create(DataObject1.class); + private abstract static class DataObject11 implements DataObject, ChildOf { + private static InstanceIdentifier IID = DataObject1.IID.child(DataObject11.class); + } + } + + private abstract static class DataObject2 implements Identifiable, DataObject { + private static InstanceIdentifier IID = InstanceIdentifier.create(DataObject2.class); + abstract static class DataObject2Key implements Identifier { + } + private abstract static class DataObject22 implements DataObject, ChildOf { + public static InstanceIdentifier IID = DataObject2.IID.child(DataObject22.class); + } + } +} \ No newline at end of file -- cgit 1.2.3-korg