Skip to content

Commit

Permalink
Add test converage to state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgodoy committed Nov 4, 2024
1 parent 9a1fa5c commit dc3200d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public class Request {
enum Status {
PENDING,
STARTED,
COMPLETED,
INERROR
COMPLETED
}

@Id
Expand Down
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();
}
}
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());
}


}

0 comments on commit dc3200d

Please sign in to comment.