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 testToInstanceIdentifierListWithNullKey() {
69 this.exception.expect(RestconfDocumentedException.class);
70 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
74 public void testToInstanceIdentifierListWithMissingKey() {
75 this.exception.expect(RestconfDocumentedException.class);
76 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
80 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
81 final InstanceIdentifierContext<?> instanceIdentifier =
82 controllerContext.toInstanceIdentifier("simple-nodes:users");
83 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
84 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
85 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
89 @Ignore //jenkins has problem with JerseyTest
90 // - we expecting problems with singletons ControllerContext as schemaContext holder
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 this.exception.expect(RestconfDocumentedException.class);
100 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
104 public void testToInstanceIdentifierCaseException() {
105 this.exception.expect(RestconfDocumentedException.class);
106 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
110 public void testToInstanceIdentifierChoiceCaseException() {
111 this.exception.expect(RestconfDocumentedException.class);
112 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
116 public void testToInstanceIdentifierWithoutNode() {
117 this.exception.expect(RestconfDocumentedException.class);
118 controllerContext.toInstanceIdentifier("simple-nodes");
122 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
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, "
128 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
129 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
130 + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
131 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
135 public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
136 initMountService(true);
137 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
138 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
139 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
143 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
144 initMountService(false);
145 this.exception.expect(RestconfDocumentedException.class);
147 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
150 public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
152 when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
154 when(mountInstance.getSchemaContext()).thenReturn(null);