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.assertTrue;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.io.FileNotFoundException;
18 import org.junit.BeforeClass;
19 import org.junit.Ignore;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
25 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
26 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 public class URITest {
34 private static SchemaContext schemaContext;
35 private static SchemaContext mountSchemaContext;
37 private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
38 private final ControllerContext controllerContext =
39 TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
42 public ExpectedException exception = ExpectedException.none();
45 public static void init() throws FileNotFoundException, ReactorException {
46 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
47 mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
51 public void testToInstanceIdentifierList() throws FileNotFoundException {
52 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
53 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
54 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
56 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
57 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
59 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
60 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
62 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
63 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
68 public void testToInstanceIdentifierWithDoubleSlash() {
69 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
70 .toInstanceIdentifier("simple-nodes:food//nonalcoholic");
71 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
73 instanceIdentifier = controllerContext
74 .toInstanceIdentifier("simple-nodes:userWithoutClass//");
75 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
77 instanceIdentifier = controllerContext
78 .toInstanceIdentifier("simple-nodes:userWithoutClass///inner-container");
79 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "inner-container");
83 public void testToInstanceIdentifierListWithNullKey() {
84 this.exception.expect(RestconfDocumentedException.class);
85 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
89 public void testToInstanceIdentifierListWithMissingKey() {
90 this.exception.expect(RestconfDocumentedException.class);
91 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
95 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
96 final InstanceIdentifierContext<?> instanceIdentifier =
97 controllerContext.toInstanceIdentifier("simple-nodes:users");
98 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
99 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
100 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
104 @Ignore //jenkins has problem with JerseyTest
105 // - we expecting problems with singletons ControllerContext as schemaContext holder
106 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
107 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
108 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
109 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
113 public void testToInstanceIdentifierChoiceException() {
114 this.exception.expect(RestconfDocumentedException.class);
115 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
119 public void testToInstanceIdentifierCaseException() {
120 this.exception.expect(RestconfDocumentedException.class);
121 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
125 public void testToInstanceIdentifierChoiceCaseException() {
126 this.exception.expect(RestconfDocumentedException.class);
127 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
131 public void testToInstanceIdentifierWithoutNode() {
132 this.exception.expect(RestconfDocumentedException.class);
133 controllerContext.toInstanceIdentifier("simple-nodes");
137 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
138 initMountService(true);
139 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
140 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
142 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, "
143 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
144 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
145 + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
146 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
150 public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
151 initMountService(true);
152 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
153 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
154 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
158 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
159 initMountService(false);
160 this.exception.expect(RestconfDocumentedException.class);
162 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
165 public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
167 when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
169 when(mountInstance.getSchemaContext()).thenReturn(null);