BUG-868: do not use YangInstanceIdentifier.getPath()
[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 import com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
19 import java.io.FileNotFoundException;
20 import java.util.Set;
21 import org.junit.BeforeClass;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
28 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 public class URITest {
38
39     private static final ControllerContext controllerContext = ControllerContext.getInstance();
40
41     @Rule
42     public ExpectedException exception = ExpectedException.none();
43
44     @BeforeClass
45     public static void init() throws FileNotFoundException {
46         Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
47         assertNotNull(allModules);
48         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
49         controllerContext.setSchemas(schemaContext);
50     }
51
52     @Test
53     public void testToInstanceIdentifierList() throws FileNotFoundException {
54         InstanceIdentifierContext instanceIdentifier = controllerContext
55                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
56         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
57
58         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
60
61         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
62         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
63
64         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
65         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
66
67     }
68
69     @Test
70     public void testToInstanceIdentifierListWithNullKey() {
71         exception.expect(RestconfDocumentedException.class);
72         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
73     }
74
75     @Test
76     public void testToInstanceIdentifierListWithMissingKey() {
77         exception.expect(RestconfDocumentedException.class);
78         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
79     }
80
81     @Test
82     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
83         InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
84         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
85         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
86         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
87     }
88
89     @Test
90     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
91         InstanceIdentifierContext instanceIdentifier = controllerContext
92                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
93         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
94     }
95
96     @Test
97     public void testToInstanceIdentifierChoiceException() {
98         exception.expect(RestconfDocumentedException.class);
99         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
100     }
101
102     @Test
103     public void testToInstanceIdentifierCaseException() {
104         exception.expect(RestconfDocumentedException.class);
105         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
106     }
107
108     @Test
109     public void testToInstanceIdentifierChoiceCaseException() {
110         exception.expect(RestconfDocumentedException.class);
111         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
112     }
113
114     @Test
115     public void testToInstanceIdentifierWithoutNode() {
116         exception.expect(RestconfDocumentedException.class);
117         controllerContext.toInstanceIdentifier("simple-nodes");
118     }
119
120     @Test
121     public void testMountPointWithExternModul() throws FileNotFoundException {
122         initMountService(true);
123         InstanceIdentifierContext 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                 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
128     }
129
130     @Test
131     public void testMountPointWithoutExternModul() throws FileNotFoundException {
132         initMountService(true);
133         InstanceIdentifierContext instanceIdentifier = controllerContext
134                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
135         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
136     }
137
138     @Test
139     public void testMountPointWithoutMountService() throws FileNotFoundException {
140         exception.expect(RestconfDocumentedException.class);
141
142         controllerContext.setMountService(null);
143         controllerContext.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         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
152     }
153
154     public void initMountService(final boolean withSchema) {
155         DOMMountPointService mountService = mock(DOMMountPointService.class);
156         controllerContext.setMountService(mountService);
157         BrokerFacade brokerFacade = mock(BrokerFacade.class);
158         RestconfImpl restconfImpl = RestconfImpl.getInstance();
159         restconfImpl.setBroker(brokerFacade);
160         restconfImpl.setControllerContext(controllerContext);
161
162         Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
163         SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
164         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
165         if (withSchema) {
166             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
167         } else {
168             when(mountInstance.getSchemaContext()).thenReturn(null);
169         }
170         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
171     }
172 }