From 757222979bc02d0aaba1870eea36413383d15bde Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Tue, 8 Nov 2016 10:13:36 +0100 Subject: HONEYCOMB-270 Add isPresent() to Readers/Customizers So that they can influence whether empty data is to be considered as present + Move registries implementations from util to impl + Introduce DelegatingReader trait + Extend GenericReader where possible to reduce duplication Change-Id: I5a416acd0c4eab1fbc30fcbe585719991dbe9215 Signed-off-by: Maros Marsalek --- .../CompositeReaderRegistryBuilderTest.java | 114 ------------------- .../read/registry/CompositeReaderRegistryTest.java | 112 ------------------- .../util/read/registry/CompositeReaderTest.java | 124 --------------------- .../util/read/registry/SubtreeReaderTest.java | 124 --------------------- .../util/read/registry/TypeHierarchyTest.java | 72 ------------ 5 files changed, 546 deletions(-) delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java (limited to 'infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry') diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java deleted file mode 100644 index d742575be..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryBuilderTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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 org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.read.registry.ReaderRegistry; -import io.fd.honeycomb.translate.util.DataObjects; -import java.util.List; -import java.util.Map; -import org.junit.Test; -import org.mockito.Mockito; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class CompositeReaderRegistryBuilderTest { - - private Reader> reader1 = - mock(DataObjects.DataObject1.class); - private Reader> reader2 = - mock(DataObjects.DataObject2.class); - private Reader> reader3 = - mock(DataObjects.DataObject3.class); - private Reader> reader31 = - mock(DataObjects.DataObject3.DataObject31.class); - - private Reader> reader4 = - mock(DataObjects.DataObject4.class); - private Reader> reader41 = - mock(DataObjects.DataObject4.DataObject41.class); - private Reader> reader411 = - mock(DataObjects.DataObject4.DataObject41.DataObject411.class); - private Reader> reader42 = - mock(DataObjects.DataObject4.DataObject42.class); - - @SuppressWarnings("unchecked") - private Reader> mock(final Class dataObjectType) { - final Reader> mock = Mockito.mock(Reader.class); - try { - when(mock.getManagedDataObjectType()) - .thenReturn(((InstanceIdentifier) dataObjectType.getDeclaredField("IID").get(null))); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - return mock; - } - - @Test - public void testCompositeStructure() throws Exception { - final CompositeReaderRegistryBuilder compositeReaderRegistryBuilder = new CompositeReaderRegistryBuilder(); - /* - Composite reader structure ordered left from right - - 1, 2, 3, 4 - 31 42, 41 - 411 - */ - compositeReaderRegistryBuilder.add(reader1); - compositeReaderRegistryBuilder.addAfter(reader2, reader1.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader3, reader2.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader31, reader1.getManagedDataObjectType()); - compositeReaderRegistryBuilder.addAfter(reader4, reader3.getManagedDataObjectType()); - compositeReaderRegistryBuilder.add(reader41); - compositeReaderRegistryBuilder.addBefore(reader42, reader41.getManagedDataObjectType()); - compositeReaderRegistryBuilder.add(reader411); - - final ReaderRegistry build = compositeReaderRegistryBuilder.build(); - - final Map, Reader>> rootReaders = - ((CompositeReaderRegistry) build).getRootReaders(); - final List> rootReaderOrder = Lists.newArrayList(rootReaders.keySet()); - - assertEquals(reader1.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(0)); - assertEquals(reader2.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(1)); - assertEquals(reader3.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(2)); - assertEquals(reader4.getManagedDataObjectType().getTargetType(), rootReaderOrder.get(3)); - - assertFalse(rootReaders.get(DataObjects.DataObject1.class) instanceof CompositeReader); - assertFalse(rootReaders.get(DataObjects.DataObject2.class) instanceof CompositeReader); - assertTrue(rootReaders.get(DataObjects.DataObject3.class) instanceof CompositeReader); - assertTrue(rootReaders.get(DataObjects.DataObject4.class) instanceof CompositeReader); - - final ImmutableMap, Reader>> childReaders = - ((CompositeReader>) rootReaders - .get(DataObjects.DataObject4.class)).getChildReaders(); - final List> orderedChildReaders = Lists.newArrayList(childReaders.keySet()); - - assertEquals(reader42.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(0)); - assertEquals(reader41.getManagedDataObjectType().getTargetType(), orderedChildReaders.get(1)); - assertTrue(childReaders.get(DataObjects.DataObject4.DataObject41.class) instanceof CompositeReader); - assertFalse(childReaders.get(DataObjects.DataObject4.DataObject42.class) instanceof CompositeReader); - } -} \ No newline at end of file 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 deleted file mode 100644 index 06cb8498f..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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> reader31; - private Reader> rootReader3; - private Reader> reader41; - private Reader> 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 > Reader mockReader(final Class 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) 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 deleted file mode 100644 index 9ae036013..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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> reader41; - private Reader> reader4; - private Reader> 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> readerK1 = - mockReader(DataObjectK.DataObjectK1.class); - final ListReader> readerK = - mockListReader(DataObjectK.class, Lists.newArrayList(new DataObjectKey(), new DataObjectKey())); - final ListReader> - compositeReaderK = (ListReader>) - 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 , K extends Identifier, B extends Builder> ListReader mockListReader( - final Class dataType, List 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) r; - } - -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java deleted file mode 100644 index 799b9553f..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/SubtreeReaderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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 org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import com.google.common.base.Optional; -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.read.Reader; -import io.fd.honeycomb.translate.util.DataObjects; -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.ChildOf; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class SubtreeReaderTest { - - @Mock - private Reader> delegate; - @Mock - private Reader> delegateLocal; - @Mock - private ReadContext ctx; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - doReturn(DataObjects.DataObject4.IID).when(delegate).getManagedDataObjectType(); - doReturn(DataObject1.IID).when(delegateLocal).getManagedDataObjectType(); - } - - @Test - public void testCreate() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - subtreeR.getBuilder(DataObjects.DataObject4.IID); - verify(delegate).getBuilder(DataObjects.DataObject4.IID); - - subtreeR.getManagedDataObjectType(); - verify(delegate, atLeastOnce()).getManagedDataObjectType(); - } - - @Test(expected = IllegalArgumentException.class) - public void testCreateInvalid() throws Exception { - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject1.IID), delegate); - } - - @Test(expected = IllegalStateException.class) - public void testReadOnlySubtreeCannotFilter() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - doReturn(Optional.fromNullable(mock(DataObjects.DataObject4.class))).when(delegate).read(DataObjects.DataObject4.IID, ctx); - subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx); - } - - @Test - public void testReadOnlySubtreeNotPresent() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID), delegate); - - doReturn(Optional.absent()).when(delegate).read(DataObjects.DataObject4.IID, ctx); - assertFalse(subtreeR.read(DataObjects.DataObject4.DataObject41.IID, ctx).isPresent()); - } - - @Test - public void testReadOnlySubtreeChild() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); - - final DataObject1 mock = mock(DataObject1.class); - final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); - doReturn(mock11).when(mock).getDataObject11(); - doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); - assertEquals(mock11, subtreeR.read(DataObject1.DataObject11.IID, ctx).get()); - } - - @Test - public void testReadEntireSubtree() throws Exception { - final Reader> subtreeR = - SubtreeReader.createForReader(Sets.newHashSet(DataObject1.DataObject11.IID), delegateLocal); - - final DataObject1 mock = mock(DataObject1.class); - final DataObject1.DataObject11 mock11 = mock(DataObject1.DataObject11.class); - doReturn(mock11).when(mock).getDataObject11(); - doReturn(Optional.fromNullable(mock)).when(delegateLocal).read(DataObject1.IID, ctx); - assertEquals(mock, subtreeR.read(DataObject1.IID, ctx).get()); - } - - public abstract static class DataObject1 implements DataObject { - public static InstanceIdentifier IID = InstanceIdentifier.create(DataObject1.class); - - public abstract DataObject11 getDataObject11(); - - public abstract static class DataObject11 implements DataObject, ChildOf { - public static InstanceIdentifier IID = DataObject1.IID.child(DataObject11.class); - } - } -} diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java deleted file mode 100644 index 3b3ea3a35..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/TypeHierarchyTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.util.DataObjects; -import org.hamcrest.CoreMatchers; -import org.junit.Test; - -public class TypeHierarchyTest { - - @Test - public void testHierarchy() throws Exception { - final TypeHierarchy typeHierarchy = TypeHierarchy.create(Sets.newHashSet( - DataObjects.DataObject4.DataObject41.DataObject411.IID, - DataObjects.DataObject4.DataObject41.IID,/* Included in previous already */ - DataObjects.DataObject1.IID, - DataObjects.DataObject3.DataObject31.IID)); - - // Roots - assertThat(typeHierarchy.getRoots().size(), is(3)); - assertThat(typeHierarchy.getRoots(), CoreMatchers - .hasItems(DataObjects.DataObject1.IID, DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); - - // Leaves - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject1.IID).size(), is(0)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.DataObject31.IID).size(), is(0)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.DataObject411.IID).size(), is(0)); - - // Intermediate leaves - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), CoreMatchers - .hasItem(DataObjects.DataObject3.DataObject31.IID)); - assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject3.IID), typeHierarchy.getAllChildren( - DataObjects.DataObject3.IID)); - - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), CoreMatchers.hasItem( - DataObjects.DataObject4.DataObject41.DataObject411.IID)); - assertEquals(typeHierarchy.getDirectChildren(DataObjects.DataObject4.DataObject41.IID), typeHierarchy.getAllChildren( - DataObjects.DataObject4.DataObject41.IID)); - - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID).size(), is(1)); - assertThat(typeHierarchy.getDirectChildren(DataObjects.DataObject4.IID), CoreMatchers - .hasItem(DataObjects.DataObject4.DataObject41.IID)); - assertThat(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).size(), is(2)); - assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.IID)); - assertTrue(typeHierarchy.getAllChildren(DataObjects.DataObject4.IID).contains(DataObjects.DataObject4.DataObject41.DataObject411.IID)); - } -} - -- cgit 1.2.3-korg