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