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.doReturn;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.io.FileNotFoundException;
18 import java.util.Optional;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 public class URITest {
37 private static EffectiveModelContext schemaContext;
38 private static EffectiveModelContext mountSchemaContext;
40 private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
41 private final ControllerContext controllerContext =
42 TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
45 public ExpectedException exception = ExpectedException.none();
48 public static void init() throws FileNotFoundException, ReactorException {
49 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
50 mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
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 testToInstanceIdentifierWithDoubleSlash() {
72 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
73 .toInstanceIdentifier("simple-nodes:food//nonalcoholic");
74 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
76 instanceIdentifier = controllerContext
77 .toInstanceIdentifier("simple-nodes:userWithoutClass//");
78 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
80 instanceIdentifier = controllerContext
81 .toInstanceIdentifier("simple-nodes:userWithoutClass///inner-container");
82 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "inner-container");
86 public void testToInstanceIdentifierListWithNullKey() {
87 this.exception.expect(RestconfDocumentedException.class);
88 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
92 public void testToInstanceIdentifierListWithMissingKey() {
93 this.exception.expect(RestconfDocumentedException.class);
94 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
98 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
99 final InstanceIdentifierContext<?> instanceIdentifier =
100 controllerContext.toInstanceIdentifier("simple-nodes:users");
101 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
102 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
103 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
107 @Ignore //jenkins has problem with JerseyTest
108 // - we expecting problems with singletons ControllerContext as schemaContext holder
109 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
110 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
111 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
112 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
116 public void testToInstanceIdentifierChoiceException() {
117 this.exception.expect(RestconfDocumentedException.class);
118 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
122 public void testToInstanceIdentifierCaseException() {
123 this.exception.expect(RestconfDocumentedException.class);
124 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
128 public void testToInstanceIdentifierChoiceCaseException() {
129 this.exception.expect(RestconfDocumentedException.class);
130 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
134 public void testToInstanceIdentifierWithoutNode() {
135 this.exception.expect(RestconfDocumentedException.class);
136 controllerContext.toInstanceIdentifier("simple-nodes");
140 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
142 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
143 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
145 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, "
146 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
147 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
148 + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
149 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
153 public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
155 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
156 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
157 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
161 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
162 this.exception.expect(RestconfDocumentedException.class);
164 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
167 public void initSchemaService() {
168 doReturn(Optional.of(FixedDOMSchemaService.of(mountSchemaContext))).when(mountInstance)
169 .getService(DOMSchemaService.class);