-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,7 @@ public class Request { | |
enum Status { | ||
PENDING, | ||
STARTED, | ||
COMPLETED, | ||
INERROR | ||
COMPLETED | ||
} | ||
|
||
@Id | ||
|
46 changes: 46 additions & 0 deletions
46
idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...tent-consumer/src/test/java/com/iluwatar/idempotentconsumer/RequestStateMachineTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
|
||
} |