summaryrefslogtreecommitdiffstats
path: root/infra/translate-impl/src/test/java
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2018-04-13 13:38:16 +0200
committerMarek Gradzki <mgradzki@cisco.com>2018-08-17 10:17:58 +0000
commitd1b102f6fafced3c7badb09ecc78fec590704c8a (patch)
treefb9c7041b25cdd36b46cf25e789c850db38f2349 /infra/translate-impl/src/test/java
parentfa641a3e06a905cb3222ebd15a2b4ab90b599efc (diff)
HONEYCOMB-431: add validation support to Writers
This patch introduces FlatWriterRegistry.validateModifications. Implementation iterates over writersOrder following bulkUpdate logic to properly support subtree writers case. Writers are now cabable of validating modifications. Commonly used implementations (GenericWriter and GenericListWriter) delegate validation capbility to Validators. Change-Id: If7a0bb0838c0b8f2c0393c989f3b03853a2ea679 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'infra/translate-impl/src/test/java')
-rw-r--r--infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericListWriterTest.java29
-rw-r--r--infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericWriterTest.java21
-rw-r--r--infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistryTest.java65
3 files changed, 111 insertions, 4 deletions
diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericListWriterTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericListWriterTest.java
index 91785b25e..20cabc5b4 100644
--- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericListWriterTest.java
+++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericListWriterTest.java
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
+import io.fd.honeycomb.translate.write.Validator;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import java.util.Collections;
@@ -42,7 +43,6 @@ public class GenericListWriterTest {
private ListWriterCustomizer<IdentifiableDataObject, DataObjectIdentifier> customizer;
@Mock
private WriteContext ctx;
- private GenericListWriter<IdentifiableDataObject, DataObjectIdentifier> writer;
@Mock
private IdentifiableDataObject before;
@Mock
@@ -51,11 +51,15 @@ public class GenericListWriterTest {
private IdentifiableDataObject after;
@Mock
private DataObjectIdentifier keyAfter;
+ @Mock
+ private Validator<IdentifiableDataObject> validator;
+
+ private GenericListWriter<IdentifiableDataObject, DataObjectIdentifier> writer;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer);
+ writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer, validator);
when(before.getKey()).thenReturn(beforeKey);
when(after.getKey()).thenReturn(keyAfter);
}
@@ -106,4 +110,25 @@ public class GenericListWriterTest {
writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer);
writer.deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
}
+
+ @Test
+ public void testValidate() throws Exception {
+ assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
+
+ final InstanceIdentifier<IdentifiableDataObject> keyedIdBefore =
+ (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
+ .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, beforeKey)));
+ final InstanceIdentifier<IdentifiableDataObject> keyedIdAfter =
+ (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
+ .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, keyAfter)));
+
+ writer.validate(DATA_OBJECT_ID, before, after, ctx);
+ verify(validator).validateUpdate(keyedIdBefore, before, after, ctx);
+
+ writer.validate(DATA_OBJECT_ID, before, null, ctx);
+ verify(validator).validateDelete(keyedIdBefore, before, ctx);
+
+ writer.validate(DATA_OBJECT_ID, null, after, ctx);
+ verify(validator).validateWrite(keyedIdAfter, after, ctx);
+ }
} \ No newline at end of file
diff --git a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericWriterTest.java b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericWriterTest.java
index c9d381ad4..1c0927b89 100644
--- a/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericWriterTest.java
+++ b/infra/translate-impl/src/test/java/io/fd/honeycomb/translate/impl/write/GenericWriterTest.java
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
+import io.fd.honeycomb.translate.write.Validator;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import org.junit.Before;
@@ -40,16 +41,19 @@ public class GenericWriterTest {
private WriterCustomizer<DataObject> customizer;
@Mock
private WriteContext ctx;
- private GenericWriter<DataObject> writer;
@Mock
private DataObject before;
@Mock
private DataObject after;
+ @Mock
+ private Validator<DataObject> validator;
+
+ private GenericWriter<DataObject> writer;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- writer = new GenericWriter<>(DATA_OBJECT_ID, customizer);
+ writer = new GenericWriter<>(DATA_OBJECT_ID, customizer, validator);
}
@Test
@@ -94,4 +98,17 @@ public class GenericWriterTest {
assertTrue(GenericWriter.isUpdateSupported(new NoopWriters.DirectUpdateWriterCustomizer()));
assertTrue(GenericWriter.isUpdateSupported(new NoopWriters.ParentImplDirectUpdateWriterCustomizer()));
}
+
+ @Test
+ public void testValidate() throws Exception {
+ assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
+ writer.validate(DATA_OBJECT_ID, before, after, ctx);
+ verify(validator).validateUpdate(DATA_OBJECT_ID, before, after, ctx);
+
+ writer.validate(DATA_OBJECT_ID, before, null, ctx);
+ verify(validator).validateDelete(DATA_OBJECT_ID, before, ctx);
+
+ writer.validate(DATA_OBJECT_ID, null, after, ctx);
+ verify(validator).validateWrite(DATA_OBJECT_ID, after, ctx);
+ }
} \ 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
index 7f4b93e01..e06197fae 100644
--- 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
@@ -365,6 +365,71 @@ public class FlatWriterRegistryTest {
}
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testValidateMissingWriter() 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.validateModifications(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx);
+ }
+
+ @Test
+ public void testValidateSingleWriter() 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
+ .validateModifications(new WriterRegistry.DataObjectUpdates(updates, ImmutableMultimap.of()), ctx);
+
+ verify(writer1).validate(iid, dataObject, dataObject, ctx);
+ verify(writer1).validate(iid2, dataObject, dataObject, ctx);
+ // Invoked when registry is being created
+ verifyNoMoreInteractions(writer1);
+ verifyZeroInteractions(writer2);
+ }
+
+ @Test
+ public void testValidateMultipleWriters() 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 create
+ updates.put(DataObject1.IID, DataObjectUpdate.create(iid, null, 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.validateModifications(new WriterRegistry.DataObjectUpdates(updates, deletes), ctx);
+
+ // Ignore order
+ verify(writer1).validate(iid, dataObject, null, ctx);
+ verify(writer1).validate(iid, null, dataObject, ctx);
+ verify(writer2).validate(iid2, dataObject2, null, ctx);
+ verify(writer2).validate(iid2, dataObject2, dataObject2, ctx);
+
+ verifyNoMoreInteractions(writer1);
+ verifyNoMoreInteractions(writer2);
+ }
+
private <D extends DataObject> void addKeyedUpdate(final Multimap<InstanceIdentifier<?>, DataObjectUpdate> updates,
final Class<D> type) throws Exception {
final InstanceIdentifier<D> iid = (InstanceIdentifier<D>) type.getDeclaredField("IID").get(null);