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.util.List;
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.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
28 import org.opendaylight.restconf.common.errors.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;
39 * Unit test for {@link ParserFieldsParameter}.
41 public class ParserFieldsParameterTest {
44 private InstanceIdentifierContext<ContainerSchemaNode> identifierContext;
48 private ContainerSchemaNode containerJukebox;
49 private QName jukeboxQName;
53 private ContainerSchemaNode containerPlayer;
54 private QName playerQName;
58 private ContainerSchemaNode containerLibrary;
59 private QName libraryQName;
61 // container augmented library
63 private ContainerSchemaNode augmentedContainerLibrary;
64 private QName augmentedLibraryQName;
68 private ListSchemaNode listAlbum;
69 private QName albumQName;
73 private LeafSchemaNode leafName;
74 private QName nameQName;
77 public void setUp() throws Exception {
78 MockitoAnnotations.initMocks(this);
79 final SchemaContext schemaContext =
80 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/jukebox"));
82 final QNameModule qNameModule = QNameModule.create(URI.create("http://example.com/ns/example-jukebox"),
83 SimpleDateFormatUtil.getRevisionFormat().parse("2015-04-04"));
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(
90 URI.create("http://example.com/ns/augmented-jukebox"),
91 SimpleDateFormatUtil.getRevisionFormat().parse("2016-05-05")),
93 this.albumQName = QName.create(qNameModule, "album");
94 this.nameQName = QName.create(qNameModule, "name");
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);
100 Mockito.when(this.containerLibrary.getQName()).thenReturn(this.libraryQName);
101 Mockito.when(this.containerJukebox.getDataChildByName(this.libraryQName)).thenReturn(this.containerLibrary);
103 Mockito.when(this.augmentedContainerLibrary.getQName()).thenReturn(this.augmentedLibraryQName);
104 Mockito.when(this.containerJukebox.getDataChildByName(this.augmentedLibraryQName))
105 .thenReturn(this.augmentedContainerLibrary);
107 Mockito.when(this.containerPlayer.getQName()).thenReturn(this.playerQName);
108 Mockito.when(this.containerJukebox.getDataChildByName(this.playerQName)).thenReturn(this.containerPlayer);
110 Mockito.when(this.listAlbum.getQName()).thenReturn(this.albumQName);
111 Mockito.when(this.containerLibrary.getDataChildByName(this.albumQName)).thenReturn(this.listAlbum);
113 Mockito.when(this.leafName.getQName()).thenReturn(this.nameQName);
114 Mockito.when(this.listAlbum.getDataChildByName(this.nameQName)).thenReturn(this.leafName);
118 * Test parse fields parameter containing only one child selected.
121 public void parseFieldsParameterSimplePathTest() {
122 final String input = "library";
123 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
125 assertNotNull(parsedFields);
126 assertEquals(1, parsedFields.size());
127 assertEquals(1, parsedFields.get(0).size());
128 assertTrue(parsedFields.get(0).contains(this.libraryQName));
132 * Test parse fields parameter containing two child nodes selected.
135 public void parseFieldsParameterDoublePathTest() {
136 final String input = "library;player";
137 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
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));
147 * Test parse fields parameter containing sub-children selected delimited by slash.
150 public void parseFieldsParameterSubPathTest() {
151 final String input = "library/album/name";
152 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
154 assertNotNull(parsedFields);
155 assertEquals(3, parsedFields.size());
157 assertEquals(1, parsedFields.get(0).size());
158 assertTrue(parsedFields.get(0).contains(this.libraryQName));
160 assertEquals(1, parsedFields.get(1).size());
161 assertTrue(parsedFields.get(1).contains(this.albumQName));
163 assertEquals(1, parsedFields.get(2).size());
164 assertTrue(parsedFields.get(2).contains(this.nameQName));
168 * Test parse fields parameter containing sub-children selected delimited by parenthesis.
171 public void parseFieldsParameterChildrenPathTest() {
172 final String input = "library(album(name))";
173 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
175 assertNotNull(parsedFields);
176 assertEquals(3, parsedFields.size());
178 assertEquals(1, parsedFields.get(0).size());
179 assertTrue(parsedFields.get(0).contains(this.libraryQName));
181 assertEquals(1, parsedFields.get(1).size());
182 assertTrue(parsedFields.get(1).contains(this.albumQName));
184 assertEquals(1, parsedFields.get(2).size());
185 assertTrue(parsedFields.get(2).contains(this.nameQName));
189 * Test parse fields parameter when augmentation with different namespace is used.
192 public void parseFieldsParameterNamespaceTest() {
193 final String input = "augmented-jukebox:augmented-library";
194 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
196 assertNotNull(parsedFields);
197 assertEquals(1, parsedFields.size());
199 assertEquals(1, parsedFields.get(0).size());
200 assertTrue(parsedFields.get(0).contains(this.augmentedLibraryQName));
204 * Test parse fields parameter containing not expected character.
207 public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
208 final String input = "*";
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) {
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());
222 * Test parse fields parameter with missing closing parenthesis.
225 public void parseFieldsParameterMissingParenthesisNegativeTest() {
226 final String input = "library(";
229 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
230 fail("Test should fail due to missing closing parenthesis");
231 } catch (final RestconfDocumentedException e) {
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());
240 * Test parse fields parameter when not existing child node selected.
243 public void parseFieldsParameterMissingChildNodeNegativeTest() {
244 final String input = "library(not-existing)";
247 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
248 fail("Test should fail due to missing child node in parent node");
249 } catch (final RestconfDocumentedException e) {
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());
258 * Test parse fields parameter with unexpected character after parenthesis.
261 public void parseFieldsParameterAfterParenthesisNegativeTest() {
262 final String input = "library(album);";
265 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
266 fail("Test should fail due to unexpected character after parenthesis");
267 } catch (final RestconfDocumentedException e) {
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());
276 * Test parse fields parameter with missing semicolon after parenthesis.
279 public void parseFieldsParameterMissingSemicolonNegativeTest() {
280 final String input = "library(album)player";
283 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
284 fail("Test should fail due to missing semicolon after parenthesis");
285 } catch (final RestconfDocumentedException e) {
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());