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.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
26 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
28 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 public class URITest {
36 private static SchemaContext schemaContext;
37 private static SchemaContext mountSchemaContext;
39 private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
40 private final ControllerContext controllerContext =
41 TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
44 public ExpectedException exception = ExpectedException.none();
47 public static void init() throws FileNotFoundException, ReactorException {
48 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
49 mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
53 public void testToInstanceIdentifierList() throws FileNotFoundException {
54 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
55 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
56 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
58 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
61 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
62 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
64 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
65 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
70 public void testToInstanceIdentifierListWithNullKey() {
71 this.exception.expect(RestconfDocumentedException.class);
72 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
76 public void testToInstanceIdentifierListWithMissingKey() {
77 this.exception.expect(RestconfDocumentedException.class);
78 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
82 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
83 final InstanceIdentifierContext<?> instanceIdentifier =
84 controllerContext.toInstanceIdentifier("simple-nodes:users");
85 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
86 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
87 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
91 @Ignore //jenkins has problem with JerseyTest
92 // - 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 this.exception.expect(RestconfDocumentedException.class);
102 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
106 public void testToInstanceIdentifierCaseException() {
107 this.exception.expect(RestconfDocumentedException.class);
108 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
112 public void testToInstanceIdentifierChoiceCaseException() {
113 this.exception.expect(RestconfDocumentedException.class);
114 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
118 public void testToInstanceIdentifierWithoutNode() {
119 this.exception.expect(RestconfDocumentedException.class);
120 controllerContext.toInstanceIdentifier("simple-nodes");
124 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
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, "
130 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, "
131 + "(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student"
132 + "[{(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)name=name}]]",
133 ImmutableList.copyOf(instanceIdentifier.getInstanceIdentifier().getPathArguments()).toString());
137 public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
138 initMountService(true);
139 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
140 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
141 assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
145 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
146 initMountService(false);
147 this.exception.expect(RestconfDocumentedException.class);
149 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
152 public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
153 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
154 final RestconfImpl restconfImpl = RestconfImpl.getInstance();
155 restconfImpl.setBroker(brokerFacade);
156 restconfImpl.setControllerContext(controllerContext);
159 when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
161 when(mountInstance.getSchemaContext()).thenReturn(null);