Skip to content

Commit

Permalink
feat: while fetching response, fetch is as CustomerResponse
Browse files Browse the repository at this point in the history
Changes id to customerId
  • Loading branch information
rajadilipkolli committed Oct 15, 2023
1 parent 11a727b commit b86ee1f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"vscode": {
"extensions": [
"vscjava.vscode-java-pack",
"vmware.vscode-boot-dev-pack"
"vmware.vscode-boot-dev-pack",
"ms-vscode-remote.remote-containers"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
package com.example.paymentservice.model.response;

public record CustomerResponse(
Long id, String name, String email, String address, int amountAvailable) {}
Long customerId, String name, String email, String address, int amountAvailable) {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.example.paymentservice.jooq.tables.records.CustomersRecord;
import com.example.paymentservice.model.response.CustomerResponse;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.jooq.DSLContext;
import org.jooq.InsertSetMoreStep;
Expand Down Expand Up @@ -114,8 +117,8 @@ public void deleteById(Long id) {
dslContext.deleteFrom(CUSTOMERS).where(CUSTOMERS.ID.eq(id)).execute();
}

private Collection<SortField<?>> getSortFields(Sort sortSpecification) {
Collection<SortField<?>> querySortFields = new ArrayList<>();
private List<SortField<?>> getSortFields(Sort sortSpecification) {
List<SortField<?>> querySortFields = new ArrayList<>();

if (sortSpecification == null) {
return querySortFields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ private Pageable createPageable(FindCustomersQuery findCustomersQuery) {
}

@Transactional(readOnly = true)
public Optional<Customer> findCustomerById(Long id) {
return customerRepository.findById(id);
public Optional<CustomerResponse> findCustomerById(Long id) {
return customerRepository.findById(id).map(customerMapper::toResponse);
}

@Transactional(readOnly = true)
Expand All @@ -70,10 +70,6 @@ public CustomerResponse saveCustomer(CustomerRequest customerRequest) {
return customerMapper.toResponse(customerRepository.save(customer));
}

public void deleteCustomerById(Long id) {
customerRepository.deleteById(id);
}

public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest) {
Customer customer =
customerRepository
Expand All @@ -89,4 +85,8 @@ public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest)
// Map the updated customer to a response object and return it
return customerMapper.toResponse(updatedCustomer);
}

public void deleteCustomerById(Long id) {
customerRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PagedResult<Customer> getAllCustomers(
}

@GetMapping("/{id}")
public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
public ResponseEntity<CustomerResponse> getCustomerById(@PathVariable Long id) {
return customerService
.findCustomerById(id)
.map(ResponseEntity::ok)
Expand All @@ -83,7 +83,7 @@ public ResponseEntity<CustomerResponse> createCustomer(
URI location =
ServletUriComponentsBuilder.fromCurrentRequest()
.path("/api/customers/{id}")
.buildAndExpand(response.id())
.buildAndExpand(response.customerId())
.toUri();
return ResponseEntity.created(location).body(response);
}
Expand All @@ -95,7 +95,7 @@ public ResponseEntity<CustomerResponse> updateCustomer(
}

@DeleteMapping("/{id}")
public ResponseEntity<Customer> deleteCustomer(@PathVariable Long id) {
public ResponseEntity<CustomerResponse> deleteCustomer(@PathVariable Long id) {
return customerService
.findCustomerById(id)
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(
webEnvironment = RANDOM_PORT,
properties = {"spring.cloud.config.enabled=false"},
properties = {"spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=false"},
classes = TestPaymentApplication.class)
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void shouldFindCustomerById() throws Exception {
this.mockMvc
.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.customerId", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
Expand Down Expand Up @@ -113,7 +113,7 @@ void shouldFindCustomerByName() throws Exception {
this.mockMvc
.perform(get("/api/customers/name/{name}", customerName))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.customerId", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
Expand All @@ -132,7 +132,7 @@ void shouldCreateNewCustomer() throws Exception {
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isCreated())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(jsonPath("$.id", notNullValue(Long.class)))
.andExpect(jsonPath("$.customerId", notNullValue(Long.class)))
.andExpect(jsonPath("$.name", is(customerRequest.name())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.address", is(customerRequest.address())))
Expand All @@ -141,7 +141,7 @@ void shouldCreateNewCustomer() throws Exception {

@Test
void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception {
Customer customer = new Customer(null, null, null, null, 0, 0);
CustomerRequest customer = new CustomerRequest(null, null, null, 0);

this.mockMvc
.perform(
Expand Down Expand Up @@ -176,6 +176,7 @@ void shouldUpdateCustomer() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.customerId").value(customerId))
.andExpect(jsonPath("$.name", is(customerRequest.name())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.address", is(customerRequest.address())))
Expand Down Expand Up @@ -210,6 +211,7 @@ void shouldDeleteCustomer() throws Exception {
this.mockMvc
.perform(delete("/api/customers/{id}", customer.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.customerId").value(customer.getId()))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

import static com.example.paymentservice.utils.AppConstants.PROFILE_TEST;
import static org.hamcrest.CoreMatchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.example.paymentservice.entities.Customer;
import com.example.paymentservice.exception.CustomerNotFoundException;
import com.example.paymentservice.model.query.FindCustomersQuery;
import com.example.paymentservice.model.request.CustomerRequest;
import com.example.paymentservice.model.response.CustomerResponse;
import com.example.paymentservice.model.response.PagedResult;
import com.example.paymentservice.services.CustomerService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -22,6 +28,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.zalando.problem.jackson.ProblemModule;
Expand Down Expand Up @@ -73,4 +80,46 @@ void shouldFetchAllCustomers() throws Exception {
.andExpect(jsonPath("$.hasNext", is(false)))
.andExpect(jsonPath("$.hasPrevious", is(false)));
}

@Test
void shouldUpdateCustomer() throws Exception {

CustomerRequest customerRequest =
new CustomerRequest(
"customerUpdatedName", "[email protected]", "junitAddress", 100);

given(customerService.updateCustomer(eq(1L), any(CustomerRequest.class)))
.willReturn(
new CustomerResponse(
1L,
"customerUpdatedName",
"[email protected]",
"junitAddress",
100));

this.mockMvc
.perform(
put("/api/customers/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.customerId", is(1L), Long.class));
}

@Test
void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception {
Long orderId = 1L;
CustomerRequest customerRequest =
new CustomerRequest(
"customerUpdatedName", "[email protected]", "junitAddress", 100);
given(customerService.updateCustomer(eq(1L), any(CustomerRequest.class)))
.willThrow(new CustomerNotFoundException(1L));

this.mockMvc
.perform(
put("/api/customers/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isNotFound());
}
}

0 comments on commit b86ee1f

Please sign in to comment.