fix of Bug 314
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URITest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
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;
9
10 import java.io.FileNotFoundException;
11 import java.util.Set;
12
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;
28
29 public class URITest {
30
31     private static final ControllerContext controllerContext = ControllerContext.getInstance();
32
33     @Rule
34     public ExpectedException exception = ExpectedException.none();
35
36     @BeforeClass
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);
42     }
43
44     @Test
45     public void testToInstanceIdentifierList() throws FileNotFoundException {
46         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
47                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
48         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
49
50         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo/full-name");
51         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "full-name");
52
53         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
54         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
55
56         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
57         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
58
59         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users/user/foo");
60         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
61     }
62
63     @Test
64     public void testToInstanceIdentifierListWithNullKey() {
65         exception.expect(ResponseException.class);
66         exception.expectMessage("HTTP 400 Bad Request");
67         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
68     }
69
70     @Test
71     public void testToInstanceIdentifierListWithMissingKey() {
72         exception.expect(ResponseException.class);
73         exception.expectMessage("HTTP 400 Bad Request");
74         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
75     }
76
77     @Test
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());
83     }
84
85     @Test
86     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
87         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
88                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic/beer");
89         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "beer");
90     }
91
92     @Test
93     public void testToInstanceIdentifierChoiceException() {
94         exception.expect(ResponseException.class);
95         exception.expectMessage("HTTP 400 Bad Request");
96         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
97     }
98
99     @Test
100     public void testToInstanceIdentifierCaseException() {
101         exception.expect(ResponseException.class);
102         exception.expectMessage("HTTP 400 Bad Request");
103         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
104     }
105
106     @Test
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");
111     }
112     
113     @Test
114     public void testToInstanceIdentifierWithoutNode() {
115         exception.expect(ResponseException.class);
116         exception.expectMessage("HTTP 400 Bad Request");
117         controllerContext.toInstanceIdentifier("simple-nodes");
118     }
119
120     @Test
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");
125         assertEquals(
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());
128     }
129
130     @Test
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());
136     }
137
138     @Test
139     public void testMountPointWithoutMountService() throws FileNotFoundException {
140         exception.expect(ResponseException.class);
141         exception.expectMessage("HTTP 503 Service Unavailable"); 
142         
143         controllerContext.setMountService(null);
144         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
145                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
146     }
147     
148     @Test
149     public void testMountPointWithoutMountPointSchema() {
150         initMountService(false);
151         exception.expect(ResponseException.class);
152         exception.expectMessage("HTTP 400 Bad Request"); 
153         
154         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
155                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
156     }
157     
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);
165
166         Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
167         SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
168         MountInstance mountInstance = mock(MountInstance.class);
169         if (withSchema)
170             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
171         else
172             when(mountInstance.getSchemaContext()).thenReturn(null);
173         when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
174     }
175 }