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