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.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
28 private static EffectiveModelContext schemaContext;
29 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
31 public NnToJsonLeafrefType() {
32 super(schemaContext, null);
33 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
37 public static void initialization() {
38 schemaContext = schemaContextLoader("/nn-to-json/leafref", schemaContext);
42 public void leafrefAbsolutePathToExistingLeafTest() throws Exception {
43 final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
44 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
48 public void leafrefRelativePathToExistingLeafTest() throws Exception {
49 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
50 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
53 @Test(expected = NullPointerException.class)
54 public void leafrefToNonExistingLeafTest() throws Exception {
55 toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
59 public void leafrefToNotLeafTest() throws Exception {
60 final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
62 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
67 public void leafrefFromLeafListToLeafTest() throws Exception {
68 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
69 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\","
70 + "*\"34[5|6|7]\".*", json);
74 public void leafrefFromLeafrefToLeafrefTest() throws Exception {
75 final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
77 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
81 private static void validateJson(final String regex, final String value) {
83 final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
84 final Matcher mtch = ptrn.matcher(value);
85 assertTrue(mtch.matches());
88 private String toJson(final String xmlDataPath) throws Exception {
89 final String uri = "main-module:cont";
90 final String pathToInputFile = xmlDataPath;
92 final NormalizedNodeContext testNN = TestRestconfUtils
93 .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext);
95 final OutputStream output = new ByteArrayOutputStream();
96 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
98 final String jsonOutput = output.toString();
104 protected MediaType getMediaType() {
105 return new MediaType(MediaType.APPLICATION_XML, null);