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;
16 import javax.ws.rs.WebApplicationException;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
20 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
21 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
22 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 * All tests are commented now because leafref isn't supported now
31 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
34 public static void initialization() {
35 dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
39 public void leafrefAbsolutePathToExistingLeafTest() {
40 String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
41 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
45 public void leafrefRelativePathToExistingLeafTest() {
46 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
47 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
51 * Tests case when reference to not existing element is present. In this case value from single node is printed as
55 public void leafrefToNonExistingLeafTest() {
56 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
57 validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json);
61 * Tests case when non leaf element is referenced. In this case value from single node is printed as string.
64 public void leafrefToNotLeafTest() {
65 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
66 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json);
70 * Tests case when leaflist element is refers to leaf.
73 public void leafrefFromLeafListToLeafTest() {
74 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
76 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
81 * Tests case when leaflist element is refers to leaf.
84 public void leafrefFromLeafrefToLeafrefTest() {
85 String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
86 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json);
89 private void validateJson(String regex, String value) {
91 Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
92 Matcher mtch = ptrn.matcher(value);
93 assertTrue(mtch.matches());
96 private String toJson(String xmlDataPath) {
98 CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlDataPath, XmlToCompositeNodeProvider.INSTANCE);
99 TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName);
100 return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
101 StructuredDataToJsonProvider.INSTANCE);
102 } catch (WebApplicationException | IOException e) {