Reduce the use of AttrBuilders
[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.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;
31
32 public class URITest {
33
34     private static SchemaContext schemaContext;
35     private static SchemaContext mountSchemaContext;
36
37     private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
38     private final ControllerContext controllerContext =
39             TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
40
41     @Rule
42     public ExpectedException exception = ExpectedException.none();
43
44     @BeforeClass
45     public static void init() throws FileNotFoundException, ReactorException {
46         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
47         mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
48     }
49
50     @Test
51     public void testToInstanceIdentifierList() throws FileNotFoundException {
52         InstanceIdentifierContext<?> instanceIdentifier = controllerContext
53                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
54         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
55
56         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
57         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
58
59         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
60         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
61
62         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
63         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
64
65     }
66
67     @Test
68     public void testToInstanceIdentifierListWithNullKey() {
69         this.exception.expect(RestconfDocumentedException.class);
70         controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
71     }
72
73     @Test
74     public void testToInstanceIdentifierListWithMissingKey() {
75         this.exception.expect(RestconfDocumentedException.class);
76         controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
77     }
78
79     @Test
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());
86     }
87
88     @Test
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");
95     }
96
97     @Test
98     public void testToInstanceIdentifierChoiceException() {
99         this.exception.expect(RestconfDocumentedException.class);
100         controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
101     }
102
103     @Test
104     public void testToInstanceIdentifierCaseException() {
105         this.exception.expect(RestconfDocumentedException.class);
106         controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
107     }
108
109     @Test
110     public void testToInstanceIdentifierChoiceCaseException() {
111         this.exception.expect(RestconfDocumentedException.class);
112         controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
113     }
114
115     @Test
116     public void testToInstanceIdentifierWithoutNode() {
117         this.exception.expect(RestconfDocumentedException.class);
118         controllerContext.toInstanceIdentifier("simple-nodes");
119     }
120
121     @Test
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");
126         assertEquals(
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());
132     }
133
134     @Test
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()));
140     }
141
142     @Test
143     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
144         initMountService(false);
145         this.exception.expect(RestconfDocumentedException.class);
146
147         controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
148     }
149
150     public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
151         if (withSchema) {
152             when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
153         } else {
154             when(mountInstance.getSchemaContext()).thenReturn(null);
155         }
156     }
157 }