Use ControllerContext non-statically
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URITest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
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;
14
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;
33
34 public class URITest {
35
36     private static SchemaContext schemaContext;
37     private static SchemaContext mountSchemaContext;
38
39     private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
40     private final ControllerContext controllerContext =
41             TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
42
43     @Rule
44     public ExpectedException exception = ExpectedException.none();
45
46     @BeforeClass
47     public static void init() throws FileNotFoundException, ReactorException {
48         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
49         mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
50     }
51
52     @Test
53     public void testToInstanceIdentifierList() throws FileNotFoundException {
54         InstanceIdentifierContext<?> instanceIdentifier = controllerContext
55                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
56         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
57
58         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
59         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
60
61         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
62         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
63
64         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
65         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
66
67     }
68
69     @Test
70     public void testToInstanceIdentifierListWithNullKey() {
71         this.exception.expect(RestconfDocumentedException.class);
72         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
73     }
74
75     @Test
76     public void testToInstanceIdentifierListWithMissingKey() {
77         this.exception.expect(RestconfDocumentedException.class);
78         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
79     }
80
81     @Test
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());
88     }
89
90     @Test
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");
97     }
98
99     @Test
100     public void testToInstanceIdentifierChoiceException() {
101         this.exception.expect(RestconfDocumentedException.class);
102         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
103     }
104
105     @Test
106     public void testToInstanceIdentifierCaseException() {
107         this.exception.expect(RestconfDocumentedException.class);
108         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
109     }
110
111     @Test
112     public void testToInstanceIdentifierChoiceCaseException() {
113         this.exception.expect(RestconfDocumentedException.class);
114         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
115     }
116
117     @Test
118     public void testToInstanceIdentifierWithoutNode() {
119         this.exception.expect(RestconfDocumentedException.class);
120         controllerContext.toInstanceIdentifier("simple-nodes");
121     }
122
123     @Test
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");
128         assertEquals(
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());
134     }
135
136     @Test
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()));
142     }
143
144     @Test
145     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
146         initMountService(false);
147         this.exception.expect(RestconfDocumentedException.class);
148
149         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
150     }
151
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);
157
158         if (withSchema) {
159             when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
160         } else {
161             when(mountInstance.getSchemaContext()).thenReturn(null);
162         }
163     }
164 }