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 com.google.common.base.Optional;
18 import com.google.common.collect.Iterables;
19 import java.io.FileNotFoundException;
21 import org.junit.BeforeClass;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
28 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 public class URITest {
39 private static final ControllerContext controllerContext = ControllerContext.getInstance();
42 public ExpectedException exception = ExpectedException.none();
45 public static void init() throws FileNotFoundException {
46 Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
47 assertNotNull(allModules);
48 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
49 controllerContext.setSchemas(schemaContext);
53 public void testToInstanceIdentifierList() throws FileNotFoundException {
54 InstanceIdentifierContext instanceIdentifier = controllerContext
55 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
56 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
58 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
61 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
62 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
64 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
65 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
70 public void testToInstanceIdentifierListWithNullKey() {
71 exception.expect(RestconfDocumentedException.class);
72 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
76 public void testToInstanceIdentifierListWithMissingKey() {
77 exception.expect(RestconfDocumentedException.class);
78 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
82 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
83 InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
84 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
85 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
86 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
90 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
91 InstanceIdentifierContext instanceIdentifier = controllerContext
92 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
93 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
97 public void testToInstanceIdentifierChoiceException() {
98 exception.expect(RestconfDocumentedException.class);
99 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
103 public void testToInstanceIdentifierCaseException() {
104 exception.expect(RestconfDocumentedException.class);
105 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
109 public void testToInstanceIdentifierChoiceCaseException() {
110 exception.expect(RestconfDocumentedException.class);
111 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
115 public void testToInstanceIdentifierWithoutNode() {
116 exception.expect(RestconfDocumentedException.class);
117 controllerContext.toInstanceIdentifier("simple-nodes");
121 public void testMountPointWithExternModul() throws FileNotFoundException {
122 initMountService(true);
123 InstanceIdentifierContext instanceIdentifier = controllerContext
124 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
126 "[(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}]]",
127 instanceIdentifier.getInstanceIdentifier().getPath().toString());
131 public void testMountPointWithoutExternModul() throws FileNotFoundException {
132 initMountService(true);
133 InstanceIdentifierContext instanceIdentifier = controllerContext
134 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
135 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
139 public void testMountPointWithoutMountService() throws FileNotFoundException {
140 exception.expect(RestconfDocumentedException.class);
142 controllerContext.setMountService(null);
143 InstanceIdentifierContext instanceIdentifier = controllerContext
144 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
148 public void testMountPointWithoutMountPointSchema() {
149 initMountService(false);
150 exception.expect(RestconfDocumentedException.class);
152 InstanceIdentifierContext instanceIdentifier = controllerContext
153 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
156 public void initMountService(final boolean withSchema) {
157 DOMMountPointService mountService = mock(DOMMountPointService.class);
158 controllerContext.setMountService(mountService);
159 BrokerFacade brokerFacade = mock(BrokerFacade.class);
160 RestconfImpl restconfImpl = RestconfImpl.getInstance();
161 restconfImpl.setBroker(brokerFacade);
162 restconfImpl.setControllerContext(controllerContext);
164 Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
165 SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
166 DOMMountPoint mountInstance = mock(DOMMountPoint.class);
168 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
170 when(mountInstance.getSchemaContext()).thenReturn(null);
172 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));