Replace old yang parser usages in restconf
[netconf.git] / restconf / 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.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38
39 public class URITest {
40
41     private static final ControllerContext controllerContext = ControllerContext.getInstance();
42
43     @Rule
44     public ExpectedException exception = ExpectedException.none();
45
46     @BeforeClass
47     public static void init() throws FileNotFoundException, ReactorException {
48         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
49         final Set<Module> allModules = schemaContext.getModules();
50         assertNotNull(allModules);
51
52         controllerContext.setSchemas(schemaContext);
53     }
54
55     @Test
56     public void testToInstanceIdentifierList() throws FileNotFoundException {
57         InstanceIdentifierContext<?> instanceIdentifier = controllerContext
58                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
60
61         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
62         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
63
64         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
65         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
66
67         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
68         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
69
70     }
71
72     @Test
73     public void testToInstanceIdentifierListWithNullKey() {
74         this.exception.expect(RestconfDocumentedException.class);
75         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
76     }
77
78     @Test
79     public void testToInstanceIdentifierListWithMissingKey() {
80         this.exception.expect(RestconfDocumentedException.class);
81         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
82     }
83
84     @Test
85     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
86         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
87         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
88         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
89         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
90     }
91
92     @Test
93     @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
94     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
95         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
96                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
97         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
98     }
99
100     @Test
101     public void testToInstanceIdentifierChoiceException() {
102         this.exception.expect(RestconfDocumentedException.class);
103         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
104     }
105
106     @Test
107     public void testToInstanceIdentifierCaseException() {
108         this.exception.expect(RestconfDocumentedException.class);
109         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
110     }
111
112     @Test
113     public void testToInstanceIdentifierChoiceCaseException() {
114         this.exception.expect(RestconfDocumentedException.class);
115         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
116     }
117
118     @Test
119     public void testToInstanceIdentifierWithoutNode() {
120         this.exception.expect(RestconfDocumentedException.class);
121         controllerContext.toInstanceIdentifier("simple-nodes");
122     }
123
124     @Test
125     public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
126         initMountService(true);
127         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
128                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
129         assertEquals(
130                 "[(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)student[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
131                 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
132     }
133
134     @Test
135     public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
136         initMountService(true);
137         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
138                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
139         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
140     }
141
142     @Test
143     public void testMountPointWithoutMountService() throws FileNotFoundException {
144         this.exception.expect(RestconfDocumentedException.class);
145
146         controllerContext.setMountService(null);
147         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
148     }
149
150     @Test
151     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
152         initMountService(false);
153         this.exception.expect(RestconfDocumentedException.class);
154
155         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
156     }
157
158     public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
159         final DOMMountPointService mountService = mock(DOMMountPointService.class);
160         controllerContext.setMountService(mountService);
161         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
162         final RestconfImpl restconfImpl = RestconfImpl.getInstance();
163         restconfImpl.setBroker(brokerFacade);
164         restconfImpl.setControllerContext(controllerContext);
165         final SchemaContext schemaContext2 = TestUtils.loadSchemaContext("/test-config-data/yang2");
166         final Set<Module> modules2 = schemaContext2.getModules();
167
168         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
169         if (withSchema) {
170             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
171         } else {
172             when(mountInstance.getSchemaContext()).thenReturn(null);
173         }
174         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
175     }
176 }