Delete netconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnToJsonWithAugmentTest.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.assertTrue;
11 import com.google.common.base.Preconditions;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import javax.ws.rs.WebApplicationException;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
20 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
21 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
22 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class NnToJsonWithAugmentTest extends AbstractBodyReaderTest {
26
27     private static SchemaContext schemaContext;
28     private final NormalizedNodeJsonBodyWriter xmlBodyWriter;
29
30     public NnToJsonWithAugmentTest() throws NoSuchFieldException,
31             SecurityException {
32         super();
33         xmlBodyWriter = new NormalizedNodeJsonBodyWriter();
34     }
35
36     @BeforeClass
37     public static void initialize() {
38         schemaContext = schemaContextLoader("/nn-to-json/augmentation",
39                 schemaContext);
40         controllerContext.setSchemas(schemaContext);
41     }
42
43     @Test
44     public void augmentedElementsToJson() throws WebApplicationException,
45             IOException {
46         final String uri = "yang:cont";
47         final String pathToInputFile = "/nn-to-json/augmentation/xml/data.xml";
48
49         final NormalizedNodeContext testNN = TestRestconfUtils
50                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
51
52         final OutputStream output = new ByteArrayOutputStream();
53         xmlBodyWriter
54                 .writeTo(testNN, null, null, null, mediaType, null, output);
55         final String jsonOutput = output.toString();
56
57         Preconditions.checkNotNull(jsonOutput);
58
59         assertTrue(jsonOutput.contains("\"cont1\"" + ":" + '{'));
60         assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf11\""));
61         assertTrue(jsonOutput.contains("\"lst1\"" + ":" + '['));
62         assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf1_1\""));
63         assertTrue(jsonOutput.contains("\"lf11\"" + ":" + "\"lf1_2\""));
64         assertTrue(jsonOutput.contains("\"lflst1\"" + ":" + "["));
65         assertTrue(jsonOutput.contains("\"lf2\"" + ":" + "\"lf2\""));
66     }
67
68     @Override
69     protected MediaType getMediaType() {
70         return new MediaType(MediaType.APPLICATION_XML, null);
71     }
72 }