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