2 * Copyright (c) 2016 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
9 package org.opendaylight.restconf.utils.parser;
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;
17 import java.text.SimpleDateFormat;
18 import java.util.List;
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;
38 * Unit test for {@link ParserFieldsParameter}
40 public class ParserFieldsParameterTest {
43 private InstanceIdentifierContext<ContainerSchemaNode> identifierContext;
47 private ContainerSchemaNode containerJukebox;
48 private QName jukeboxQName;
52 private ContainerSchemaNode containerPlayer;
53 private QName playerQName;
57 private ContainerSchemaNode containerLibrary;
58 private QName libraryQName;
60 // container augmented library
62 private ContainerSchemaNode augmentedContainerLibrary;
63 private QName augmentedLibraryQName;
67 private ListSchemaNode listAlbum;
68 private QName albumQName;
72 private LeafSchemaNode leafName;
73 private QName nameQName;
76 public void setUp() throws Exception {
77 MockitoAnnotations.initMocks(this);
78 final SchemaContext schemaContext = TestRestconfUtils.loadSchemaContext("/jukebox");
80 final QNameModule qNameModule = QNameModule.create(URI.create("http://example.com/ns/example-jukebox"),
81 new SimpleDateFormat("yyyy-MM-dd").parse("2015-04-04"));
83 jukeboxQName = QName.create(qNameModule, "jukebox");
84 playerQName = QName.create(qNameModule, "player");
85 libraryQName = QName.create(qNameModule, "library");
86 augmentedLibraryQName = QName.create(
88 URI.create("http://example.com/ns/augmented-jukebox"),
89 new SimpleDateFormat("yyyy-MM-dd").parse("2016-05-05")),
91 albumQName = QName.create(qNameModule, "album");
92 nameQName = QName.create(qNameModule, "name");
94 Mockito.when(identifierContext.getSchemaContext()).thenReturn(schemaContext);
95 Mockito.when(containerJukebox.getQName()).thenReturn(jukeboxQName);
96 Mockito.when(identifierContext.getSchemaNode()).thenReturn(containerJukebox);
98 Mockito.when(containerLibrary.getQName()).thenReturn(libraryQName);
99 Mockito.when(containerJukebox.getDataChildByName(libraryQName)).thenReturn(containerLibrary);
101 Mockito.when(augmentedContainerLibrary.getQName()).thenReturn(augmentedLibraryQName);
102 Mockito.when(containerJukebox.getDataChildByName(augmentedLibraryQName)).thenReturn(augmentedContainerLibrary);
104 Mockito.when(containerPlayer.getQName()).thenReturn(playerQName);
105 Mockito.when(containerJukebox.getDataChildByName(playerQName)).thenReturn(containerPlayer);
107 Mockito.when(listAlbum.getQName()).thenReturn(albumQName);
108 Mockito.when(containerLibrary.getDataChildByName(albumQName)).thenReturn(listAlbum);
110 Mockito.when(leafName.getQName()).thenReturn(nameQName);
111 Mockito.when(listAlbum.getDataChildByName(nameQName)).thenReturn(leafName);
115 * Test parse fields parameter containing only one child selected
118 public void parseFieldsParameterSimplePathTest() {
119 final String input = "library";
120 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
122 assertNotNull(parsedFields);
123 assertEquals(1, parsedFields.size());
124 assertEquals(1, parsedFields.get(0).size());
125 assertTrue(parsedFields.get(0).contains(libraryQName));
129 * Test parse fields parameter containing two child nodes selected
132 public void parseFieldsParameterDoublePathTest() {
133 final String input = "library;player";
134 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
136 assertNotNull(parsedFields);
137 assertEquals(1, parsedFields.size());
138 assertEquals(2, parsedFields.get(0).size());
139 assertTrue(parsedFields.get(0).contains(libraryQName));
140 assertTrue(parsedFields.get(0).contains(playerQName));
144 * Test parse fields parameter containing sub-children selected delimited by slash
147 public void parseFieldsParameterSubPathTest() {
148 final String input = "library/album/name";
149 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
151 assertNotNull(parsedFields);
152 assertEquals(3, parsedFields.size());
154 assertEquals(1, parsedFields.get(0).size());
155 assertTrue(parsedFields.get(0).contains(libraryQName));
157 assertEquals(1, parsedFields.get(1).size());
158 assertTrue(parsedFields.get(1).contains(albumQName));
160 assertEquals(1, parsedFields.get(2).size());
161 assertTrue(parsedFields.get(2).contains(nameQName));
165 * Test parse fields parameter containing sub-children selected delimited by parenthesis
168 public void parseFieldsParameterChildrenPathTest() {
169 final String input = "library(album(name))";
170 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
172 assertNotNull(parsedFields);
173 assertEquals(3, parsedFields.size());
175 assertEquals(1, parsedFields.get(0).size());
176 assertTrue(parsedFields.get(0).contains(libraryQName));
178 assertEquals(1, parsedFields.get(1).size());
179 assertTrue(parsedFields.get(1).contains(albumQName));
181 assertEquals(1, parsedFields.get(2).size());
182 assertTrue(parsedFields.get(2).contains(nameQName));
186 * Test parse fields parameter when augmentation with different namespace is used
189 public void parseFieldsParameterNamespaceTest() {
190 final String input = "augmented-jukebox:augmented-library";
191 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
193 assertNotNull(parsedFields);
194 assertEquals(1, parsedFields.size());
196 assertEquals(1, parsedFields.get(0).size());
197 assertTrue(parsedFields.get(0).contains(augmentedLibraryQName));
201 * Test parse fields parameter containing not expected character
204 public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
205 final String input = "*";
208 ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
209 fail("Test should fail due to not expected character used in parameter input value");
210 } catch (final RestconfDocumentedException e) {
212 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
213 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
214 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
219 * Test parse fields parameter with missing closing parenthesis
222 public void parseFieldsParameterMissingParenthesisNegativeTest() {
223 final String input = "library(";
226 ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
227 fail("Test should fail due to missing closing parenthesis");
228 } catch (final RestconfDocumentedException e) {
230 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
231 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
232 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
237 * Test parse fields parameter when not existing child node selected
240 public void parseFieldsParameterMissingChildNodeNegativeTest() {
241 final String input = "library(not-existing)";
244 ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
245 fail("Test should fail due to missing child node in parent node");
246 } catch (final RestconfDocumentedException e) {
248 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
249 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
250 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
255 * Test parse fields parameter with unexpected character after parenthesis
258 public void parseFieldsParameterAfterParenthesisNegativeTest() {
259 final String input = "library(album);";
262 ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
263 fail("Test should fail due to unexpected character after parenthesis");
264 } catch (final RestconfDocumentedException e) {
266 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
267 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
268 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
273 * Test parse fields parameter with missing semicolon after parenthesis
276 public void parseFieldsParameterMissingSemicolonNegativeTest() {
277 final String input = "library(album)player";
280 ParserFieldsParameter.parseFieldsParameter(identifierContext, input);
281 fail("Test should fail due to missing semicolon after parenthesis");
282 } catch (final RestconfDocumentedException e) {
284 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
285 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
286 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());