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;
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;
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;
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
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);
}
.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);
}
.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);
}
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);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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<String> expectedData = Builders.<String>leafSetBuilder().withNodeIdentifier(
new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
ImmutableList.<LeafSetEntryNode<String>>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);
}
.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<String> expectedData = Builders.<String>orderedLeafSetBuilder().withNodeIdentifier(
new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
ImmutableList.<LeafSetEntryNode<String>>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);
}
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);
}
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);
+ }
}