diff --git a/idempotent-consumer/src/main/java/com/iluwatar/idempotentconsumer/Request.java b/idempotent-consumer/src/main/java/com/iluwatar/idempotentconsumer/Request.java index 484b79550823..29f5d6fba375 100644 --- a/idempotent-consumer/src/main/java/com/iluwatar/idempotentconsumer/Request.java +++ b/idempotent-consumer/src/main/java/com/iluwatar/idempotentconsumer/Request.java @@ -41,8 +41,7 @@ public class Request { enum Status { PENDING, STARTED, - COMPLETED, - INERROR + COMPLETED } @Id diff --git a/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/AppTest.java b/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/AppTest.java new file mode 100644 index 000000000000..e161b2edc30d --- /dev/null +++ b/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/AppTest.java @@ -0,0 +1,46 @@ +package com.iluwatar.idempotentconsumer; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.CommandLineRunner; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Application test + */ +class AppTest { + + @Test + void main() { + assertDoesNotThrow(() -> App.main(new String[] {})); + } + + @Test + void run() throws Exception { + RequestService requestService = Mockito.mock(RequestService.class); + RequestRepository requestRepository = Mockito.mock(RequestRepository.class); + UUID uuid = UUID.randomUUID(); + Request requestPending = new Request(uuid); + Request requestStarted = new Request(uuid, Request.Status.STARTED); + Request requestCompleted = new Request(uuid, Request.Status.COMPLETED); + when(requestService.create(any())).thenReturn(requestPending); + when(requestService.start(any())).thenReturn(requestStarted); + when(requestService.complete(any())).thenReturn(requestCompleted); + + CommandLineRunner runner = new App().run(requestService, requestRepository); + + runner.run(); + + verify(requestService, times(3)).create(any()); + verify(requestService, times(2)).start(any()); + verify(requestService, times(1)).complete(any()); + verify(requestRepository, times(1)).count(); + } +} \ No newline at end of file diff --git a/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/RequestStateMachineTests.java b/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/RequestStateMachineTests.java new file mode 100644 index 000000000000..af779648c8a3 --- /dev/null +++ b/idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/RequestStateMachineTests.java @@ -0,0 +1,59 @@ +package com.iluwatar.idempotentconsumer; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class RequestStateMachineTests { + private RequestStateMachine requestStateMachine; + + @BeforeEach + public void setUp() { + requestStateMachine = new RequestStateMachine(); + } + + @Test + void transitionPendingToStarted() { + Request startedRequest = requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.PENDING), + Request.Status.STARTED); + assertEquals(Request.Status.STARTED, startedRequest.getStatus()); + } + + @Test + void transitionAnyToPending_shouldThrowError() { + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.PENDING), + Request.Status.PENDING)); + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.STARTED), + Request.Status.PENDING)); + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.COMPLETED), + Request.Status.PENDING)); + } + + @Test + void transitionCompletedToAny_shouldThrowError() { + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.COMPLETED), + Request.Status.PENDING)); + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.COMPLETED), + Request.Status.STARTED)); + assertThrows(InvalidNextStateException.class, + () -> requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.COMPLETED), + Request.Status.COMPLETED)); + } + + @Test + void transitionStartedToCompleted() { + Request completedRequest = requestStateMachine.next(new Request(UUID.randomUUID(), Request.Status.STARTED), + Request.Status.COMPLETED); + assertEquals(Request.Status.COMPLETED, completedRequest.getStatus()); + } + + +} \ No newline at end of file