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.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.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
33 import org.opendaylight.netconf.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;
39 public class URITest {
41 private static final ControllerContext controllerContext = ControllerContext.getInstance();
44 public ExpectedException exception = ExpectedException.none();
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);
55 public void testToInstanceIdentifierList() throws FileNotFoundException {
56 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
57 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
58 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
60 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
61 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
63 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
64 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
66 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
67 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
72 public void testToInstanceIdentifierListWithNullKey() {
73 exception.expect(RestconfDocumentedException.class);
74 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
78 public void testToInstanceIdentifierListWithMissingKey() {
79 exception.expect(RestconfDocumentedException.class);
80 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
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());
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");
100 public void testToInstanceIdentifierChoiceException() {
101 exception.expect(RestconfDocumentedException.class);
102 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
106 public void testToInstanceIdentifierCaseException() {
107 exception.expect(RestconfDocumentedException.class);
108 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
112 public void testToInstanceIdentifierChoiceCaseException() {
113 exception.expect(RestconfDocumentedException.class);
114 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
118 public void testToInstanceIdentifierWithoutNode() {
119 exception.expect(RestconfDocumentedException.class);
120 controllerContext.toInstanceIdentifier("simple-nodes");
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");
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());
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()));
142 public void testMountPointWithoutMountService() throws FileNotFoundException {
143 exception.expect(RestconfDocumentedException.class);
145 controllerContext.setMountService(null);
146 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
150 public void testMountPointWithoutMountPointSchema() {
151 initMountService(false);
152 exception.expect(RestconfDocumentedException.class);
154 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
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);
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);
169 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
171 when(mountInstance.getSchemaContext()).thenReturn(null);
173 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));