Commit 69db231f authored by Angelo Veltens's avatar Angelo Veltens

implement CodesController

parent 35451104
package de.codecentric.ticketscanner;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
@JsonRootName("ticket")
public class CodeRequest {
@JacksonXmlProperty(localName = "id", isAttribute = true)
private UUID ticketId;
}
package de.codecentric.ticketscanner;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Value;
@Value
@JsonRootName("code")
class CodeResponse {
@JacksonXmlProperty(isAttribute = true)
public final String id;
public TicketResponse ticket;
}
package de.codecentric.ticketscanner;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("codes")
public class CodesController {
private final CodeGenerator codeGenerator;
public CodesController(CodeGenerator codeGenerator) {
this.codeGenerator = codeGenerator;
}
@PostMapping(produces = "application/xml", consumes = MediaType.APPLICATION_XML_VALUE)
CodeResponse registerTicket(@RequestBody CodeRequest request) {
return new CodeResponse(codeGenerator.generate(), new TicketResponse(request.getTicketId()));
}
}
package de.codecentric.ticketscanner;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Value;
import java.util.UUID;
@Value
@JsonRootName("ticket")
class TicketResponse {
@JacksonXmlProperty(isAttribute = true)
public UUID id;
}
package de.codecentric.ticketscanner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.UUID;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class CodesControllerTest {
@Mock
CodeGenerator codeGenerator;
@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);
assertThat(response).isNotNull();
assertThat(response.id).isEqualTo("ABC123");
assertThat(response.ticket).isNotNull();
assertThat(response.ticket.id).isEqualTo(ticketId);
}
}
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