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.ImmutableList;
19 import com.google.common.collect.Iterables;
20 import java.io.FileNotFoundException;
22 import org.junit.BeforeClass;
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;
38 public class URITest {
40 private static final ControllerContext controllerContext = ControllerContext.getInstance();
43 public ExpectedException exception = ExpectedException.none();
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);
54 public void testToInstanceIdentifierList() throws FileNotFoundException {
55 InstanceIdentifierContext instanceIdentifier = controllerContext
56 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
57 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
59 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
60 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
62 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
63 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
65 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
66 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
71 public void testToInstanceIdentifierListWithNullKey() {
72 exception.expect(RestconfDocumentedException.class);
73 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
77 public void testToInstanceIdentifierListWithMissingKey() {
78 exception.expect(RestconfDocumentedException.class);
79 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
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());
91 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
92 final InstanceIdentifierContext instanceIdentifier = controllerContext
93 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
94 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
98 public void testToInstanceIdentifierChoiceException() {
99 exception.expect(RestconfDocumentedException.class);
100 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
104 public void testToInstanceIdentifierCaseException() {
105 exception.expect(RestconfDocumentedException.class);
106 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
110 public void testToInstanceIdentifierChoiceCaseException() {
111 exception.expect(RestconfDocumentedException.class);
112 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
116 public void testToInstanceIdentifierWithoutNode() {
117 exception.expect(RestconfDocumentedException.class);
118 controllerContext.toInstanceIdentifier("simple-nodes");
122 public void testMountPointWithExternModul() throws FileNotFoundException {
123 initMountService(true);
124 final InstanceIdentifierContext instanceIdentifier = controllerContext
125 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
127 "[(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}]]",
128 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
132 public void testMountPointWithoutExternModul() throws FileNotFoundException {
133 initMountService(true);
134 final InstanceIdentifierContext instanceIdentifier = controllerContext
135 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
136 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
140 public void testMountPointWithoutMountService() throws FileNotFoundException {
141 exception.expect(RestconfDocumentedException.class);
143 controllerContext.setMountService(null);
144 controllerContext.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 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
155 public void initMountService(final boolean withSchema) {
156 final DOMMountPointService mountService = mock(DOMMountPointService.class);
157 controllerContext.setMountService(mountService);
158 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
159 final RestconfImpl restconfImpl = RestconfImpl.getInstance();
160 restconfImpl.setBroker(brokerFacade);
161 restconfImpl.setControllerContext(controllerContext);
163 final Set<Module> modules2 = TestUtils.loadModulesFrom("/test-config-data/yang2");
164 final SchemaContext schemaContext2 = TestUtils.loadSchemaContext(modules2);
165 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
167 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
169 when(mountInstance.getSchemaContext()).thenReturn(null);
171 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));