7b6f1448515b68fe5ab435997fa7ab88cd8ed109
[netconf.git] / restconf / sal-rest-connector / 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.text.SimpleDateFormat;
18 import java.util.List;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
28 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
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                 new SimpleDateFormat("yyyy-MM-dd").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                         new SimpleDateFormat("yyyy-MM-dd").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)).thenReturn(this.augmentedContainerLibrary);
105
106         Mockito.when(this.containerPlayer.getQName()).thenReturn(this.playerQName);
107         Mockito.when(this.containerJukebox.getDataChildByName(this.playerQName)).thenReturn(this.containerPlayer);
108
109         Mockito.when(this.listAlbum.getQName()).thenReturn(this.albumQName);
110         Mockito.when(this.containerLibrary.getDataChildByName(this.albumQName)).thenReturn(this.listAlbum);
111
112         Mockito.when(this.leafName.getQName()).thenReturn(this.nameQName);
113         Mockito.when(this.listAlbum.getDataChildByName(this.nameQName)).thenReturn(this.leafName);
114     }
115
116     /**
117      * Test parse fields parameter containing only one child selected
118      */
119     @Test
120     public void parseFieldsParameterSimplePathTest() {
121         final String input = "library";
122         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
123
124         assertNotNull(parsedFields);
125         assertEquals(1, parsedFields.size());
126         assertEquals(1, parsedFields.get(0).size());
127         assertTrue(parsedFields.get(0).contains(this.libraryQName));
128     }
129
130     /**
131      * Test parse fields parameter containing two child nodes selected
132      */
133     @Test
134     public void parseFieldsParameterDoublePathTest() {
135         final String input = "library;player";
136         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
137
138         assertNotNull(parsedFields);
139         assertEquals(1, parsedFields.size());
140         assertEquals(2, parsedFields.get(0).size());
141         assertTrue(parsedFields.get(0).contains(this.libraryQName));
142         assertTrue(parsedFields.get(0).contains(this.playerQName));
143     }
144
145     /**
146      * Test parse fields parameter containing sub-children selected delimited by slash
147      */
148     @Test
149     public void parseFieldsParameterSubPathTest() {
150         final String input = "library/album/name";
151         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
152
153         assertNotNull(parsedFields);
154         assertEquals(3, parsedFields.size());
155
156         assertEquals(1, parsedFields.get(0).size());
157         assertTrue(parsedFields.get(0).contains(this.libraryQName));
158
159         assertEquals(1, parsedFields.get(1).size());
160         assertTrue(parsedFields.get(1).contains(this.albumQName));
161
162         assertEquals(1, parsedFields.get(2).size());
163         assertTrue(parsedFields.get(2).contains(this.nameQName));
164     }
165
166     /**
167      * Test parse fields parameter containing sub-children selected delimited by parenthesis
168      */
169     @Test
170     public void parseFieldsParameterChildrenPathTest() {
171         final String input = "library(album(name))";
172         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
173
174         assertNotNull(parsedFields);
175         assertEquals(3, parsedFields.size());
176
177         assertEquals(1, parsedFields.get(0).size());
178         assertTrue(parsedFields.get(0).contains(this.libraryQName));
179
180         assertEquals(1, parsedFields.get(1).size());
181         assertTrue(parsedFields.get(1).contains(this.albumQName));
182
183         assertEquals(1, parsedFields.get(2).size());
184         assertTrue(parsedFields.get(2).contains(this.nameQName));
185     }
186
187     /**
188      * Test parse fields parameter when augmentation with different namespace is used
189      */
190     @Test
191     public void parseFieldsParameterNamespaceTest() {
192         final String input = "augmented-jukebox:augmented-library";
193         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
194
195         assertNotNull(parsedFields);
196         assertEquals(1, parsedFields.size());
197
198         assertEquals(1, parsedFields.get(0).size());
199         assertTrue(parsedFields.get(0).contains(this.augmentedLibraryQName));
200     }
201
202     /**
203      * Test parse fields parameter containing not expected character
204      */
205     @Test
206     public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
207         final String input = "*";
208
209         try {
210             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
211             fail("Test should fail due to not expected character used in parameter input value");
212         } catch (final RestconfDocumentedException e) {
213             // Bad request
214             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
215             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
216             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
217         }
218     }
219
220     /**
221      * Test parse fields parameter with missing closing parenthesis
222      */
223     @Test
224     public void parseFieldsParameterMissingParenthesisNegativeTest() {
225         final String input = "library(";
226
227         try {
228             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
229             fail("Test should fail due to missing closing parenthesis");
230         } catch (final RestconfDocumentedException e) {
231             // Bad request
232             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
233             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
234             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
235         }
236     }
237
238     /**
239      * Test parse fields parameter when not existing child node selected
240      */
241     @Test
242     public void parseFieldsParameterMissingChildNodeNegativeTest() {
243         final String input = "library(not-existing)";
244
245         try {
246             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
247             fail("Test should fail due to missing child node in parent node");
248         } catch (final RestconfDocumentedException e) {
249             // Bad request
250             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
251             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
252             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
253         }
254     }
255
256     /**
257      * Test parse fields parameter with unexpected character after parenthesis
258      */
259     @Test
260     public void parseFieldsParameterAfterParenthesisNegativeTest() {
261         final String input = "library(album);";
262
263         try {
264             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
265             fail("Test should fail due to unexpected character after parenthesis");
266         } catch (final RestconfDocumentedException e) {
267             // Bad request
268             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
269             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
270             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
271         }
272     }
273
274     /**
275      * Test parse fields parameter with missing semicolon after parenthesis
276      */
277     @Test
278     public void parseFieldsParameterMissingSemicolonNegativeTest() {
279         final String input = "library(album)player";
280
281         try {
282             ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
283             fail("Test should fail due to missing semicolon after parenthesis");
284         } catch (final RestconfDocumentedException e) {
285             // Bad request
286             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
287             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
288             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
289         }
290     }
291 }