PageRenderTime 125ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/br/com/builders/treinamento/tests/CustomerE2ETest.java

https://bitbucket.org/developermaster/orlando-burli-platformbuilders
Java | 138 lines | 101 code | 30 blank | 7 comment | 2 complexity | 88f42abf636ec96f3079d07f266e622d MD5 | raw file
  1. package br.com.builders.treinamento.tests;
  2. import static org.junit.Assert.assertEquals;
  3. import java.util.Locale;
  4. import org.junit.Before;
  5. import org.junit.Rule;
  6. import org.junit.Test;
  7. import org.junit.rules.ExpectedException;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.data.mongodb.core.MongoTemplate;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.test.context.junit4.SpringRunner;
  15. import org.springframework.web.client.HttpClientErrorException;
  16. import org.springframework.web.client.RestTemplate;
  17. import com.github.javafaker.Faker;
  18. import com.google.gson.Gson;
  19. import com.mongodb.BasicDBObject;
  20. import br.com.builders.treinamento.builders.CustomerBuilder;
  21. import br.com.builders.treinamento.dto.CustomerDto;
  22. import br.com.builders.treinamento.exception.ErrorMessage;
  23. @RunWith(SpringRunner.class)
  24. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  25. @EnableAutoConfiguration
  26. public class CustomerE2ETest {
  27. private static final String API_CUSTOMERS = "http://localhost:8080/api/customers";
  28. @Autowired
  29. private MongoTemplate mongoTemplate;
  30. private Faker facker;
  31. @Rule
  32. public final ExpectedException thrown = ExpectedException.none();
  33. @Test
  34. public void insertMustPass() throws Exception {
  35. final CustomerDto joseph = this.buildCustomerJoseph();
  36. final RestTemplate template = new RestTemplate();
  37. final ResponseEntity<String> response = template.postForEntity(API_CUSTOMERS, joseph, String.class);
  38. assertEquals(201, response.getStatusCodeValue());
  39. assertEquals(API_CUSTOMERS + "/" + joseph.getId(), response.getHeaders().get("location").get(0));
  40. }
  41. @Test
  42. public void insertMustFailNoName() {
  43. this.thrown.expect(HttpClientErrorException.class);
  44. final CustomerDto joseph = this.buildCustomerJoseph();
  45. joseph.setName(null);
  46. final RestTemplate template = new RestTemplate();
  47. final ResponseEntity<String> response = template.postForEntity(API_CUSTOMERS, joseph, String.class);
  48. System.out.println(response.getBody());
  49. assertEquals(400, response.getStatusCodeValue());
  50. final ErrorMessage errorMessage = new Gson().fromJson(response.getBody(), ErrorMessage.class);
  51. assertEquals("Customer name was not informed.", errorMessage.getMessage());
  52. assertEquals("400", errorMessage.getCode());
  53. }
  54. @Test
  55. public void getMustFailCustomerNotFound() throws Exception {
  56. this.thrown.expect(HttpClientErrorException.class);
  57. final String customerId = "XXX-123123-MASD4";
  58. final RestTemplate template = new RestTemplate();
  59. final ResponseEntity<String> response = template.getForEntity(API_CUSTOMERS + "/" + customerId, String.class);
  60. assertEquals(404, response.getStatusCodeValue());
  61. final ErrorMessage errorMessage = new Gson().fromJson(response.getBody(), ErrorMessage.class);
  62. assertEquals("Customer not found with the id " + customerId + ".", errorMessage.getMessage());
  63. }
  64. public CustomerDto buildCustomerJoseph() {
  65. // @formatter:off
  66. return CustomerBuilder.create()
  67. .withId("xxx-123-90xxs")
  68. .withName("Joseph Dalton")
  69. .withCrm("C12338")
  70. .withBaseUrl("http://www.terra.com.br")
  71. .withLogin("joseph@gmail.com")
  72. .build();
  73. // @formatter:on
  74. }
  75. public CustomerDto buildCustomerMirian() {
  76. // @formatter:off
  77. return CustomerBuilder.create()
  78. .withId("AAS-123i8-YU87")
  79. .withName("Mirian Clark")
  80. .withCrm("C7655")
  81. .withBaseUrl("http://www.mirian.com")
  82. .withLogin("mirian.clark@terra.com.br")
  83. .build();
  84. // @formatter:on
  85. }
  86. public CustomerDto buildRandomCustomer() {
  87. // @formatter:off
  88. final String fullName = this.facker.name().fullName();
  89. return CustomerBuilder.create()
  90. .withId(this.facker.idNumber().ssnValid())
  91. .withName(fullName)
  92. .withCrm(this.facker.idNumber().valid())
  93. .withBaseUrl("http://wwww."+ this.facker.university().name().replaceAll(" ", "").toLowerCase() + ".com")
  94. .withLogin(fullName.replaceAll(" ", ".").toLowerCase() + "@gmail.com")
  95. .build();
  96. // @formatter:on
  97. }
  98. @Before
  99. public void prepare() {
  100. this.facker = new Faker(Locale.ENGLISH);
  101. // Clear the data before each test.
  102. for (final String collectionName : this.mongoTemplate.getCollectionNames()) {
  103. if (!collectionName.startsWith("system.")) {
  104. this.mongoTemplate.getCollection(collectionName).remove(new BasicDBObject());
  105. }
  106. }
  107. }
  108. }