Prevent ConfigPusher from killing its thread
[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");
51         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
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     }
60
61     @Test
62     public void testToInstanceIdentifierListWithNullKey() {
63         exception.expect(ResponseException.class);
64         exception.expectMessage("HTTP 400 Bad Request");
65         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
66     }
67
68     @Test
69     public void testToInstanceIdentifierListWithMissingKey() {
70         exception.expect(ResponseException.class);
71         exception.expectMessage("HTTP 400 Bad Request");
72         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
73     }
74
75     @Test
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());
81     }
82
83     @Test
84     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
85         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
86                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
87         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
88     }
89
90     @Test
91     public void testToInstanceIdentifierChoiceException() {
92         exception.expect(ResponseException.class);
93         exception.expectMessage("HTTP 400 Bad Request");
94         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
95     }
96
97     @Test
98     public void testToInstanceIdentifierCaseException() {
99         exception.expect(ResponseException.class);
100         exception.expectMessage("HTTP 400 Bad Request");
101         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
102     }
103
104     @Test
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");
109     }
110     
111     @Test
112     public void testToInstanceIdentifierWithoutNode() {
113         exception.expect(ResponseException.class);
114         exception.expectMessage("HTTP 400 Bad Request");
115         controllerContext.toInstanceIdentifier("simple-nodes");
116     }
117
118     @Test
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");
123         assertEquals(
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());
126     }
127
128     @Test
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());
134     }
135
136     @Test
137     public void testMountPointWithoutMountService() throws FileNotFoundException {
138         exception.expect(ResponseException.class);
139         exception.expectMessage("HTTP 503 Service Unavailable"); 
140         
141         controllerContext.setMountService(null);
142         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
143                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
144     }
145     
146     @Test
147     public void testMountPointWithoutMountPointSchema() {
148         initMountService(false);
149         exception.expect(ResponseException.class);
150         exception.expectMessage("HTTP 400 Bad Request"); 
151         
152         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
153                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
154     }
155     
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);
163
164         Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
165         SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
166         MountInstance mountInstance = mock(MountInstance.class);
167         if (withSchema)
168             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
169         else
170             when(mountInstance.getSchemaContext()).thenReturn(null);
171         when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
172     }
173 }