Search in sources :

Example 1 with Customer

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Customer(com.poc.restfulpoc.entities.Customer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with Customer

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);
}
Also used : Customer(com.poc.restfulpoc.entities.Customer) Date(java.util.Date) LocalDate(java.time.LocalDate) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with Customer

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);
}
Also used : Customer(com.poc.restfulpoc.entities.Customer) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with Customer

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");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Customer(com.poc.restfulpoc.entities.Customer) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) Test(org.junit.jupiter.api.Test)

Example 5 with Customer

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");
    }
}
Also used : Customer(com.poc.restfulpoc.entities.Customer) Date(java.util.Date)

Aggregations

Customer (com.poc.restfulpoc.entities.Customer)10 AbstractRestFulPOCApplicationTest (com.poc.restfulpoc.AbstractRestFulPOCApplicationTest)6 Test (org.junit.jupiter.api.Test)6 DisplayName (org.junit.jupiter.api.DisplayName)3 Date (java.util.Date)2 ResponseEntity (org.springframework.http.ResponseEntity)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LocalDate (java.time.LocalDate)1 ZoneId (java.time.ZoneId)1 Response (javax.ws.rs.core.Response)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1