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;
16 import com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
19 import java.io.FileNotFoundException;
21 import org.junit.BeforeClass;
22 import org.junit.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 public class URITest {
41 private static final ControllerContext controllerContext = ControllerContext.getInstance();
44 public ExpectedException exception = ExpectedException.none();
47 public static void init() throws FileNotFoundException, ReactorException {
48 final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
49 final Set<Module> allModules = schemaContext.getModules();
50 assertNotNull(allModules);
52 controllerContext.setSchemas(schemaContext);
56 public void testToInstanceIdentifierList() throws FileNotFoundException {
57 InstanceIdentifierContext<?> instanceIdentifier = controllerContext
58 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
61 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
62 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
64 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
65 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
67 instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
68 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
73 public void testToInstanceIdentifierListWithNullKey() {
74 this.exception.expect(RestconfDocumentedException.class);
75 controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
79 public void testToInstanceIdentifierListWithMissingKey() {
80 this.exception.expect(RestconfDocumentedException.class);
81 controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
85 public void testToInstanceIdentifierContainer() throws FileNotFoundException {
86 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
87 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
88 assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
89 assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
93 @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
94 public void testToInstanceIdentifierChoice() throws FileNotFoundException {
95 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
96 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
97 assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
101 public void testToInstanceIdentifierChoiceException() {
102 this.exception.expect(RestconfDocumentedException.class);
103 controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
107 public void testToInstanceIdentifierCaseException() {
108 this.exception.expect(RestconfDocumentedException.class);
109 controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
113 public void testToInstanceIdentifierChoiceCaseException() {
114 this.exception.expect(RestconfDocumentedException.class);
115 controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
119 public void testToInstanceIdentifierWithoutNode() {
120 this.exception.expect(RestconfDocumentedException.class);
121 controllerContext.toInstanceIdentifier("simple-nodes");
125 public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
126 initMountService(true);
127 final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
128 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
130 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, (urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student, (urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)student[{(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 testMountPointWithoutMountService() throws FileNotFoundException {
144 this.exception.expect(RestconfDocumentedException.class);
146 controllerContext.setMountService(null);
147 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
151 public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
152 initMountService(false);
153 this.exception.expect(RestconfDocumentedException.class);
155 controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
158 public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
159 final DOMMountPointService mountService = mock(DOMMountPointService.class);
160 controllerContext.setMountService(mountService);
161 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
162 final RestconfImpl restconfImpl = RestconfImpl.getInstance();
163 restconfImpl.setBroker(brokerFacade);
164 restconfImpl.setControllerContext(controllerContext);
165 final SchemaContext schemaContext2 = TestUtils.loadSchemaContext("/test-config-data/yang2");
166 final Set<Module> modules2 = schemaContext2.getModules();
168 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
170 when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
172 when(mountInstance.getSchemaContext()).thenReturn(null);
174 when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));