* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.sal.restconf.impl.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
import java.net.URI;
import java.util.ArrayList;
-import java.util.Date;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
-import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
-import org.opendaylight.netconf.sal.rest.impl.RestconfApplication;
import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
-import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.common.Revision;
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.NodeIdentifierWithPredicates;
}
}
- private static BrokerFacade brokerFacade;
- private static RestconfImpl restconfImpl;
private static SchemaContext schemaContextYangsIetf;
private static SchemaContext schemaContextTestModule;
+ private static SchemaContext schemaContextModules;
+ private static SchemaContext schemaContextBehindMountPoint;
+
@SuppressWarnings("rawtypes")
private static NormalizedNode answerFromGet;
- private static SchemaContext schemaContextModules;
- private static SchemaContext schemaContextBehindMountPoint;
+ private BrokerFacade brokerFacade;
+ private RestconfImpl restconfImpl;
+ private ControllerContext controllerContext;
+ private DOMMountPoint mountInstance;
private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf";
public static void init() throws Exception {
schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
- brokerFacade = mock(BrokerFacade.class);
- restconfImpl = RestconfImpl.getInstance();
- restconfImpl.setBroker(brokerFacade);
- answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
-
schemaContextModules = TestUtils.loadSchemaContext("/modules");
schemaContextBehindMountPoint = TestUtils.loadSchemaContext("/modules/modules-behind-mount-point");
+
+ answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
}
@Override
// enable(TestProperties.DUMP_ENTITY);
// enable(TestProperties.RECORD_LOG_LEVEL);
// set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+ mountInstance = mock(DOMMountPoint.class);
+ controllerContext = TestRestconfUtils.newControllerContext(schemaContextYangsIetf, mountInstance);
+ brokerFacade = mock(BrokerFacade.class);
+ restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
+
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
- new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
- resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
- resourceConfig.registerClasses(new RestconfApplication().getClasses());
+ new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext),
+ new JsonNormalizedNodeBodyReader(controllerContext),
+ new RestconfDocumentedExceptionMapper(controllerContext));
return resourceConfig;
}
- private static void setControllerContext(final SchemaContext schemaContext) {
- final ControllerContext controllerContext = ControllerContext.getInstance();
+ private void setControllerContext(final SchemaContext schemaContext) {
controllerContext.setSchemas(schemaContext);
- restconfImpl.setControllerContext(controllerContext);
}
/**
@Test
public void getDataWithUrlMountPoint() throws Exception {
when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
- Mockito.anyString())).thenReturn(
- prepareCnDataForMountPointTest(false));
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+ isNull())).thenReturn(prepareCnDataForMountPointTest(false));
when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
- ControllerContext.getInstance().setMountService(mockMountService);
String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
public void getDataWithSlashesBehindMountPoint() throws Exception {
final YangInstanceIdentifier awaitedInstanceIdentifier = prepareInstanceIdentifierForList();
when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), eq(awaitedInstanceIdentifier),
- Mockito.anyString())).thenReturn(prepareCnDataForSlashesBehindMountPointTest());
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
- when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+ isNull())).thenReturn(prepareCnDataForSlashesBehindMountPointTest());
- ControllerContext.getInstance().setMountService(mockMountService);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"
+ "test-module:cont/lst1/GigabitEthernet0%2F0%2F0%2F0";
}
private static QName newTestModuleQName(final String localPart) throws Exception {
- final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-09");
- final URI uri = URI.create("test:module");
- return QName.create(uri, revision, localPart);
+ return QName.create(URI.create("test:module"), Revision.of("2014-01-09"), localPart);
}
@Test
public void getDataMountPointIntoHighestElement() throws Exception {
when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
- Mockito.anyString())).thenReturn(
- prepareCnDataForMountPointTest(true));
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
- when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+ isNull())).thenReturn(prepareCnDataForMountPointTest(true));
- ControllerContext.getInstance().setMountService(mockMountService);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
// /modules
@Test
public void getModulesTest() throws Exception {
- final ControllerContext controllerContext = ControllerContext.getInstance();
- controllerContext.setGlobalSchema(schemaContextModules);
- restconfImpl.setControllerContext(controllerContext);
+ setControllerContext(schemaContextModules);
final String uri = "/modules";
assertEquals("module2", qname.getLocalName());
assertEquals("module:2", qname.getNamespace().toString());
- assertEquals("2014-01-02", qname.getFormattedRevision());
+ assertEquals("2014-01-02", qname.getRevision().get().toString());
response = target(uri).request("application/yang.api+json").get();
assertEquals(200, response.getStatus());
}
}
- private static Matcher validateOperationsResponseXml(final String searchIn, final String rpcName,
- final String namespace) {
- final StringBuilder regex = new StringBuilder();
-
- regex.append("^");
-
- regex.append(".*<operations");
- regex.append(".*xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
- regex.append(".*>");
-
- regex.append(".*<");
- regex.append(".*" + rpcName);
- regex.append(".*" + namespace);
- regex.append(".*/");
- regex.append(".*>");
-
- regex.append(".*</operations.*");
- regex.append(".*>");
-
- regex.append(".*");
- regex.append("$");
- final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
- return ptrn.matcher(searchIn);
- }
-
// /operations/pathToMountPoint/yang-ext:mount
@Ignore
@Test
public void getOperationsBehindMountPointTest() throws Exception {
setControllerContext(schemaContextModules);
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
- ControllerContext.getInstance().setMountService(mockMountService);
final String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
public void getModulesBehindMountPoint() throws Exception {
setControllerContext(schemaContextModules);
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
- ControllerContext.getInstance().setMountService(mockMountService);
final String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
public void getModuleBehindMountPoint() throws Exception {
setControllerContext(schemaContextModules);
- final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
- final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
- when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
- ControllerContext.getInstance().setMountService(mockMountService);
final String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"
+ "module1-behind-mount-point/2014-02-03";
final QName module = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
assertEquals("module1-behind-mount-point", module.getLocalName());
- assertEquals("2014-02-03", module.getFormattedRevision());
+ assertEquals("2014-02-03", module.getRevision().get().toString());
assertEquals("module:1:behind:mount:point", module.getNamespace().toString());
}
@SuppressWarnings("unchecked")
- private static void mockReadOperationalDataMethod() {
+ private void mockReadOperationalDataMethod() {
when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
}
@SuppressWarnings("unchecked")
- private static void mockReadConfigurationDataMethod() {
- when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class), Mockito.anyString()))
+ private void mockReadConfigurationDataMethod() {
+ when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class), isNull()))
.thenReturn(answerFromGet);
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
- private static void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
+ private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
try {
final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont");
final YangInstanceIdentifier ii = YangInstanceIdentifier.builder().node(qNameDepth1Cont).build();
final NormalizedNode value =
- (Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build());
+ Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build();
when(brokerFacade.readConfigurationData(eq(ii))).thenReturn(value);
restconfImpl.readConfigurationData("nested-module:depth1-cont", uriInfo);
fail("Expected RestconfDocumentedException");
return;
}
- final Map<String, NodeData> expChildMap = Maps.newHashMap();
+ final Map<String, NodeData> expChildMap = new HashMap<>();
for (final NodeData expChild : (List<NodeData>) nodeData.data) {
expChildMap.put(expChild.key.toString(), expChild);
}
"Unexpected child element for parent \"" + element.getLocalName() + "\": "
+ actualElement.getLocalName(), expChild);
- if ((expChild.data == null) || (expChild.data instanceof List)) {
+ if (expChild.data == null || expChild.data instanceof List) {
verifyContainerElement(actualElement, expChild);
} else {
assertEquals("Text content for element: " + actualElement.getLocalName(), expChild.data,