1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
10 import java.io.FileNotFoundException;
13 import org.junit.BeforeClass;
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
17 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
18 import org.opendaylight.controller.sal.core.api.mount.MountService;
19 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
20 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
21 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
22 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
23 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 public class URITest {
31 private static final ControllerContext controllerContext = ControllerContext.getInstance();
34 public ExpectedException exception = ExpectedException.none();
37 public static void init() throws FileNotFoundException {
38 Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
39 assertNotNull(allModules);
40 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
41 controllerContext.setSchemas(schemaContext);
45 public void testToInstanceIdentifierList() throws FileNotFoundException {
46 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
47 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
48 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
50 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo/full-name");
51 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "full-name");
53 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
54 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
56 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
57 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
59 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users/user/foo");
60 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
64 public void testToInstanceIdentifierListWithNullKey() {
65 exception.expect(ResponseException.class);
66 exception.expectMessage("HTTP 400 Bad Request");
67 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
71 public void testToInstanceIdentifierListWithMissingKey() {
72 exception.expect(ResponseException.class);
73 exception.expectMessage("HTTP 400 Bad Request");
74 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
78 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
79 InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
80 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
81 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
82 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
86 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
87 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
88 .toInstanceIdentifier("simple-nodes:food/nonalcoholic/beer");
89 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "beer");
93 public void testToInstanceIdentifierChoiceException() {
94 exception.expect(ResponseException.class);
95 exception.expectMessage("HTTP 400 Bad Request");
96 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
100 public void testToInstanceIdentifierCaseException() {
101 exception.expect(ResponseException.class);
102 exception.expectMessage("HTTP 400 Bad Request");
103 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
107 public void testToInstanceIdentifierChoiceCaseException() {
108 exception.expect(ResponseException.class);
109 exception.expectMessage("HTTP 400 Bad Request");
110 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
114 public void testToInstanceIdentifierWithoutNode() {
115 exception.expect(ResponseException.class);
116 exception.expectMessage("HTTP 400 Bad Request");
117 controllerContext.toInstanceIdentifier("simple-nodes");
121 public void testMountPointWithExternModul() throws FileNotFoundException {
122 initMountService(true);
123 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
124 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
126 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, (urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
127 instanceIdentifier.getInstanceIdentifier().getPath().toString());
131 public void testMountPointWithoutExternModul() throws FileNotFoundException {
132 initMountService(true);
133 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
134 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
135 assertEquals(true, instanceIdentifier.getInstanceIdentifier().getPath().isEmpty());
139 public void testMountPointWithoutMountService() throws FileNotFoundException {
140 exception.expect(ResponseException.class);
141 exception.expectMessage("HTTP 503 Service Unavailable");
143 controllerContext.setMountService(null);
144 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
145 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
149 public void testMountPointWithoutMountPointSchema() {
150 initMountService(false);
151 exception.expect(ResponseException.class);
152 exception.expectMessage("HTTP 400 Bad Request");
154 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
155 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
158 public void initMountService(boolean withSchema) {
159 MountService mountService = mock(MountService.class);
160 controllerContext.setMountService(mountService);
161 BrokerFacade brokerFacade = mock(BrokerFacade.class);
162 RestconfImpl restconfImpl = RestconfImpl.getInstance();
163 restconfImpl.setBroker(brokerFacade);
164 restconfImpl.setControllerContext(controllerContext);
166 Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
167 SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
168 MountInstance mountInstance = mock(MountInstance.class);
170 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
172 when(mountInstance.getSchemaContext()).thenReturn(null);
173 when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);