diff options
author | Henrik Andersson <henrik.b.andersson@est.tech> | 2019-04-09 10:55:51 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-04-09 10:55:51 +0000 |
commit | a64dfa11dba25f9e22ae931a4e5c9b177e6527e0 (patch) | |
tree | 4f2f6653ed113ca6b543e9c33ed567103e9e313f /src/main/java/org/onap | |
parent | dc280ce19354f127d4f1ffd1ff83f6642a7eeb6a (diff) | |
parent | 4393d54cb1f76beef314dd6810f00624d87b0db6 (diff) |
Merge "Fix required config validation"
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r-- | src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java | 1 | ||||
-rw-r--r-- | src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java | 42 |
2 files changed, 32 insertions, 11 deletions
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java index 0f1aaa9..a7d211f 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java @@ -33,6 +33,7 @@ import org.onap.dcaegen2.services.pmmapper.utils.GSONRequired; @NoArgsConstructor
public class MeasFilterConfig {
+ @GSONRequired
@SerializedName("filters")
public List<Filter> filters;
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java index e956398..258b831 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java @@ -25,8 +25,13 @@ import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; +import lombok.NonNull; + import java.lang.reflect.Field; import java.lang.reflect.Type; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; /** @@ -38,20 +43,35 @@ public class RequiredFieldDeserializer<T> implements JsonDeserializer<T> { @Override public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { T obj = new Gson().fromJson(jsonElement, type); - for (Field field : obj.getClass().getDeclaredFields()) { - if (field.getAnnotation(GSONRequired.class) != null) { - field.setAccessible(true); - try { - if (field.get(obj) == null) { - throw new JsonParseException(String.format("Field: '%s', is required but not found.", field.getName())); - } - } catch (Exception exception) { - throw new JsonParseException("Failed to check fields.", exception); - } + validateRequiredFields(obj.getClass().getDeclaredFields(), obj); + return obj; + } + + private void validateRequiredFields(@NonNull Field[] fields, @NonNull Object pojo) { + if (pojo instanceof List) { + final List<?> pojoList = (List<?>) pojo; + for (final Object pojoListPojo : pojoList) { + validateRequiredFields(pojoListPojo.getClass().getDeclaredFields(), pojoListPojo); } } - return obj; + Stream.of(fields) + .filter(field -> field.getAnnotation(GSONRequired.class) != null) + .forEach(field -> { + try { + field.setAccessible(true); + Object fieldObj = Optional.ofNullable(field.get(pojo)) + .orElseThrow(()-> new JsonParseException( + String.format("Field '%s' in class '%s' is required but not found.", + field.getName(), pojo.getClass().getSimpleName()))); + + Field[] declaredFields = fieldObj.getClass().getDeclaredFields(); + validateRequiredFields(declaredFields, fieldObj); + } + catch (Exception exception) { + throw new JsonParseException("Failed to check fields.", exception); + } + }); } } |