Commit 8c33702e authored by Angelo Veltens's avatar Angelo Veltens

generate unique code using random string

parent cd940756
POST http://localhost:8080/codes
Content-Type: application/xml
<ticket id="e7358ae2-d3db-4069-9602-b9e0055bb8f3">
<event id="36937a9d-ee73-4f43-b2ee-c17c0d1eba30">
<name>Hamlet</name>
</event>
<validity>
<from>2019-10-02 06:00</from>
<to>2019-10-03 10:00</to>
</validity>
</ticket>
###
package de.codecentric.ticketscanner;
import org.hibernate.cfg.NotYetImplementedException;
import org.springframework.stereotype.Component;
@Component
public class RandomStringGenerator {
public String generate() {
throw new NotYetImplementedException();
}
}
package de.codecentric.ticketscanner;
import de.codecentric.ticketscanner.persistence.TicketData;
import de.codecentric.ticketscanner.persistence.TicketRepository;
import org.hibernate.cfg.NotYetImplementedException;
import org.springframework.stereotype.Component;
@Component
public class UniqueCodeGenerationStrategy implements CodeGenerationStrategy{
private final TicketRepository repository;
private final RandomStringGenerator randomStringGenerator;
public UniqueCodeGenerationStrategy(TicketRepository repository) {
public UniqueCodeGenerationStrategy(TicketRepository repository, RandomStringGenerator randomStringGenerator) {
this.repository = repository;
this.randomStringGenerator = randomStringGenerator;
}
@Override
public String next() {
throw new NotYetImplementedException();
int maxTries = 3;
for (int i = 1; i<= maxTries; i++) {
String random = randomStringGenerator.generate();
TicketData data = repository.findByCode(random);
if (data == null) {
return random;
}
}
throw new RuntimeException("Failed to find a random code that is not in use");
}
}
......@@ -2,11 +2,8 @@ package de.codecentric.ticketscanner;
import de.codecentric.ticketscanner.persistence.TicketRepository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.UUID;
......@@ -21,8 +18,9 @@ class TicketScannerFeature {
@Test
void successful_check_in_with_valid_ticket() {
// given
RandomStringGenerator randomStringGenerator = new RandomStringGenerator();
RegisteredTickets registeredTickets = new RegisteredTickets(repository);
CodeGenerator codeGenerator = new CodeGenerator(new UniqueCodeGenerationStrategy(repository));
CodeGenerator codeGenerator = new CodeGenerator(new UniqueCodeGenerationStrategy(repository, randomStringGenerator));
TicketScanner ticketScanner = new TicketScanner(codeGenerator, registeredTickets);
Ticket registeredTicket = new Ticket(UUID.randomUUID());
// when
......
package de.codecentric.ticketscanner;
import de.codecentric.ticketscanner.persistence.TicketData;
import de.codecentric.ticketscanner.persistence.TicketRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@ExtendWith(MockitoExtension.class)
@DisplayName("unique code generation strategy")
class UniqueCodeGenerationStrategyTest {
@Mock
TicketRepository repository;
@Mock
RandomStringGenerator randomStringGenerator;
@Test
void generates_unique_random_string() {
given(randomStringGenerator.generate()).willReturn("RANDOM");
String code = new UniqueCodeGenerationStrategy(repository, randomStringGenerator).next();
assertThat(code).isEqualTo("RANDOM");
}
@Test
void regenerates_random_string_if_already_in_use_as_code() {
given(randomStringGenerator.generate()).willReturn("RANDOM1", "RANDOM2");
given(repository.findByCode("RANDOM1")).willReturn(new TicketData("RANDOM1", UUID.randomUUID()));
String code = new UniqueCodeGenerationStrategy(repository, randomStringGenerator).next();
assertThat(code).isEqualTo("RANDOM2");
}
@Test
void throws_exception_after_3_failed_tries() {
given(randomStringGenerator.generate()).willReturn("RANDOM1", "RANDOM2", "RANDOM3", "RANDOM4");
given(repository.findByCode(any())).willReturn(new TicketData("RANDOM", UUID.randomUUID()));
assertThatThrownBy(() -> new UniqueCodeGenerationStrategy(repository, randomStringGenerator).next()).hasMessage("Failed to find a random code that is not in use");
}
}
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