use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.
the class CustomerController method getCustomer.
/**
* Get customer using id. Returns HTTP 404 if customer not found
*
* @param customerId a {@link java.lang.Long} object.
* @return retrieved customer
* @throws com.poc.restfulpoc.exception.EntityNotFoundException if any.
*/
@GetMapping(value = "/customers/{customerId}", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public ResponseEntity<Customer> getCustomer(@PathVariable("customerId") @NotBlank Long customerId) throws EntityNotFoundException {
log.info("Fetching Customer with id {}", customerId);
final Customer user = customerService.getCustomer(customerId);
return new ResponseEntity<>(user, HttpStatus.OK);
}
use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.
the class CustomerControllerITTest method testInValidCustomer.
@Test
@DisplayName("Tests InValid Customer")
public void testInValidCustomer() {
Customer newCustomer = Customer.builder().firstName(" ").lastName("Steale").dateOfBirth(new Date(new Date().getTime() + TimeUnit.DAYS.toMillis(100))).build();
ResponseEntity<Customer> response = template.postForEntity(base, newCustomer, Customer.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
newCustomer = Customer.builder().firstName(null).lastName("Steale").build();
response = template.postForEntity(base, newCustomer, Customer.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
}
use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.
the class CustomerControllerITTest method testCreateCustomer.
@Test
@DisplayName("Creating Customer")
public void testCreateCustomer() throws Exception {
// @formatter:off
final Customer customer = Customer.builder().firstName("Gary").lastName("Steale").dateOfBirth(Date.from(LocalDate.of(1984, Month.MARCH, 8).atStartOfDay(ZoneId.of("UTC")).toInstant())).address(Address.builder().street("Main Street").town("Portadown").county("Armagh").postcode("BT359JK").build()).build();
// @formatter:on
ResponseEntity<Customer> response = template.postForEntity(base, customer, Customer.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getHeaders().getContentLength()).isEqualTo(0);
String location = response.getHeaders().getFirst("Location");
assertThat(location).contains(base);
response = template.getForEntity(location, Customer.class);
Customer returnedCustomer = response.getBody();
assertThat(customer.getFirstName()).isEqualTo(returnedCustomer.getFirstName());
assertThat(customer.getLastName()).isEqualTo(returnedCustomer.getLastName());
assertThat(customer.getDateOfBirth()).isEqualTo(returnedCustomer.getDateOfBirth());
assertThat(customer.getAddress().getStreet()).isEqualTo(returnedCustomer.getAddress().getStreet());
assertThat(customer.getAddress().getTown()).isEqualTo(returnedCustomer.getAddress().getTown());
assertThat(customer.getAddress().getCounty()).isEqualTo(returnedCustomer.getAddress().getCounty());
assertThat(customer.getAddress().getPostcode()).isEqualTo(returnedCustomer.getAddress().getPostcode());
Customer newCustomer = Customer.builder().firstName("Andy").lastName("Steale").address(Address.builder().street("Main Street").town("Portadown").county("Armagh").postcode("BT359JK").build()).build();
response = template.postForEntity(base, newCustomer, Customer.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getHeaders().getContentLength()).isEqualTo(0);
location = response.getHeaders().getFirst("Location");
assertThat(location).contains(base);
response = template.getForEntity(location, Customer.class);
returnedCustomer = response.getBody();
assertThat(returnedCustomer.getDateOfBirth()).isNull();
newCustomer = Customer.builder().firstName("Gary").lastName("Steale").address(Address.builder().street("Main Street").town("Portadown").county("Armagh").postcode("BT359JK").build()).build();
response = template.postForEntity(base, newCustomer, Customer.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
}
use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.
the class CustomerControllerITTest method testUpdateCustomer.
@Test
public void testUpdateCustomer() throws Exception {
final Long customerId = getCustomerIdByFirstName("Raja");
final ResponseEntity<Customer> getCustomerResponse = template.getForEntity(String.format("%s/%s", base, customerId), Customer.class);
assertThat(getCustomerResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(getCustomerResponse.getHeaders().getContentType().toString()).isEqualTo(JSON_CONTENT_TYPE);
final Customer persistedCustomer = getCustomerResponse.getBody();
assertThat(persistedCustomer.getFirstName()).isEqualTo("Raja");
assertThat(persistedCustomer.getLastName()).isEqualTo("Kolli");
assertThat(persistedCustomer.getDateOfBirth()).hasDayOfMonth(10).hasMonth(1).hasYear(1982);
assertThat(persistedCustomer.getAddress().getStreet()).isEqualTo("High Street");
assertThat(persistedCustomer.getAddress().getTown()).isEqualTo("Belfast");
assertThat(persistedCustomer.getAddress().getCounty()).isEqualTo("India");
assertThat(persistedCustomer.getAddress().getPostcode()).isEqualTo("BT893PY");
persistedCustomer.setFirstName("Wayne");
persistedCustomer.setLastName("Rooney");
/* PUT updated customer */
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<Customer> entity = new HttpEntity<Customer>(persistedCustomer, headers);
final ResponseEntity<Customer> response = template.exchange(String.format("%s/%s", base, customerId), HttpMethod.PUT, entity, Customer.class, customerId);
assertThat(response.getBody()).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
final Customer updatedCustomer = response.getBody();
assertThat(updatedCustomer.getFirstName()).isEqualTo("Wayne");
assertThat(updatedCustomer.getLastName()).isEqualTo("Rooney");
assertThat(updatedCustomer.getDateOfBirth()).hasDayOfMonth(10).hasMonth(1).hasYear(1982);
assertThat(updatedCustomer.getAddress().getStreet()).isEqualTo("High Street");
assertThat(updatedCustomer.getAddress().getTown()).isEqualTo("Belfast");
assertThat(updatedCustomer.getAddress().getCounty()).isEqualTo("India");
assertThat(updatedCustomer.getAddress().getPostcode()).isEqualTo("BT893PY");
}
use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.
the class CustomerValidator method validate.
/**
* {@inheritDoc}
*/
@Override
public void validate(@NonNull Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "FirstName is Mandatory");
final Customer customer = (Customer) target;
final Date dob = customer.getDateOfBirth();
if (Objects.nonNull(dob) && dob.after(new Date())) {
errors.rejectValue("dateOfBirth", ERROR_CODE, "Date Of Birth Should be before today");
errors.reject(ERROR_CODE, "Entity Not Processable");
}
}
Aggregations