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