e343ee50af93b3ef262203c2ead4ddb98656054a
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / restconf / utils / parser / ParserFieldsParameterTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.restconf.utils.parser;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.net.URI;
17 import java.util.List;
18 import java.util.Set;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
28 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37
38 /**
39  * Unit test for {@link ParserFieldsParameter}.
40  */
41 public class ParserFieldsParameterTest {
42
43     @Mock
44     private InstanceIdentifierContext<ContainerSchemaNode> identifierContext;
45
46     // container jukebox
47     @Mock
48     private ContainerSchemaNode containerJukebox;
49     private QName jukeboxQName;
50
51     // container player
52     @Mock
53     private ContainerSchemaNode containerPlayer;
54     private QName playerQName;
55
56     // container library
57     @Mock
58     private ContainerSchemaNode containerLibrary;
59     private QName libraryQName;
60
61     // container augmented library
62     @Mock
63     private ContainerSchemaNode augmentedContainerLibrary;
64     private QName augmentedLibraryQName;
65
66     // list album
67     @Mock
68     private ListSchemaNode listAlbum;
69     private QName albumQName;
70
71     // leaf name
72     @Mock
73     private LeafSchemaNode leafName;
74     private QName nameQName;
75
76     @Before
77     public void setUp() throws Exception {
78         MockitoAnnotations.initMocks(this);
79         final SchemaContext schemaContext =
80                 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/jukebox"));
81
82         final QNameModule qNameModule = QNameModule.create(URI.create("http://example.com/ns/example-jukebox"),
83             SimpleDateFormatUtil.getRevisionFormat().parse("2015-04-04"));
84
85         this.jukeboxQName = QName.create(qNameModule, "jukebox");
86         this.playerQName = QName.create(qNameModule, "player");
87         this.libraryQName = QName.create(qNameModule, "library");
88         this.augmentedLibraryQName = QName.create(
89                 QNameModule.create(
90                         URI.create("http://example.com/ns/augmented-jukebox"),
91                         SimpleDateFormatUtil.getRevisionFormat().parse("2016-05-05")),
92                 "augmented-library");
93         this.albumQName = QName.create(qNameModule, "album");
94         this.nameQName = QName.create(qNameModule, "name");
95
96         Mockito.when(this.identifierContext.getSchemaContext()).thenReturn(schemaContext);
97         Mockito.when(this.containerJukebox.getQName()).thenReturn(this.jukeboxQName);
98         Mockito.when(this.identifierContext.getSchemaNode()).thenReturn(this.containerJukebox);
99
100         Mockito.when(this.containerLibrary.getQName()).thenReturn(this.libraryQName);
101         Mockito.when(this.containerJukebox.getDataChildByName(this.libraryQName)).thenReturn(this.containerLibrary);
102
103         Mockito.when(this.augmentedContainerLibrary.getQName()).thenReturn(this.augmentedLibraryQName);
104         Mockito.when(this.containerJukebox.getDataChildByName(this.augmentedLibraryQName))
105                 .thenReturn(this.augmentedContainerLibrary);
106
107         Mockito.when(this.containerPlayer.getQName()).thenReturn(this.playerQName);
108         Mockito.when(this.containerJukebox.getDataChildByName(this.playerQName)).thenReturn(this.containerPlayer);
109
110         Mockito.when(this.listAlbum.getQName()).thenReturn(this.albumQName);
111         Mockito.when(this.containerLibrary.getDataChildByName(this.albumQName)).thenReturn(this.listAlbum);
112
113         Mockito.when(this.leafName.getQName()).thenReturn(this.nameQName);
114         Mockito.when(this.listAlbum.getDataChildByName(this.nameQName)).thenReturn(this.leafName);
115     }
116
117     /**
118      * Test parse fields parameter containing only one child selected.
119      */
120     @Test
121     public void parseFieldsParameterSimplePathTest() {
122         final String input = "library";
123         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
124
125         assertNotNull(parsedFields);
126         assertEquals(1, parsedFields.size());
127         assertEquals(1, parsedFields.get(0).size());
128         assertTrue(parsedFields.get(0).contains(this.libraryQName));
129     }
130
131     /**
132      * Test parse fields parameter containing two child nodes selected.
133      */
134     @Test
135     public void parseFieldsParameterDoublePathTest() {
136         final String input = "library;player";
137         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
138
139         assertNotNull(parsedFields);
140         assertEquals(1, parsedFields.size());
141         assertEquals(2, parsedFields.get(0).size());
142         assertTrue(parsedFields.get(0).contains(this.libraryQName));
143         assertTrue(parsedFields.get(0).contains(this.playerQName));
144     }
145
146     /**
147      * Test parse fields parameter containing sub-children selected delimited by slash.
148      */
149     @Test
150     public void parseFieldsParameterSubPathTest() {
151         final String input = "library/album/name";
152         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
153
154         assertNotNull(parsedFields);
155         assertEquals(3, parsedFields.size());
156
157         assertEquals(1, parsedFields.get(0).size());
158         assertTrue(parsedFields.get(0).contains(this.libraryQName));
159
160         assertEquals(1, parsedFields.get(1).size());
161         assertTrue(parsedFields.get(1).contains(this.albumQName));
162
163         assertEquals(1, parsedFields.get(2).size());
164         assertTrue(parsedFields.get(2).contains(this.nameQName));
165     }
166
167     /**
168      * Test parse fields parameter containing sub-children selected delimited by parenthesis.
169      */
170     @Test
171     public void parseFieldsParameterChildrenPathTest() {
172         final String input = "library(album(name))";
173         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
174
175         assertNotNull(parsedFields);
176         assertEquals(3, parsedFields.size());
177
178         assertEquals(1, parsedFields.get(0).size());
179         assertTrue(parsedFields.get(0).contains(this.libraryQName));
180
181         assertEquals(1, parsedFields.get(1).size());
182         assertTrue(parsedFields.get(1).contains(this.albumQName));
183
184         assertEquals(1, parsedFields.get(2).size());
185         assertTrue(parsedFields.get(2).contains(this.nameQName));
186     }
187
188     /**
189      * Test parse fields parameter when augmentation with different namespace is used.
190      */
191     @Test
192     public void parseFieldsParameterNamespaceTest() {
193         final String input = "augmented-jukebox:augmented-library";
194         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
195
196         assertNotNull(parsedFields);
197         assertEquals(1, parsedFields.size());
198
199         assertEquals(1, parsedFields.get(0).size());
200         assertTrue(parsedFields.get(0).contains(this.augmentedLibraryQName));
201     }
202
203     /**
204      * Test parse fields parameter containing not expected character.
205      */
206     @Test
207     public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
208         final String input = "*";
209
210         try {
211             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
212             fail("Test should fail due to not expected character used in parameter input value");
213         } catch (final RestconfDocumentedException e) {
214             // Bad request
215             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
216             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
217             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
218         }
219     }
220
221     /**
222      * Test parse fields parameter with missing closing parenthesis.
223      */
224     @Test
225     public void parseFieldsParameterMissingParenthesisNegativeTest() {
226         final String input = "library(";
227
228         try {
229             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
230             fail("Test should fail due to missing closing parenthesis");
231         } catch (final RestconfDocumentedException e) {
232             // Bad request
233             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
234             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
235             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
236         }
237     }
238
239     /**
240      * Test parse fields parameter when not existing child node selected.
241      */
242     @Test
243     public void parseFieldsParameterMissingChildNodeNegativeTest() {
244         final String input = "library(not-existing)";
245
246         try {
247             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
248             fail("Test should fail due to missing child node in parent node");
249         } catch (final RestconfDocumentedException e) {
250             // Bad request
251             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
252             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
253             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
254         }
255     }
256
257     /**
258      * Test parse fields parameter with unexpected character after parenthesis.
259      */
260     @Test
261     public void parseFieldsParameterAfterParenthesisNegativeTest() {
262         final String input = "library(album);";
263
264         try {
265             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
266             fail("Test should fail due to unexpected character after parenthesis");
267         } catch (final RestconfDocumentedException e) {
268             // Bad request
269             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
270             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
271             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
272         }
273     }
274
275     /**
276      * Test parse fields parameter with missing semicolon after parenthesis.
277      */
278     @Test
279     public void parseFieldsParameterMissingSemicolonNegativeTest() {
280         final String input = "library(album)player";
281
282         try {
283             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
284             fail("Test should fail due to missing semicolon after parenthesis");
285         } catch (final RestconfDocumentedException e) {
286             // Bad request
287             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
288             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
289             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
290         }
291     }
292 }