eef9e414e93e75d90ec29719d7f93b85952c9400
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URITest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.io.FileNotFoundException;
18 import java.util.Set;
19
20 import org.junit.BeforeClass;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
25 import org.opendaylight.controller.sal.core.api.mount.MountService;
26 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
29 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 public class URITest {
37
38     private static final ControllerContext controllerContext = ControllerContext.getInstance();
39
40     @Rule
41     public ExpectedException exception = ExpectedException.none();
42
43     @BeforeClass
44     public static void init() throws FileNotFoundException {
45         Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
46         assertNotNull(allModules);
47         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
48         controllerContext.setSchemas(schemaContext);
49     }
50
51     @Test
52     public void testToInstanceIdentifierList() throws FileNotFoundException {
53         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
54                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
55         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
56
57         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
58         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
59
60         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
61         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
62
63         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
64         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
65
66     }
67
68     @Test
69     public void testToInstanceIdentifierListWithNullKey() {
70         exception.expect(ResponseException.class);
71         exception.expectMessage("HTTP 400 Bad Request");
72         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
73     }
74
75     @Test
76     public void testToInstanceIdentifierListWithMissingKey() {
77         exception.expect(ResponseException.class);
78         exception.expectMessage("HTTP 400 Bad Request");
79         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
80     }
81
82     @Test
83     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
84         InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
85         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
86         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
87         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
88     }
89
90     @Test
91     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
92         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
93                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
94         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
95     }
96
97     @Test
98     public void testToInstanceIdentifierChoiceException() {
99         exception.expect(ResponseException.class);
100         exception.expectMessage("HTTP 400 Bad Request");
101         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
102     }
103
104     @Test
105     public void testToInstanceIdentifierCaseException() {
106         exception.expect(ResponseException.class);
107         exception.expectMessage("HTTP 400 Bad Request");
108         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
109     }
110
111     @Test
112     public void testToInstanceIdentifierChoiceCaseException() {
113         exception.expect(ResponseException.class);
114         exception.expectMessage("HTTP 400 Bad Request");
115         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
116     }
117     
118     @Test
119     public void testToInstanceIdentifierWithoutNode() {
120         exception.expect(ResponseException.class);
121         exception.expectMessage("HTTP 400 Bad Request");
122         controllerContext.toInstanceIdentifier("simple-nodes");
123     }
124
125     @Test
126     public void testMountPointWithExternModul() throws FileNotFoundException {
127         initMountService(true);
128         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
129                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
130         assertEquals(
131                 "[(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}]]",
132                 instanceIdentifier.getInstanceIdentifier().getPath().toString());
133     }
134
135     @Test
136     public void testMountPointWithoutExternModul() throws FileNotFoundException {
137         initMountService(true);
138         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
139                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
140         assertEquals(true, instanceIdentifier.getInstanceIdentifier().getPath().isEmpty());
141     }
142
143     @Test
144     public void testMountPointWithoutMountService() throws FileNotFoundException {
145         exception.expect(ResponseException.class);
146         exception.expectMessage("HTTP 503 Service Unavailable"); 
147         
148         controllerContext.setMountService(null);
149         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
150                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
151     }
152     
153     @Test
154     public void testMountPointWithoutMountPointSchema() {
155         initMountService(false);
156         exception.expect(ResponseException.class);
157         exception.expectMessage("HTTP 400 Bad Request"); 
158         
159         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
160                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
161     }
162     
163     public void initMountService(boolean withSchema) {
164         MountService mountService = mock(MountService.class);
165         controllerContext.setMountService(mountService);
166         BrokerFacade brokerFacade = mock(BrokerFacade.class);
167         RestconfImpl restconfImpl = RestconfImpl.getInstance();
168         restconfImpl.setBroker(brokerFacade);
169         restconfImpl.setControllerContext(controllerContext);
170
171         Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
172         SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
173         MountInstance mountInstance = mock(MountInstance.class);
174         if (withSchema)
175             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
176         else
177             when(mountInstance.getSchemaContext()).thenReturn(null);
178         when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
179     }
180 }