fa0bed18204f516f06f5940c86b4c0b46099f480
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.template.search;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonObject;
25 import org.bson.Document;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28
29 import static org.assertj.core.api.Java6Assertions.assertThat;
30
31 class JsonUtilsTest {
32
33     private static final Gson GSON_HELPER = new Gson();
34     private JsonUtils utils;
35
36     @BeforeEach
37     void setUp() {
38         utils = new JsonUtils();
39     }
40
41     private static final String NOTIFICATION_JSON = "{\n\"event\": {\n" +
42             "    \"commonEventHeader\": {\n" +
43             "      \"domain\": \"notification\",\n" +
44             "      \"eventName\": \"vFirewallBroadcastPackets\"\n" +
45             "    },\n" +
46             "    \"notificationFields\": {\n" +
47             "      \"changeIdentifier\": \"PM_MEAS_FILES\",\n" +
48             "      \"arrayOfNamedHashMap\": [{\n" +
49             "        \"name\": \"A20161221.1031-1041.bin.gz\",\n" +
50             "        \"hashMap\": {\n" +
51             "          \"fileformatType\": \"org.3GPP.32.435#measCollec\",\n" +
52             "          \"fileFormatVersion\": \"V10\"\n"+
53             "        }\n" +
54             "      }, {\n" +
55             "        \"name\": \"A20161222.1042-1102.bin.gz\",\n" +
56             "        \"hashMap\": {\n" +
57             "          \"fileFormatType\": \"org.3GPP.32.435#measCollec\",\n" +
58             "          \"fileFormatVersion\": \"1.0.0\"\n" +
59             "        }\n" +
60             "      }],\n" +
61             "      \"notificationFieldsVersion\": \"2.0\"\n}\n\n}}";
62     private static final String EXPECTED_FLATTENED_NOTIFICATION = "{" +
63             " \":event:commonEventHeader:domain\" : \"notification\"," +
64             " \":event:commonEventHeader:eventName\" : \"vFirewallBroadcastPackets\"," +
65             " \":event:notificationFields:changeIdentifier\" : \"PM_MEAS_FILES\"," +
66             " \":event:notificationFields:arrayOfNamedHashMap[0]:name\" : \"A20161221.1031-1041.bin.gz\"," +
67             " \":event:notificationFields:arrayOfNamedHashMap[0]:hashMap:fileformatType\" : \"org.3GPP.32.435#measCollec\"," +
68             " \":event:notificationFields:arrayOfNamedHashMap[0]:hashMap:fileFormatVersion\" : \"V10\"," +
69             " \":event:notificationFields:arrayOfNamedHashMap[1]:name\" : \"A20161222.1042-1102.bin.gz\"," +
70             " \":event:notificationFields:arrayOfNamedHashMap[1]:hashMap:fileFormatType\" : \"org.3GPP.32.435#measCollec\"," +
71             " \":event:notificationFields:arrayOfNamedHashMap[1]:hashMap:fileFormatVersion\" : \"1.0.0\"," +
72             " \":event:notificationFields:notificationFieldsVersion\" : \"2.0\" }";
73
74     @Test
75     void shouldFlattenNestedJsonAndSeparateKeysWithDoubleHash(){
76         JsonObject templateJson = GSON_HELPER.fromJson(NOTIFICATION_JSON, JsonObject.class);
77
78         JsonObject result = utils.flatten(templateJson);
79
80         assertThat(result).isEqualTo(GSON_HELPER.fromJson(EXPECTED_FLATTENED_NOTIFICATION, JsonObject.class));
81     }
82
83     @Test
84     void shouldWorkOnEmptyJsonObject(){
85         JsonObject result = utils.flatten(new JsonObject());
86
87         assertThat(result.toString()).isEqualTo("{}");
88     }
89
90     @Test
91     void shouldFlattenObjectWithArrayValue(){
92         String expectedFlattenedObjectWithArray = "{" +
93                 " \":sample[0]\": 1," +
94                 " \":sample[1]\": 2," +
95                 " \":sample[2]\": 3}";
96         JsonObject jsonWithPrimitivesArray = GSON_HELPER.fromJson("{\"sample\": [1, 2, 3]}", JsonObject.class);
97
98         JsonObject result = utils.flatten(jsonWithPrimitivesArray);
99
100         assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedObjectWithArray, JsonObject.class));
101     }
102
103     @Test
104     void shouldFlattenObjectWithEmptyArrayValue(){
105         String expectedFlattenedObjectWithEmptyArray = "{\":sample\": []}";
106         JsonObject jsonWithEmptyArrayValue = GSON_HELPER.fromJson("{\"sample\": []}", JsonObject.class);
107
108         JsonObject result = utils.flatten(jsonWithEmptyArrayValue);
109
110         assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedObjectWithEmptyArray, JsonObject.class));
111     }
112
113     @Test
114     void shouldFlattenNestedObjectWithEmptyObjectValue(){
115         String expectedFlattenedNestedObjectWithEmptyObject = "{\":sample:key\": {}}";
116         JsonObject nestedJsonWithEmptyObject = GSON_HELPER.fromJson("{\"sample\": {\"key\":{}}}", JsonObject.class);
117
118         JsonObject result = utils.flatten(nestedJsonWithEmptyObject);
119
120         assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedNestedObjectWithEmptyObject, JsonObject.class));
121     }
122
123     @Test
124     void shouldFlattenObjectWithDifferentDataTypes(){
125         String jsonWithDifferentDataTypes = "{ \"topLevelKey\": {\"sampleInt\": 1, \"sampleBool\": false, \"sampleDouble\": 10.0, \"sampleString\": \"str\"}}";
126         String expectedResult = "{\":topLevelKey:sampleInt\": 1," +
127                 " \":topLevelKey:sampleBool\": \"false\"," +
128                 " \":topLevelKey:sampleDouble\": 10.0," +
129                 " \":topLevelKey:sampleString\": \"str\"}";
130         JsonObject templateJson = GSON_HELPER.fromJson(jsonWithDifferentDataTypes, JsonObject.class);
131
132         JsonObject result = utils.flatten(templateJson);
133
134         assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedResult, JsonObject.class));
135     }
136
137     @Test
138     void shouldHandleNullValues(){
139         String jsonWithNullValue = "{ \"topLevelKey\": {\"sampleNull\": null, \"sampleString\": \"str\"}}";
140         String expectedResult = "{\":topLevelKey:sampleNull\": null," +
141                 " \":topLevelKey:sampleString\": \"str\"}";
142         JsonObject templateJson = GSON_HELPER.fromJson(jsonWithNullValue, JsonObject.class);
143
144         JsonObject result = utils.flatten(templateJson);
145
146         assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedResult, JsonObject.class));
147     }
148
149     @Test
150     void shouldFlattenBsonDocument(){
151         Document documentInput = Document.parse(NOTIFICATION_JSON);
152
153         Document result = utils.flatten(documentInput);
154
155         assertThat(result.toJson()).isEqualTo(EXPECTED_FLATTENED_NOTIFICATION);
156     }
157
158     @Test
159     void shouldNotChangeEmptyBsonDocument(){
160         Document input = Document.parse("{}");
161
162         Document result = utils.flatten(input);
163
164         assertThat(result.toJson()).isEqualTo("{ }");
165     }
166 }