Search in sources :

Example 6 with Customer

use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.

the class CustomerController method updateCustomer.

/**
 * Update customer with given customer id.
 *
 * @param customer the customer
 * @param customerId a {@link java.lang.Long} object.
 * @return a {@link org.springframework.http.ResponseEntity} object.
 * @throws com.poc.restfulpoc.exception.EntityNotFoundException if any.
 */
@PutMapping(value = { "/customers/{customerId}" })
public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer, @PathVariable("customerId") Long customerId) throws EntityNotFoundException {
    log.info("Updating Customer {}", customerId);
    final Customer currentUser = customerService.getCustomer(customerId);
    currentUser.setFirstName(customer.getFirstName());
    currentUser.setLastName(customer.getLastName());
    currentUser.setDateOfBirth(customer.getDateOfBirth());
    currentUser.setAddress(customer.getAddress());
    customerService.updateCustomer(currentUser);
    return new ResponseEntity<>(currentUser, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Customer(com.poc.restfulpoc.entities.Customer) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 7 with Customer

use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.

the class CustomerControllerITTest method testGetCustomerById.

@Test
public void testGetCustomerById() throws Exception {
    final Long customerId = getCustomerIdByFirstName("Raja");
    final ResponseEntity<Customer> response = template.getForEntity(String.format("%s%s", base, customerId), Customer.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(response.getHeaders().getContentType().toString()).isEqualTo(JSON_CONTENT_TYPE);
    final Customer customer = response.getBody();
    assertThat(customer.getFirstName()).isEqualTo("Raja");
    assertThat(customer.getLastName()).isEqualTo("Kolli");
    assertThat(customer.getDateOfBirth()).hasDayOfMonth(10).hasMonth(1).hasYear(1982);
    assertThat(customer.getAddress().getStreet()).isEqualTo("High Street");
    assertThat(customer.getAddress().getTown()).isEqualTo("Belfast");
    assertThat(customer.getAddress().getCounty()).isEqualTo("India");
    assertThat(customer.getAddress().getPostcode()).isEqualTo("BT893PY");
}
Also used : Customer(com.poc.restfulpoc.entities.Customer) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) Test(org.junit.jupiter.api.Test)

Example 8 with Customer

use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.

the class CustomerControllerITTest method testRemoveCustomer.

@Test
public void testRemoveCustomer() throws Exception {
    final Long customerId = getCustomerIdByFirstName("Raja");
    final ResponseEntity<Customer> response = template.getForEntity(String.format("%s/%s", base, customerId), Customer.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(response.getHeaders().getContentType().toString()).isEqualTo(JSON_CONTENT_TYPE);
    final Customer customer = response.getBody();
    assertThat(customer.getFirstName()).isEqualTo("Raja");
    assertThat(customer.getLastName()).isEqualTo("Kolli");
    assertThat(customer.getDateOfBirth()).hasDayOfMonth(10).hasMonth(1).hasYear(1982);
    assertThat(customer.getAddress().getStreet()).isEqualTo("High Street");
    assertThat(customer.getAddress().getTown()).isEqualTo("Belfast");
    assertThat(customer.getAddress().getCounty()).isEqualTo("India");
    assertThat(customer.getAddress().getPostcode()).isEqualTo("BT893PY");
    /* delete customer */
    template.delete(String.format("%s/%s", base, customerId), Customer.class);
    // Sleeping for 1 second so that JMS message is consumed
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    /* attempt to get customer and ensure we get a 404 */
    final ResponseEntity<Customer> secondCallResponse = template.getForEntity(String.format("%s/%s", base, customerId), Customer.class);
    assertThat(secondCallResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Also used : Customer(com.poc.restfulpoc.entities.Customer) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) Test(org.junit.jupiter.api.Test)

Example 9 with Customer

use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.

the class CXFRSServiceImplTest method testGetCustomers.

@Test
@DisplayName("Test Customers")
void testGetCustomers() throws Exception {
    final WebClient wc = WebClient.create("http://localhost:" + port + API_PATH);
    wc.accept("application/json");
    wc.path("/customers/");
    Response response = wc.get(Response.class);
    assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    final List<Customer> custList = convertJsonToCustomers(replyString);
    assertThat(custList).isNotEmpty().size().isGreaterThan(1);
    // Reverse to the starting URI
    wc.back(true);
    wc.path("/customers/").path(custList.get(0).getId());
    response = wc.get(Response.class);
    assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
    replyString = response.readEntity(String.class);
    final ObjectMapper mapper = new ObjectMapper();
    final Customer cust = mapper.readValue(replyString, Customer.class);
    assertThat(replyString).isNotNull();
    assertThat(cust.getId()).isEqualTo(custList.get(0).getId());
}
Also used : Response(javax.ws.rs.core.Response) Customer(com.poc.restfulpoc.entities.Customer) WebClient(org.apache.cxf.jaxrs.client.WebClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) AbstractRestFulPOCApplicationTest(com.poc.restfulpoc.AbstractRestFulPOCApplicationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 10 with Customer

use of com.poc.restfulpoc.entities.Customer in project POC by rajadilipkolli.

the class DataBuilder method run.

public void run() throws Exception {
    log.debug("Loading test data...");
    final ZoneId defaultZoneId = ZoneId.of("UTC");
    // @formatter:off
    final Customer customer1 = Customer.builder().firstName("Raja").lastName("Kolli").dateOfBirth(Date.from(LocalDate.of(1982, Month.JANUARY, 10).atStartOfDay(defaultZoneId).toInstant())).address(Address.builder().county("India").postcode("BT893PY").street("High Street").town("Belfast").build()).build();
    final Customer customer2 = Customer.builder().firstName("Paul").lastName("Jones").dateOfBirth(Date.from(LocalDate.of(1973, Month.JANUARY, 03).atStartOfDay(defaultZoneId).toInstant())).address(Address.builder().street("Main Street").town("Lurgan").county("Armagh").postcode("BT283FG").build()).build();
    final Customer customer3 = Customer.builder().firstName("Steve").lastName("Toale").dateOfBirth(Date.from(LocalDate.of(1979, Month.MARCH, 8).atStartOfDay(defaultZoneId).toInstant())).address(Address.builder().street("Main Street").town("Newry").county("Down").postcode("BT359JK").build()).build();
    // @formatter:on
    customerRepository.saveAll(Stream.of(customer1, customer2, customer3).collect(Collectors.toList()));
    log.debug("Test data loaded...");
}
Also used : ZoneId(java.time.ZoneId) Customer(com.poc.restfulpoc.entities.Customer)

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