Mass conversion to static methods
[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 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;
26
27 public class NnToJsonLeafrefType extends AbstractBodyReaderTest {
28
29     private static SchemaContext schemaContext;
30     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
31
32     public NnToJsonLeafrefType() throws NoSuchFieldException, SecurityException {
33         super();
34         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
35     }
36
37     @BeforeClass
38     public static void initialization() {
39         schemaContext = schemaContextLoader("/nn-to-json/leafref",
40                 schemaContext);
41         controllerContext.setSchemas(schemaContext);
42     }
43
44     @Test
45     public void leafrefAbsolutePathToExistingLeafTest()
46  throws Exception {
47         final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
48         validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
49     }
50
51     @Test
52     public void leafrefRelativePathToExistingLeafTest()
53  throws Exception {
54         final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
55         validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
56     }
57
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");
61     }
62
63     @Test
64     public void leafrefToNotLeafTest() throws Exception {
65         final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
66         validateJson(
67                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
68                 json);
69     }
70
71     @Test
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");
74         validateJson(
75                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"34[5|6|7]\",*\"34[5|6|7]\",*\"34[5|6|7]\".*",
76                 json);
77     }
78
79     @Test
80     public void leafrefFromLeafrefToLeafrefTest() throws Exception {
81         final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
82         validateJson(
83                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
84                 json);
85     }
86
87     private static void validateJson(final String regex, final String value) {
88         assertNotNull(value);
89         final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
90         final Matcher mtch = ptrn.matcher(value);
91         assertTrue(mtch.matches());
92     }
93
94     private String toJson(final String xmlDataPath) throws Exception {
95         final String uri = "main-module:cont";
96         final String pathToInputFile = xmlDataPath;
97
98         final NormalizedNodeContext testNN = TestRestconfUtils
99                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
100
101         final OutputStream output = new ByteArrayOutputStream();
102         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
103                 output);
104         final String jsonOutput = output.toString();
105
106         return jsonOutput;
107     }
108
109     @Override
110     protected MediaType getMediaType() {
111         return new MediaType(MediaType.APPLICATION_XML, null);
112     }
113 }