import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
private static RestconfImpl restconfImpl;
private static SchemaContext schemaContextYangsIetf;
private static SchemaContext schemaContextTestModule;
+ @SuppressWarnings("rawtypes")
private static NormalizedNode answerFromGet;
private static SchemaContext schemaContextModules;
/**
* MountPoint test. URI represents mount point.
*/
+ @SuppressWarnings("unchecked")
@Test
public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException, ParseException {
when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class))).thenReturn(
* Slashes in URI behind mount point. lst1 element with key GigabitEthernet0%2F0%2F0%2F0 (GigabitEthernet0/0/0/0) is
* requested via GET HTTP operation. It is tested whether %2F character is replaced with simple / in
* InstanceIdentifier parameter in method
- * {@link BrokerFacade#readConfigurationDataBehindMountPoint(MountInstance, YangInstanceIdentifier)} which is called in
+ * {@link BrokerFacade#readConfigurationData(DOMMountPoint, YangInstanceIdentifier)} which is called in
* method {@link RestconfImpl#readConfigurationData}
*
* @throws ParseException
// /operations
@Test
- @Ignore // FIXME restconf-netconf yang schema has to be updated for operations container
public void getOperationsTest() throws FileNotFoundException, UnsupportedEncodingException {
setControllerContext(schemaContextModules);
}
private void validateOperationsResponseXml(final Document responseDoc, final SchemaContext schemaContext) {
+
final Element operationsElem = responseDoc.getDocumentElement();
assertEquals(RESTCONF_NS, operationsElem.getNamespaceURI());
assertEquals("operations", operationsElem.getLocalName());
-
- final HashSet<QName> foundOperations = new HashSet<>();
-
final NodeList operationsList = operationsElem.getChildNodes();
- for(int i = 0;i < operationsList.getLength();i++) {
- final org.w3c.dom.Node operation = operationsList.item(i);
+ final HashSet<String> foundOperations = new HashSet<>();
- final String namespace = operation.getNamespaceURI();
- final String name = operation.getLocalName();
- final QName opQName = QName.create(URI.create(namespace), null, name);
- foundOperations.add(opQName);
+ for (int i = 0; i < operationsList.getLength(); i++) {
+ final org.w3c.dom.Node operation = operationsList.item(i);
+ foundOperations.add(operation.getLocalName());
}
- for(final RpcDefinition schemaOp : schemaContext.getOperations()) {
- assertTrue(foundOperations.contains(schemaOp.getQName().withoutRevision()));
+ for (final RpcDefinition schemaOp : schemaContext.getOperations()) {
+ assertTrue(foundOperations.contains(schemaOp.getQName().getLocalName()));
}
-
}
// /operations/pathToMountPoint/yang-ext:mount
@Test
- @Ignore // FIXME fix the way to provide operations overview functionality asap
public void getOperationsBehindMountPointTest() throws FileNotFoundException, UnsupportedEncodingException {
setControllerContext(schemaContextModules);
private Matcher validateOperationsResponseJson(final String searchIn, final String rpcName, final String moduleName) {
final StringBuilder regex = new StringBuilder();
- regex.append("^");
-
- regex.append(".*\\{");
- regex.append(".*\"");
-
- // operations prefix optional
- regex.append("(");
- regex.append("ietf-restconf:");
- regex.append("|)");
- // :operations prefix optional
-
- regex.append("operations\"");
- regex.append(".*:");
- regex.append(".*\\{");
-
- regex.append(".*\"" + moduleName);
- regex.append(":");
- regex.append(rpcName + "\"");
- regex.append(".*\\[");
- regex.append(".*null");
- regex.append(".*\\]");
-
- regex.append(".*\\}");
- regex.append(".*\\}");
-
- regex.append(".*");
- regex.append("$");
+ regex.append(".*\"" + rpcName + "\"");
final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
return ptrn.matcher(searchIn);
}
- private void prepareMockForModulesTest(final ControllerContext mockedControllerContext)
- throws FileNotFoundException {
- final SchemaContext schemaContext = TestUtils.loadSchemaContext("/modules");
- mockedControllerContext.setGlobalSchema(schemaContext);
- // when(mockedControllerContext.getGlobalSchema()).thenReturn(schemaContext);
- }
-
private int get(final String uri, final String mediaType) {
return target(uri).request(mediaType).get().getStatus();
}
type string;
}
*/
+ @SuppressWarnings("rawtypes")
private NormalizedNode prepareCnDataForMountPointTest(final boolean wrapToCont) throws URISyntaxException, ParseException {
final String testModuleDate = "2014-01-09";
final ContainerNode contChild = Builders
}
+ @SuppressWarnings("unchecked")
private void mockReadOperationalDataMethod() {
when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
}
+ @SuppressWarnings("unchecked")
private void mockReadConfigurationDataMethod() {
when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
}
+ @SuppressWarnings("rawtypes")
private NormalizedNode prepareCnDataForSlashesBehindMountPointTest() throws ParseException {
return ImmutableMapEntryNodeBuilder
.create()
assertFalse(matcher.matches());
}
- @Test
- @Ignore
- public void getDataWithUriDepthParameterTest() throws UnsupportedEncodingException {
- setControllerContext(schemaContextModules);
-
- final CompositeNode depth1Cont = toCompositeNode(toCompositeNodeData(
- toNestedQName("depth1-cont"),
- toCompositeNodeData(
- toNestedQName("depth2-cont1"),
- toCompositeNodeData(
- toNestedQName("depth3-cont1"),
- toCompositeNodeData(toNestedQName("depth4-cont1"),
- toSimpleNodeData(toNestedQName("depth5-leaf1"), "depth5-leaf1-value")),
- toSimpleNodeData(toNestedQName("depth4-leaf1"), "depth4-leaf1-value")),
- toSimpleNodeData(toNestedQName("depth3-leaf1"), "depth3-leaf1-value")),
- toCompositeNodeData(
- toNestedQName("depth2-cont2"),
- toCompositeNodeData(
- toNestedQName("depth3-cont2"),
- toCompositeNodeData(toNestedQName("depth4-cont2"),
- toSimpleNodeData(toNestedQName("depth5-leaf2"), "depth5-leaf2-value")),
- toSimpleNodeData(toNestedQName("depth4-leaf2"), "depth4-leaf2-value")),
- toSimpleNodeData(toNestedQName("depth3-leaf2"), "depth3-leaf2-value")),
- toSimpleNodeData(toNestedQName("depth2-leaf1"), "depth2-leaf1-value")));
-
- final Module module = TestUtils.findModule(schemaContextModules.getModules(), "nested-module");
- assertNotNull(module);
-
- final DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode("depth1-cont", module);
- assertNotNull(dataSchemaNode);
-
- when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class))).thenReturn(
- TestUtils.compositeNodeToDatastoreNormalizedNode(depth1Cont, dataSchemaNode));
-
- // Test config with depth 1
-
- Response response = target("/config/nested-module:depth1-cont").queryParam("depth", "1")
- .request("application/xml").get();
-
- verifyXMLResponse(response, expectEmptyContainer("depth1-cont"));
-
- // Test config with depth 2
-
- response = target("/config/nested-module:depth1-cont").queryParam("depth", "2").request("application/xml")
- .get();
-
- // String
- // xml="<depth1-cont><depth2-cont1/><depth2-cont2/><depth2-leaf1>depth2-leaf1-value</depth2-leaf1></depth1-cont>";
- // Response mr=mock(Response.class);
- // when(mr.getEntity()).thenReturn( new
- // java.io.StringBufferInputStream(xml) );
-
- verifyXMLResponse(
- response,
- expectContainer("depth1-cont", expectEmptyContainer("depth2-cont1"),
- expectEmptyContainer("depth2-cont2"), expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
-
- // Test config with depth 3
-
- response = target("/config/nested-module:depth1-cont").queryParam("depth", "3").request("application/xml")
- .get();
-
- verifyXMLResponse(
- response,
- expectContainer(
- "depth1-cont",
- expectContainer("depth2-cont1", expectEmptyContainer("depth3-cont1"),
- expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
- expectContainer("depth2-cont2", expectEmptyContainer("depth3-cont2"),
- expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
- expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
-
- // Test config with depth 4
-
- response = target("/config/nested-module:depth1-cont").queryParam("depth", "4").request("application/xml")
- .get();
-
- verifyXMLResponse(
- response,
- expectContainer(
- "depth1-cont",
- expectContainer(
- "depth2-cont1",
- expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
- expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
- expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
- expectContainer(
- "depth2-cont2",
- expectContainer("depth3-cont2", expectEmptyContainer("depth4-cont2"),
- expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
- expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
- expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
-
- // Test config with depth 5
-
- response = target("/config/nested-module:depth1-cont").queryParam("depth", "5").request("application/xml")
- .get();
-
- verifyXMLResponse(
- response,
- expectContainer(
- "depth1-cont",
- expectContainer(
- "depth2-cont1",
- expectContainer(
- "depth3-cont1",
- expectContainer("depth4-cont1",
- expectLeaf("depth5-leaf1", "depth5-leaf1-value")),
- expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
- expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
- expectContainer(
- "depth2-cont2",
- expectContainer(
- "depth3-cont2",
- expectContainer("depth4-cont2",
- expectLeaf("depth5-leaf2", "depth5-leaf2-value")),
- expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
- expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
- expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
-
- // Test config with depth unbounded
-
- response = target("/config/nested-module:depth1-cont").queryParam("depth", "unbounded")
- .request("application/xml").get();
-
- verifyXMLResponse(
- response,
- expectContainer(
- "depth1-cont",
- expectContainer(
- "depth2-cont1",
- expectContainer(
- "depth3-cont1",
- expectContainer("depth4-cont1",
- expectLeaf("depth5-leaf1", "depth5-leaf1-value")),
- expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
- expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
- expectContainer(
- "depth2-cont2",
- expectContainer(
- "depth3-cont2",
- expectContainer("depth4-cont2",
- expectLeaf("depth5-leaf2", "depth5-leaf2-value")),
- expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
- expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
- expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
-
- // Test operational
-
- final CompositeNode depth2Cont1 = toCompositeNode(toCompositeNodeData(
- toNestedQName("depth2-cont1"),
- toCompositeNodeData(
- toNestedQName("depth3-cont1"),
- toCompositeNodeData(toNestedQName("depth4-cont1"),
- toSimpleNodeData(toNestedQName("depth5-leaf1"), "depth5-leaf1-value")),
- toSimpleNodeData(toNestedQName("depth4-leaf1"), "depth4-leaf1-value")),
- toSimpleNodeData(toNestedQName("depth3-leaf1"), "depth3-leaf1-value")));
-
- assertTrue(dataSchemaNode instanceof DataNodeContainer);
- DataSchemaNode depth2cont1Schema = null;
- for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
- if (childNode.getQName().getLocalName().equals("depth2-cont1")) {
- depth2cont1Schema = childNode;
- break;
- }
- }
- assertNotNull(depth2Cont1);
-
- when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(
- TestUtils.compositeNodeToDatastoreNormalizedNode(depth2Cont1, depth2cont1Schema));
-
- response = target("/operational/nested-module:depth1-cont/depth2-cont1").queryParam("depth", "3")
- .request("application/xml").get();
-
- verifyXMLResponse(
- response,
- expectContainer(
- "depth2-cont1",
- expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
- expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
- expectLeaf("depth3-leaf1", "depth3-leaf1-value")));
- }
-
/**
* Tests behavior when invalid value of depth URI parameter
*/
getDataWithInvalidDepthParameterTest(mockInfo);
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
try {
final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont");
}
}
+ @SuppressWarnings("unused")
private void verifyXMLResponse(final Response response, final NodeData nodeData) {
final Document doc = response.readEntity(Document.class);
// Document doc = TestUtils.loadDocumentFrom((InputStream) response.getEntity());
}
}
- private NodeData expectContainer(final String name, final NodeData... childData) {
- return new NodeData(name, Lists.newArrayList(childData));
- }
-
- private NodeData expectEmptyContainer(final String name) {
- return new NodeData(name, null);
- }
-
- private NodeData expectLeaf(final String name, final Object value) {
- return new NodeData(name, value);
- }
-
- private QName toNestedQName(final String localName) {
- return QName.create("urn:nested:module", "2014-06-3", localName);
- }
-
- @SuppressWarnings("unchecked")
- private CompositeNode toCompositeNode(final NodeData nodeData) {
- final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
- builder.setQName((QName) nodeData.key);
-
- for (final NodeData child : (List<NodeData>) nodeData.data) {
- if (child.data instanceof List) {
- builder.add(toCompositeNode(child));
- } else {
- builder.addLeaf((QName) child.key, child.data);
- }
- }
-
- return builder.toInstance();
- }
-
- private NodeData toCompositeNodeData(final QName key, final NodeData... childData) {
- return new NodeData(key, Lists.newArrayList(childData));
- }
-
- private NodeData toSimpleNodeData(final QName key, final Object value) {
- return new NodeData(key, value);
- }
-
}