4ea613098bd0e61d2b5a73e23973a7e541f631dd
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnToJsonLeafrefType.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
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.NormalizedNodeContext;
23 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25
26 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
27
28     private static EffectiveModelContext schemaContext;
29     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
30
31     public NnToJsonLeafrefType() {
32         super(schemaContext, null);
33         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
34     }
35
36     @BeforeClass
37     public static void initialization() {
38         schemaContext = schemaContextLoader("/nn-to-json/leafref", schemaContext);
39     }
40
41     @Test
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);
45     }
46
47     @Test
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);
51     }
52
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");
56     }
57
58     @Test
59     public void leafrefToNotLeafTest() throws Exception {
60         final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
61         validateJson(
62                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
63                 json);
64     }
65
66     @Test
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);
71     }
72
73     @Test
74     public void leafrefFromLeafrefToLeafrefTest() throws Exception {
75         final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
76         validateJson(
77                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
78                 json);
79     }
80
81     private static void validateJson(final String regex, final String value) {
82         assertNotNull(value);
83         final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
84         final Matcher mtch = ptrn.matcher(value);
85         assertTrue(mtch.matches());
86     }
87
88     private String toJson(final String xmlDataPath) throws Exception {
89         final String uri = "main-module:cont";
90         final String pathToInputFile = xmlDataPath;
91
92         final NormalizedNodeContext testNN = TestRestconfUtils
93                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext);
94
95         final OutputStream output = new ByteArrayOutputStream();
96         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
97                 output);
98         final String jsonOutput = output.toString();
99
100         return jsonOutput;
101     }
102
103     @Override
104     protected MediaType getMediaType() {
105         return new MediaType(MediaType.APPLICATION_XML, null);
106     }
107 }