diff options
author | 2016-06-29 09:14:51 +0200 | |
---|---|---|
committer | 2016-07-13 11:24:26 +0200 | |
commit | fb41618f6e78d5f1a20bbf37c45282c5fc15387d (patch) | |
tree | 6ac9da72b8a53c01f1ef2f97562a70b838c510f5 /v3po/translate-utils/src/test | |
parent | d222ccedd09b5ee76cdcb367ae96c91f49e5f287 (diff) |
HONEYCOMB-94 Reimplement writer registry with better ordering options
Now the registry is flat and allows for full control of writer execution order
Change-Id: I864e1d676588ffe59b596145e0829e81b1a1ed2f
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'v3po/translate-utils/src/test')
4 files changed, 547 insertions, 189 deletions
diff --git a/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/impl/write/util/DelegatingWriterRegistryTest.java b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/impl/write/util/DelegatingWriterRegistryTest.java deleted file mode 100644 index f51e49db5..000000000 --- a/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/impl/write/util/DelegatingWriterRegistryTest.java +++ /dev/null @@ -1,189 +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.v3po.translate.impl.write.util; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -import io.fd.honeycomb.v3po.translate.util.write.DelegatingWriterRegistry; -import io.fd.honeycomb.v3po.translate.write.WriteContext; -import io.fd.honeycomb.v3po.translate.write.WriteFailedException; -import io.fd.honeycomb.v3po.translate.write.Writer; -import io.fd.honeycomb.v3po.translate.write.WriterRegistry; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Vpp; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppState; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class DelegatingWriterRegistryTest { - - private final InstanceIdentifier<Vpp> vppId; - private final InstanceIdentifier<VppState> vppStateId; - private final InstanceIdentifier<Interfaces> interfaceId; - - private WriteContext ctx; - private Writer<Vpp> writer; - private Writer<VppState> vppStateWriter; - private Writer<Interfaces> interfacesWriter; - - private DelegatingWriterRegistry registry; - - public DelegatingWriterRegistryTest() { - vppId = InstanceIdentifier.create(Vpp.class); - vppStateId = InstanceIdentifier.create(VppState.class); - interfaceId = InstanceIdentifier.create(Interfaces.class); - } - - @SuppressWarnings("unchecked") - private <D extends DataObject> Writer<D> mockWriter(Class<D> clazz) { - final Writer<D> mock = (Writer<D>) Mockito.mock(Writer.class); - doReturn(InstanceIdentifier.create(clazz)).when(mock).getManagedDataObjectType(); - return mock; - } - - private DataObject mockDataObject(final String name, final Class<? extends DataObject> classToMock) { - final DataObject dataBefore = mock(classToMock, name); - doReturn(classToMock).when(dataBefore).getImplementedInterface(); - return dataBefore; - } - - @SuppressWarnings("unchecked") - private static Map<InstanceIdentifier<?>, DataObject> asMap(DataObject... objects) { - final Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>(); - for (DataObject object : objects) { - final Class<? extends DataObject> implementedInterface = - (Class<? extends DataObject>) object.getImplementedInterface(); - final InstanceIdentifier<?> id = InstanceIdentifier.create(implementedInterface); - map.put(id, object); - } - return map; - } - - @Before - public void setUp() { - ctx = mock(WriteContext.class); - writer = mockWriter(Vpp.class); - vppStateWriter = mockWriter(VppState.class); - interfacesWriter = mockWriter(Interfaces.class); - - final List<Writer<? extends DataObject>> writers = new ArrayList<>(); - writers.add(writer); - writers.add(vppStateWriter); - writers.add(interfacesWriter); - - registry = new DelegatingWriterRegistry(writers); - } - - @Test(expected = UnsupportedOperationException.class) - public void testGetManagedDataObjectType() { - registry.getManagedDataObjectType(); - } - - @Test - public void testBulkUpdateRevert() throws Exception { - // Prepare data changes: - final DataObject dataBefore1 = mockDataObject("Vpp before", Vpp.class); - final DataObject dataAfter1 = mockDataObject("Vpp after", Vpp.class); - - final DataObject dataBefore2 = mockDataObject("VppState before", VppState.class); - final DataObject dataAfter2 = mockDataObject("VppState after", VppState.class); - - // Fail on update - Mockito.doThrow(new WriteFailedException(InstanceIdentifier.create(Vpp.class), "vpp failed")) - .when(vppStateWriter).update(vppStateId, dataBefore2, dataAfter2, ctx); - - // Run the test - try { - registry.update(asMap(dataBefore1, dataBefore2), asMap(dataAfter1, dataAfter2), ctx); - } catch (WriterRegistry.BulkUpdateException e) { - // Check second update failed - assertEquals(vppStateId, e.getFailedId()); - verify(writer).update(vppId, dataBefore1, dataAfter1, ctx); - verify(vppStateWriter).update(vppStateId, dataBefore2, dataAfter2, ctx); - - // Try to revert changes - e.revertChanges(); - - // Check revert was successful - verify(writer).update(vppId, dataAfter1, dataBefore1, ctx); - verify(vppStateWriter, never()).update(vppStateId, dataAfter2, dataBefore2, ctx); - - return; - } - fail("BulkUpdateException expected"); - } - - @Test - public void testBulkUpdateRevertFail() throws Exception { - // Prepare data changes: - final DataObject dataBefore1 = mockDataObject("Vpp before", Vpp.class); - final DataObject dataAfter1 = mockDataObject("Vpp after", Vpp.class); - - final DataObject dataBefore2 = mockDataObject("VppState before", VppState.class); - final DataObject dataAfter2 = mockDataObject("VppState after", VppState.class); - - final DataObject dataBefore3 = mockDataObject("Interfaces before", Interfaces.class); - final DataObject dataAfter3 = mockDataObject("Interfaces after", Interfaces.class); - - // Fail on the third update - doThrow(new WriteFailedException(InstanceIdentifier.create(Vpp.class), "vpp failed")).when(interfacesWriter) - .update(interfaceId, dataBefore3, dataAfter3, ctx); - - // Fail on the second revert - doThrow(new WriteFailedException(InstanceIdentifier.create(Vpp.class), "vpp failed")).when(writer) - .update(vppId, dataAfter1, dataBefore1, ctx); - - // Run the test - try { - registry.update(asMap(dataBefore1, dataBefore2, dataBefore3), asMap(dataAfter1, dataAfter2, dataAfter3), ctx); - } catch (WriterRegistry.BulkUpdateException e) { - // Check third update failed - assertEquals(interfaceId, e.getFailedId()); - verify(writer).update(vppId, dataBefore1, dataAfter1, ctx); - verify(vppStateWriter).update(vppStateId, dataBefore2, dataAfter2, ctx); - verify(interfacesWriter).update(interfaceId, dataBefore3, dataAfter3, ctx); - - // Try to revert changes - try { - e.revertChanges(); - } catch (WriterRegistry.Reverter.RevertFailedException e2) { - // Check second revert failed - assertEquals(Collections.singletonList(vppId), e2.getNotRevertedChanges()); - verify(writer).update(vppId, dataAfter1, dataBefore1, ctx); - verify(vppStateWriter).update(vppStateId, dataAfter2, dataBefore2, ctx); - verify(interfacesWriter, never()).update(interfaceId, dataAfter3, dataBefore3, ctx); - return; - } - fail("WriterRegistry.Revert.RevertFailedException expected"); - } - fail("BulkUpdateException expected"); - } -}
\ No newline at end of file diff --git a/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryBuilderTest.java b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryBuilderTest.java new file mode 100644 index 000000000..ec407b685 --- /dev/null +++ b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryBuilderTest.java @@ -0,0 +1,156 @@ +package io.fd.honeycomb.v3po.translate.util.write.registry; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItems; +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.mock; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import io.fd.honeycomb.v3po.translate.write.Writer; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.opendaylight.yangtools.yang.binding.ChildOf; +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.addWriter(mockWriter(DataObject3.class)); + flatWriterRegistryBuilder.addWriter(mockWriter(DataObject4.class)); + flatWriterRegistryBuilder.addWriterBefore(mockWriter(DataObject2.class), + Lists.newArrayList(DataObject3.IID, DataObject4.IID)); + flatWriterRegistryBuilder.addWriterBefore(mockWriter(DataObject1.class), DataObject2.IID); + final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = + flatWriterRegistryBuilder.getMappedWriters(); + + final ArrayList<InstanceIdentifier<?>> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObject1.IID, typesInList.get(0)); + assertEquals(DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObject3.IID), equalTo(DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObject3.IID), equalTo(DataObject4.IID))); + } + + @Test + public void testRelationsAfter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 3 + -> 4 + */ + flatWriterRegistryBuilder.addWriter(mockWriter(DataObject1.class)); + flatWriterRegistryBuilder.addWriterAfter(mockWriter(DataObject2.class), DataObject1.IID); + flatWriterRegistryBuilder.addWriterAfter(mockWriter(DataObject3.class), DataObject2.IID); + flatWriterRegistryBuilder.addWriterAfter(mockWriter(DataObject4.class), DataObject2.IID); + final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = + flatWriterRegistryBuilder.getMappedWriters(); + + final List<InstanceIdentifier<?>> typesInList = Lists.newArrayList(mappedWriters.keySet()); + assertEquals(DataObject1.IID, typesInList.get(0)); + assertEquals(DataObject2.IID, typesInList.get(1)); + assertThat(typesInList.get(2), anyOf(equalTo(DataObject3.IID), equalTo(DataObject4.IID))); + assertThat(typesInList.get(3), anyOf(equalTo(DataObject3.IID), equalTo(DataObject4.IID))); + } + + @Test(expected = IllegalArgumentException.class) + public void testRelationsLoop() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + /* + 1 -> 2 -> 1 + */ + flatWriterRegistryBuilder.addWriter(mockWriter(DataObject1.class)); + flatWriterRegistryBuilder.addWriterAfter(mockWriter(DataObject2.class), DataObject1.IID); + flatWriterRegistryBuilder.addWriterAfter(mockWriter(DataObject1.class), DataObject2.IID); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddWriterTwice() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.addWriter(mockWriter(DataObject1.class)); + flatWriterRegistryBuilder.addWriter(mockWriter(DataObject1.class)); + } + + @Test + public void testAddSubtreeWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + flatWriterRegistryBuilder.addSubtreeWriter( + Sets.newHashSet(DataObject4.DataObject5.IID, + DataObject4.DataObject5.IID), + mockWriter(DataObject4.class)); + final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = + flatWriterRegistryBuilder.getMappedWriters(); + final ArrayList<InstanceIdentifier<?>> typesInList = Lists.newArrayList(mappedWriters.keySet()); + + assertEquals(DataObject4.IID, typesInList.get(0)); + assertEquals(1, typesInList.size()); + } + + @Test + public void testCreateSubtreeWriter() throws Exception { + final Writer<?> forWriter = SubtreeWriter.createForWriter(Sets.newHashSet( + DataObject4.DataObject5.IID, + DataObject4.DataObject5.DataObject51.IID, + DataObject4.DataObject6.IID), + mockWriter(DataObject4.class)); + assertThat(forWriter, instanceOf(SubtreeWriter.class)); + assertThat(((SubtreeWriter<?>) forWriter).getHandledChildTypes().size(), is(3)); + assertThat(((SubtreeWriter<?>) forWriter).getHandledChildTypes(), hasItems(DataObject4.DataObject5.IID, + DataObject4.DataObject6.IID, DataObject4.DataObject5.DataObject51.IID)); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateInvalidSubtreeWriter() throws Exception { + SubtreeWriter.createForWriter(Sets.newHashSet( + InstanceIdentifier.create(DataObject3.class).child(DataObject3.DataObject31.class)), + mockWriter(DataObject4.class)); + } + + @SuppressWarnings("unchecked") + private Writer<? extends DataObject> mockWriter(final Class<? extends DataObject> doClass) + throws NoSuchFieldException, IllegalAccessException { + final Writer mock = mock(Writer.class); + when(mock.getManagedDataObjectType()).thenReturn((InstanceIdentifier<?>) doClass.getDeclaredField("IID").get(null)); + return mock; + } + + private abstract static class DataObject1 implements DataObject { + static InstanceIdentifier<DataObject1> IID = InstanceIdentifier.create(DataObject1.class); + } + private abstract static class DataObject2 implements DataObject { + static InstanceIdentifier<DataObject2> IID = InstanceIdentifier.create(DataObject2.class); + } + private abstract static class DataObject3 implements DataObject { + static InstanceIdentifier<DataObject3> IID = InstanceIdentifier.create(DataObject3.class); + private abstract static class DataObject31 implements DataObject, ChildOf<DataObject3> { + static InstanceIdentifier<DataObject31> IID = DataObject3.IID.child(DataObject31.class); + } + } + private abstract static class DataObject4 implements DataObject { + static InstanceIdentifier<DataObject4> IID = InstanceIdentifier.create(DataObject4.class); + private abstract static class DataObject5 implements DataObject, ChildOf<DataObject4> { + static InstanceIdentifier<DataObject5> IID = DataObject4.IID.child(DataObject5.class); + private abstract static class DataObject51 implements DataObject, ChildOf<DataObject5> { + static InstanceIdentifier<DataObject51> IID = DataObject5.IID.child(DataObject51.class); + } + } + private abstract static class DataObject6 implements DataObject, ChildOf<DataObject4> { + static InstanceIdentifier<DataObject6> IID = DataObject4.IID.child(DataObject6.class); + } + } + +}
\ No newline at end of file diff --git a/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryTest.java b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryTest.java new file mode 100644 index 000000000..7fb5779f3 --- /dev/null +++ b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/FlatWriterRegistryTest.java @@ -0,0 +1,295 @@ +package io.fd.honeycomb.v3po.translate.util.write.registry; + +import static org.hamcrest.CoreMatchers.hasItem; +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.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.v3po.translate.write.DataObjectUpdate; +import io.fd.honeycomb.v3po.translate.write.WriteContext; +import io.fd.honeycomb.v3po.translate.write.Writer; +import io.fd.honeycomb.v3po.translate.write.WriterRegistry; +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<DataObject1> writer1; + @Mock + private Writer<DataObject2> writer2; + @Mock + private Writer<DataObject3> writer3; + @Mock + private WriteContext ctx; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer1.getManagedDataObjectType()).thenReturn(DataObject1.IID); + when(writer2.getManagedDataObjectType()).thenReturn(DataObject2.IID); + when(writer3.getManagedDataObjectType()).thenReturn(DataObject3.IID); + } + + @Test + public void testSingleUpdate() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObject1.IID, writer1)); + + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final DataObject1 before = mock(DataObject1.class); + final DataObject1 after = mock(DataObject1.class); + flatWriterRegistry.update(iid, before, after, ctx); + + verify(writer1).update(iid, before, after, ctx); + } + + @Test + public void testMultipleUpdatesForSingleWriter() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObject1.IID, writer1, DataObject2.IID, writer2)); + + final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final InstanceIdentifier<DataObject1> iid2 = InstanceIdentifier.create(DataObject1.class); + final DataObject1 dataObject = mock(DataObject1.class); + updates.put(DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + updates.put(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(DataObject1.IID, writer1, DataObject2.IID, writer2)); + + final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final DataObject1 dataObject = mock(DataObject1.class); + updates.put(DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier<DataObject2> iid2 = InstanceIdentifier.create(DataObject2.class); + final DataObject2 dataObject2 = mock(DataObject2.class); + updates.put(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(DataObject1.IID, writer1, DataObject2.IID, writer2)); + + final Multimap<InstanceIdentifier<?>, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final DataObject1 dataObject = mock(DataObject1.class); + deletes.put(DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + final InstanceIdentifier<DataObject2> iid2 = InstanceIdentifier.create(DataObject2.class); + final DataObject2 dataObject2 = mock(DataObject2.class); + deletes.put(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(DataObject1.IID, writer1, DataObject2.IID, writer2)); + + final Multimap<InstanceIdentifier<?>, DataObjectUpdate.DataObjectDelete> deletes = HashMultimap.create(); + final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final DataObject1 dataObject = mock(DataObject1.class); + // Writer 1 delete + deletes.put(DataObject1.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid, dataObject, null))); + // Writer 1 update + updates.put(DataObject1.IID, DataObjectUpdate.create(iid, dataObject, dataObject)); + final InstanceIdentifier<DataObject2> iid2 = InstanceIdentifier.create(DataObject2.class); + final DataObject2 dataObject2 = mock(DataObject2.class); + // Writer 2 delete + deletes.put(DataObject2.IID, ((DataObjectUpdate.DataObjectDelete) DataObjectUpdate.create(iid2, dataObject2, null))); + // Writer 2 update + updates.put(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(DataObject1.IID, writer1)); + + final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObject1.class); + addUpdate(updates, DataObject2.class); + flatWriterRegistry.update(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx); + } + + @Test + public void testMultipleUpdatesOneFailing() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of(DataObject1.IID, writer1, 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<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObject1.class); + addUpdate(updates, 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(), hasItem(InstanceIdentifier.create(DataObject2.class))); + assertThat(e.getFailedIds(), hasItem(InstanceIdentifier.create(DataObject1.class))); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithSuccess() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObject1.IID, writer1, DataObject2.IID, writer2, 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<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObject1.class); + addUpdate(updates, DataObject3.class); + final InstanceIdentifier<DataObject2> iid2 = InstanceIdentifier.create(DataObject2.class); + final DataObject2 before2 = mock(DataObject2.class); + final DataObject2 after2 = mock(DataObject2.class); + updates.put(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(); + // Revert changes. Successful updates are iterated in reverse + inOrder.verify(writer2) + .update(iid2, after2, before2, ctx); + inOrder.verify(writer1) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + verifyNoMoreInteractions(writer3); + } + } + + @Test + public void testMultipleUpdatesOneFailingThenRevertWithFail() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry( + ImmutableMap.of(DataObject1.IID, writer1, DataObject2.IID, writer2, 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<InstanceIdentifier<?>, DataObjectUpdate> updates = HashMultimap.create(); + addUpdate(updates, DataObject1.class); + addUpdate(updates, DataObject2.class); + addUpdate(updates, 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(); + } catch (WriterRegistry.Reverter.RevertFailedException e1) { + assertThat(e1.getNotRevertedChanges().size(), is(1)); + assertThat(e1.getNotRevertedChanges(), hasItem(InstanceIdentifier.create(DataObject1.class))); + } + } + } + + private <D extends DataObject> void addUpdate(final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates, + final Class<D> type) throws Exception { + final InstanceIdentifier<D> iid = (InstanceIdentifier<D>) type.getDeclaredField("IID").get(null); + updates.put(iid, DataObjectUpdate.create(iid, mock(type), mock(type))); + } + + @Test(expected = NullPointerException.class) + public void testSingleUpdateMissingWriter() throws Exception { + final FlatWriterRegistry flatWriterRegistry = + new FlatWriterRegistry(ImmutableMap.of()); + + final InstanceIdentifier<DataObject1> iid = InstanceIdentifier.create(DataObject1.class); + final DataObject1 before = mock(DataObject1.class); + final DataObject1 after = mock(DataObject1.class); + flatWriterRegistry.update(iid, before, after, ctx); + } + + private abstract static class DataObject1 implements DataObject { + static final InstanceIdentifier<DataObject1> IID = InstanceIdentifier.create(DataObject1.class); + } + private abstract static class DataObject2 implements DataObject { + static final InstanceIdentifier<DataObject2> IID = InstanceIdentifier.create(DataObject2.class); + } + private abstract static class DataObject3 implements DataObject { + static final InstanceIdentifier<DataObject3> IID = InstanceIdentifier.create(DataObject3.class); + } +}
\ No newline at end of file diff --git a/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/SubtreeWriterTest.java b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/SubtreeWriterTest.java new file mode 100644 index 000000000..b7dcadc73 --- /dev/null +++ b/v3po/translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/util/write/registry/SubtreeWriterTest.java @@ -0,0 +1,96 @@ +/* + * 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.v3po.translate.util.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.v3po.translate.write.Writer; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubtreeWriterTest { + + @Mock + Writer<DataObject1> writer; + @Mock + Writer<DataObject1.DataObject11> writer11; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(writer.getManagedDataObjectType()).thenReturn(DataObject1.IID); + when(writer11.getManagedDataObjectType()).thenReturn(DataObject1.DataObject11.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(DataObject1.IID), writer); + } + + @Test + public void testSubtreeWriterCreation() throws Exception { + final SubtreeWriter<?> forWriter = (SubtreeWriter<?>) SubtreeWriter.createForWriter(Sets.newHashSet( + DataObject1.DataObject11.IID, + DataObject1.DataObject11.DataObject111.IID, + DataObject1.DataObject12.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( + DataObject1.DataObject11.DataObject111.IID), + writer); + + assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType()); + assertEquals(1, forWriter.getHandledChildTypes().size()); + assertThat(forWriter.getHandledChildTypes(), hasItem(DataObject1.DataObject11.DataObject111.IID)); + } + + private abstract static class DataObject1 implements DataObject { + static InstanceIdentifier<DataObject1> IID = InstanceIdentifier.create(DataObject1.class); + private abstract static class DataObject11 implements DataObject, ChildOf<DataObject1> { + static InstanceIdentifier<DataObject11> IID = DataObject1.IID.child(DataObject11.class); + private abstract static class DataObject111 implements DataObject, ChildOf<DataObject11> { + static InstanceIdentifier<DataObject111> IID = DataObject11.IID.child(DataObject111.class); + } + } + private abstract static class DataObject12 implements DataObject, ChildOf<DataObject1> { + static InstanceIdentifier<DataObject12> IID = DataObject1.IID.child(DataObject12.class); + } + } +}
\ No newline at end of file |