Bump upstreams for Silicon
[netconf.git] / restconf / restconf-nb-bierman02 / 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.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.io.FileNotFoundException;
18 import java.util.Optional;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34
35 public class URITest {
36
37     private static EffectiveModelContext schemaContext;
38     private static EffectiveModelContext mountSchemaContext;
39
40     private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
41     private final ControllerContext controllerContext =
42             TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
43
44     @Rule
45     public ExpectedException exception = ExpectedException.none();
46
47     @BeforeClass
48     public static void init() throws FileNotFoundException, ReactorException {
49         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
50         mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
51     }
52
53     @Test
54     public void testToInstanceIdentifierList() throws FileNotFoundException {
55         InstanceIdentifierContext<?> instanceIdentifier = controllerContext
56                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
57         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
58
59         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
60         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
61
62         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
63         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
64
65         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
66         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
67
68     }
69
70     @Test
71     public void testToInstanceIdentifierWithDoubleSlash() {
72         InstanceIdentifierContext<?> instanceIdentifier = controllerContext
73                 .toInstanceIdentifier("simple-nodes:food//nonalcoholic");
74         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
75
76         instanceIdentifier = controllerContext
77                 .toInstanceIdentifier("simple-nodes:userWithoutClass//");
78         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
79
80         instanceIdentifier = controllerContext
81                 .toInstanceIdentifier("simple-nodes:userWithoutClass///inner-container");
82         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "inner-container");
83     }
84
85     @Test
86     public void testToInstanceIdentifierListWithNullKey() {
87         this.exception.expect(RestconfDocumentedException.class);
88         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
89     }
90
91     @Test
92     public void testToInstanceIdentifierListWithMissingKey() {
93         this.exception.expect(RestconfDocumentedException.class);
94         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
95     }
96
97     @Test
98     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
99         final InstanceIdentifierContext<?> instanceIdentifier =
100                 controllerContext.toInstanceIdentifier("simple-nodes:users");
101         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
102         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
103         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
104     }
105
106     @Test
107     @Ignore //jenkins has problem with JerseyTest
108     // - we expecting problems with singletons ControllerContext as schemaContext holder
109     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
110         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
111                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
112         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
113     }
114
115     @Test
116     public void testToInstanceIdentifierChoiceException() {
117         this.exception.expect(RestconfDocumentedException.class);
118         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
119     }
120
121     @Test
122     public void testToInstanceIdentifierCaseException() {
123         this.exception.expect(RestconfDocumentedException.class);
124         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
125     }
126
127     @Test
128     public void testToInstanceIdentifierChoiceCaseException() {
129         this.exception.expect(RestconfDocumentedException.class);
130         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
131     }
132
133     @Test
134     public void testToInstanceIdentifierWithoutNode() {
135         this.exception.expect(RestconfDocumentedException.class);
136         controllerContext.toInstanceIdentifier("simple-nodes");
137     }
138
139     @Test
140     public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
141         initSchemaService();
142         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
143                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
144         assertEquals(
145                 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, "
146                         + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
147                         + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
148                         + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
149                 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
150     }
151
152     @Test
153     public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
154         initSchemaService();
155         final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
156                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
157         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
158     }
159
160     @Test
161     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
162         this.exception.expect(RestconfDocumentedException.class);
163
164         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
165     }
166
167     public void initSchemaService() {
168         doReturn(Optional.of(FixedDOMSchemaService.of(mountSchemaContext))).when(mountInstance)
169             .getService(DOMSchemaService.class);
170     }
171 }