Commit 35451104 authored by Angelo Veltens's avatar Angelo Veltens

register ticket api test

parent 0e6e50c9
package de.codecentric.ticketscanner;
import org.springframework.stereotype.Component;
@Component
public class CodeGenerator {
public String generate() {
return "";
}
}
package de.codecentric.ticketscanner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RegisterTicketApiTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private CodeGenerator codeGenerator;
@Test
public void responds_with_code_when_registering_ticket() throws Exception {
given(codeGenerator.generate()).willReturn("123ABC");
String ticketId = "11111111-aaaa-bbbb-cccc-123456789012";
ResponseEntity<String> response = whenTicketIsRegistered(ticketId);
thenAValidCodeIsReturnedForTheTicket(response, "123ABC", ticketId);
}
private ResponseEntity<String> whenTicketIsRegistered(String ticketId) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
String ticket = String.join(
"/n",
"<ticket id=\""+ticketId+"\">",
" <event id=\"36937a9d-ee73-4f43-b2ee-c17c0d1eba30\">",
" <name>Hamlet</name>",
" </event>",
"</ticket>"
);
HttpEntity<String> entity = new HttpEntity<String>(ticket ,headers);
return this.restTemplate.postForEntity("http://localhost:" + port + "/codes", entity, String.class);
}
private void thenAValidCodeIsReturnedForTheTicket(ResponseEntity<String> response, String code, String ticketId) {
assertThat(response.getBody()).isXmlEqualTo(
String.join(
"/n",
"<code id=\""+code+"\">" +
" <ticket id=\""+ticketId+"\" />\n" +
"</code>"
)
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
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