Skip to content

Commit

Permalink
Improved saga instance repository test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dartartem committed Jul 27, 2021
1 parent 6374f63 commit 8e1fd3f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ private Mono<Void> saveDestinationsAndResources(SagaInstance sagaInstance) {
public Mono<SagaInstance> find(String sagaType, String sagaId) {
Flux<DestinationAndResource> destinationAndResources = eventuateJdbcStatementExecutor.queryForList(sagaInstanceRepositorySql.getSelectFromSagaInstanceParticipantsSql(),
(row, rowMetadata) -> new DestinationAndResource(row.get("destination").toString(), row.get("resource").toString()),
sagaId, sagaType);

sagaType, sagaId);

Mono<List<SagaInstance>> result = destinationAndResources.collectList().flatMap(dar ->
eventuateJdbcStatementExecutor.queryForList(sagaInstanceRepositorySql.getSelectFromSagaInstanceSql(),
Expand All @@ -96,10 +95,7 @@ public Mono<SagaInstance> find(String sagaType, String sagaId) {
new SerializedSagaData(row.get("saga_data_type").toString(), row.get("saga_data_json").toString()), new HashSet<>(dar)),
sagaType, sagaId).collectList());

return result.flatMap(sagaInstances -> {
if (sagaInstances.isEmpty()) return Mono.error(new RuntimeException(String.format("Cannot find saga instance %s %s", sagaType, sagaId)));
else return Mono.just(sagaInstances.get(0));
});
return result.flatMapMany(Flux::fromIterable).single();
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ dependencies {
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
testCompile "io.eventuate.common:eventuate-common-id:$eventuateCommonVersion"
}

test {
def profile = System.env['SPRING_PROFILES_ACTIVE']
if (profile != null && profile != "" && !profile.toLowerCase().contains("mysql")) {
exclude '**/**'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ReactiveSagaInstanceRepositoryJdbcIntegrationTest.Config.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@EnableAutoConfiguration
public class ReactiveSagaInstanceRepositoryJdbcIntegrationTest {

private String sagaType = UUID.randomUUID().toString();
private SagaInstance sagaInstance =
new SagaInstance(sagaType, null, "SomeState", "lastRequestId", new SerializedSagaData("sagaDatType", "{}"), Collections.emptySet());
private String sagaType = generateId();
private String sagaState = generateId();
private String lastRequestId = generateId();
private String sagaDataType = generateId();
private String destination = generateId();
private String resource = generateId();

@Configuration
@Import(EventuateCommonReactiveDatabaseConfiguration.class)
Expand All @@ -44,21 +49,64 @@ public ReactiveSagaInstanceRepository sagaInstanceRepository(EventuateReactiveJd
@Autowired
private ReactiveSagaInstanceRepository sagaInstanceRepository;

private SagaInstance sagaInstance;

@Test
public void shouldSaveLoadUpdate() {
assertNotNull(sagaInstance.getStateName());
sagaInstance = createSagaInstance();
sagaInstanceRepository.save(sagaInstance).block();
SagaInstance result = sagaInstanceRepository.find(sagaType, sagaInstance.getId()).block();
assertNotNull(result);
assertEquals(sagaInstance.getStateName(), result.getStateName());
sagaInstance = sagaInstanceRepository.find(sagaType, sagaInstance.getId()).block();
assertSavedSagaInstance();
updateSagaInstance();
assertUpdatedSagaInstance();
}

private void assertSavedSagaInstance() {
assertEquals(sagaState, sagaInstance.getStateName());
assertEquals(lastRequestId, sagaInstance.getLastRequestId());
assertEquals(sagaDataType, sagaInstance.getSerializedSagaData().getSagaDataType());
assertEquals("{}", sagaInstance.getSerializedSagaData().getSagaDataJSON());
Set<DestinationAndResource> destinationAndResources = sagaInstance.getDestinationsAndResources();
assertEquals(singleton(new DestinationAndResource(destination, resource)), destinationAndResources);
DestinationAndResource destinationAndResource = destinationAndResources.stream().findAny().get();
assertEquals(destination, destinationAndResource.getDestination());
assertEquals(resource, destinationAndResource.getResource());
}

private void updateSagaInstance() {
sagaInstance.setStateName("UpdatedState");
sagaInstance.setLastRequestId("UpdateLastId");
sagaInstance.setSerializedSagaData(new SerializedSagaData("UpdatedSagaType", "{\"value\" : \"updatedValue\"}"));
sagaInstance.getDestinationsAndResources().add(new DestinationAndResource("newDestination", "newResource"));
sagaInstanceRepository.update(sagaInstance).block();
result = sagaInstanceRepository.find(sagaType, sagaInstance.getId()).block();
assertEquals("UpdatedState", result.getStateName());
sagaInstance = sagaInstanceRepository.find(sagaType, sagaInstance.getId()).block();
}

private void assertUpdatedSagaInstance() {
assertEquals("UpdatedState", sagaInstance.getStateName());
assertEquals("UpdateLastId", sagaInstance.getLastRequestId());
assertEquals("UpdatedSagaType", sagaInstance.getSerializedSagaData().getSagaDataType());
assertEquals("{\"value\" : \"updatedValue\"}", sagaInstance.getSerializedSagaData().getSagaDataJSON());

Set<DestinationAndResource> destinationAndResources = sagaInstance.getDestinationsAndResources();
assertEquals(new HashSet<>(asList(new DestinationAndResource(destination, resource), new DestinationAndResource("newDestination", "newResource"))), destinationAndResources);
}

@Test(expected = RuntimeException.class)
public void shouldThrowExceptionWhenInstanceNotFound() {
sagaInstanceRepository.find(sagaType, "unknown").block();
}

private SagaInstance createSagaInstance() {
return new SagaInstance(sagaType,
null,
sagaState,
lastRequestId,
new SerializedSagaData(sagaDataType, "{}"),
singleton(new DestinationAndResource(destination, resource)));
}

private String generateId() {
return UUID.randomUUID().toString();
}
}

0 comments on commit 8e1fd3f

Please sign in to comment.