Commit a3cf9c44 authored by Angelo Veltens's avatar Angelo Veltens

refactor code to be a plain value type

parent 1e0e9e4d
package de.codecentric.ticketscanner;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;
import java.util.UUID;
@EqualsAndHashCode
@ToString
@Value
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();
}
private final String value;
}
......@@ -10,7 +10,7 @@ public class CodeGenerator {
this.strategy = strategy;
}
public Code generateFor(Ticket ticket) {
return new Code(strategy.next(), ticket);
public Code generate() {
return new Code(strategy.next());
}
}
......@@ -13,8 +13,8 @@ class CodeResponse {
public TicketResponse ticket;
public CodeResponse(Code code) {
this.id = code.getId();
this.ticket = new TicketResponse(code.getTicketId());
public CodeResponse(Code code, Ticket ticket) {
this.id = code.getValue();
this.ticket = new TicketResponse(ticket.getId());
}
}
......@@ -20,6 +20,6 @@ public class CodesController {
CodeResponse registerTicket(@RequestBody CodeRequest request) {
Ticket ticket = new Ticket(request.getTicketId());
Code code = ticketScanner.register(ticket);
return new CodeResponse(code);
return new CodeResponse(code, ticket);
}
}
......@@ -12,7 +12,7 @@ public class TicketScanner {
}
public Code register(Ticket ticket) {
return codeGenerator.generateFor(ticket);
return codeGenerator.generate();
}
public Ticket scan(Code code) {
......
......@@ -21,17 +21,10 @@ class CodeGeneratorSpec {
codeGenerator = new CodeGenerator(new StaticCodeGenerationStrategy("XYZ123"));
}
@Test
void generates_a_new_code_for_a_ticket() {
UUID ticketId = UUID.randomUUID();
Code code = codeGenerator.generateFor(new Ticket(ticketId));
assertThat(code.getTicketId()).isEqualTo(ticketId);
}
@Test
void generates_a_new_code_with_a_alphanumeric_id() {
UUID ticketId = UUID.randomUUID();
Code code = codeGenerator.generateFor(new Ticket(ticketId));
assertThat(code.getId()).isEqualTo("XYZ123");
Code code = codeGenerator.generate();
assertThat(code).isEqualTo(new Code("XYZ123"));
}
}
......@@ -23,7 +23,7 @@ public class CodesControllerTest {
request.setTicketId(ticketId);
Ticket ticket = new Ticket(ticketId);
given(ticketScanner.register(ticket)).willReturn(new Code("ABC123", ticket));
given(ticketScanner.register(ticket)).willReturn(new Code("ABC123"));
CodeResponse response = new CodesController(ticketScanner).registerTicket(request);
......
......@@ -32,13 +32,13 @@ public class RegisterTicketApiTest {
@Test
public void responds_with_code_when_registering_ticket() throws Exception {
String ticketId = "11111111-aaaa-bbbb-cccc-123456789012";
givenCodeForTicket("123ABC", ticketId);
givenCodeForTicket("123ABC");
ResponseEntity<String> response = whenTicketIsRegistered(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 void givenCodeForTicket(String code) {
given(ticketScanner.register(any())).willReturn(new Code(code));
}
private ResponseEntity<String> whenTicketIsRegistered(String ticketId) {
......
......@@ -39,8 +39,8 @@ class TicketScannerSpec {
@BeforeEach
void setUp() {
Ticket registeredTicket = new Ticket(UUID.randomUUID());
generatedCode = new Code(CODE, registeredTicket);
given(codeGenerator.generateFor(registeredTicket)).willReturn(generatedCode);
generatedCode = new Code(CODE);
given(codeGenerator.generate()).willReturn(generatedCode);
ticketCode = ticketScanner.register(registeredTicket);
}
......
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