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;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 public class URITest {
42 private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
45 public ExpectedException exception = ExpectedException.none();
48 public static void init() throws FileNotFoundException, ReactorException {
49 final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
50 final Set<Module> allModules = schemaContext.getModules();
51 assertNotNull(allModules);
53 CONTROLLER_CONTEXT.setSchemas(schemaContext);
57 public void testToInstanceIdentifierList() throws FileNotFoundException {
58 InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
59 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
60 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
62 instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
63 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
65 instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo/boo");
66 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
68 instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user//boo");
69 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
74 public void testToInstanceIdentifierListWithNullKey() {
75 this.exception.expect(RestconfDocumentedException.class);
76 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/null/boo");
80 public void testToInstanceIdentifierListWithMissingKey() {
81 this.exception.expect(RestconfDocumentedException.class);
82 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo");
86 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
87 final InstanceIdentifierContext<?> instanceIdentifier =
88 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users");
89 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
90 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
91 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
95 @Ignore //jenkins has problem with JerseyTest
96 // - we expecting problems with singletons ControllerContext as schemaContext holder
97 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
98 final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
99 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
100 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
104 public void testToInstanceIdentifierChoiceException() {
105 this.exception.expect(RestconfDocumentedException.class);
106 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack");
110 public void testToInstanceIdentifierCaseException() {
111 this.exception.expect(RestconfDocumentedException.class);
112 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/sports-arena");
116 public void testToInstanceIdentifierChoiceCaseException() {
117 this.exception.expect(RestconfDocumentedException.class);
118 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
122 public void testToInstanceIdentifierWithoutNode() {
123 this.exception.expect(RestconfDocumentedException.class);
124 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes");
128 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
129 initMountService(true);
130 final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
131 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
133 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, "
134 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
135 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
136 + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
137 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
141 public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
142 initMountService(true);
143 final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
144 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
145 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
149 public void testMountPointWithoutMountService() throws FileNotFoundException {
150 this.exception.expect(RestconfDocumentedException.class);
152 CONTROLLER_CONTEXT.setMountService(null);
153 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
157 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
158 initMountService(false);
159 this.exception.expect(RestconfDocumentedException.class);
161 CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
164 public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
165 final DOMMountPointService mountService = mock(DOMMountPointService.class);
166 CONTROLLER_CONTEXT.setMountService(mountService);
167 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
168 final RestconfImpl restconfImpl = RestconfImpl.getInstance();
169 restconfImpl.setBroker(brokerFacade);
170 restconfImpl.setControllerContext(CONTROLLER_CONTEXT);
171 final SchemaContext schemaContext2 = TestUtils.loadSchemaContext("/test-config-data/yang2");
172 final Set<Module> modules2 = schemaContext2.getModules();
174 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
176 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
178 when(mountInstance.getSchemaContext()).thenReturn(null);
180 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));