use of io.spine.core.MessageInvalid in project core-java by SpineEventEngine.
the class ValidatingFilter method accept.
@Override
public Optional<Ack> accept(E envelope) {
checkNotNull(envelope);
final Optional<MessageInvalid> violation = validator.validate(envelope);
if (violation.isPresent()) {
final Error error = violation.get().asError();
final Any packedId = Identifier.pack(envelope.getId());
final Ack result = reject(packedId, error);
return Optional.of(result);
} else {
return Optional.absent();
}
}
use of io.spine.core.MessageInvalid in project core-java by SpineEventEngine.
the class CommandValidator method isTenantIdValid.
private Optional<MessageInvalid> isTenantIdValid(CommandEnvelope envelope) {
final TenantId tenantId = envelope.getTenantId();
final boolean tenantSpecified = !isDefault(tenantId);
final Command command = envelope.getCommand();
if (commandBus.isMultitenant()) {
if (!tenantSpecified) {
final MessageInvalid report = missingTenantId(command);
return of(report);
}
} else {
if (tenantSpecified) {
final MessageInvalid report = tenantIdInapplicable(command);
return of(report);
}
}
return absent();
}
use of io.spine.core.MessageInvalid in project core-java by SpineEventEngine.
the class EventValidatorShould method validate_event_messages.
@Test
public void validate_event_messages() {
final MessageValidator messageValidator = mock(MessageValidator.class);
when(messageValidator.validate(any(Message.class))).thenReturn(newArrayList(ConstraintViolation.getDefaultInstance(), ConstraintViolation.getDefaultInstance()));
final Event event = eventFactory.createEvent(Sample.messageOfType(ProjectCreated.class));
final EventValidator eventValidator = new EventValidator(messageValidator);
final Optional<MessageInvalid> error = eventValidator.validate(EventEnvelope.of(event));
assertTrue(error.isPresent());
final Error actualError = error.get().asError();
assertEquals(EventValidationError.getDescriptor().getFullName(), actualError.getType());
}
use of io.spine.core.MessageInvalid in project core-java by SpineEventEngine.
the class EventValidator method validate.
@Override
public Optional<MessageInvalid> validate(EventEnvelope envelope) {
checkNotNull(envelope);
final Event event = envelope.getOuterObject();
MessageInvalid result = null;
final List<ConstraintViolation> violations = messageValidator.validate(event);
if (!violations.isEmpty()) {
result = onConstraintViolations(event, violations);
}
return Optional.fromNullable(result);
}
use of io.spine.core.MessageInvalid in project core-java by SpineEventEngine.
the class CommandValidator method isCommandValid.
private Optional<MessageInvalid> isCommandValid(CommandEnvelope envelope) {
final Command command = envelope.getCommand();
final List<ConstraintViolation> violations = inspect(envelope);
InvalidCommandException exception = null;
if (!violations.isEmpty()) {
exception = onConstraintViolations(command, violations);
commandBus.commandStore().storeWithError(command, exception);
}
return Optional.<MessageInvalid>fromNullable(exception);
}
Aggregations