This is an automated email from the ASF dual-hosted git repository. jsinovassinnaik pushed a commit to branch UNOMI-775-add-validation-endpoint in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 2bea481148fe8027041912031e459bd20136d89b Author: jsinovassin <[email protected]> AuthorDate: Tue May 2 17:07:14 2023 +0200 remove eventType from ValidationException --- .../unomi/schema/api/ValidationException.java | 15 ------------ .../unomi/schema/impl/SchemaServiceImpl.java | 28 ++++++++++------------ .../java/org/apache/unomi/itests/JSONSchemaIT.java | 2 +- 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java index c5481da83..58610b285 100644 --- a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java +++ b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java @@ -23,17 +23,10 @@ package org.apache.unomi.schema.api; */ public class ValidationException extends Exception { - private String eventType; - public ValidationException(String message) { super(message); } - public ValidationException(String message, String eventType) { - super(message); - this.eventType = eventType; - } - public ValidationException(Throwable throwable) { super(throwable); } @@ -41,12 +34,4 @@ public class ValidationException extends Exception { public ValidationException(String message, Throwable throwable) { super(message, throwable); } - - public void setEventType(String eventType) { - this.eventType = eventType; - } - - public String getEventType() { - return eventType; - } } diff --git a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java index 5adcbc010..ca54fd243 100644 --- a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java +++ b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java @@ -124,10 +124,14 @@ public class SchemaServiceImpl implements SchemaService { Map<String, Set<ValidationError>> errorsPerEventType = new HashMap<>(); JsonNode eventsNodes = parseData(events); eventsNodes.forEach(event -> { + String eventType = null; try { - Set<ValidationError> errors = validateNodeEvent(event); + eventType = extractEventType(event); + JsonSchemaWrapper eventSchema = getSchemaForEventType(eventType); + JsonSchema jsonSchema = getJsonSchema(eventSchema.getItemId()); + + Set<ValidationError> errors = validate(event, jsonSchema); if (!errors.isEmpty()) { - String eventType = event.get("eventType").asText(); if (errorsPerEventType.containsKey(eventType)) { errorsPerEventType.get(eventType).addAll(errors); } else { @@ -136,11 +140,11 @@ public class SchemaServiceImpl implements SchemaService { } } catch (ValidationException e) { Set<ValidationError> errors = buildCustomErrorMessage(e.getMessage()); - String eventType = e.getEventType() != null ? e.getEventType() : GENERIC_ERROR_KEY; - if (errorsPerEventType.containsKey(eventType)) { - errorsPerEventType.get(eventType).addAll(errors); + String eventTypeOrErrorKey = eventType != null ? eventType : GENERIC_ERROR_KEY; + if (errorsPerEventType.containsKey(eventTypeOrErrorKey)) { + errorsPerEventType.get(eventTypeOrErrorKey).addAll(errors); } else { - errorsPerEventType.put(eventType, errors); + errorsPerEventType.put(eventTypeOrErrorKey, errors); } } }); @@ -154,14 +158,6 @@ public class SchemaServiceImpl implements SchemaService { return errors; } - private Set<ValidationError> validateNodeEvent(JsonNode event) throws ValidationException { - String eventType = extractEventType(event); - JsonSchemaWrapper eventSchema = getSchemaForEventType(eventType); - JsonSchema jsonSchema = getJsonSchema(eventSchema.getItemId()); - - return validate(event, jsonSchema); - } - @Override public JsonSchemaWrapper getSchema(String schemaId) { return schemasById.get(schemaId); @@ -182,7 +178,7 @@ public class SchemaServiceImpl implements SchemaService { @Override public JsonSchemaWrapper getSchemaForEventType(String eventType) throws ValidationException { if (StringUtils.isEmpty(eventType)) { - throw new ValidationException("eventType missing", eventType); + throw new ValidationException("eventType missing"); } return schemasById.values().stream() @@ -192,7 +188,7 @@ public class SchemaServiceImpl implements SchemaService { jsonSchemaWrapper.getName() != null && jsonSchemaWrapper.getName().equals(eventType)) .findFirst() - .orElseThrow(() -> new ValidationException("Schema not found for event type: " + eventType, eventType)); + .orElseThrow(() -> new ValidationException("Schema not found for event type: " + eventType)); } @Override diff --git a/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java b/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java index cd4373b48..366f3ece5 100644 --- a/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/JSONSchemaIT.java @@ -269,7 +269,7 @@ public class JSONSchemaIT extends BaseIT { keepTrying("No error should have been detected", () -> { try { - return schemaService.validateEvents(listEvents.toString()).get("flattened").isEmpty(); + return schemaService.validateEvents(listEvents.toString()).isEmpty(); } catch (Exception e) { return false; }
