OAM NF Adopter SNMP Manager test code coverage
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / test / java / org / o / ran / oam / nf / oam / adopter / snmp / 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.snmp.manager;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Arrays;
29 import java.util.List;
30 import lombok.experimental.UtilityClass;
31 import org.apache.commons.io.IOUtils;
32
33 @UtilityClass
34 final class JsonUtils {
35     private static final List<String> WHITE_LIST =
36             Arrays.asList("eventId", "actualJO");
37     private static final List<String> WHITE_LIST_EPOCH =
38             Arrays.asList("eventId", "actualJO", "startEpochMicrosec", "lastEpochMicrosec");
39     private static final String COMMON_EVENT_HEADER = "commonEventHeader";
40     private static final String EVENT = "event";
41
42     static String readJson(final String url) throws IOException {
43         return IOUtils.toString(JsonUtils.class.getResourceAsStream(url), StandardCharsets.UTF_8);
44     }
45
46     public static void compareResult(final String expected, final String actual) {
47         final JsonObject expectedJO = JsonParser.parseString(expected).getAsJsonObject();
48         final JsonObject actualJO = JsonParser.parseString(actual).getAsJsonObject();
49         removeCommonEventHeaderFields(expectedJO, actualJO, WHITE_LIST);
50         assertEquals(expectedJO, actualJO);
51     }
52
53     private static void removeCommonEventHeaderFields(final JsonObject expectedJO, final JsonObject actualJO,
54             final List<String> asList) {
55         asList.forEach(wipe -> {
56             expectedJO.get(EVENT).getAsJsonObject().getAsJsonObject(COMMON_EVENT_HEADER).remove(wipe);
57             actualJO.get(EVENT).getAsJsonObject().getAsJsonObject(COMMON_EVENT_HEADER).remove(wipe);
58         });
59     }
60
61     public static void compareResultSkipEpoch(final String expected, final String actual) {
62         final JsonObject expectedJO = JsonParser.parseString(expected).getAsJsonObject();
63         final JsonObject actualJO = JsonParser.parseString(actual).getAsJsonObject();
64         removeCommonEventHeaderFields(expectedJO, actualJO, WHITE_LIST_EPOCH);
65         assertEquals(expectedJO, actualJO);
66     }
67 }