diff options
Diffstat (limited to 'utils-test/src/main/java/org/onap/policy/common/utils/gson')
-rw-r--r-- | utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java | 71 |
1 files changed, 28 insertions, 43 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java index 6ae42faf..8276ea4c 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,22 +28,20 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.google.re2j.Pattern; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.apache.commons.jexl3.JexlBuilder; import org.apache.commons.jexl3.JexlContext; import org.apache.commons.jexl3.JexlEngine; -import org.apache.commons.jexl3.JexlException; import org.apache.commons.jexl3.MapContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,6 +49,7 @@ import org.slf4j.LoggerFactory; /** * Utilities used to test encoding and decoding of Policy objects. */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) public class GsonTestUtils { private static final Logger logger = LoggerFactory.getLogger(GsonTestUtils.class); @@ -67,6 +67,7 @@ public class GsonTestUtils { /** * Used to encode and decode an object via gson. */ + @Getter private Gson gson; /** @@ -79,19 +80,6 @@ public class GsonTestUtils { } /** - * Constructs the object. - * - * @param gson used to encode via gson - */ - protected GsonTestUtils(Gson gson) { - this.gson = gson; - } - - public Gson getGson() { - return gson; - } - - /** * Serializes and then deserializes an object using gson. * * @param object the object to be serialized @@ -110,7 +98,6 @@ public class GsonTestUtils { * * @param object the object to be encoded * @param expected the expected value - * @throws Exception if the file cannot be read */ public void compareGson(Object object, Class<?> expected) { compareGson(object, new File(expected.getSimpleName() + ".json")); @@ -123,11 +110,10 @@ public class GsonTestUtils { * * @param object the object to be encoded * @param expected the expected value - * @throws Exception if the file cannot be read */ public void compareGson(Object object, File expected) { // file is not required to have a full path - find it via getResource() - URL url = object.getClass().getResource(expected.getName()); + var url = object.getClass().getResource(expected.getName()); if (url == null) { throw new JsonParseException(new FileNotFoundException(expected.getName())); } @@ -169,7 +155,11 @@ public class GsonTestUtils { JsonElement gsonjo = reorder(gson.fromJson(sgson, JsonElement.class)); JsonElement expjo = reorder(expected); - assertEquals(expjo.toString(), gsonjo.toString()); + /* + * As this method is only used within junit tests, it is OK to use assert calls, + * thus sonar is disabled. + */ + assertEquals(expjo.toString(), gsonjo.toString()); // NOSONAR } /** @@ -179,7 +169,7 @@ public class GsonTestUtils { * @throws IOException if an error occurs */ protected String readFile(File file) throws IOException { - return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); + return Files.readString(file.toPath()); } @@ -193,7 +183,7 @@ public class GsonTestUtils { * @return the text, after interpolating the script elements */ public String applyScripts(String text, Object object) { - Matcher mat = SCRIPT_PAT.matcher(text); + var mat = SCRIPT_PAT.matcher(text); if (!mat.find()) { // contains no script elements - just return it as is return text; @@ -205,30 +195,25 @@ public class GsonTestUtils { context.set("obj", object); // work our way through the text, interpolating script elements as we go - StringBuilder bldr = new StringBuilder(); - int ilast = 0; + var bldr = new StringBuilder(); + var ilast = 0; mat.reset(); while (mat.find(ilast)) { // append segment that appears between last match and this int inext = mat.start(); - bldr.append(text.substring(ilast, inext)); + bldr.append(text, ilast, inext); // next match begins after the current match ilast = mat.end(); // interpolate the script String script = mat.group(1); - try { - /* - * Note: must use "eng" instead of "engineInstance" to ensure that we use - * the same engine that's associated with the bindings. - */ - Object result = eng.createExpression(script).evaluate(context); - bldr.append(result == null ? "null" : result.toString()); - - } catch (JexlException e) { - throw new JsonParseException("cannot expand element: " + mat.group(), e); - } + /* + * Note: must use "eng" instead of "engineInstance" to ensure that we use + * the same engine that's associated with the bindings. + */ + Object result = eng.createExpression(script).evaluate(context); + bldr.append(result == null ? "null" : result.toString()); } // append final segment @@ -271,11 +256,11 @@ public class GsonTestUtils { * @return a new object, without the null items */ public JsonObject reorder(JsonObject jsonObj) { - JsonObject newjo = new JsonObject(); + var newjo = new JsonObject(); // sort the keys before copying to the new object List<Entry<String, JsonElement>> sortedSet = new ArrayList<>(jsonObj.entrySet()); - Collections.sort(sortedSet, (left, right) -> left.getKey().compareTo(right.getKey())); + sortedSet.sort(Entry.comparingByKey()); for (Entry<String, JsonElement> ent : sortedSet) { JsonElement val = ent.getValue(); @@ -297,7 +282,7 @@ public class GsonTestUtils { * @return a new array, with null items removed from all elements */ public JsonArray reorder(JsonArray jsonArray) { - JsonArray newarr = new JsonArray(); + var newarr = new JsonArray(); for (JsonElement ent : jsonArray) { newarr.add(reorder(ent)); } |