|
| 1 | +package test.loom.eventsourcing; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import autoparams.AutoSource; |
| 6 | +import autoparams.customization.Customization; |
| 7 | +import autoparams.mockito.MockitoCustomizer; |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.util.Optional; |
| 14 | +import loom.eventsourcing.StreamCommand; |
| 15 | +import loom.eventsourcing.StreamCommandTypeStrategy; |
| 16 | +import loom.type.TypeFormatter; |
| 17 | +import loom.type.TypeResolver; |
| 18 | +import loom.type.TypeStrategy; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import test.loom.CreateUser; |
| 22 | +import test.loom.User; |
| 23 | + |
| 24 | +class StreamCommandTypeStrategy_specs { |
| 25 | + |
| 26 | + @ParameterizedTest |
| 27 | + @AutoSource |
| 28 | + @Customization(MockitoCustomizer.class) |
| 29 | + void sut_implements_TypeStrategy(StreamCommandTypeStrategy sut) { |
| 30 | + assertThat(sut).isInstanceOf(TypeStrategy.class); |
| 31 | + } |
| 32 | + |
| 33 | + @ParameterizedTest |
| 34 | + @AutoSource |
| 35 | + @Customization(MockitoCustomizer.class) |
| 36 | + void tryFormatType_returns_empty(TypeResolver resolver) { |
| 37 | + StreamCommandTypeStrategy sut = new StreamCommandTypeStrategy( |
| 38 | + () -> TypeStrategy.create(TypeFormatter.forTypeName(), resolver)); |
| 39 | + |
| 40 | + Optional<String> actual = sut.tryFormatType(User.class); |
| 41 | + |
| 42 | + assertThat(actual).isEmpty(); |
| 43 | + } |
| 44 | + |
| 45 | + @ParameterizedTest |
| 46 | + @AutoSource |
| 47 | + @Customization(MockitoCustomizer.class) |
| 48 | + void tryFormatTypeOf_correctly_formats_type_of_stream_command( |
| 49 | + TypeResolver resolver, |
| 50 | + StreamCommand<CreateUser> value |
| 51 | + ) { |
| 52 | + StreamCommandTypeStrategy sut = new StreamCommandTypeStrategy( |
| 53 | + () -> TypeStrategy.create(TypeFormatter.forTypeName(), resolver)); |
| 54 | + |
| 55 | + Optional<String> actual = sut.tryFormatTypeOf(value); |
| 56 | + |
| 57 | + assertThat(actual).contains("streamcommand:test.loom.CreateUser"); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @AutoSource |
| 62 | + @Customization(MockitoCustomizer.class) |
| 63 | + void tryFormatTypeOf_returns_empty_for_non_stream_command_value( |
| 64 | + TypeResolver resolver, |
| 65 | + CreateUser value |
| 66 | + ) { |
| 67 | + StreamCommandTypeStrategy sut = new StreamCommandTypeStrategy( |
| 68 | + () -> TypeStrategy.create(TypeFormatter.forTypeName(), resolver)); |
| 69 | + |
| 70 | + Optional<String> actual = sut.tryFormatTypeOf(value); |
| 71 | + |
| 72 | + assertThat(actual).isEmpty(); |
| 73 | + } |
| 74 | + |
| 75 | + @ParameterizedTest |
| 76 | + @AutoSource |
| 77 | + @Customization(MockitoCustomizer.class) |
| 78 | + void tryResolveType_returns_empty_optional_for_invalid_formatted_type( |
| 79 | + TypeStrategy payloadStrategy, |
| 80 | + String formattedType |
| 81 | + ) { |
| 82 | + StreamCommandTypeStrategy sut = |
| 83 | + new StreamCommandTypeStrategy(() -> payloadStrategy); |
| 84 | + |
| 85 | + Optional<Type> actual = sut.tryResolveType(formattedType); |
| 86 | + |
| 87 | + assertThat(actual).isEmpty(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void tryResolveType_returns_type_for_formatted_stream_command_type() { |
| 92 | + // Arrange |
| 93 | + StreamCommandTypeStrategy sut = new StreamCommandTypeStrategy( |
| 94 | + () -> TypeStrategy.create( |
| 95 | + TypeFormatter.forTypeName(), |
| 96 | + TypeResolver.forClassName() |
| 97 | + ) |
| 98 | + ); |
| 99 | + |
| 100 | + String formattedType = "streamcommand:test.loom.CreateUser"; |
| 101 | + |
| 102 | + // Act |
| 103 | + Optional<Type> actual = sut.tryResolveType(formattedType); |
| 104 | + |
| 105 | + // Assert |
| 106 | + assertThat(actual).isNotEmpty(); |
| 107 | + } |
| 108 | + |
| 109 | + @ParameterizedTest |
| 110 | + @AutoSource |
| 111 | + void tryResolveType_returns_correct_stream_command_type( |
| 112 | + StreamCommand<CreateUser> command, |
| 113 | + ObjectMapper mapper |
| 114 | + ) throws JsonProcessingException { |
| 115 | + // Arrange |
| 116 | + StreamCommandTypeStrategy sut = new StreamCommandTypeStrategy( |
| 117 | + () -> TypeStrategy.create( |
| 118 | + TypeFormatter.forTypeName(), |
| 119 | + TypeResolver.forClassName() |
| 120 | + ) |
| 121 | + ); |
| 122 | + |
| 123 | + // Act |
| 124 | + Type type = sut.resolveType("streamcommand:test.loom.CreateUser"); |
| 125 | + |
| 126 | + // Assert |
| 127 | + String json = mapper.writeValueAsString(command); |
| 128 | + JavaType javaType = TypeFactory.defaultInstance().constructType(type); |
| 129 | + Object value = mapper.readValue(json, javaType); |
| 130 | + assertThat(value).usingRecursiveComparison().isEqualTo(command); |
| 131 | + } |
| 132 | +} |
0 commit comments