diff options
Diffstat (limited to 'infra/data-impl/src/test/java/io')
7 files changed, 940 insertions, 180 deletions
diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegatorTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegatorTest.java index 26a936f23..432833be3 100644 --- a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegatorTest.java +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegatorTest.java @@ -64,7 +64,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; -public class ModifiableDataTreeDelegatorTest { +public class ModifiableDataTreeDelegatorTest extends ModificationBaseTest { @Mock private WriterRegistry writer; @@ -90,7 +90,7 @@ public class ModifiableDataTreeDelegatorTest { @Before public void setUp() throws Exception { initMocks(this); - dataTree = ModificationDiffTest.getDataTree(); + dataTree = getDataTree(); when(contextBroker.newReadWriteTransaction()).thenReturn(tx); when(tx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); @@ -98,39 +98,39 @@ public class ModifiableDataTreeDelegatorTest { final Map.Entry<InstanceIdentifier<?>, DataObject> parsed = new AbstractMap.SimpleEntry<>(DEFAULT_ID, DEFAULT_DATA_OBJECT); when(serializer.fromNormalizedNode(any(YangInstanceIdentifier.class), any(NormalizedNode.class))).thenReturn(parsed); - configDataTree = new ModifiableDataTreeDelegator(serializer, dataTree, ModificationDiffTest.getSchemaCtx(), writer, contextBroker); + configDataTree = new ModifiableDataTreeDelegator(serializer, dataTree, getSchemaCtx(), writer, contextBroker); } @Test public void testRead() throws Exception { - final ContainerNode topContainer = ModificationDiffTest.getTopContainer("topContainer"); - ModificationDiffTest.addNodeToTree(dataTree, topContainer, ModificationDiffTest.TOP_CONTAINER_ID); + final ContainerNode topContainer = getTopContainer("topContainer"); + addNodeToTree(dataTree, topContainer, TOP_CONTAINER_ID); final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = - configDataTree.read(ModificationDiffTest.TOP_CONTAINER_ID); + configDataTree.read(TOP_CONTAINER_ID); final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read2 = - configDataTree.newModification().read(ModificationDiffTest.TOP_CONTAINER_ID); + configDataTree.newModification().read(TOP_CONTAINER_ID); final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = read.get(); final Optional<NormalizedNode<?, ?>> normalizedNodeOptional2 = read2.get(); assertEquals(normalizedNodeOptional, normalizedNodeOptional2); assertTrue(normalizedNodeOptional.isPresent()); assertEquals(topContainer, normalizedNodeOptional.get()); - assertEquals(dataTree.takeSnapshot().readNode(ModificationDiffTest.TOP_CONTAINER_ID), normalizedNodeOptional); + assertEquals(dataTree.takeSnapshot().readNode(TOP_CONTAINER_ID), normalizedNodeOptional); } @Test public void testCommitSuccessful() throws Exception { - final MapNode nestedList = ModificationDiffTest.getNestedList("listEntry", "listValue"); + final MapNode nestedList = getNestedList("listEntry", "listValue"); final DataModification dataModification = configDataTree.newModification(); - dataModification.write(ModificationDiffTest.NESTED_LIST_ID, nestedList); + dataModification.write(NESTED_LIST_ID, nestedList); dataModification.validate(); dataModification.commit(); final Multimap<InstanceIdentifier<?>, DataObjectUpdate> map = HashMultimap.create(); map.put(DEFAULT_ID, DataObjectUpdate.create(DEFAULT_ID, DEFAULT_DATA_OBJECT, DEFAULT_DATA_OBJECT)); verify(writer).update(eq(new WriterRegistry.DataObjectUpdates(map, ImmutableMultimap.of())), any(WriteContext.class)); - assertEquals(nestedList, dataTree.takeSnapshot().readNode(ModificationDiffTest.NESTED_LIST_ID).get()); + assertEquals(nestedList, dataTree.takeSnapshot().readNode(NESTED_LIST_ID).get()); } private static DataObject mockDataObject(final String name, final Class<? extends DataObject> classToMock) { @@ -141,7 +141,7 @@ public class ModifiableDataTreeDelegatorTest { @Test public void testCommitUndoSuccessful() throws Exception { - final MapNode nestedList = ModificationDiffTest.getNestedList("listEntry", "listValue"); + final MapNode nestedList = getNestedList("listEntry", "listValue"); // Fail on update: final WriterRegistry.Reverter reverter = mock(WriterRegistry.Reverter.class); @@ -152,7 +152,7 @@ public class ModifiableDataTreeDelegatorTest { try { // Run the test final DataModification dataModification = configDataTree.newModification(); - dataModification.write(ModificationDiffTest.NESTED_LIST_ID, nestedList); + dataModification.write(NESTED_LIST_ID, nestedList); dataModification.validate(); dataModification.commit(); fail("WriterRegistry.RevertSuccessException was expected"); @@ -165,7 +165,7 @@ public class ModifiableDataTreeDelegatorTest { @Test public void testCommitUndoFailed() throws Exception { - final MapNode nestedList = ModificationDiffTest.getNestedList("listEntry", "listValue"); + final MapNode nestedList = getNestedList("listEntry", "listValue"); // Fail on update: final WriterRegistry.Reverter reverter = mock(WriterRegistry.Reverter.class); @@ -182,7 +182,7 @@ public class ModifiableDataTreeDelegatorTest { try { // Run the test final DataModification dataModification = configDataTree.newModification(); - dataModification.write(ModificationDiffTest.NESTED_LIST_ID, nestedList); + dataModification.write(NESTED_LIST_ID, nestedList); dataModification.validate(); dataModification.commit(); fail("WriterRegistry.Reverter.RevertFailedException was expected"); @@ -201,14 +201,14 @@ public class ModifiableDataTreeDelegatorTest { public void testToBindingAware() throws Exception { when(serializer.fromNormalizedNode(any(YangInstanceIdentifier.class), eq(null))).thenReturn(null); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> biNodes = new HashMap<>(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> biNodes = new HashMap<>(); // delete final QName nn1 = QName.create("namespace", "nn1"); final YangInstanceIdentifier yid1 = mockYid(nn1); final InstanceIdentifier iid1 = mockIid(yid1, DataObject1.class); final NormalizedNode nn1B = mockNormalizedNode(nn1); final DataObject1 do1B = mockDataObject(yid1, iid1, nn1B, DataObject1.class); - biNodes.put(yid1, ModificationDiff.NormalizedNodeUpdate.create(yid1, nn1B, null)); + biNodes.put(yid1, NormalizedNodeUpdate.create(yid1, nn1B, null)); // create final QName nn2 = QName.create("namespace", "nn1"); @@ -216,7 +216,7 @@ public class ModifiableDataTreeDelegatorTest { final InstanceIdentifier iid2 = mockIid(yid2, DataObject2.class);; final NormalizedNode nn2A = mockNormalizedNode(nn2); final DataObject2 do2A = mockDataObject(yid2, iid2, nn2A, DataObject2.class); - biNodes.put(yid2, ModificationDiff.NormalizedNodeUpdate.create(yid2, null, nn2A)); + biNodes.put(yid2, NormalizedNodeUpdate.create(yid2, null, nn2A)); // update final QName nn3 = QName.create("namespace", "nn1"); @@ -226,7 +226,7 @@ public class ModifiableDataTreeDelegatorTest { final DataObject3 do3B = mockDataObject(yid3, iid3, nn3B, DataObject3.class); final NormalizedNode nn3A = mockNormalizedNode(nn3); final DataObject3 do3A = mockDataObject(yid3, iid3, nn3A, DataObject3.class);; - biNodes.put(yid3, ModificationDiff.NormalizedNodeUpdate.create(yid3, nn3B, nn3A)); + biNodes.put(yid3, NormalizedNodeUpdate.create(yid3, nn3B, nn3A)); final WriterRegistry.DataObjectUpdates dataObjectUpdates = ModifiableDataTreeDelegator.toBindingAware(biNodes, serializer); diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationBaseTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationBaseTest.java new file mode 100644 index 000000000..c7d78faf6 --- /dev/null +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationBaseTest.java @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2017 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.data.impl; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; + +abstract class ModificationBaseTest extends ModificationMetadata { + + void addNodeToTree(final DataTree dataTree, final NormalizedNode<?, ?> node, + final YangInstanceIdentifier id) + throws DataValidationFailedException { + DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot(); + DataTreeModification dataTreeModification = dataTreeSnapshot.newModification(); + dataTreeModification.write(id, node); + dataTreeModification.ready(); + dataTree.validate(dataTreeModification); + DataTreeCandidate prepare = dataTree.prepare(dataTreeModification); + dataTree.commit(prepare); + } + + protected TipProducingDataTree getDataTree() throws ReactorException { + final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION); + dataTree.setSchemaContext(getSchemaCtx()); + return dataTree; + } + + ContainerNode getTopContainer(final String stringValue) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue)) + .build(); + } + + ContainerNode getTopContainerWithLeafList(final String... stringValue) { + final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.leafSetBuilder(); + for (final String value : stringValue) { + leafSetBuilder.withChild(Builders.<String>leafSetEntryBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(NESTED_LEAF_LIST_QNAME, value)) + .withValue(value) + .build()); + } + + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(FOR_LEAF_LIST_QNAME)) + .withChild(leafSetBuilder + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LEAF_LIST_QNAME)) + .build()) + .build()) + .build(); + } + + MapNode getNestedList(final String listItemName, final String text) { + return Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME)) + .withChild( + Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, + NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(TEXT_LEAF_QNAME, text)) + .build() + ) + .build(); + } + + MapNode getDeepList(final String listItemName) { + return Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DEEP_LIST_QNAME)) + .withChild( + Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(DEEP_LIST_QNAME, + NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName)) + .build() + ) + .build(); + } + + SchemaContext getSchemaCtx() throws ReactorException { + final CrossSourceStatementReactor.BuildAction buildAction = YangInferencePipeline.RFC6020_REACTOR.newBuild(); + buildAction.addSource( + new YangStatementSourceImpl(ModificationDiffTest.class.getResourceAsStream("/test-diff.yang"))); + return buildAction.buildEffective(); + } + + DataTreeModification getModification(final TipProducingDataTree dataTree) { + final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot(); + return dataTreeSnapshot.newModification(); + } + + + DataTreeCandidateTip prepareModification(final TipProducingDataTree dataTree, + final DataTreeModification dataTreeModification) + throws DataValidationFailedException { + dataTreeModification.ready(); + dataTree.validate(dataTreeModification); + return dataTree.prepare(dataTreeModification); + } + + ModificationDiff getModificationDiff(final DataTreeCandidateTip prepare) throws ReactorException { + return new ModificationDiff.ModificationDiffBuilder() + .setCtx(getSchemaCtx()) + .build(prepare.getRootNode()); + } + + NormalizedNodeUpdate getNormalizedNodeUpdateForAfterType( + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates, + final Class<? extends NormalizedNode<?, ?>> containerNodeClass) { + return updates.values().stream() + .filter(update -> containerNodeClass.isAssignableFrom(update.getDataAfter().getClass())) + .findFirst().get(); + } + + NormalizedNodeUpdate getNormalizedNodeUpdateForBeforeType( + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates, + final Class<? extends NormalizedNode<?, ?>> containerNodeClass) { + return updates.values().stream() + .filter(update -> containerNodeClass.isAssignableFrom(update.getDataBefore().getClass())) + .findFirst().get(); + } + + void assertUpdate(final NormalizedNodeUpdate update, + final YangInstanceIdentifier idExpected, + final NormalizedNode<?, ?> beforeExpected, + final NormalizedNode<?, ?> afterExpected) { + assertThat(update.getId(), is(idExpected)); + assertThat(update.getDataBefore(), is(beforeExpected)); + assertThat(update.getDataAfter(), is(afterExpected)); + } + + + ContainerNode getNestedContainerWithLeafList(final String... stringValue) { + final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.leafSetBuilder(); + for (final String value : stringValue) { + leafSetBuilder.withChild(Builders.<String>leafSetEntryBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(NESTED_CONTAINER_LEAF_LIST, value)) + .withValue(value) + .build()); + } + + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(leafSetBuilder + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_LEAF_LIST)) + .build()) + .build(); + } + + ContainerNode getNestedContainerWithChoice(final String caseLeafValue) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.choiceBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CHOICE)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNDER_NESTED_CASE)) + .withValue(caseLeafValue) + .build()).build()) + .build(); + } + + MapEntryNode getNestedListEntry(final String listItemName, final String text) { + return Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, + NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(TEXT_LEAF_QNAME, text)) + .build(); + } + + MapEntryNode getNestedListInContainerEntry(final String listItemName) { + return Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_IN_CONTAINER_QNAME, + IN_CONTAINER_NAME_LEAF_QNAME, listItemName)) + .withChild(ImmutableNodes.leafNode(IN_CONTAINER_NAME_LEAF_QNAME, listItemName)) + .build(); + } + + MapNode getNestedList(MapEntryNode... entries) { + return Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME)) + .withValue(Arrays.asList(entries)) + .build(); + } + + MapNode getNestedListInContainer(MapEntryNode... entries) { + return Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_IN_CONTAINER_QNAME)) + .withValue(Arrays.asList(entries)) + .build(); + } + + ContainerNode getNestedContWithLeafUnderAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_LEAF)) + .withValue(value).build()).build(); + } + + ContainerNode getNestedContWithListUnderAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_LIST)) + .withChild(Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(AUG_LIST, AUG_LIST_KEY, + value)) + .build()) + .build()).build(); + } + + ContainerNode getNestedContWithContUnderAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER_LEAF)) + .withValue(value) + .build()) + .build()).build(); + } + + ContainerNode getNestedContWithLeafListUnderAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.leafSetBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_LEAFLIST)) + .withChildValue(value) + .build()).build(); + } + + ContainerNode getNestedContWithContainerUnderNestedAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_AUG_CONTAINER)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifier(NESTED_AUG_CONTAINER_LEAF)) + .withValue(value) + .build()).build()).build()).build(); + } + + ContainerNode getNestedContWithLeafListUnderNestedAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)) + .withChild(Builders.leafSetBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_AUG_LEAF_LIST)) + .withChildValue(value) + .build()).build()).build(); + } + + ContainerNode getNestedContWithLeafUnderNestedAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_AUG_LEAF)) + .withValue(value) + .build()).build()).build(); + } + + ContainerNode getNestedContWithListUnderNestedAug(String value) { + return Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)) + .withChild(Builders.mapBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_AUG_LIST)) + .withChild(Builders.mapEntryBuilder() + .withNodeIdentifier( + new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_AUG_LIST, + NESTED_AUG_LIST_KEY, value)).build()) + .build()).build()).build(); + } + + TipProducingDataTree prepareStateBeforeWithTopContainer(final NormalizedNode<?, ?> topContainerData) + throws ReactorException, DataValidationFailedException { + final TipProducingDataTree dataTree = getDataTree(); + final DataTreeModification dataTreeModificationOriginal = getModification(dataTree); + // non presence, but with valid child list + dataTreeModificationOriginal.write(TOP_CONTAINER_ID, topContainerData); + final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModificationOriginal); + dataTree.commit(prepare); + return dataTree; + } + + DataTreeCandidateTip prepareStateAfterEmpty(final TipProducingDataTree dataTree) + throws DataValidationFailedException { + final NormalizedNode<?, ?> topContainerModified = Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .build(); + final DataTreeModification dataTreeModificationModified = getModification(dataTree); + dataTreeModificationModified.write(TOP_CONTAINER_ID, topContainerModified); + return prepareModification(dataTree, dataTreeModificationModified); + } + + void assertCollectionContainsOnlyDeletes(final ModificationDiff modificationDiff) { + assertTrue(modificationDiff.getUpdates().entrySet().stream().map(Map.Entry::getValue) + .map(NormalizedNodeUpdate::getDataAfter) + .filter(Objects::nonNull).collect(Collectors.toList()).isEmpty()); + } + + void assertNodeModificationPresent(final ModificationDiff modificationDiff, final Set<QName> expected) { + assertTrue(modificationDiff.getUpdates().values().stream() + .allMatch(update -> expected.contains(update.getId().getLastPathArgument().getNodeType()))); + } +} diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffAugRewriteDeleteTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffAugRewriteDeleteTest.java new file mode 100644 index 000000000..c2b959809 --- /dev/null +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffAugRewriteDeleteTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2017 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.data.impl; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.common.collect.ImmutableSet; +import org.junit.Test; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ModificationDiffAugRewriteDeleteTest extends ModificationBaseTest { + + private static final Logger LOG = LoggerFactory.getLogger(ModificationDiffAugRewriteDeleteTest.class); + + @Test + public void testWriteNonPresenceNonEmptyContainerAugWithLeaf() + throws ReactorException, DataValidationFailedException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithLeafUnderAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(AUG_LEAF)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerAugWithList() + throws ReactorException, DataValidationFailedException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithListUnderAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(AUG_LIST)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerAugWithNonPresenceContainer() + throws ReactorException, DataValidationFailedException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithContUnderAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(AUG_CONTAINER_LEAF)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerAugWithLeafList() + throws ReactorException, DataValidationFailedException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithLeafListUnderAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(AUG_LEAFLIST)); + } +} diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffNestedAugRewriteDeleteTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffNestedAugRewriteDeleteTest.java new file mode 100644 index 000000000..9c92b4f85 --- /dev/null +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffNestedAugRewriteDeleteTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2017 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.data.impl; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.common.collect.ImmutableSet; +import org.junit.Test; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ModificationDiffNestedAugRewriteDeleteTest extends ModificationBaseTest { + + private static final Logger LOG = LoggerFactory.getLogger(ModificationDiffNestedAugRewriteDeleteTest.class); + + @Test + public void testWriteNonPresenceNonEmptyContainerNestedAugWithContainer() + throws DataValidationFailedException, ReactorException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithContainerUnderNestedAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_AUG_CONTAINER_LEAF)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerNestedAugWithLeafList() + throws DataValidationFailedException, ReactorException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithLeafListUnderNestedAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_AUG_LEAF_LIST)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerNestedAugWithList() + throws DataValidationFailedException, ReactorException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithListUnderNestedAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_AUG_LIST)); + } + + @Test + public void testWriteNonPresenceNonEmptyContainerNestedAugWithLeaf() + throws DataValidationFailedException, ReactorException { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedContWithLeafUnderNestedAug("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_AUG_LEAF)); + } +} diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffRewriteDeleteTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffRewriteDeleteTest.java new file mode 100644 index 000000000..a1ede9691 --- /dev/null +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffRewriteDeleteTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2017 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.data.impl; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.common.collect.ImmutableSet; +import org.junit.Test; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ModificationDiffRewriteDeleteTest extends ModificationBaseTest { + + private static final Logger LOG = LoggerFactory.getLogger(ModificationDiffRewriteDeleteTest.class); + + /** + * Covers case when non-presence container was already filled with some data, + * and modification removes data by overriding by empty list + */ + @Test + public void testWriteNonPresenceNonEmptyContainerPreviousDataOverrideByEmpty() throws Exception { + final MapEntryNode alreadyPresent = getNestedListEntry("value", "txt"); + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedList(alreadyPresent)) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_LIST_QNAME)); + } + + /** + * Covers case when non-presence container was already filled with some data, + * and modification removes data by overriding by empty list. This case tests + * when there are multiple nested non-presence containers + */ + @Test + public void testWriteNonPresenceMultipleNonEmptyContainerPreviousDataOverrideByEmpty() throws Exception { + final MapEntryNode alreadyPresent = getNestedListInContainerEntry("key"); + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + // another non-presence container with list entry + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(getNestedListInContainer(alreadyPresent)) + .build()) + // direct list entry + .withChild(getNestedList(alreadyPresent)) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect two new entries + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(2)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, + ImmutableSet.of(NESTED_LIST_IN_CONTAINER_QNAME, NESTED_LIST_QNAME)); + } + + /** + * Covers case when non-presence container was already filled with some data, + * and modification removes data by overriding. Tests case with leaf + */ + @Test + public void testWriteNonPresenceNonEmptyContainerLeaf() throws Exception { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + // another non-presence container with leaf + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_QNAME)) + .withChild(Builders.leafBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_CONTAINER_VAL)) + .withValue("val").build()) + .build()) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_CONTAINER_VAL)); + } + + /** + * Covers case when non-presence container was already filled with some data, + * and modification removes data by overriding. Tests case with leaf-list + */ + @Test + public void testWriteNonPresenceNonEmptyContainerLeafList() throws Exception { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + // another non-presence container with leaf + .withChild(getNestedContainerWithLeafList()) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_CONTAINER_LEAF_LIST)); + } + + /** + * Covers case when non-presence container was already filled with some data, + * and modification removes data by overriding. Tests case with choice/case + */ + @Test + public void testWriteNonPresenceNonEmptyContainerWithChoice() throws Exception { + final TipProducingDataTree dataTree = prepareStateBeforeWithTopContainer(Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + // another non-presence container with leaf + .withChild(getNestedContainerWithChoice("val")) + .build()); + // now we have state with some data + + // just empty non presence container + final DataTreeCandidateTip prepareModified = prepareStateAfterEmpty(dataTree); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + // is delete + assertCollectionContainsOnlyDeletes(modificationDiff); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(UNDER_NESTED_CASE)); + } +} diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffTest.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffTest.java index 664b57378..cd980dd1e 100644 --- a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffTest.java +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationDiffTest.java @@ -23,59 +23,26 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import com.google.common.base.Optional; +import com.google.common.collect.ImmutableSet; import java.util.Map; import org.junit.Test; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; -import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; -import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class ModificationDiffTest { +public class ModificationDiffTest extends ModificationBaseTest { - static final QName TOP_CONTAINER_QNAME = - QName.create("urn:opendaylight:params:xml:ns:yang:test:diff", "2015-01-05", "top-container"); - static final QName STRING_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "string"); - static final QName NAME_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "name"); - static final QName TEXT_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "text"); - static final QName NESTED_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-list"); - static final QName DEEP_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "deep-list"); - static final QName EMPTY_QNAME = QName.create(TOP_CONTAINER_QNAME, "empty"); - static final QName IN_EMPTY_QNAME = QName.create(TOP_CONTAINER_QNAME, "in-empty"); - - static final QName FOR_LEAF_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "for-leaf-list"); - static final QName NESTED_LEAF_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-leaf-list"); - - - static final QName WITH_CHOICE_CONTAINER_QNAME = - QName.create("urn:opendaylight:params:xml:ns:yang:test:diff", "2015-01-05", "with-choice"); - static final QName CHOICE_QNAME = QName.create(WITH_CHOICE_CONTAINER_QNAME, "choice"); - static final QName IN_CASE1_LEAF_QNAME = QName.create(WITH_CHOICE_CONTAINER_QNAME, "in-case1"); - static final QName IN_CASE2_LEAF_QNAME = QName.create(WITH_CHOICE_CONTAINER_QNAME, "in-case2"); - - static final QName PRESENCE_CONTAINER_QNAME = QName.create(TOP_CONTAINER_QNAME, "presence"); - - static final YangInstanceIdentifier TOP_CONTAINER_ID = YangInstanceIdentifier.of(TOP_CONTAINER_QNAME); - static final YangInstanceIdentifier NESTED_LIST_ID = TOP_CONTAINER_ID.node(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME)); + private static final Logger LOG = LoggerFactory.getLogger(ModificationDiffTest.class); @Test public void testInitialWrite() throws Exception { @@ -150,17 +117,14 @@ public class ModificationDiffTest { dataTreeModification.write(WITH_CHOICE_CONTAINER_ID, containerWithChoice); final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertUpdate(getNormalizedNodeUpdateForAfterType(updates, ContainerNode.class), WITH_CHOICE_CONTAINER_ID, null, containerWithChoice); } - private DataTreeModification getModification(final TipProducingDataTree dataTree) { - final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot(); - return dataTreeSnapshot.newModification(); - } + @Test public void testWriteNonPresenceEmptyContainer() throws Exception { @@ -175,6 +139,65 @@ public class ModificationDiffTest { assertThat(modificationDiff.getUpdates().size(), is(0)); } + /** + * Covers case when both non-presence and nested is not present + */ + @Test + public void testWriteNonPresenceNonEmptyContainer() throws Exception { + final TipProducingDataTree dataTree = getDataTree(); + final DataTreeModification dataTreeModification = getModification(dataTree); + // non presence ,but with valid child list + final NormalizedNode<?, ?> topContainer = Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedList("value","txt")) + .build(); + + dataTreeModification.write(TOP_CONTAINER_ID, topContainer); + final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); + + final ModificationDiff modificationDiff = getModificationDiff(prepare); + + assertThat(modificationDiff.getUpdates().size(), is(1)); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_LIST_QNAME)); + } + + /** + * Covers case when non-presence container was already filled with some data, + * and modification just ads some other nested data + */ + @Test + public void testWriteNonPresenceNonEmptyContainerPreviousData() throws Exception { + final TipProducingDataTree dataTree = getDataTree(); + final DataTreeModification dataTreeModificationOriginal = getModification(dataTree); + // non presence, but with valid child list + final MapEntryNode alreadyPresent = getNestedListEntry("value", "txt"); + final NormalizedNode<?, ?> topContainerOriginal = Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedList(alreadyPresent)) + .build(); + + dataTreeModificationOriginal.write(TOP_CONTAINER_ID, topContainerOriginal); + final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModificationOriginal); + dataTree.commit(prepare); + // now we have state with some data + + final MapEntryNode newEntry = getNestedListEntry("value2", "txt2"); + final NormalizedNode<?, ?> topContainerModified = Builders.containerBuilder() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) + .withChild(getNestedList(alreadyPresent, newEntry)) + .build(); + final DataTreeModification dataTreeModificationModified = getModification(dataTree); + dataTreeModificationModified.write(TOP_CONTAINER_ID, topContainerModified); + final DataTreeCandidateTip prepareModified = prepareModification(dataTree, dataTreeModificationModified); + + final ModificationDiff modificationDiff = getModificationDiff(prepareModified); + // should detect one new entry + LOG.debug(modificationDiff.toString()); + assertThat(modificationDiff.getUpdates().size(), is(1)); + assertNodeModificationPresent(modificationDiff, ImmutableSet.of(NESTED_LIST_QNAME)); + } + + @Test public void testWriteNonPresenceEmptyNestedContainer() throws Exception { final TipProducingDataTree dataTree = getDataTree(); @@ -195,14 +218,6 @@ public class ModificationDiffTest { assertThat(modificationDiff.getUpdates().size(), is(1)); } - private DataTreeCandidateTip prepareModification(final TipProducingDataTree dataTree, - final DataTreeModification dataTreeModification) - throws DataValidationFailedException { - dataTreeModification.ready(); - dataTree.validate(dataTreeModification); - return dataTree.prepare(dataTreeModification); - } - @Test public void testUpdateWrite() throws Exception { final TipProducingDataTree dataTree = getDataTree(); @@ -214,16 +229,13 @@ public class ModificationDiffTest { dataTreeModification.write(TOP_CONTAINER_ID, topContainerAfter); final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertThat(updates.values().size(), is(1)); assertUpdate(updates.values().iterator().next(), TOP_CONTAINER_ID, topContainer, topContainerAfter); } - private ModificationDiff getModificationDiff(final DataTreeCandidateTip prepare) throws ReactorException { - return ModificationDiff.recursivelyFromCandidateRoot(prepare.getRootNode(), getSchemaCtx()); - } @Test public void testUpdateMerge() throws Exception { @@ -236,7 +248,7 @@ public class ModificationDiffTest { dataTreeModification.merge(TOP_CONTAINER_ID, topContainerAfter); final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertThat(updates.values().size(), is(1)); assertUpdate(updates.values().iterator().next(), TOP_CONTAINER_ID, topContainer, topContainerAfter); @@ -252,7 +264,7 @@ public class ModificationDiffTest { dataTreeModification.delete(TOP_CONTAINER_ID); final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertThat(updates.values().size(), is(1)); assertUpdate(updates.values().iterator().next(), TOP_CONTAINER_ID, topContainer, null); @@ -276,7 +288,7 @@ public class ModificationDiffTest { dataTree.validate(dataTreeModification); DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification); - Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertUpdate(getNormalizedNodeUpdateForAfterType(updates, MapEntryNode.class), @@ -301,14 +313,6 @@ public class ModificationDiffTest { assertThat(updates.size(), is(1 /*Actual list entry*/)); } // - private void assertUpdate(final ModificationDiff.NormalizedNodeUpdate update, - final YangInstanceIdentifier idExpected, - final NormalizedNode<?, ?> beforeExpected, - final NormalizedNode<?, ?> afterExpected) { - assertThat(update.getId(), is(idExpected)); - assertThat(update.getDataBefore(), is(beforeExpected)); - assertThat(update.getDataAfter(), is(afterExpected)); - } @Test public void testWriteTopContainerAndInnerList() throws Exception { @@ -332,7 +336,7 @@ public class ModificationDiffTest { final DataTreeCandidateTip prepare = prepareModification(dataTree, dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(2)); assertThat(updates.values().size(), is(2)); @@ -345,21 +349,7 @@ public class ModificationDiffTest { listEntryId)); } - private ModificationDiff.NormalizedNodeUpdate getNormalizedNodeUpdateForAfterType( - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates, - final Class<? extends NormalizedNode<?, ?>> containerNodeClass) { - return updates.values().stream() - .filter(update -> containerNodeClass.isAssignableFrom(update.getDataAfter().getClass())) - .findFirst().get(); - } - private ModificationDiff.NormalizedNodeUpdate getNormalizedNodeUpdateForBeforeType( - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates, - final Class<? extends NormalizedNode<?, ?>> containerNodeClass) { - return updates.values().stream() - .filter(update -> containerNodeClass.isAssignableFrom(update.getDataBefore().getClass())) - .findFirst().get(); - } @Test public void testWriteDeepList() throws Exception { @@ -415,7 +405,7 @@ public class ModificationDiffTest { prepare = dataTree.prepare(dataTreeModification); dataTree.commit(prepare); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertUpdate(getNormalizedNodeUpdateForAfterType(updates, MapEntryNode.class), deepListEntryId, null, deepListEntry); } @@ -450,88 +440,10 @@ public class ModificationDiffTest { dataTree.validate(dataTreeModification); prepare = dataTree.prepare(dataTreeModification); - final Map<YangInstanceIdentifier, ModificationDiff.NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); + final Map<YangInstanceIdentifier, NormalizedNodeUpdate> updates = getModificationDiff(prepare).getUpdates(); assertThat(updates.size(), is(1)); assertUpdate(getNormalizedNodeUpdateForBeforeType(updates, MapEntryNode.class), listItemId, mapNode.getValue().iterator().next(), null); } - static void addNodeToTree(final DataTree dataTree, final NormalizedNode<?, ?> node, - final YangInstanceIdentifier id) - throws DataValidationFailedException { - DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot(); - DataTreeModification dataTreeModification = dataTreeSnapshot.newModification(); - dataTreeModification.write(id, node); - dataTreeModification.ready(); - dataTree.validate(dataTreeModification); - DataTreeCandidate prepare = dataTree.prepare(dataTreeModification); - dataTree.commit(prepare); - } - - static TipProducingDataTree getDataTree() throws ReactorException { - final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION); - dataTree.setSchemaContext(getSchemaCtx()); - return dataTree; - } - - static ContainerNode getTopContainer(final String stringValue) { - return Builders.containerBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) - .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue)) - .build(); - } - - static ContainerNode getTopContainerWithLeafList(final String... stringValue) { - final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.leafSetBuilder(); - for (final String value : stringValue) { - leafSetBuilder.withChild(Builders.<String>leafSetEntryBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue<>(NESTED_LEAF_LIST_QNAME, value)) - .withValue(value) - .build()); - } - return Builders.containerBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME)) - .withChild(Builders.containerBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(FOR_LEAF_LIST_QNAME)) - .withChild(leafSetBuilder - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LEAF_LIST_QNAME)) - .build()) - .build()) - .build(); - } - - static MapNode getNestedList(final String listItemName, final String text) { - return Builders.mapBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME)) - .withChild( - Builders.mapEntryBuilder() - .withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, - NAME_LEAF_QNAME, listItemName)) - .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName)) - .withChild(ImmutableNodes.leafNode(TEXT_LEAF_QNAME, text)) - .build() - ) - .build(); - } - - private MapNode getDeepList(final String listItemName) { - return Builders.mapBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DEEP_LIST_QNAME)) - .withChild( - Builders.mapEntryBuilder() - .withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifierWithPredicates(DEEP_LIST_QNAME, - NAME_LEAF_QNAME, listItemName)) - .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName)) - .build() - ) - .build(); - } - - static SchemaContext getSchemaCtx() throws ReactorException { - final CrossSourceStatementReactor.BuildAction buildAction = YangInferencePipeline.RFC6020_REACTOR.newBuild(); - buildAction.addSource(new YangStatementSourceImpl(ModificationDiffTest.class.getResourceAsStream("/test-diff.yang"))); - return buildAction.buildEffective(); - } }
\ No newline at end of file diff --git a/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationMetadata.java b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationMetadata.java new file mode 100644 index 000000000..27d16307a --- /dev/null +++ b/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModificationMetadata.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017 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.data.impl; + +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +abstract class ModificationMetadata { + + private static final String YANG_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:test:diff"; + final QName TOP_CONTAINER_QNAME = QName.create(YANG_NAMESPACE, "2015-01-05", "top-container"); + final QName STRING_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "string"); + final QName NAME_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "name"); + final QName TEXT_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "text"); + final QName NESTED_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-list"); + final QName DEEP_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "deep-list"); + final QName EMPTY_QNAME = QName.create(TOP_CONTAINER_QNAME, "empty"); + + final QName FOR_LEAF_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "for-leaf-list"); + final QName NESTED_LEAF_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-leaf-list"); + + final QName WITH_CHOICE_CONTAINER_QNAME = QName.create(YANG_NAMESPACE, "2015-01-05", "with-choice"); + final QName CHOICE_QNAME = QName.create(WITH_CHOICE_CONTAINER_QNAME, "choice"); + final QName IN_CASE1_LEAF_QNAME = QName.create(WITH_CHOICE_CONTAINER_QNAME, "in-case1"); + + final QName PRESENCE_CONTAINER_QNAME = QName.create(TOP_CONTAINER_QNAME, "presence"); + + final QName NESTED_CONTAINER_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-container"); + final QName NESTED_LIST_IN_CONTAINER_QNAME = QName.create(NESTED_CONTAINER_QNAME, "nested-list-in-container"); + final QName IN_CONTAINER_NAME_LEAF_QNAME = QName.create(NESTED_LIST_IN_CONTAINER_QNAME, "name"); + final QName NESTED_CONTAINER_VAL = QName.create(NESTED_CONTAINER_QNAME, "nested-container-val"); + final QName NESTED_CONTAINER_LEAF_LIST = QName.create(NESTED_CONTAINER_QNAME, "nested-container-leaf-list"); + + final QName NESTED_CHOICE = QName.create(NESTED_CONTAINER_QNAME,"nested-choice"); + final QName NESTED_CASE=QName.create(NESTED_CHOICE,"nested-case"); + final QName UNDER_NESTED_CASE=QName.create(NESTED_CASE,"under-nested-case"); + + final QName AUG_LEAF = QName.create(NESTED_CONTAINER_QNAME,"under-aug-leaf"); + final QName AUG_LEAFLIST = QName.create(NESTED_CONTAINER_QNAME,"under-aug-leaflist"); + final QName AUG_CONTAINER = QName.create(NESTED_CONTAINER_QNAME,"under-aug-container"); + final QName AUG_CONTAINER_LEAF = QName.create(NESTED_CONTAINER_QNAME,"under-aug-cont-leaf"); + final QName AUG_LIST = QName.create(NESTED_CONTAINER_QNAME,"under-aug-list"); + final QName AUG_LIST_KEY = QName.create(NESTED_CONTAINER_QNAME,"under-aug-list-key"); + + final QName NESTED_AUG_CONTAINER = QName.create(AUG_CONTAINER, "nested-under-aug-container"); + final QName NESTED_AUG_CONTAINER_LEAF = QName.create(AUG_CONTAINER, "nested-under-aug-container-leaf"); + final QName NESTED_AUG_LEAF = QName.create(AUG_CONTAINER, "nested-under-aug-leaf"); + final QName NESTED_AUG_LIST = QName.create(AUG_CONTAINER, "nested-under-aug-list"); + final QName NESTED_AUG_LIST_KEY = QName.create(AUG_CONTAINER, "nested-under-aug-list-key"); + final QName NESTED_AUG_LEAF_LIST = QName.create(AUG_CONTAINER, "nested-under-aug-leaf-list"); + + final YangInstanceIdentifier TOP_CONTAINER_ID = YangInstanceIdentifier.of(TOP_CONTAINER_QNAME); + final YangInstanceIdentifier NESTED_LIST_ID = TOP_CONTAINER_ID.node(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME)); +} |