summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry
diff options
context:
space:
mode:
Diffstat (limited to 'infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry')
-rw-r--r--infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java112
-rw-r--r--infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java124
2 files changed, 236 insertions, 0 deletions
diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java
new file mode 100644
index 000000000..06cb8498f
--- /dev/null
+++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.registry;
+
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject3;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject3.DataObject31;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject4;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.Reader;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InOrder;
+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 CompositeReaderRegistryTest {
+
+ @Mock
+ private ReadContext ctx;
+ private CompositeReaderRegistry reg;
+ private Reader<DataObject31, Builder<DataObject31>> reader31;
+ private Reader<DataObject3, Builder<DataObject3>> rootReader3;
+ private Reader<DataObject41, Builder<DataObject41>> reader41;
+ private Reader<DataObject4, Builder<DataObject4>> rootReader4;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ reader31 = mockReader(DataObject31.class);
+ rootReader3 =
+ spy(CompositeReader.createForReader(
+ mockReader(DataObject3.class),
+ ImmutableMap.of(DataObject31.class, reader31)));
+
+ reader41 = mockReader(DataObject41.class);
+ rootReader4 =
+ spy(CompositeReader.createForReader(
+ mockReader(DataObject4.class), ImmutableMap.of(
+ DataObject41.class, reader41)));
+
+ reg = new CompositeReaderRegistry(Lists.newArrayList(rootReader3, rootReader4));
+ }
+
+ @Test
+ public void testReadAll() throws Exception {
+ reg.readAll(ctx);
+
+ // Invoked according to composite ordering
+ final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41);
+ inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ inOrder.verify(rootReader4).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ inOrder.verify(reader41).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ }
+
+ @Test
+ public void testReadSingleRoot() throws Exception {
+ reg.read(DataObject3.IID, ctx);
+
+ // Invoked according to composite ordering
+ final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41);
+ inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class));
+
+ // Only subtree under DataObject3 should be read
+ verify(rootReader4, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ }
+
+ @SuppressWarnings("unchecked")
+ static <D extends DataObject, B extends Builder<D>> Reader<D, B> mockReader(final Class<D> dataType)
+ throws Exception {
+ final Reader r = mock(Reader.class);
+ final Object iid = dataType.getDeclaredField("IID").get(null);
+ when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid);
+ final Builder builder = mock(Builder.class);
+ when(builder.build()).thenReturn(mock(dataType));
+ when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder);
+ when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType)));
+ return (Reader<D, B>) r;
+ }
+} \ No newline at end of file
diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java
new file mode 100644
index 000000000..9ae036013
--- /dev/null
+++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.registry;
+
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject4;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObjectK;
+import static io.fd.honeycomb.translate.util.DataObjects.DataObjectKey;
+import static io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryTest.mockReader;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+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.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import io.fd.honeycomb.translate.read.ListReader;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.Reader;
+import io.fd.honeycomb.translate.util.DataObjects;
+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.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class CompositeReaderTest {
+
+ @Mock
+ private ReadContext ctx;
+ private Reader<DataObject41, Builder<DataObject41>> reader41;
+ private Reader<DataObject4, Builder<DataObject4>> reader4;
+ private Reader<DataObject4, Builder<DataObject4>> compositeReader;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ reader41 = mockReader(DataObject41.class);
+ reader4 = mockReader(DataObject4.class);
+ compositeReader = CompositeReader
+ .createForReader(reader4, ImmutableMap.of(DataObject41.class, reader41));
+ }
+
+ @Test
+ public void testReadCurrent() throws Exception {
+ compositeReader.read(DataObject4.IID, ctx);
+ verify(reader4).readCurrentAttributes(eq(DataObject4.IID), any(Builder.class), eq(ctx));
+ verify(reader41).read(DataObject41.IID, ctx);
+ }
+
+ @Test
+ public void testReadJustChild() throws Exception {
+ // Delegating read to child
+ compositeReader.read(DataObject41.IID, ctx);
+ verify(reader4, times(0))
+ .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class));
+ verify(reader41).read(DataObject41.IID, ctx);
+ }
+
+ @Test
+ public void testReadFallback() throws Exception {
+ // Delegating read to delegate as a fallback since IID does not fit, could be handled by the delegate if its
+ // a subtree handler
+ compositeReader.read(DataObjects.DataObject4.DataObject42.IID, ctx);
+ verify(reader4).read(DataObjects.DataObject4.DataObject42.IID, ctx);
+ verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class));
+ }
+
+ @Test
+ public void testList() throws Exception {
+ final Reader<DataObjectK.DataObjectK1, Builder<DataObjectK.DataObjectK1>> readerK1 =
+ mockReader(DataObjectK.DataObjectK1.class);
+ final ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>> readerK =
+ mockListReader(DataObjectK.class, Lists.newArrayList(new DataObjectKey(), new DataObjectKey()));
+ final ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>>
+ compositeReaderK = (ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>>)
+ CompositeReader.createForReader(readerK, ImmutableMap.of(DataObject41.class, readerK1));
+
+ compositeReaderK.readList(DataObjectK.IID, ctx);
+
+ verify(readerK).getAllIds(DataObjectK.IID, ctx);
+ verify(readerK, times(2))
+ .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class));
+ }
+
+ @SuppressWarnings("unchecked")
+ static <D extends DataObject & Identifiable<K>, K extends Identifier<D>, B extends Builder<D>> ListReader<D, K, B> mockListReader(
+ final Class<D> dataType, List<K> keys)
+ throws Exception {
+ final ListReader r = mock(ListReader.class);
+ final Object iid = dataType.getDeclaredField("IID").get(null);
+ when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid);
+ final Builder builder = mock(Builder.class);
+ when(builder.build()).thenReturn(mock(dataType));
+ when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder);
+ when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType)));
+ when(r.getAllIds(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(keys);
+ return (ListReader<D, K, B>) r;
+ }
+
+} \ No newline at end of file