More test for improving of code coverage + test refactoring
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonIdentityrefTest.java
1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.IOException;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import javax.ws.rs.WebApplicationException;
11
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
15 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
16 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.*;
19 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
20
21 public class CnSnToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader {
22
23     @BeforeClass
24     public static void initialization() {
25         dataLoad("/cnsn-to-json/identityref", 2, "identityref-module", "cont");
26     }
27
28     @Test
29     public void identityrefToJsonTest() {
30         String json = null;
31         try {
32             QName valueAsQname = TestUtils.buildQName("name_test", "identityref:module", "2013-12-2");
33             json = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(valueAsQname), modules,
34                     dataSchemaNode, StructuredDataToJsonProvider.INSTANCE);
35         } catch (WebApplicationException | IOException e) {
36             // shouldn't end here
37             assertTrue(false);
38         }
39         assertNotNull(json);
40         Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"identityref-module:name_test\".*",
41                 Pattern.DOTALL);
42         Matcher mtch = ptrn.matcher(json);
43
44         assertTrue(mtch.matches());
45     }
46
47     @Test
48     public void identityrefToJsonWithoutQNameTest() {
49         String json = null;
50         try {
51             String value = "not q name value";
52             json = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(value), modules,
53                     dataSchemaNode, StructuredDataToJsonProvider.INSTANCE);
54         } catch (WebApplicationException | IOException e) {
55             // shouldn't end here
56             assertTrue(false);
57         }
58         System.out.println(json);
59         assertNotNull(json);
60         Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"not q name value\".*", Pattern.DOTALL);
61         Matcher mtch = ptrn.matcher(json);
62
63         assertTrue(mtch.matches());
64     }
65
66     private CompositeNode prepareCompositeNode(Object value) {
67         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
68                 ModifyAction.CREATE, null);
69         MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont1"), cont, null,
70                 ModifyAction.CREATE, null);
71         cont.getChildren().add(cont1);
72
73         MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont1, value,
74                 ModifyAction.CREATE, null);
75
76         cont1.getChildren().add(lf1);
77         cont1.init();
78         cont.init();
79
80         return cont;
81     }
82
83 }