00dd603ce6e610a22a587f226a1d8a2f89813ebe
[netconf.git] / restconf / sal-rest-connector / 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.NormalizedNodeJsonBodyWriter;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
27
28     private static SchemaContext schemaContext;
29     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
30
31     public NnToJsonLeafrefType() throws NoSuchFieldException, SecurityException {
32         super();
33         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
34     }
35
36     @BeforeClass
37     public static void initialization() {
38         schemaContext = schemaContextLoader("/nn-to-json/leafref",
39                 schemaContext);
40         CONTROLLER_CONTEXT.setSchemas(schemaContext);
41     }
42
43     @Test
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);
47     }
48
49     @Test
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);
53     }
54
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");
58     }
59
60     @Test
61     public void leafrefToNotLeafTest() throws Exception {
62         final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
63         validateJson(
64                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
65                 json);
66     }
67
68     @Test
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);
73     }
74
75     @Test
76     public void leafrefFromLeafrefToLeafrefTest() throws Exception {
77         final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
78         validateJson(
79                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
80                 json);
81     }
82
83     private static void validateJson(final String regex, final String value) {
84         assertNotNull(value);
85         final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
86         final Matcher mtch = ptrn.matcher(value);
87         assertTrue(mtch.matches());
88     }
89
90     private String toJson(final String xmlDataPath) throws Exception {
91         final String uri = "main-module:cont";
92         final String pathToInputFile = xmlDataPath;
93
94         final NormalizedNodeContext testNN = TestRestconfUtils
95                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
96
97         final OutputStream output = new ByteArrayOutputStream();
98         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
99                 output);
100         final String jsonOutput = output.toString();
101
102         return jsonOutput;
103     }
104
105     @Override
106     protected MediaType getMediaType() {
107         return new MediaType(MediaType.APPLICATION_XML, null);
108     }
109 }