Delete netconf
[controller.git] / opendaylight / md-sal / 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 import com.google.common.util.concurrent.UncheckedExecutionException;
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.NormalizedNodeJsonBodyWriter;
22 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
23 import org.opendaylight.controller.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         controllerContext.setSchemas(schemaContext);
41     }
42
43     @Test
44     public void leafrefAbsolutePathToExistingLeafTest()
45  throws Exception {
46         final String json = toJson("/nn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
47         validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
48     }
49
50     @Test
51     public void leafrefRelativePathToExistingLeafTest()
52  throws Exception {
53         final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
54         validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
55     }
56
57     @Test(expected = UncheckedExecutionException.class)
58     public void leafrefToNonExistingLeafTest() throws Exception {
59         toJson("/nn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
60     }
61
62     @Test
63     public void leafrefToNotLeafTest() throws Exception {
64         final String json = toJson("/nn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
65         validateJson(
66                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44\".*",
67                 json);
68     }
69
70     @Test
71     public void leafrefFromLeafListToLeafTest() throws Exception {
72         final String json = toJson("/nn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
73         validateJson(
74                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"346\",*\"347\",*\"345\".*",
75                 json);
76     }
77
78     @Test
79     public void leafrefFromLeafrefToLeafrefTest() throws Exception {
80         final String json = toJson("/nn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
81         validateJson(
82                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*",
83                 json);
84     }
85
86     private void validateJson(final String regex, final String value) {
87         assertNotNull(value);
88         final Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
89         final Matcher mtch = ptrn.matcher(value);
90         assertTrue(mtch.matches());
91     }
92
93     private String toJson(final String xmlDataPath) throws Exception {
94         final String uri = "main-module:cont";
95         final String pathToInputFile = xmlDataPath;
96
97         final NormalizedNodeContext testNN = TestRestconfUtils
98                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
99
100         final OutputStream output = new ByteArrayOutputStream();
101         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
102                 output);
103         final String jsonOutput = output.toString();
104
105         return jsonOutput;
106     }
107
108     @Override
109     protected MediaType getMediaType() {
110         return new MediaType(MediaType.APPLICATION_XML, null);
111     }
112 }