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");
51 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
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");
62 public void testToInstanceIdentifierListWithNullKey() {
63 exception.expect(ResponseException.class);
64 exception.expectMessage("HTTP 400 Bad Request");
65 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
69 public void testToInstanceIdentifierListWithMissingKey() {
70 exception.expect(ResponseException.class);
71 exception.expectMessage("HTTP 400 Bad Request");
72 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
76 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
77 InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
78 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
79 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
80 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
84 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
85 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
86 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
87 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
91 public void testToInstanceIdentifierChoiceException() {
92 exception.expect(ResponseException.class);
93 exception.expectMessage("HTTP 400 Bad Request");
94 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
98 public void testToInstanceIdentifierCaseException() {
99 exception.expect(ResponseException.class);
100 exception.expectMessage("HTTP 400 Bad Request");
101 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
105 public void testToInstanceIdentifierChoiceCaseException() {
106 exception.expect(ResponseException.class);
107 exception.expectMessage("HTTP 400 Bad Request");
108 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
112 public void testToInstanceIdentifierWithoutNode() {
113 exception.expect(ResponseException.class);
114 exception.expectMessage("HTTP 400 Bad Request");
115 controllerContext.toInstanceIdentifier("simple-nodes");
119 public void testMountPointWithExternModul() throws FileNotFoundException {
120 initMountService(true);
121 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
122 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
124 "[(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}]]",
125 instanceIdentifier.getInstanceIdentifier().getPath().toString());
129 public void testMountPointWithoutExternModul() throws FileNotFoundException {
130 initMountService(true);
131 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
132 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
133 assertEquals(true, instanceIdentifier.getInstanceIdentifier().getPath().isEmpty());
137 public void testMountPointWithoutMountService() throws FileNotFoundException {
138 exception.expect(ResponseException.class);
139 exception.expectMessage("HTTP 503 Service Unavailable");
141 controllerContext.setMountService(null);
142 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
143 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
147 public void testMountPointWithoutMountPointSchema() {
148 initMountService(false);
149 exception.expect(ResponseException.class);
150 exception.expectMessage("HTTP 400 Bad Request");
152 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
153 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
156 public void initMountService(boolean withSchema) {
157 MountService mountService = mock(MountService.class);
158 controllerContext.setMountService(mountService);
159 BrokerFacade brokerFacade = mock(BrokerFacade.class);
160 RestconfImpl restconfImpl = RestconfImpl.getInstance();
161 restconfImpl.setBroker(brokerFacade);
162 restconfImpl.setControllerContext(controllerContext);
164 Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
165 SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
166 MountInstance mountInstance = mock(MountInstance.class);
168 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
170 when(mountInstance.getSchemaContext()).thenReturn(null);
171 when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);