Merge "Initial doc structure"
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-manager / src / test / java / org / o / ran / oam / nf / oam / adopter / pm / rest / manager / JsonUtils.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Arrays;
31 import java.util.List;
32 import lombok.experimental.UtilityClass;
33 import org.apache.commons.io.IOUtils;
34
35 @UtilityClass
36 final class JsonUtils {
37     private static final List<String> WHITE_LIST = Arrays.asList("eventId", "startEpochMicrosec", "lastEpochMicrosec");
38     private static final String EVENT_LIST = "eventList";
39     private static final String COMMON_EVENT_HEADER = "commonEventHeader";
40
41     static String readJson(final String url) throws IOException {
42         return IOUtils.toString(JsonUtils.class.getResourceAsStream(url), StandardCharsets.UTF_8);
43     }
44
45     public static void compareResult(final String expected, final String actual) {
46         final JsonObject expectedJO = JsonParser.parseString(expected).getAsJsonObject();
47         final JsonObject actualJO = JsonParser.parseString(actual).getAsJsonObject();
48         removeCommonEventHeaderFields(expectedJO.get(EVENT_LIST).getAsJsonArray(),
49                 actualJO.get(EVENT_LIST).getAsJsonArray(), WHITE_LIST);
50         assertEquals(expectedJO, actualJO);
51     }
52
53     private static void removeCommonEventHeaderFields(final JsonArray expectedJO, final JsonArray actualJO,
54             final List<String> asList) {
55         asList.forEach(wipe -> {
56             expectedJO.forEach(jsonElement -> removeCommonEventHeaderFields(jsonElement, wipe));
57             actualJO.forEach(jsonElement -> removeCommonEventHeaderFields(jsonElement, wipe));
58         });
59     }
60
61     private static void removeCommonEventHeaderFields(final JsonElement jsonElement, final String wipe) {
62         jsonElement.getAsJsonObject().getAsJsonObject(COMMON_EVENT_HEADER).remove(wipe);
63     }
64 }