Merge "Leafref and identityref types to Json"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ToJsonIdentityrefTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.util.Set;
7 import java.util.regex.*;
8
9 import javax.ws.rs.WebApplicationException;
10
11 import org.junit.*;
12 import org.opendaylight.yangtools.yang.data.api.*;
13 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
14 import org.opendaylight.yangtools.yang.model.api.*;
15
16 public class ToJsonIdentityrefTest {
17
18     private static Set<Module> modules;
19     private static DataSchemaNode dataSchemaNode;
20
21     @BeforeClass
22     public static void initialization() {
23         modules = TestUtils.resolveModules("/yang-to-json-conversion/identityref");
24         assertEquals(2, modules.size());
25         Module module = TestUtils.resolveModule("identityref-module", modules);
26         assertNotNull(module);
27         dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont");
28         assertNotNull(dataSchemaNode);
29
30     }
31
32     @Test
33     public void identityrefToJsonTest() {
34         String json = null;
35         try {
36             json = TestUtils
37                     .writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), null, modules, dataSchemaNode);
38         } catch (WebApplicationException | IOException e) {
39             // shouldn't end here
40             assertTrue(false);
41         }
42         assertNotNull(json);
43         Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"identityref-module:name_test\".*",
44                 Pattern.DOTALL);
45         Matcher mtch = ptrn.matcher(json);
46
47         assertTrue(mtch.matches());
48     }
49
50     private CompositeNode prepareCompositeNode() {
51         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
52                 ModifyAction.CREATE, null);
53         MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont,
54                 TestUtils.buildQName("name_test", "identityref:module", "2013-12-2"), ModifyAction.CREATE, null);
55         cont.getChildren().add(lf1);
56         cont.init();
57
58         return cont;
59     }
60
61 }