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.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;
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)).thenReturn(this.augmentedContainerLibrary);
106 Mockito.when(this.containerPlayer.getQName()).thenReturn(this.playerQName);
107 Mockito.when(this.containerJukebox.getDataChildByName(this.playerQName)).thenReturn(this.containerPlayer);
109 Mockito.when(this.listAlbum.getQName()).thenReturn(this.albumQName);
110 Mockito.when(this.containerLibrary.getDataChildByName(this.albumQName)).thenReturn(this.listAlbum);
112 Mockito.when(this.leafName.getQName()).thenReturn(this.nameQName);
113 Mockito.when(this.listAlbum.getDataChildByName(this.nameQName)).thenReturn(this.leafName);
117 * Test parse fields parameter containing only one child selected
120 public void parseFieldsParameterSimplePathTest() {
121 final String input = "library";
122 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
124 assertNotNull(parsedFields);
125 assertEquals(1, parsedFields.size());
126 assertEquals(1, parsedFields.get(0).size());
127 assertTrue(parsedFields.get(0).contains(this.libraryQName));
131 * Test parse fields parameter containing two child nodes selected
134 public void parseFieldsParameterDoublePathTest() {
135 final String input = "library;player";
136 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
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));
146 * Test parse fields parameter containing sub-children selected delimited by slash
149 public void parseFieldsParameterSubPathTest() {
150 final String input = "library/album/name";
151 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
153 assertNotNull(parsedFields);
154 assertEquals(3, parsedFields.size());
156 assertEquals(1, parsedFields.get(0).size());
157 assertTrue(parsedFields.get(0).contains(this.libraryQName));
159 assertEquals(1, parsedFields.get(1).size());
160 assertTrue(parsedFields.get(1).contains(this.albumQName));
162 assertEquals(1, parsedFields.get(2).size());
163 assertTrue(parsedFields.get(2).contains(this.nameQName));
167 * Test parse fields parameter containing sub-children selected delimited by parenthesis
170 public void parseFieldsParameterChildrenPathTest() {
171 final String input = "library(album(name))";
172 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
174 assertNotNull(parsedFields);
175 assertEquals(3, parsedFields.size());
177 assertEquals(1, parsedFields.get(0).size());
178 assertTrue(parsedFields.get(0).contains(this.libraryQName));
180 assertEquals(1, parsedFields.get(1).size());
181 assertTrue(parsedFields.get(1).contains(this.albumQName));
183 assertEquals(1, parsedFields.get(2).size());
184 assertTrue(parsedFields.get(2).contains(this.nameQName));
188 * Test parse fields parameter when augmentation with different namespace is used
191 public void parseFieldsParameterNamespaceTest() {
192 final String input = "augmented-jukebox:augmented-library";
193 final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
195 assertNotNull(parsedFields);
196 assertEquals(1, parsedFields.size());
198 assertEquals(1, parsedFields.get(0).size());
199 assertTrue(parsedFields.get(0).contains(this.augmentedLibraryQName));
203 * Test parse fields parameter containing not expected character
206 public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
207 final String input = "*";
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) {
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());
221 * Test parse fields parameter with missing closing parenthesis
224 public void parseFieldsParameterMissingParenthesisNegativeTest() {
225 final String input = "library(";
228 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
229 fail("Test should fail due to missing closing parenthesis");
230 } catch (final RestconfDocumentedException e) {
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());
239 * Test parse fields parameter when not existing child node selected
242 public void parseFieldsParameterMissingChildNodeNegativeTest() {
243 final String input = "library(not-existing)";
246 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
247 fail("Test should fail due to missing child node in parent node");
248 } catch (final RestconfDocumentedException e) {
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());
257 * Test parse fields parameter with unexpected character after parenthesis
260 public void parseFieldsParameterAfterParenthesisNegativeTest() {
261 final String input = "library(album);";
264 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
265 fail("Test should fail due to unexpected character after parenthesis");
266 } catch (final RestconfDocumentedException e) {
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());
275 * Test parse fields parameter with missing semicolon after parenthesis
278 public void parseFieldsParameterMissingSemicolonNegativeTest() {
279 final String input = "library(album)player";
282 ParserFieldsParameter.parseFieldsParameter(this.identifierContext, input);
283 fail("Test should fail due to missing semicolon after parenthesis");
284 } catch (final RestconfDocumentedException e) {
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());