Search in sources :

Example 1 with MessageInvalid

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();
    }
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) MessageInvalid(io.spine.core.MessageInvalid) Any(com.google.protobuf.Any)

Example 2 with MessageInvalid

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();
}
Also used : InvalidCommandException.onMissingTenantId(io.spine.server.commandbus.InvalidCommandException.onMissingTenantId) TenantId(io.spine.core.TenantId) InvalidCommandException.onInapplicableTenantId(io.spine.server.commandbus.InvalidCommandException.onInapplicableTenantId) Command(io.spine.core.Command) MessageInvalid(io.spine.core.MessageInvalid)

Example 3 with MessageInvalid

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());
}
Also used : Message(com.google.protobuf.Message) Event(io.spine.core.Event) Error(io.spine.base.Error) EventValidationError(io.spine.core.EventValidationError) MessageValidator(io.spine.validate.MessageValidator) MessageInvalid(io.spine.core.MessageInvalid) ProjectCreated(io.spine.test.event.ProjectCreated) Test(org.junit.Test)

Example 4 with MessageInvalid

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);
}
Also used : ConstraintViolation(io.spine.validate.ConstraintViolation) Event(io.spine.core.Event) MessageInvalid(io.spine.core.MessageInvalid)

Example 5 with MessageInvalid

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);
}
Also used : Command(io.spine.core.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) MessageInvalid(io.spine.core.MessageInvalid)

Aggregations

MessageInvalid (io.spine.core.MessageInvalid)5 Error (io.spine.base.Error)2 Command (io.spine.core.Command)2 Event (io.spine.core.Event)2 ConstraintViolation (io.spine.validate.ConstraintViolation)2 Any (com.google.protobuf.Any)1 Message (com.google.protobuf.Message)1 Ack (io.spine.core.Ack)1 EventValidationError (io.spine.core.EventValidationError)1 TenantId (io.spine.core.TenantId)1 InvalidCommandException.onInapplicableTenantId (io.spine.server.commandbus.InvalidCommandException.onInapplicableTenantId)1 InvalidCommandException.onMissingTenantId (io.spine.server.commandbus.InvalidCommandException.onMissingTenantId)1 ProjectCreated (io.spine.test.event.ProjectCreated)1 MessageValidator (io.spine.validate.MessageValidator)1 Test (org.junit.Test)1