Commit 1e0e9e4d authored by Angelo Veltens's avatar Angelo Veltens

implement code generator with static strategy

parent ef9a18a9
package de.codecentric.ticketscanner;
public interface CodeGenerationStrategy {
String next();
}
package de.codecentric.ticketscanner;
import org.hibernate.cfg.NotYetImplementedException;
import org.springframework.stereotype.Component;
@Component
public class CodeGenerator {
private final CodeGenerationStrategy strategy;
public CodeGenerator(CodeGenerationStrategy strategy) {
this.strategy = strategy;
}
public Code generateFor(Ticket ticket) {
throw new NotYetImplementedException();
return new Code(strategy.next(), ticket);
}
}
package de.codecentric.ticketscanner;
import org.hibernate.cfg.NotYetImplementedException;
import org.springframework.stereotype.Component;
@Component
public class UniqueCodeGenerationStrategy implements CodeGenerationStrategy{
@Override
public String next() {
throw new NotYetImplementedException();
}
}
package de.codecentric.ticketscanner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(MockitoExtension.class)
@DisplayName("a code generator")
class CodeGeneratorSpec {
private CodeGenerator codeGenerator;
@BeforeEach
void setUp() {
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");
}
}
package de.codecentric.ticketscanner;
public class StaticCodeGenerationStrategy implements CodeGenerationStrategy {
private final String staticValue;
StaticCodeGenerationStrategy(String staticValue) {
this.staticValue = staticValue;
}
@Override
public String next() {
return staticValue;
}
}
......@@ -14,7 +14,7 @@ public class TicketScannerFeature {
@Test
public void successful_check_in_with_valid_ticket() {
// given
CodeGenerator codeGenerator = new CodeGenerator();
CodeGenerator codeGenerator = new CodeGenerator(new StaticCodeGenerationStrategy("WHATEVER"));
TicketScanner ticketScanner = new TicketScanner(codeGenerator);
Ticket registeredTicket = new Ticket(UUID.randomUUID());
// when
......
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