Search in sources :

Example 1 with MessageValidator

use of io.spine.validate.MessageValidator in project core-java by SpineEventEngine.

the class EventBusShould method call_onError_if_event_is_invalid.

@Test
public void call_onError_if_event_is_invalid() {
    final MessageValidator validator = mock(MessageValidator.class);
    doReturn(newArrayList(ConstraintViolation.getDefaultInstance())).when(validator).validate(any(Message.class));
    final EventBus eventBus = EventBus.newBuilder().setStorageFactory(storageFactory).setEventValidator(validator).build();
    eventBus.register(new ProjectCreatedSubscriber());
    final boolean isValid = eventBus.validate(Given.EventMessage.projectCreated(), responseObserver);
    assertFalse(isValid);
    assertReturnedExceptionAndNoResponse(InvalidEventException.class, responseObserver);
}
Also used : Message(com.google.protobuf.Message) MessageValidator(io.spine.validate.MessageValidator) Test(org.junit.Test)

Example 2 with MessageValidator

use of io.spine.validate.MessageValidator in project core-java by SpineEventEngine.

the class EventBusBuilderShould method allow_custom_message_validators.

@Test
public void allow_custom_message_validators() {
    final StorageFactory storageFactory = StorageFactorySwitch.newInstance(newName("test"), false).get();
    final MessageValidator validator = mock(MessageValidator.class);
    final EventBus eventBus = builder().setEventValidator(validator).setStorageFactory(storageFactory).build();
    assertEquals(validator, eventBus.getMessageValidator());
}
Also used : StorageFactory(io.spine.server.storage.StorageFactory) MessageValidator(io.spine.validate.MessageValidator) Test(org.junit.Test)

Example 3 with MessageValidator

use of io.spine.validate.MessageValidator 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 MessageValidator

use of io.spine.validate.MessageValidator in project core-java by SpineEventEngine.

the class EventBusBuilderShould method return_set_EventValidator.

@Test
public void return_set_EventValidator() {
    final MessageValidator validator = MessageValidator.newInstance();
    assertEquals(validator, builder().setEventValidator(validator).getEventValidator().get());
}
Also used : MessageValidator(io.spine.validate.MessageValidator) Test(org.junit.Test)

Aggregations

MessageValidator (io.spine.validate.MessageValidator)4 Test (org.junit.Test)4 Message (com.google.protobuf.Message)2 Error (io.spine.base.Error)1 Event (io.spine.core.Event)1 EventValidationError (io.spine.core.EventValidationError)1 MessageInvalid (io.spine.core.MessageInvalid)1 StorageFactory (io.spine.server.storage.StorageFactory)1 ProjectCreated (io.spine.test.event.ProjectCreated)1