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.nn.to.json.test;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
13 import java.io.ByteArrayOutputStream;
14 import java.io.OutputStream;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
21 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
22 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
28 private static SchemaContext schemaContext;
29 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
31 public NnToJsonLeafrefType() throws NoSuchFieldException, SecurityException {
33 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
37 public static void initialization() {
38 schemaContext = schemaContextLoader("/nn-to-json/leafref",
40 CONTROLLER_CONTEXT.setSchemas(schemaContext);
44 public void leafrefAbsolutePathToExistingLeafTest() throws Exception {
45 final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
46 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
50 public void leafrefRelativePathToExistingLeafTest() throws Exception {
51 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
52 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
55 @Test(expected = NullPointerException.class)
56 public void leafrefToNonExistingLeafTest() throws Exception {
57 toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
61 public void leafrefToNotLeafTest() throws Exception {
62 final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
64 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
69 public void leafrefFromLeafListToLeafTest() throws Exception {
70 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
71 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\","
72 + "*\"34[5|6|7]\".*", json);
76 public void leafrefFromLeafrefToLeafrefTest() throws Exception {
77 final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
79 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
83 private static void validateJson(final String regex, final String value) {
85 final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
86 final Matcher mtch = ptrn.matcher(value);
87 assertTrue(mtch.matches());
90 private String toJson(final String xmlDataPath) throws Exception {
91 final String uri = "main-module:cont";
92 final String pathToInputFile = xmlDataPath;
94 final NormalizedNodeContext testNN = TestRestconfUtils
95 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
97 final OutputStream output = new ByteArrayOutputStream();
98 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
100 final String jsonOutput = output.toString();
106 protected MediaType getMediaType() {
107 return new MediaType(MediaType.APPLICATION_XML, null);