Merge "Fixed for bug 1197"
[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 com.google.common.collect.Iterables;
18 import java.io.FileNotFoundException;
19 import java.util.Set;
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.RestconfDocumentedException;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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(RestconfDocumentedException.class);
71         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
72     }
73
74     @Test
75     public void testToInstanceIdentifierListWithMissingKey() {
76         exception.expect(RestconfDocumentedException.class);
77         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
78     }
79
80     @Test
81     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
82         InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
83         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
84         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
85         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
86     }
87
88     @Test
89     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
90         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
91                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
92         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
93     }
94
95     @Test
96     public void testToInstanceIdentifierChoiceException() {
97         exception.expect(RestconfDocumentedException.class);
98         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
99     }
100
101     @Test
102     public void testToInstanceIdentifierCaseException() {
103         exception.expect(RestconfDocumentedException.class);
104         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
105     }
106
107     @Test
108     public void testToInstanceIdentifierChoiceCaseException() {
109         exception.expect(RestconfDocumentedException.class);
110         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
111     }
112
113     @Test
114     public void testToInstanceIdentifierWithoutNode() {
115         exception.expect(RestconfDocumentedException.class);
116         controllerContext.toInstanceIdentifier("simple-nodes");
117     }
118
119     @Test
120     public void testMountPointWithExternModul() throws FileNotFoundException {
121         initMountService(true);
122         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
123                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
124         assertEquals(
125                 "[(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}]]",
126                 instanceIdentifier.getInstanceIdentifier().getPath().toString());
127     }
128
129     @Test
130     public void testMountPointWithoutExternModul() throws FileNotFoundException {
131         initMountService(true);
132         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
133                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
134         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
135     }
136
137     @Test
138     public void testMountPointWithoutMountService() throws FileNotFoundException {
139         exception.expect(RestconfDocumentedException.class);
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(RestconfDocumentedException.class);
150
151         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
152                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
153     }
154
155     public void initMountService(final boolean withSchema) {
156         MountService mountService = mock(MountService.class);
157         controllerContext.setMountService(mountService);
158         BrokerFacade brokerFacade = mock(BrokerFacade.class);
159         RestconfImpl restconfImpl = RestconfImpl.getInstance();
160         restconfImpl.setBroker(brokerFacade);
161         restconfImpl.setControllerContext(controllerContext);
162
163         Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
164         SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
165         MountInstance mountInstance = mock(MountInstance.class);
166         if (withSchema) {
167             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
168         } else {
169             when(mountInstance.getSchemaContext()).thenReturn(null);
170         }
171         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountInstance);
172     }
173 }