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