Merge changes I7e42fc60,I6a7335b4,Ie11ee943,Ic48123a3,I89b6349e,I23a09513,Ibe5395dd...
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / ToJsonIdentityrefTest.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.restconf.impl.test.TestUtils;
15 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
16 import org.opendaylight.yangtools.yang.data.api.*;
17 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
18
19 public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader {
20
21     @BeforeClass
22     public static void initialization() {
23         dataLoad("/cnsn-to-json/identityref", 2, "identityref-module", "cont");
24     }
25
26     @Test
27     public void identityrefToJsonTest() {
28         String json = null;
29         try {
30             json = TestUtils.writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), modules, dataSchemaNode);
31         } catch (WebApplicationException | IOException e) {
32             // shouldn't end here
33             assertTrue(false);
34         }
35         assertNotNull(json);
36         Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"identityref-module:name_test\".*",
37                 Pattern.DOTALL);
38         Matcher mtch = ptrn.matcher(json);
39
40         assertTrue(mtch.matches());
41     }
42
43     private CompositeNode prepareCompositeNode() {
44         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
45                 ModifyAction.CREATE, null);
46         MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont1"), cont, null,
47                 ModifyAction.CREATE, null);
48         cont.getChildren().add(cont1);
49
50         MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont1,
51                 TestUtils.buildQName("name_test", "identityref:module", "2013-12-2"), ModifyAction.CREATE, null);
52         cont1.getChildren().add(lf1);
53         cont1.init();
54         cont.init();
55
56         return cont;
57     }
58
59 }