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 --- .../registry/FlatWriterRegistryBuilderTest.java | 193 ++++++++++++ .../write/registry/FlatWriterRegistryTest.java | 328 +++++++++++++++++++++ .../impl/write/registry/SubtreeWriterTest.java | 85 ++++++ 3 files changed, 606 insertions(+) create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java create mode 100644 infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java (limited to 'infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry') diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java new file mode 100644 index 000000000..3a6767265 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryBuilderTest.java @@ -0,0 +1,193 @@ +/* + * 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.impl.write.registry; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimaps; +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.hamcrest.CoreMatchers; +import org.junit.Test; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class FlatWriterRegistryBuilderTest { + + @Test + public void testRelationsBefore() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 3 + -> 4 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject3.class)); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject4.class)); + flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject2.class), + Lists.newArrayList(DataObjects.DataObject3.IID, DataObjects.DataObject4.IID)); + flatWriterRegistryBuilder.addBefore(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + + final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); + assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + } + + @Test + public void testBuild() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + + final InstanceIdentifier id = InstanceIdentifier.create(DataObjects.DataObject3.class); + final DataObjectUpdate update = mock(DataObjectUpdate.class); + doReturn(id).when(update).getId(); + final DataObjects.DataObject3 before = mock(DataObjects.DataObject3.class); + final DataObjects.DataObject3 after = mock(DataObjects.DataObject3.class); + when(update.getDataBefore()).thenReturn(before); + when(update.getDataAfter()).thenReturn(after); + + WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id, update)), + Multimaps.forMap(Collections.emptyMap())); + final WriteContext ctx = mock(WriteContext.class); + build.update(updates, ctx); + + verify(writer).update(id, before, after, ctx); + } + + @Test(expected = IllegalArgumentException.class) + public void testBuildUnknownWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + + final InstanceIdentifier id2 = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjectUpdate update2 = mock(DataObjectUpdate.class); + final WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id2, update2)), + Multimaps.forMap(Collections.emptyMap())); + build.update(updates, mock(WriteContext.class)); + } + + @Test + public void testRelationsAfter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 3 + -> 4 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject3.class), DataObjects.DataObject2.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject4.class), + Lists.newArrayList(DataObjects.DataObject2.IID, DataObjects.DataObject3.IID)); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + + final List> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObjects.DataObject1.IID, typesInList.get(0)); + assertEquals(DataObjects.DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObjects.DataObject3.IID), equalTo(DataObjects.DataObject4.IID))); + } + + @Test(expected = IllegalArgumentException.class) + public void testRelationsLoop() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 1 + */ + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject1.class), DataObjects.DataObject2.IID); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddWriterTwice() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); + } + + @Test + public void testAddSubtreeWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.subtreeAdd( + Sets.newHashSet(DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.IID), + mockWriter(DataObjects.DataObject4.class)); + final ImmutableMap, Writer> mappedWriters = + flatWriterRegistryBuilder.getMappedHandlers(); + final ArrayList> typesInList = Lists.newArrayList(mappedWriters.keySet()); + + assertEquals(DataObjects.DataObject4.IID, typesInList.get(0)); + assertEquals(1, typesInList.size()); + } + + @Test + public void testCreateSubtreeWriter() throws Exception { + final Writer forWriter = SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.DataObject411.IID, + DataObjects.DataObject4.DataObject42.IID), + mockWriter(DataObjects.DataObject4.class)); + assertThat(forWriter, instanceOf(SubtreeWriter.class)); + assertThat(((SubtreeWriter) forWriter).getHandledChildTypes().size(), is(3)); + assertThat(((SubtreeWriter) forWriter).getHandledChildTypes(), CoreMatchers.hasItems(DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject42.IID, DataObjects.DataObject4.DataObject41.DataObject411.IID)); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateInvalidSubtreeWriter() throws Exception { + SubtreeWriter.createForWriter(Sets.newHashSet( + InstanceIdentifier.create(DataObjects.DataObject3.class).child(DataObjects.DataObject3.DataObject31.class)), + mockWriter(DataObjects.DataObject4.class)); + } + + @SuppressWarnings("unchecked") + private Writer mockWriter(final Class doClass) + throws NoSuchFieldException, IllegalAccessException { + final Writer mock = mock(Writer.class); + when(mock.getManagedDataObjectType()).thenReturn((InstanceIdentifier) doClass.getDeclaredField("IID").get(null)); + return mock; + } + +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java new file mode 100644 index 000000000..65742df33 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java @@ -0,0 +1,328 @@ +/* + * 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.impl.write.registry; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.util.DataObjects.DataObject1; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class FlatWriterRegistryTest { + + @Mock + private Writer writer1; + @Mock + private Writer writer2; + @Mock + private Writer writer3; + @Mock + private Writer writer4; + @Mock + private WriteContext ctx; + @Mock + private WriteContext revertWriteContext; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer1.getManagedDataObjectType()).thenReturn(DataObjects.DataObject1.IID); + when(writer2.getManagedDataObjectType()).thenReturn(DataObjects.DataObject2.IID); + when(writer3.getManagedDataObjectType()).thenReturn(DataObjects.DataObject3.IID); + } + + @Test + public void testMultipleUpdatesForSingleWriter() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid2, dataObject, dataObject)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + + verify(writer1).update(iid, dataObject, dataObject, ctx); + verify(writer1).update(iid2, dataObject, dataObject, ctx); + // Invoked when registry is being created + verifyNoMoreInteractions(writer1); + verifyZeroInteractions(writer2); + } + + @Test + public void testMultipleUpdatesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); + inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test + public void testMultipleDeletesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(ImmutableMultimap.of(), deletes), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + // Reversed order of invocation, first writer2 and then writer1 + inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); + inOrder.verify(writer1).update(iid, dataObject, null, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test + public void testMultipleUpdatesAndDeletesForMultipleWriters() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + final Multimap, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier iid = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjects.DataObject1 dataObject = mock(DataObjects.DataObject1.class); + // Writer 1 delete + deletes.put(DataObjects.DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + // Writer 1 update + updates.put(DataObjects.DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 dataObject2 = mock(DataObjects.DataObject2.class); + // Writer 2 delete + deletes.put(DataObjects.DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); + // Writer 2 update + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, dataObject2, dataObject2)); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, deletes), ctx); + + final InOrder inOrder = inOrder(writer1, writer2); + // Reversed order of invocation, first writer2 and then writer1 for deletes + inOrder.verify(writer2).update(iid2, dataObject2, null, ctx); + inOrder.verify(writer1).update(iid, dataObject, null, ctx); + // Then also updates are processed + inOrder.verify(writer1).update(iid, dataObject, dataObject, ctx); + inOrder.verify(writer2).update(iid2, dataObject2, dataObject2, ctx); + + verifyNoMoreInteractions(writer1); + verifyNoMoreInteractions(writer2); + } + + @Test(expected = IllegalArgumentException.class) + public void testMultipleUpdatesOneMissing() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + } + + @Test + public void testMultipleUpdatesOneFailing() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + assertThat(e.getFailedIds().size(), is(2)); + assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject2.class))); + assertThat(e.getFailedIds(), CoreMatchers.hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithSuccess() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject3.class); + final InstanceIdentifier iid2 = InstanceIdentifier.create(DataObjects.DataObject2.class); + final DataObjects.DataObject2 before2 = mock(DataObjects.DataObject2.class); + final DataObjects.DataObject2 after2 = mock(DataObjects.DataObject2.class); + updates.put(DataObjects.DataObject2.IID, DataObjectUpdate.create(iid2, before2, after2)); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + assertThat(e.getFailedIds().size(), is(1)); + + final InOrder inOrder = inOrder(writer1, writer2, writer3); + inOrder.verify(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + inOrder.verify(writer2) + .update(iid2, before2, after2, ctx); + inOrder.verify(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + e.revertChanges(revertWriteContext); + // Revert changes. Successful updates are iterated in reverse + // also binding other write context,to verify if update context is not reused + inOrder.verify(writer2) + .update(iid2, after2, before2, revertWriteContext); + inOrder.verify(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), eq(revertWriteContext)); + verifyNoMoreInteractions(writer3); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithFail() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject2.IID, writer2, DataObjects.DataObject3.IID, writer3)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer3) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObjects.DataObject1.class); + addUpdate(updates, DataObjects.DataObject2.class); + addUpdate(updates, DataObjects.DataObject3.class); + + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + // Writer1 always fails from now + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + try { + e.revertChanges(revertWriteContext); + } catch (WriterRegistry.Reverter.RevertFailedException e1) { + assertThat(e1.getNotRevertedChanges().size(), is(1)); + assertThat(e1.getNotRevertedChanges(), CoreMatchers + .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + } + + @Test + public void testMutlipleUpdatesWithOneKeyedContainer() throws Exception { + final InstanceIdentifier internallyKeyedIdentifier = InstanceIdentifier.create(DataObject1.class) + .child(DataObjects.DataObject1ChildK.class, new DataObjects.DataObject1ChildKey()); + + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObjects.DataObject1.IID, writer1, DataObjects.DataObject1ChildK.IID,writer4)); + + // Writer1 always fails + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), + any(WriteContext.class)); + + final Multimap, DataObjectUpdate> updates = HashMultimap.create(); + addKeyedUpdate(updates,DataObjects.DataObject1ChildK.class); + addUpdate(updates, DataObjects.DataObject1.class); + try { + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + fail("Bulk update should have failed on writer1"); + } catch (WriterRegistry.BulkUpdateException e) { + // Writer1 always fails from now + doThrow(new RuntimeException()).when(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), + any(WriteContext.class)); + try { + e.revertChanges(revertWriteContext); + } catch (WriterRegistry.Reverter.RevertFailedException e1) { + assertThat(e1.getNotRevertedChanges().size(), is(1)); + assertThat(e1.getNotRevertedChanges(), CoreMatchers + .hasItem(InstanceIdentifier.create(DataObjects.DataObject1.class))); + } + } + } + + private void addKeyedUpdate(final Multimap, DataObjectUpdate> updates, + final Class type) throws Exception { + final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); + final InstanceIdentifier keyedIid = (InstanceIdentifier) type.getDeclaredField("INTERNALLY_KEYED_IID").get(null); + updates.put(iid, DataObjectUpdate.create(keyedIid, mock(type), mock(type))); + } + + private void addUpdate(final Multimap, DataObjectUpdate> updates, + final Class type) throws Exception { + final InstanceIdentifier iid = (InstanceIdentifier) type.getDeclaredField("IID").get(null); + updates.put(iid, DataObjectUpdate.create(iid, mock(type), mock(type))); + } +} \ No newline at end of file diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java new file mode 100644 index 000000000..ff2f83158 --- /dev/null +++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/SubtreeWriterTest.java @@ -0,0 +1,85 @@ +/* + * 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.impl.write.registry; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Sets; +import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.write.Writer; +import java.util.Collections; +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubtreeWriterTest { + + @Mock + Writer writer; + @Mock + Writer writer11; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.IID); + when(writer11.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.DataObject41.IID); + } + + @Test(expected = IllegalArgumentException.class) + public void testSubtreeWriterCreationFail() throws Exception { + // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType + SubtreeWriter.createForWriter(Collections.singleton(InstanceIdentifier.create(DataObject.class)), writer); + } + + @Test(expected = IllegalArgumentException.class) + public void testSubtreeWriterCreationFailInvalidIid() throws Exception { + // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType + SubtreeWriter.createForWriter(Collections.singleton(DataObjects.DataObject4.IID), writer); + } + + @Test + public void testSubtreeWriterCreation() throws Exception { + final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.IID, + DataObjects.DataObject4.DataObject41.DataObject411.IID, + DataObjects.DataObject4.DataObject42.IID), + writer); + + assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); + assertEquals(3, forWriter.getHandledChildTypes().size()); + } + + @Test + public void testSubtreeWriterHandledTypes() throws Exception { + final SubtreeWriter forWriter = (SubtreeWriter) SubtreeWriter.createForWriter(Sets.newHashSet( + DataObjects.DataObject4.DataObject41.DataObject411.IID), + writer); + + assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); + assertEquals(1, forWriter.getHandledChildTypes().size()); + assertThat(forWriter.getHandledChildTypes(), CoreMatchers.hasItem(DataObjects.DataObject4.DataObject41.DataObject411.IID)); + } + +} \ No newline at end of file -- cgit 1.2.3-korg