2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
13 import java.io.IOException;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import javax.ws.rs.WebApplicationException;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
22 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
23 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
24 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 * All tests are commented now because leafref isn't supported now
33 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
36 public static void initialization() {
37 dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
41 public void leafrefAbsolutePathToExistingLeafTest() {
42 String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
43 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
47 public void leafrefRelativePathToExistingLeafTest() {
48 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
49 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
53 * Tests case when reference to not existing element is present. In this
54 * case value from single node is printed as string.
57 public void leafrefToNonExistingLeafTest() {
58 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
59 validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json);
63 * Tests case when non leaf element is referenced. In this case value from
64 * single node is printed as string.
67 public void leafrefToNotLeafTest() {
68 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
69 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json);
73 * Tests case when leaflist element is refers to leaf.
76 public void leafrefFromLeafListToLeafTest() {
77 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
79 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
84 * Tests case when leaflist element is refers to leaf.
87 public void leafrefFromLeafrefToLeafrefTest() {
88 String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
89 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json);
92 private void validateJson(String regex, String value) {
94 Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
95 Matcher mtch = ptrn.matcher(value);
96 assertTrue(mtch.matches());
99 private String toJson(String xmlDataPath) {
101 CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlDataPath, XmlToCompositeNodeProvider.INSTANCE);
102 TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName);
103 return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
104 StructuredDataToJsonProvider.INSTANCE);
105 } catch (WebApplicationException | IOException e) {