Cleanup: Remove passing around of DataPersistenceProvider
[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.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.controller.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.controller.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
38 public class URITest {
39
40     private static final ControllerContext controllerContext = ControllerContext.getInstance();
41
42     @Rule
43     public ExpectedException exception = ExpectedException.none();
44
45     @BeforeClass
46     public static void init() throws FileNotFoundException {
47         final Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
48         assertNotNull(allModules);
49         final SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
50         controllerContext.setSchemas(schemaContext);
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 testToInstanceIdentifierListWithNullKey() {
72         exception.expect(RestconfDocumentedException.class);
73         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
74     }
75
76     @Test
77     public void testToInstanceIdentifierListWithMissingKey() {
78         exception.expect(RestconfDocumentedException.class);
79         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
80     }
81
82     @Test
83     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
84         final InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
85         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
86         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
87         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
88     }
89
90     @Test
91     @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
92     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
93         final InstanceIdentifierContext instanceIdentifier = controllerContext
94                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
95         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
96     }
97
98     @Test
99     public void testToInstanceIdentifierChoiceException() {
100         exception.expect(RestconfDocumentedException.class);
101         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
102     }
103
104     @Test
105     public void testToInstanceIdentifierCaseException() {
106         exception.expect(RestconfDocumentedException.class);
107         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
108     }
109
110     @Test
111     public void testToInstanceIdentifierChoiceCaseException() {
112         exception.expect(RestconfDocumentedException.class);
113         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
114     }
115
116     @Test
117     public void testToInstanceIdentifierWithoutNode() {
118         exception.expect(RestconfDocumentedException.class);
119         controllerContext.toInstanceIdentifier("simple-nodes");
120     }
121
122     @Test
123     public void testMountPointWithExternModul() throws FileNotFoundException {
124         initMountService(true);
125         final InstanceIdentifierContext instanceIdentifier = controllerContext
126                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
127         assertEquals(
128                 "[(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}]]",
129                 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
130     }
131
132     @Test
133     public void testMountPointWithoutExternModul() throws FileNotFoundException {
134         initMountService(true);
135         final InstanceIdentifierContext instanceIdentifier = controllerContext
136                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
137         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
138     }
139
140     @Test
141     public void testMountPointWithoutMountService() throws FileNotFoundException {
142         exception.expect(RestconfDocumentedException.class);
143
144         controllerContext.setMountService(null);
145         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
146     }
147
148     @Test
149     public void testMountPointWithoutMountPointSchema() {
150         initMountService(false);
151         exception.expect(RestconfDocumentedException.class);
152
153         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
154     }
155
156     public void initMountService(final boolean withSchema) {
157         final DOMMountPointService mountService = mock(DOMMountPointService.class);
158         controllerContext.setMountService(mountService);
159         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
160         final RestconfImpl restconfImpl = RestconfImpl.getInstance();
161         restconfImpl.setBroker(brokerFacade);
162         restconfImpl.setControllerContext(controllerContext);
163
164         final Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
165         final SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
166         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
167         if (withSchema) {
168             when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
169         } else {
170             when(mountInstance.getSchemaContext()).thenReturn(null);
171         }
172         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
173     }
174 }