Merge "Reformat sdnr devicemanager-onf to ONAP code style"
[ccsdk/features.git] / sdnr / wt / common / src / test / java / org / onap / ccsdk / features / sdnr / wt / common / test / TestJsonAssert.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
23
24 import static org.junit.Assert.*;
25
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.junit.Test;
29 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
30 import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert;
31
32 public class TestJsonAssert {
33
34     @Test
35     public void testGenericTypes() {
36         try {
37             JSONAssert.assertEquals("test boolean", "{ \"test\":true}", "{ \"test\":true}", true);
38         } catch (JSONException e) {
39             fail(e.getMessage());
40         }
41         try {
42             JSONAssert.assertEquals("test int", "{ \"test\":2}", "{ \"test\":2}", true);
43         } catch (JSONException e) {
44             fail(e.getMessage());
45         }
46         try {
47             JSONAssert.assertEquals("test string", "{ \"test\":\"abc\"}", "{ \"test\":\"abc\"}", true);
48         } catch (JSONException e) {
49             fail(e.getMessage());
50         }
51
52     }
53
54     @Test
55     public void testGenericTypesFails() {
56         try {
57             JSONAssert.assertEquals("test boolean", "{ \"test\":true}", "{ \"test\":false}", true);
58             fail("test boolean not failed, but has to");
59         } catch (JSONException e) {
60             fail("problem with json");
61         } catch (AssertionError e) {
62
63         }
64         try {
65             JSONAssert.assertEquals("test int", "{ \"test\":2}", "{ \"test\":3}", true);
66             fail("test int not failed, but has to");
67         } catch (JSONException e) {
68             fail("problem with json");
69         } catch (AssertionError e) {
70
71         }
72         try {
73             JSONAssert.assertEquals("test string", "{ \"test\":\"abc\"}", "{ \"test\":\"abcd\"}", true);
74             fail("test string not failed, but has to");
75         } catch (JSONException e) {
76             fail("problem with json");
77         } catch (AssertionError e) {
78
79         }
80
81     }
82
83     @Test
84     public void testObject() {
85         try {
86             JSONAssert.assertEquals("test object", "{ \"test\":{\"more\":{\"x\":1,\"y\":\"2\",\"z\":{}}}}",
87                     "{ \"test\":{\"more\":{\"x\":1,\"z\":{},\"y\":\"2\"}}}", true);
88         } catch (JSONException e) {
89             fail(e.getMessage());
90         }
91     }
92
93     @Test
94     public void testObjectFails() {
95         try {
96             JSONAssert.assertEquals("test object", "{ \"test\":{\"more\":{\"x\":1,\"y\":\"2\",\"z\":{}}}}",
97                     "{ \"test\":{\"more\":{\"x\":1,\"z\":{}}}}", true);
98             fail("test object not failed, but has to");
99         } catch (JSONException e) {
100             fail("problem with json");
101         } catch (AssertionError e) {
102
103         }
104     }
105
106     @Test
107     public void testArrayStrict() {
108         try {
109             JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
110                     "{ \"test\":[\"a\",\"b\",\"c\"]}", true);
111         } catch (JSONException e) {
112             fail(e.getMessage());
113         }
114     }
115
116     @Test
117     public void testArrayStrictFails() {
118         try {
119             JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
120                     "{ \"test\":[\"a\",\"c\",\"b\"]}", true);
121             fail("test object not failed, but has to");
122         } catch (JSONException e) {
123             fail("problem with json");
124         } catch (AssertionError e) {
125
126         }
127     }
128
129     @Test
130     public void testArrayNonStrict() {
131         try {
132             JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
133                     "{ \"test\":[\"a\",\"c\",\"b\"]}", false);
134         } catch (JSONException e) {
135             fail(e.getMessage());
136         }
137     }
138
139     @Test
140     public void testArrayNonStrictFails() {
141         try {
142             JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
143                     "{ \"test\":[\"a\",\"c\",\"b\",\"d\"]}", false);
144             fail("test object not failed, but has to");
145         } catch (JSONException e) {
146             fail("problem with json");
147         } catch (AssertionError e) {
148
149         }
150         try {
151             JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
152                     "{ \"test\":[\"a\",\"b\",\"d\"]}", false);
153             fail("test object not failed, but has to");
154         } catch (JSONException e) {
155             fail("problem with json");
156         } catch (AssertionError e) {
157
158         }
159     }
160
161     @Test
162     public void testNullParamCheck() {
163
164         try {
165
166             HtAssert.nonnull("test", new JSONArray(), null);
167             fail("exception not thrown");
168         } catch (Exception e) {
169             assertTrue(e.getMessage().contains("org.onap.ccsdk.features.sdnr.wt.common.test.TestJsonAssert")
170                     && e.getMessage().contains("testNullParamCheck"));
171         }
172     }
173 }