Commit 2bf2c57e authored by Angelo Veltens's avatar Angelo Veltens

refactor tests to mock TicketScanner

parent 69db231f
package de.codecentric.ticketscanner;
import lombok.EqualsAndHashCode;
import java.util.UUID;
@EqualsAndHashCode
public class Code {
private String id;
private final Ticket ticket;
public Code(String id, Ticket ticket) {
this.id = id;
this.ticket = ticket;
}
String getId() {
return id;
}
UUID getTicketId() {
return ticket.getId();
}
}
......@@ -12,4 +12,9 @@ class CodeResponse {
public final String id;
public TicketResponse ticket;
public CodeResponse(Code code) {
this.id = code.getId();
this.ticket = new TicketResponse(code.getTicketId());
}
}
......@@ -10,14 +10,16 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("codes")
public class CodesController {
private final CodeGenerator codeGenerator;
private final TicketScanner ticketScanner;
public CodesController(CodeGenerator codeGenerator) {
this.codeGenerator = codeGenerator;
public CodesController(TicketScanner ticketScanner) {
this.ticketScanner = ticketScanner;
}
@PostMapping(produces = "application/xml", consumes = MediaType.APPLICATION_XML_VALUE)
CodeResponse registerTicket(@RequestBody CodeRequest request) {
return new CodeResponse(codeGenerator.generate(), new TicketResponse(request.getTicketId()));
Ticket ticket = new Ticket(request.getTicketId());
Code code = ticketScanner.register(ticket);
return new CodeResponse(code);
}
}
package de.codecentric.ticketscanner;
import lombok.EqualsAndHashCode;
import java.util.UUID;
@EqualsAndHashCode
public class Ticket {
private final UUID id;
public Ticket(UUID id) {
this.id = id;
}
UUID getId() {
return id;
}
}
......@@ -3,8 +3,8 @@ package de.codecentric.ticketscanner;
import org.springframework.stereotype.Component;
@Component
public class CodeGenerator {
public String generate() {
return "";
public class TicketScanner {
public Code register(Ticket ticket) {
return null;
}
}
......@@ -14,15 +14,19 @@ import static org.mockito.BDDMockito.given;
public class CodesControllerTest {
@Mock
CodeGenerator codeGenerator;
TicketScanner ticketScanner;
@Test
public void responds_with_a_valid_code() {
UUID ticketId = UUID.randomUUID();
CodeRequest request = new CodeRequest();
request.setTicketId(ticketId);
given(codeGenerator.generate()).willReturn("ABC123");
CodeResponse response = new CodesController(codeGenerator).registerTicket(request);
Ticket ticket = new Ticket(ticketId);
given(ticketScanner.register(ticket)).willReturn(new Code("ABC123", ticket));
CodeResponse response = new CodesController(ticketScanner).registerTicket(request);
assertThat(response).isNotNull();
assertThat(response.id).isEqualTo("ABC123");
assertThat(response.ticket).isNotNull();
......
......@@ -10,7 +10,10 @@ import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@RunWith(SpringRunner.class)
......@@ -24,14 +27,18 @@ public class RegisterTicketApiTest {
private TestRestTemplate restTemplate;
@MockBean
private CodeGenerator codeGenerator;
private TicketScanner ticketScanner;
@Test
public void responds_with_code_when_registering_ticket() throws Exception {
given(codeGenerator.generate()).willReturn("123ABC");
String ticketId = "11111111-aaaa-bbbb-cccc-123456789012";
givenCodeForTicket("123ABC", ticketId);
ResponseEntity<String> response = whenTicketIsRegistered(ticketId);
thenAValidCodeIsReturnedForTheTicket(response, "123ABC", ticketId);
thenTheCodeIsReturnedForTheTicket(response, "123ABC", ticketId);
}
private void givenCodeForTicket(String code, String ticketId) {
given(ticketScanner.register(any())).willReturn(new Code(code, new Ticket(UUID.fromString(ticketId))));
}
private ResponseEntity<String> whenTicketIsRegistered(String ticketId) {
......@@ -49,7 +56,7 @@ public class RegisterTicketApiTest {
return this.restTemplate.postForEntity("http://localhost:" + port + "/codes", entity, String.class);
}
private void thenAValidCodeIsReturnedForTheTicket(ResponseEntity<String> response, String code, String ticketId) {
private void thenTheCodeIsReturnedForTheTicket(ResponseEntity<String> response, String code, String ticketId) {
assertThat(response.getBody()).isXmlEqualTo(
String.join(
"/n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment