Remove explicit default super-constructor calls
[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.NormalizedNodeJsonBodyWriter;
23 import org.opendaylight.restconf.common.context.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         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
33     }
34
35     @BeforeClass
36     public static void initialization() {
37         schemaContext = schemaContextLoader("/nn-to-json/leafref",
38                 schemaContext);
39         CONTROLLER_CONTEXT.setSchemas(schemaContext);
40     }
41
42     @Test
43     public void leafrefAbsolutePathToExistingLeafTest() throws Exception {
44         final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
45         validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
46     }
47
48     @Test
49     public void leafrefRelativePathToExistingLeafTest() throws Exception {
50         final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
51         validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
52     }
53
54     @Test(expected = NullPointerException.class)
55     public void leafrefToNonExistingLeafTest() throws Exception {
56         toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
57     }
58
59     @Test
60     public void leafrefToNotLeafTest() throws Exception {
61         final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
62         validateJson(
63                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
64                 json);
65     }
66
67     @Test
68     public void leafrefFromLeafListToLeafTest() throws Exception {
69         final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
70         validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\","
71                 + "*\"34[5|6|7]\".*", json);
72     }
73
74     @Test
75     public void leafrefFromLeafrefToLeafrefTest() throws Exception {
76         final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
77         validateJson(
78                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
79                 json);
80     }
81
82     private static void validateJson(final String regex, final String value) {
83         assertNotNull(value);
84         final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
85         final Matcher mtch = ptrn.matcher(value);
86         assertTrue(mtch.matches());
87     }
88
89     private String toJson(final String xmlDataPath) throws Exception {
90         final String uri = "main-module:cont";
91         final String pathToInputFile = xmlDataPath;
92
93         final NormalizedNodeContext testNN = TestRestconfUtils
94                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
95
96         final OutputStream output = new ByteArrayOutputStream();
97         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
98                 output);
99         final String jsonOutput = output.toString();
100
101         return jsonOutput;
102     }
103
104     @Override
105     protected MediaType getMediaType() {
106         return new MediaType(MediaType.APPLICATION_XML, null);
107     }
108 }