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 com.google.common.util.concurrent.UncheckedExecutionException;
14 import java.io.ByteArrayOutputStream;
15 import java.io.OutputStream;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
23 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
24 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
29 private static SchemaContext schemaContext;
30 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
32 public NnToJsonLeafrefType() throws NoSuchFieldException, SecurityException {
34 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
38 public static void initialization() {
39 schemaContext = schemaContextLoader("/nn-to-json/leafref",
41 controllerContext.setSchemas(schemaContext);
45 public void leafrefAbsolutePathToExistingLeafTest()
47 final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
48 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
52 public void leafrefRelativePathToExistingLeafTest()
54 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
55 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
58 @Test(expected = UncheckedExecutionException.class)
59 public void leafrefToNonExistingLeafTest() throws Exception {
60 toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
64 public void leafrefToNotLeafTest() throws Exception {
65 final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
67 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
72 public void leafrefFromLeafListToLeafTest() throws Exception {
73 final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
75 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\",*\"34[5|6|7]\".*",
80 public void leafrefFromLeafrefToLeafrefTest() throws Exception {
81 final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
83 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
87 private static void validateJson(final String regex, final String value) {
89 final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
90 final Matcher mtch = ptrn.matcher(value);
91 assertTrue(mtch.matches());
94 private String toJson(final String xmlDataPath) throws Exception {
95 final String uri = "main-module:cont";
96 final String pathToInputFile = xmlDataPath;
98 final NormalizedNodeContext testNN = TestRestconfUtils
99 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
101 final OutputStream output = new ByteArrayOutputStream();
102 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
104 final String jsonOutput = output.toString();
110 protected MediaType getMediaType() {
111 return new MediaType(MediaType.APPLICATION_XML, null);