X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Frestconf-nb-rfc8040%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Frestconf%2Fnb%2Frfc8040%2Frests%2Futils%2FReadDataTransactionUtilTest.java;h=428496125b29e898adadaf1aad0c8dd5ef551464;hb=1b3ec6502fd1b1e69704f938f7e80c4f9e9bf2e3;hp=995073f8605142cef2143150fa8219c2deafe66e;hpb=f402dcd49a468e018192c96151bef3a0cdf70d62;p=netconf.git diff --git a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java index 995073f860..428496125b 100644 --- a/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java +++ b/restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java @@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; @@ -19,9 +20,13 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate import com.google.common.collect.ImmutableList; import java.util.Collections; +import java.util.List; import java.util.Optional; +import java.util.Set; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.UriInfo; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -31,13 +36,16 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.mdsal.dom.api.DOMDataBroker; import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction; import org.opendaylight.mdsal.dom.api.DOMTransactionChain; +import org.opendaylight.netconf.dom.api.NetconfDataTreeService; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.context.WriterParameters; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler; -import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper; +import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy; +import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy; +import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy; import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -58,7 +66,10 @@ public class ReadDataTransactionUtilTest { private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER = new YangInstanceIdentifier .NodeIdentifier(QName.create("ns", "2016-02-28", "container")); - private TransactionVarsWrapper wrapper; + private RestconfStrategy mdsalStrategy; + private RestconfStrategy netconfStrategy; + @Mock + private NetconfDataTreeService netconfService; @Mock private DOMTransactionChain transactionChain; @Mock @@ -88,17 +99,21 @@ public class ReadDataTransactionUtilTest { DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class); Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any()); - wrapper = new TransactionVarsWrapper(this.context, null, new TransactionChainHandler(mockDataBroker)); + mdsalStrategy = new MdsalRestconfStrategy(new TransactionChainHandler(mockDataBroker)); + netconfStrategy = new NetconfRestconfStrategy(this.netconfService); } @Test public void readDataConfigTest() { doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path); doReturn(DATA.path).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy); + assertEquals(DATA.data3, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy); assertEquals(DATA.data3, normalizedNode); } @@ -108,10 +123,14 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.CONFIGURATION, DATA.path); doReturn(immediateFluentFuture(Optional.empty())).when(read) .read(LogicalDatastoreType.OPERATIONAL, DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path); + doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).get(DATA.path); doReturn(DATA.path).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy); + assertEquals(DATA.data3, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy); assertEquals(DATA.data3, normalizedNode); } @@ -121,10 +140,14 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.path2); doReturn(immediateFluentFuture(Optional.empty())).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path2); + doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2); + doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2); doReturn(DATA.path2).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy); + assertEquals(DATA.data2, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy); assertEquals(DATA.data2, normalizedNode); } @@ -132,10 +155,13 @@ public class ReadDataTransactionUtilTest { public void readDataNonConfigTest() { doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read) .read(LogicalDatastoreType.OPERATIONAL, DATA.path2); + doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2); doReturn(DATA.path2).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.NONCONFIG; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy); + assertEquals(DATA.data2, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy); assertEquals(DATA.data2, normalizedNode); } @@ -145,16 +171,20 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.CONFIGURATION, DATA.path); doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read) .read(LogicalDatastoreType.OPERATIONAL, DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path); doReturn(DATA.path).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); final ContainerNode checkingData = Builders .containerBuilder() .withNodeIdentifier(NODE_IDENTIFIER) .withChild(DATA.contentLeaf) .withChild(DATA.contentLeaf2) .build(); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy); + assertEquals(checkingData, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy); assertEquals(checkingData, normalizedNode); } @@ -164,15 +194,20 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.CONFIGURATION, DATA.path); doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read) .read(LogicalDatastoreType.OPERATIONAL, DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path); + doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path); doReturn(DATA.path).when(context).getInstanceIdentifier(); - final NormalizedNode normalizedNode = ReadDataTransactionUtil.readData( - RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext); final ContainerNode checkingData = Builders .containerBuilder() .withNodeIdentifier(NODE_IDENTIFIER) .withChild(DATA.contentLeaf) .withChild(DATA.contentLeaf2) .build(); + NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path, + mdsalStrategy); + assertEquals(checkingData, normalizedNode); + + normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path, netconfStrategy); assertEquals(checkingData, normalizedNode); } @@ -182,15 +217,19 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.path3); doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(this.netconfService).get(DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(this.netconfService).getConfig(DATA.path3); doReturn(DATA.path3).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); final MapNode checkingData = Builders .mapBuilder() .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("ns", "2016-02-28", "list"))) .withChild(DATA.checkData) .build(); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path3, mdsalStrategy); + assertEquals(checkingData, normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path3, netconfStrategy); assertEquals(checkingData, normalizedNode); } @@ -200,14 +239,18 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.path3); doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(this.netconfService).get(DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(this.netconfService) + .getConfig(DATA.path3); doReturn(DATA.path3).when(context).getInstanceIdentifier(); - - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext); - final MapNode expectedData = Builders.orderedMapBuilder() .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname)).withChild(DATA.checkData) .build(); + NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, + mdsalStrategy); + assertEquals(expectedData, normalizedNode); + + normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, netconfStrategy); assertEquals(expectedData, normalizedNode); } @@ -217,17 +260,21 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.path3); doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(this.netconfService).get(DATA.path3); + doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(this.netconfService) + .getConfig(DATA.path3); doReturn(DATA.path3).when(context).getInstanceIdentifier(); - - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext); - final UnkeyedListNode expectedData = Builders.unkeyedListBuilder() .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname)) .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier( new YangInstanceIdentifier.NodeIdentifier(DATA.listQname)) .withChild(DATA.unkeyedListEntryNode1.getValue().iterator().next()) .withChild(DATA.unkeyedListEntryNode2.getValue().iterator().next()).build()).build(); + NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, + DATA.path3, mdsalStrategy); + assertEquals(expectedData, normalizedNode); + + normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, netconfStrategy); assertEquals(expectedData, normalizedNode); } @@ -237,15 +284,20 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath); doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath); + doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(this.netconfService) + .get(DATA.leafSetNodePath); + doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(this.netconfService) + .getConfig(DATA.leafSetNodePath); doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier(); - - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext); - final LeafSetNode expectedData = Builders.leafSetBuilder().withNodeIdentifier( new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue( ImmutableList.>builder().addAll(DATA.leafSetNode1.getValue()) .addAll(DATA.leafSetNode2.getValue()).build()).build(); + NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, + mdsalStrategy); + assertEquals(expectedData, normalizedNode); + + normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, netconfStrategy); assertEquals(expectedData, normalizedNode); } @@ -255,15 +307,20 @@ public class ReadDataTransactionUtilTest { .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath); doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath); + doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(this.netconfService) + .get(DATA.leafSetNodePath); + doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(this.netconfService) + .getConfig(DATA.leafSetNodePath); doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier(); - - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext); - final LeafSetNode expectedData = Builders.orderedLeafSetBuilder().withNodeIdentifier( new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue( ImmutableList.>builder().addAll(DATA.orderedLeafSetNode1.getValue()) .addAll(DATA.orderedLeafSetNode2.getValue()).build()).build(); + NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, + mdsalStrategy); + assertEquals(expectedData, normalizedNode); + + normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, netconfStrategy); assertEquals(expectedData, normalizedNode); } @@ -271,18 +328,23 @@ public class ReadDataTransactionUtilTest { public void readDataWrongPathOrNoContentTest() { doReturn(immediateFluentFuture(Optional.empty())).when(read) .read(LogicalDatastoreType.CONFIGURATION, DATA.path2); + doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2); doReturn(DATA.path2).when(context).getInstanceIdentifier(); final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG; - final NormalizedNode normalizedNode = - ReadDataTransactionUtil.readData(valueOfContent, wrapper, schemaContext); + NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy); + assertNull(normalizedNode); + + normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy); assertNull(normalizedNode); } @Test(expected = RestconfDocumentedException.class) public void readDataFailTest() { - final String valueOfContent = RestconfDataServiceConstant.ReadData.READ_TYPE_TX; - final NormalizedNode normalizedNode = ReadDataTransactionUtil.readData( - valueOfContent, wrapper, schemaContext); + final String valueOfContent = "nonsense"; + NormalizedNode normalizedNode = readData(valueOfContent, null, mdsalStrategy); + assertNull(normalizedNode); + + normalizedNode = readData(valueOfContent, null, netconfStrategy); assertNull(normalizedNode); } @@ -495,4 +557,60 @@ public class ReadDataTransactionUtilTest { assertNull(writerParameters.getWithDefault()); assertFalse(writerParameters.isTagged()); } + + /** + * Test when parameter is present at most once. + */ + @Test + public void checkParameterCountTest() { + ReadDataTransactionUtil.checkParameterCount(List.of("all"), RestconfDataServiceConstant.ReadData.CONTENT); + } + + /** + * Test when parameter is present more than once. + */ + @Test + public void checkParameterCountNegativeTest() { + final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, + () -> ReadDataTransactionUtil.checkParameterCount(List.of("config", "nonconfig", "all"), + RestconfDataServiceConstant.ReadData.CONTENT)); + assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType()); + assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag()); + assertEquals("Error status code is not correct", 400, ex.getErrors().get(0).getErrorTag().getStatusCode()); + } + + + /** + * Test when all parameters are allowed. + */ + @Test + public void checkParametersTypesTest() { + ReadDataTransactionUtil.checkParametersTypes(Set.of("content"), + RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH); + } + + /** + * Test when not allowed parameter type is used. + */ + @Test + public void checkParametersTypesNegativeTest() { + final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, + () -> ReadDataTransactionUtil.checkParametersTypes(Set.of("not-allowed-parameter"), + RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH)); + assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType()); + assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag()); + assertEquals("Error status code is not correct", 400, ex.getErrors().get(0).getErrorTag().getStatusCode()); + } + + /** + * Read specific type of data from data store via transaction. + * + * @param valueOfContent type of data to read (config, state, all) + * @param strategy {@link RestconfStrategy} - wrapper for variables + * @return {@link NormalizedNode} + */ + private @Nullable NormalizedNode readData(final @NonNull String valueOfContent, + final YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy) { + return ReadDataTransactionUtil.readData(valueOfContent, path, strategy, null, schemaContext); + } }