summaryrefslogtreecommitdiffstats
path: root/src/main/java/org
diff options
context:
space:
mode:
authoremartin <ephraim.martin@est.tech>2019-04-05 09:51:13 +0000
committeremartin <ephraim.martin@est.tech>2019-04-05 09:51:13 +0000
commit4393d54cb1f76beef314dd6810f00624d87b0db6 (patch)
treea1b98ff121e8ad06351a8c333d825d8f47e4d1b6 /src/main/java/org
parent49d2deae8aa7b57ecf6fb692803594c1bae8e8bf (diff)
Fix required config validation
*Nested objects and list validation Change-Id: Icf615a610dd6e669d66ae07adc1316ca167a671b Issue-ID: DCAEGEN2-1327 Signed-off-by: emartin <ephraim.martin@est.tech>
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasFilterConfig.java1
-rw-r--r--src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequiredFieldDeserializer.java42
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);
+ }
+ });
}
}