2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
24 import static org.junit.Assert.*;
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;
32 public class TestJsonAssert {
35 public void testGenericTypes() {
37 JSONAssert.assertEquals("test boolean", "{ \"test\":true}", "{ \"test\":true}", true);
38 } catch (JSONException e) {
42 JSONAssert.assertEquals("test int", "{ \"test\":2}", "{ \"test\":2}", true);
43 } catch (JSONException e) {
47 JSONAssert.assertEquals("test string", "{ \"test\":\"abc\"}", "{ \"test\":\"abc\"}", true);
48 } catch (JSONException e) {
55 public void testGenericTypesFails() {
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) {
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) {
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) {
84 public void testObject() {
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) {
94 public void testObjectFails() {
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) {
107 public void testArrayStrict() {
109 JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
110 "{ \"test\":[\"a\",\"b\",\"c\"]}", true);
111 } catch (JSONException e) {
112 fail(e.getMessage());
117 public void testArrayStrictFails() {
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) {
130 public void testArrayNonStrict() {
132 JSONAssert.assertEquals("test array strict", "{ \"test\":[\"a\",\"b\",\"c\"]}",
133 "{ \"test\":[\"a\",\"c\",\"b\"]}", false);
134 } catch (JSONException e) {
135 fail(e.getMessage());
140 public void testArrayNonStrictFails() {
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) {
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) {
162 public void testNullParamCheck() {
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"));