diff options
Diffstat (limited to 'src')
3 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/org/onap/cps/temporal/controller/event/listener/exception/InvalidEventEnvelopException.java b/src/main/java/org/onap/cps/temporal/controller/event/listener/exception/InvalidEventEnvelopException.java index df4e756..1d8269a 100644 --- a/src/main/java/org/onap/cps/temporal/controller/event/listener/exception/InvalidEventEnvelopException.java +++ b/src/main/java/org/onap/cps/temporal/controller/event/listener/exception/InvalidEventEnvelopException.java @@ -25,17 +25,21 @@ import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; +import org.onap.cps.event.model.CpsDataUpdatedEvent; /** * Class representing an invalid event envelop exception. + * It refers to the invalid event and details the invalid fields it has. */ @Getter public class InvalidEventEnvelopException extends EventListenerException { + private final CpsDataUpdatedEvent cpsDataUpdatedEvent; private final List<InvalidField> invalidFields = new ArrayList<>(); - public InvalidEventEnvelopException(final String message) { + public InvalidEventEnvelopException(final String message, final CpsDataUpdatedEvent cpsDataUpdatedEvent) { super(message); + this.cpsDataUpdatedEvent = cpsDataUpdatedEvent; } public void addInvalidField(final InvalidField invalidField) { @@ -48,7 +52,9 @@ public class InvalidEventEnvelopException extends EventListenerException { @Override public String getMessage() { - return String.format("%s. invalidFields: %s", super.getMessage(), this.invalidFields.toString()); + return + String.format("%s. Event: %s. Invalid fields: %s", + super.getMessage(), this.cpsDataUpdatedEvent, this.invalidFields.toString()); } @AllArgsConstructor diff --git a/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java b/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java index 79c9d92..2e4b88e 100644 --- a/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java +++ b/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java @@ -89,9 +89,10 @@ public class DataUpdatedEventListener { private void validateEventEnvelop(final CpsDataUpdatedEvent cpsDataUpdatedEvent) { - final var invalidEventEnvelopException = new InvalidEventEnvelopException("Validation failure"); + final var invalidEventEnvelopException = + new InvalidEventEnvelopException("Validation failure", cpsDataUpdatedEvent); - // Schema + // Validate schema if (cpsDataUpdatedEvent.getSchema() == null) { invalidEventEnvelopException.addInvalidField( new InvalidEventEnvelopException.InvalidField( @@ -99,22 +100,22 @@ public class DataUpdatedEventListener { CpsDataUpdatedEvent.Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT .value())); } - // Id + // Validate id if (!StringUtils.hasText(cpsDataUpdatedEvent.getId())) { invalidEventEnvelopException.addInvalidField( new InvalidEventEnvelopException.InvalidField( MISSING, "id", null, null)); } - // Source - if (cpsDataUpdatedEvent.getSource() == null || !cpsDataUpdatedEvent.getSource().equals(EVENT_SOURCE)) { + // Validate source + if (!EVENT_SOURCE.equals(cpsDataUpdatedEvent.getSource())) { invalidEventEnvelopException.addInvalidField( new InvalidEventEnvelopException.InvalidField( UNEXPECTED, "source", cpsDataUpdatedEvent.getSource() != null ? cpsDataUpdatedEvent.getSource().toString() : null, EVENT_SOURCE.toString())); } - // Type - if (cpsDataUpdatedEvent.getType() == null || !cpsDataUpdatedEvent.getType().equals(EVENT_TYPE)) { + // Validate type + if (!EVENT_TYPE.equals(cpsDataUpdatedEvent.getType())) { invalidEventEnvelopException.addInvalidField( new InvalidEventEnvelopException.InvalidField( UNEXPECTED, "type", cpsDataUpdatedEvent.getType(), EVENT_TYPE)); diff --git a/src/test/groovy/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListenerSpec.groovy b/src/test/groovy/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListenerSpec.groovy index d3a407c..35ed977 100644 --- a/src/test/groovy/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListenerSpec.groovy +++ b/src/test/groovy/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListenerSpec.groovy @@ -78,6 +78,7 @@ class DataUpdatedEventListenerSpec extends Specification { objectUnderTest.consume(invalidEvent) then: 'an exception is thrown with 4 invalid fields' def e = thrown(InvalidEventEnvelopException) + e.getCpsDataUpdatedEvent() == invalidEvent e.getInvalidFields().size() == 4 e.getInvalidFields().contains( new InvalidEventEnvelopException.InvalidField( @@ -104,6 +105,7 @@ class DataUpdatedEventListenerSpec extends Specification { objectUnderTest.consume(invalidEvent) then: 'an exception is thrown with 2 invalid fields' def e = thrown(InvalidEventEnvelopException) + e.getCpsDataUpdatedEvent() == invalidEvent e.getInvalidFields().size() == 2 e.getInvalidFields().contains( new InvalidEventEnvelopException.InvalidField( |