From c58f445fcef56a6440f3b21d54b390700623c48c Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Tue, 21 Aug 2018 17:55:30 +0530 Subject: Test coverage Increase test coverage Change-Id: I43e3149d0d4b07dc8e260f3d5fac025197882f3a Issue-ID: SDC-1673 Signed-off-by: siddharth0905 --- .../openecomp/core/utilities/json/JsonUtil.java | 326 ++++++++++----------- 1 file changed, 163 insertions(+), 163 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java index 83ad43ca65..b83ad2782d 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package org.openecomp.core.utilities.json; @@ -22,6 +22,17 @@ import com.google.gson.JsonIOException; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import org.apache.commons.collections4.CollectionUtils; import org.everit.json.schema.EnumSchema; import org.everit.json.schema.Schema; @@ -35,186 +46,175 @@ import org.openecomp.core.utilities.deserializers.RequirementDefinitionDeseriali import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringReader; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - /** * The type Json util. */ public class JsonUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class); - private static final GsonBuilder gsonBuilder; - private static final Gson gson; - - static { - gsonBuilder = new GsonBuilder(); - gsonBuilder.registerTypeAdapter(RequirementDefinition.class, new - RequirementDefinitionDeserializer()); - gson = gsonBuilder.create(); - } - - private JsonUtil() { - } - - /** - * Object 2 json string. - * - * @param obj the obj - * @return the string - */ - public static String object2Json(Object obj) { - return sbObject2Json(obj).toString(); - - } - - /** - * Sb object 2 json string builder. - * - * @param obj the obj - * @return the string builder - */ - public static StringBuilder sbObject2Json(Object obj) { - return new StringBuilder(new GsonBuilder().setPrettyPrinting().create().toJson(obj)); - } - - /** - * Json 2 object t. - * - * @param the type parameter - * @param json the json - * @param classOfT the class of t - * @return the t - */ - public static T json2Object(String json, Class classOfT) { - T typ; - try { - try (Reader br = new StringReader(json)) { - typ = gson.fromJson(br, classOfT); - } - } catch (JsonIOException | JsonSyntaxException | IOException exception) { - throw new RuntimeException(exception); + + private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class); + private static final GsonBuilder gsonBuilder; + private static final Gson gson; + + static { + gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(RequirementDefinition.class, new + RequirementDefinitionDeserializer()); + gson = gsonBuilder.create(); } - return typ; - } - - /** - * Json 2 object t. - * - * @param the type parameter - * @param is the is - * @param classOfT the class of t - * @return the t - */ - public static T json2Object(InputStream is, Class classOfT) { - T type; - try (Reader br = new BufferedReader(new InputStreamReader(is))) { - type = new Gson().fromJson(br, classOfT); - } - catch (JsonIOException | JsonSyntaxException | IOException exception) { - throw new RuntimeException(exception); + + private JsonUtil() { } - return type; - } - - - /** - * Is valid json boolean. - * - * @param json the json - * @return the boolean - */ - //todo check https://github.com/stleary/JSON-java as replacement for this code - public static boolean isValidJson(String json) { - try { - return new JsonParser().parse(json).isJsonObject(); - } catch (JsonSyntaxException jse) { - LOGGER.error("Invalid json, Failed to parse json", jse); - return false; + + /** + * Object 2 json string. + * + * @param obj the obj + * @return the string + */ + public static String object2Json(Object obj) { + return sbObject2Json(obj).toString(); + } - } - - /** - * Validate list. - * - * @param json the json - * @param jsonSchema the json schema - * @return the list - */ - public static List validate(String json, String jsonSchema) { - List validationErrors = validateUsingEverit(json, jsonSchema); - return validationErrors == null ? null - : validationErrors.stream().map(JsonUtil::mapValidationExceptionToMessage) - .collect(Collectors.toList()); - } - - private static String mapValidationExceptionToMessage(ValidationException exception) { - Object schema = exception.getViolatedSchema(); - - if (schema instanceof EnumSchema) { - return mapEnumViolationToMessage(exception); - } else if (schema instanceof StringSchema) { - return mapStringViolationToMessage(exception); + + /** + * Sb object 2 json string builder. + * + * @param obj the obj + * @return the string builder + */ + public static StringBuilder sbObject2Json(Object obj) { + return new StringBuilder(new GsonBuilder().setPrettyPrinting().create().toJson(obj)); } - return exception.getMessage(); - } - - private static String mapEnumViolationToMessage(ValidationException exception) { - Set possibleValues = ((EnumSchema) exception.getViolatedSchema()).getPossibleValues(); - return exception.getMessage().replaceFirst("enum value", possibleValues.size() == 1 - ? String.format("value. %s is the only possible value for this field", - possibleValues.iterator().next()) - : String.format("value. Possible values: %s", CommonMethods - .collectionToCommaSeparatedString( - possibleValues.stream().map(Object::toString).collect(Collectors.toList())))); - } - - private static String mapStringViolationToMessage(ValidationException validationException) { - if (ValidationType.PATTERN.getKeyword().equals(validationException.getKeyword())) { - String message = validationException.getMessage(); - String value = message.substring(message.indexOf("["), message.indexOf("]") + 1); - return message.replace("string " + value, value + " is not valid value. It"); + /** + * Json 2 object t. + * + * @param the type parameter + * @param json the json + * @param classOfT the class of t + * @return the t + */ + public static T json2Object(String json, Class classOfT) { + T typ; + try { + try (Reader br = new StringReader(json)) { + typ = gson.fromJson(br, classOfT); + } + } catch (JsonIOException | JsonSyntaxException | IOException exception) { + throw new RuntimeException(exception); + } + return typ; } - return validationException.getMessage(); - } - - private static List validateUsingEverit(String json, String jsonSchema) { - LOGGER.debug( - String.format("validateUsingEverit start, json=%s, jsonSchema=%s", json, jsonSchema)); - if (json == null || jsonSchema == null) { - throw new IllegalArgumentException("Input strings json and jsonSchema can not be null"); + + /** + * Json 2 object t. + * + * @param the type parameter + * @param is the is + * @param classOfT the class of t + * @return the t + */ + public static T json2Object(InputStream is, Class classOfT) { + T type; + try (Reader br = new BufferedReader(new InputStreamReader(is))) { + type = new Gson().fromJson(br, classOfT); + } catch (JsonIOException | JsonSyntaxException | IOException exception) { + throw new RuntimeException(exception); + } + return type; } - Schema schemaObj = SchemaLoader.load(new JSONObject(jsonSchema)); - try { - schemaObj.validate(new JSONObject(json)); - } catch (ValidationException ve) { - return CollectionUtils.isEmpty(ve.getCausingExceptions()) ? Collections.singletonList(ve) - : ve.getCausingExceptions(); + + /** + * Is valid json boolean. + * + * @param json the json + * @return the boolean + */ + //todo check https://github.com/stleary/JSON-java as replacement for this code + public static boolean isValidJson(String json) { + try { + return new JsonParser().parse(json).isJsonObject(); + } catch (JsonSyntaxException jse) { + LOGGER.error("Invalid json, Failed to parse json", jse); + return false; + } } - return null; - } - private enum ValidationType { - PATTERN("pattern"); + /** + * Validate list. + * + * @param json the json + * @param jsonSchema the json schema + * @return the list + */ + public static List validate(String json, String jsonSchema) { + List validationErrors = validateUsingEverit(json, jsonSchema); + return validationErrors == null ? null + : validationErrors.stream().map(JsonUtil::mapValidationExceptionToMessage) + .collect(Collectors.toList()); + } - private String keyword; + private static String mapValidationExceptionToMessage(ValidationException exception) { + Object schema = exception.getViolatedSchema(); - private ValidationType(String keyword) { - this.keyword = keyword; + if (schema instanceof EnumSchema) { + return mapEnumViolationToMessage(exception); + } else if (schema instanceof StringSchema) { + return mapStringViolationToMessage(exception); + } + + return exception.getMessage(); + } + + private static String mapEnumViolationToMessage(ValidationException exception) { + Set possibleValues = ((EnumSchema) exception.getViolatedSchema()).getPossibleValues(); + return exception.getMessage().replaceFirst("enum value", possibleValues.size() == 1 + ? String.format("value. %s is the only possible value for this field", + possibleValues.iterator().next()) + : String.format("value. Possible values: %s", CommonMethods + .collectionToCommaSeparatedString( + possibleValues.stream().map(Object::toString).collect(Collectors.toList())))); + } + + private static String mapStringViolationToMessage(ValidationException validationException) { + if (ValidationType.PATTERN.getKeyword().equals(validationException.getKeyword())) { + String message = validationException.getMessage(); + String value = message.substring(message.indexOf("["), message.indexOf("]") + 1); + return message.replace("string " + value, value + " is not valid value. It"); + } + return validationException.getMessage(); } - String getKeyword() { - return keyword; + private static List validateUsingEverit(String json, String jsonSchema) { + LOGGER.debug( + String.format("validateUsingEverit start, json=%s, jsonSchema=%s", json, jsonSchema)); + if (json == null || jsonSchema == null) { + throw new IllegalArgumentException("Input strings json and jsonSchema can not be null"); + } + + Schema schemaObj = SchemaLoader.load(new JSONObject(jsonSchema)); + try { + schemaObj.validate(new JSONObject(json)); + } catch (ValidationException ve) { + return CollectionUtils.isEmpty(ve.getCausingExceptions()) ? Collections.singletonList(ve) + : ve.getCausingExceptions(); + } + return null; + } + + private enum ValidationType { + PATTERN("pattern"); + + private String keyword; + + ValidationType(String keyword) { + this.keyword = keyword; + } + + String getKeyword() { + return keyword; + } } - } } -- cgit 1.2.3-korg