2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.template.search;
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;
29 import static org.assertj.core.api.Java6Assertions.assertThat;
33 private static final Gson GSON_HELPER = new Gson();
34 private JsonUtils utils;
38 utils = new JsonUtils();
41 private static final String NOTIFICATION_JSON = "{\n\"event\": {\n" +
42 " \"commonEventHeader\": {\n" +
43 " \"domain\": \"notification\",\n" +
44 " \"eventName\": \"vFirewallBroadcastPackets\"\n" +
46 " \"notificationFields\": {\n" +
47 " \"changeIdentifier\": \"PM_MEAS_FILES\",\n" +
48 " \"arrayOfNamedHashMap\": [{\n" +
49 " \"name\": \"A20161221.1031-1041.bin.gz\",\n" +
51 " \"fileformatType\": \"org.3GPP.32.435#measCollec\",\n" +
52 " \"fileFormatVersion\": \"V10\"\n"+
55 " \"name\": \"A20161222.1042-1102.bin.gz\",\n" +
57 " \"fileFormatType\": \"org.3GPP.32.435#measCollec\",\n" +
58 " \"fileFormatVersion\": \"1.0.0\"\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\" }";
75 void shouldFlattenNestedJsonAndSeparateKeysWithDoubleHash(){
76 JsonObject templateJson = GSON_HELPER.fromJson(NOTIFICATION_JSON, JsonObject.class);
78 JsonObject result = utils.flatten(templateJson);
80 assertThat(result).isEqualTo(GSON_HELPER.fromJson(EXPECTED_FLATTENED_NOTIFICATION, JsonObject.class));
84 void shouldWorkOnEmptyJsonObject(){
85 JsonObject result = utils.flatten(new JsonObject());
87 assertThat(result.toString()).isEqualTo("{}");
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);
98 JsonObject result = utils.flatten(jsonWithPrimitivesArray);
100 assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedObjectWithArray, JsonObject.class));
104 void shouldFlattenObjectWithEmptyArrayValue(){
105 String expectedFlattenedObjectWithEmptyArray = "{\":sample\": []}";
106 JsonObject jsonWithEmptyArrayValue = GSON_HELPER.fromJson("{\"sample\": []}", JsonObject.class);
108 JsonObject result = utils.flatten(jsonWithEmptyArrayValue);
110 assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedObjectWithEmptyArray, JsonObject.class));
114 void shouldFlattenNestedObjectWithEmptyObjectValue(){
115 String expectedFlattenedNestedObjectWithEmptyObject = "{\":sample:key\": {}}";
116 JsonObject nestedJsonWithEmptyObject = GSON_HELPER.fromJson("{\"sample\": {\"key\":{}}}", JsonObject.class);
118 JsonObject result = utils.flatten(nestedJsonWithEmptyObject);
120 assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedFlattenedNestedObjectWithEmptyObject, JsonObject.class));
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);
132 JsonObject result = utils.flatten(templateJson);
134 assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedResult, JsonObject.class));
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);
144 JsonObject result = utils.flatten(templateJson);
146 assertThat(result).isEqualTo(GSON_HELPER.fromJson(expectedResult, JsonObject.class));
150 void shouldFlattenBsonDocument(){
151 Document documentInput = Document.parse(NOTIFICATION_JSON);
153 Document result = utils.flatten(documentInput);
155 assertThat(result.toJson()).isEqualTo(EXPECTED_FLATTENED_NOTIFICATION);
159 void shouldNotChangeEmptyBsonDocument(){
160 Document input = Document.parse("{}");
162 Document result = utils.flatten(input);
164 assertThat(result.toJson()).isEqualTo("{ }");