2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
17 import java.io.FileNotFoundException;
20 import org.junit.BeforeClass;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
25 import org.opendaylight.controller.sal.core.api.mount.MountService;
26 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 public class URITest {
38 private static final ControllerContext controllerContext = ControllerContext.getInstance();
41 public ExpectedException exception = ExpectedException.none();
44 public static void init() throws FileNotFoundException {
45 Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
46 assertNotNull(allModules);
47 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
48 controllerContext.setSchemas(schemaContext);
52 public void testToInstanceIdentifierList() throws FileNotFoundException {
53 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
54 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
55 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
57 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
58 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
60 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
61 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
63 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
64 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
69 public void testToInstanceIdentifierListWithNullKey() {
70 exception.expect(RestconfDocumentedException.class);
71 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
75 public void testToInstanceIdentifierListWithMissingKey() {
76 exception.expect(RestconfDocumentedException.class);
77 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
81 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
82 InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
83 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
84 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
85 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
89 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
90 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
91 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
92 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
96 public void testToInstanceIdentifierChoiceException() {
97 exception.expect(RestconfDocumentedException.class);
98 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
102 public void testToInstanceIdentifierCaseException() {
103 exception.expect(RestconfDocumentedException.class);
104 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
108 public void testToInstanceIdentifierChoiceCaseException() {
109 exception.expect(RestconfDocumentedException.class);
110 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
114 public void testToInstanceIdentifierWithoutNode() {
115 exception.expect(RestconfDocumentedException.class);
116 controllerContext.toInstanceIdentifier("simple-nodes");
120 public void testMountPointWithExternModul() throws FileNotFoundException {
121 initMountService(true);
122 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
123 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
125 "[(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)name=name}]]",
126 instanceIdentifier.getInstanceIdentifier().getPath().toString());
130 public void testMountPointWithoutExternModul() throws FileNotFoundException {
131 initMountService(true);
132 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
133 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
134 assertEquals(true, instanceIdentifier.getInstanceIdentifier().getPath().isEmpty());
138 public void testMountPointWithoutMountService() throws FileNotFoundException {
139 exception.expect(RestconfDocumentedException.class);
141 controllerContext.setMountService(null);
142 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
143 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
147 public void testMountPointWithoutMountPointSchema() {
148 initMountService(false);
149 exception.expect(RestconfDocumentedException.class);
151 InstanceIdWithSchemaNode instanceIdentifier = controllerContext
152 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
155 public void initMountService(boolean withSchema) {
156 MountService mountService = mock(MountService.class);
157 controllerContext.setMountService(mountService);
158 BrokerFacade brokerFacade = mock(BrokerFacade.class);
159 RestconfImpl restconfImpl = RestconfImpl.getInstance();
160 restconfImpl.setBroker(brokerFacade);
161 restconfImpl.setControllerContext(controllerContext);
163 Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
164 SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
165 MountInstance mountInstance = mock(MountInstance.class);
167 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
169 when(mountInstance.getSchemaContext()).thenReturn(null);
170 when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);