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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.