Skip to content

Commit

Permalink
feat : adds response mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Oct 19, 2023
1 parent ae27899 commit fb61fdd
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.custom.sequence.mapper;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.model.response.CustomerResponse;
import org.springframework.stereotype.Service;

@Service
public class CustomerMapper {
public CustomerResponse mapToResponse(Customer saved) {
return new CustomerResponse(saved.getId(), saved.getText());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.custom.sequence.mapper;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.entities.Order;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.OrderResponse;
import org.springframework.stereotype.Service;

@Service
public class OrderMapper {

public OrderResponse getOrderResponse(Order order) {
Customer customer = order.getCustomer();
return new OrderResponse(
order.getId(),
order.getText(),
new CustomerResponse(customer.getId(), customer.getText()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.custom.sequence.model.response;

public record CustomerResponse(String id, String text) {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.example.custom.sequence.model.response;

public record OrderResponse(String id, String text, CustomerDTO customerDTO) {}
public record OrderResponse(String id, String text, CustomerResponse customer) {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.custom.sequence.services;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.mapper.CustomerMapper;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.repositories.CustomerRepository;
import java.util.List;
Expand All @@ -19,6 +21,7 @@
public class CustomerService {

private final CustomerRepository customerRepository;
private final CustomerMapper customerMapper;

@Transactional(readOnly = true)
public PagedResult<Customer> findAllCustomers(
Expand Down Expand Up @@ -49,8 +52,9 @@ public Optional<Customer> findCustomerById(String id) {
return customerRepository.findById(id);
}

public Customer saveCustomer(Customer customer) {
return customerRepository.save(customer);
public CustomerResponse saveCustomer(Customer customer) {
Customer saved = customerRepository.save(customer);
return customerMapper.mapToResponse(saved);
}

public void deleteCustomerById(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.custom.sequence.services;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.entities.Order;
import com.example.custom.sequence.model.response.CustomerDTO;
import com.example.custom.sequence.mapper.OrderMapper;
import com.example.custom.sequence.model.response.OrderResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.repositories.OrderRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,13 +17,11 @@

@Service
@Transactional
@RequiredArgsConstructor
public class OrderService {

private final OrderRepository orderRepository;

public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
private final OrderMapper orderMapper;

public PagedResult<OrderResponse> findAllOrders(
int pageNo, int pageSize, String sortBy, String sortDir) {
Expand All @@ -37,7 +35,7 @@ public PagedResult<OrderResponse> findAllOrders(
Page<Order> ordersPage = orderRepository.findAll(pageable);

List<OrderResponse> orderResponseList =
ordersPage.getContent().stream().map(this::getOrderDTO).toList();
ordersPage.getContent().stream().map(orderMapper::getOrderResponse).toList();

return new PagedResult<>(
orderResponseList,
Expand All @@ -51,26 +49,14 @@ public PagedResult<OrderResponse> findAllOrders(
}

public Optional<OrderResponse> findOrderById(String id) {
return convertToOrderDTO(orderRepository.findById(id));
return orderRepository.findById(id).map(orderMapper::getOrderResponse);
}

public OrderResponse saveOrder(Order order) {
return getOrderDTO(orderRepository.save(order));
return orderMapper.getOrderResponse(orderRepository.save(order));
}

public void deleteOrderById(String id) {
orderRepository.deleteById(id);
}

private Optional<OrderResponse> convertToOrderDTO(Optional<Order> optionalOrder) {
return optionalOrder.map(this::getOrderDTO);
}

private OrderResponse getOrderDTO(Order order) {
Customer customer = order.getCustomer();
return new OrderResponse(
order.getId(),
order.getText(),
new CustomerDTO(customer.getId(), customer.getText()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.custom.sequence.web.api;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.model.response.CustomerResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.http.ProblemDetail;

public interface CustomerAPI {

/**
* POST /api/customers
*
* @param customer (required)
* @return Created (status code 201) or Bad Request (status code 400)
*/
@Operation(
operationId = "createCustomer",
tags = {"customer-controller"},
responses = {
@ApiResponse(
responseCode = "201",
description = "Created",
content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = CustomerResponse.class))
}),
@ApiResponse(
responseCode = "400",
description = "Bad Request",
content = {
@Content(
mediaType = MediaType.APPLICATION_PROBLEM_JSON_VALUE,
schema = @Schema(implementation = ProblemDetail.class))
})
})
CustomerResponse createCustomer(
@Valid @RequestBody(description = "", required = true) Customer customer);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.custom.sequence.web.controllers;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.services.CustomerService;
import com.example.custom.sequence.utils.AppConstants;
import com.example.custom.sequence.web.api.CustomerAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -23,7 +25,7 @@
@RestController
@RequestMapping("/api/customers")
@Slf4j
public class CustomerController {
public class CustomerController implements CustomerAPI {

private final CustomerService customerService;

Expand Down Expand Up @@ -67,12 +69,13 @@ public ResponseEntity<Customer> getCustomerById(@PathVariable String id) {

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Customer createCustomer(@RequestBody @Validated Customer customer) {
@Override
public CustomerResponse createCustomer(@RequestBody @Validated Customer customer) {
return customerService.saveCustomer(customer);
}

@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(
public ResponseEntity<CustomerResponse> updateCustomer(
@PathVariable String id, @RequestBody Customer customer) {
return customerService
.findCustomerById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.mockito.BDDMockito.willDoNothing;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.mapper.CustomerMapper;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.repositories.CustomerRepository;
import java.util.List;
Expand All @@ -25,6 +27,7 @@
class CustomerServiceTest {

@Mock private CustomerRepository customerRepository;
@Mock private CustomerMapper customerMapper;

@InjectMocks private CustomerService customerService;

Expand Down Expand Up @@ -69,12 +72,13 @@ void findCustomerById() {
void saveCustomer() {
// given
given(customerRepository.save(getCustomer())).willReturn(getCustomer());
given(customerMapper.mapToResponse(getCustomer())).willReturn(getCustomerResponse());
// when
Customer persistedCustomer = customerService.saveCustomer(getCustomer());
CustomerResponse persistedCustomer = customerService.saveCustomer(getCustomer());
// then
assertThat(persistedCustomer).isNotNull();
assertThat(persistedCustomer.getId()).isEqualTo("CUS_1");
assertThat(persistedCustomer.getText()).isEqualTo("junitTest");
assertThat(persistedCustomer.id()).isEqualTo("CUS_1");
assertThat(persistedCustomer.text()).isEqualTo("junitTest");
}

@Test
Expand All @@ -93,4 +97,8 @@ private Customer getCustomer() {
customer.setText("junitTest");
return customer;
}

private CustomerResponse getCustomerResponse() {
return new CustomerResponse("CUS_1", "junitTest");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.entities.Order;
import com.example.custom.sequence.mapper.OrderMapper;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.OrderResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.repositories.OrderRepository;
Expand All @@ -28,6 +30,7 @@
class OrderServiceTest {

@Mock private OrderRepository orderRepository;
@Mock private OrderMapper orderMapper;

@InjectMocks private OrderService orderService;

Expand Down Expand Up @@ -57,6 +60,7 @@ void findAllOrders() {
void findOrderById() {
// given
given(orderRepository.findById("1")).willReturn(Optional.of(getOrder()));
given(orderMapper.getOrderResponse(getOrder())).willReturn(getOrderResponse());
// when
Optional<OrderResponse> optionalOrder = orderService.findOrderById("1");
// then
Expand All @@ -70,6 +74,7 @@ void findOrderById() {
void saveOrder() {
// given
given(orderRepository.save(getOrder())).willReturn(getOrder());
given(orderMapper.getOrderResponse(getOrder())).willReturn(getOrderResponse());
// when
OrderResponse persistedOrder = orderService.saveOrder(getOrder());
// then
Expand Down Expand Up @@ -98,4 +103,8 @@ private Order getOrder() {
order.setCustomer(customer);
return order;
}

private OrderResponse getOrderResponse() {
return new OrderResponse("1", "junitText", new CustomerResponse("1", "custText"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.services.CustomerService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -97,7 +98,7 @@ void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception {
@Test
void shouldCreateNewCustomer() throws Exception {
given(customerService.saveCustomer(any(Customer.class)))
.willAnswer((invocation) -> invocation.getArgument(0));
.willReturn(new CustomerResponse("CUS_1", "some text"));

Customer customer = new Customer("CUS_1", "some text", new ArrayList<>());
this.mockMvc
Expand Down Expand Up @@ -138,7 +139,7 @@ void shouldUpdateCustomer() throws Exception {
Customer customer = new Customer(customerId, "Updated text", new ArrayList<>());
given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer));
given(customerService.saveCustomer(any(Customer.class)))
.willAnswer((invocation) -> invocation.getArgument(0));
.willReturn(new CustomerResponse("CUS_1", "Updated text"));

this.mockMvc
.perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.example.custom.sequence.entities.Customer;
import com.example.custom.sequence.entities.Order;
import com.example.custom.sequence.model.response.CustomerDTO;
import com.example.custom.sequence.model.response.CustomerResponse;
import com.example.custom.sequence.model.response.OrderResponse;
import com.example.custom.sequence.model.response.PagedResult;
import com.example.custom.sequence.services.OrderService;
Expand Down Expand Up @@ -61,9 +61,12 @@ void setUp() {
@Test
void shouldFetchAllOrders() throws Exception {
List<OrderResponse> orderResponseList = new ArrayList<>();
orderResponseList.add(new OrderResponse("1", "text 1", new CustomerDTO("1", "customer1")));
orderResponseList.add(new OrderResponse("2", "text 2", new CustomerDTO("1", "customer1")));
orderResponseList.add(new OrderResponse("3", "text 3", new CustomerDTO("1", "customer1")));
orderResponseList.add(
new OrderResponse("1", "text 1", new CustomerResponse("1", "customer1")));
orderResponseList.add(
new OrderResponse("2", "text 2", new CustomerResponse("1", "customer1")));
orderResponseList.add(
new OrderResponse("3", "text 3", new CustomerResponse("1", "customer1")));
Page<OrderResponse> page = new PageImpl<>(orderResponseList);
PagedResult<OrderResponse> orderPagedResult = new PagedResult<>(page);
given(orderService.findAllOrders(0, 10, "id", "asc")).willReturn(orderPagedResult);
Expand All @@ -86,7 +89,9 @@ void shouldFindOrderById() throws Exception {
String orderId = "1";
OrderResponse order =
new OrderResponse(
orderId, "text 1", new CustomerDTO(customer.getId(), customer.getText()));
orderId,
"text 1",
new CustomerResponse(customer.getId(), customer.getText()));
given(orderService.findOrderById(orderId)).willReturn(Optional.of(order));

this.mockMvc
Expand Down Expand Up @@ -148,7 +153,7 @@ void shouldUpdateOrder() throws Exception {
new OrderResponse(
orderId,
"Updated text",
new CustomerDTO(customer.getId(), customer.getText()));
new CustomerResponse(customer.getId(), customer.getText()));
given(orderService.findOrderById(orderId)).willReturn(Optional.of(orderResponse));
given(orderService.saveOrder(any(Order.class)))
.willReturn(new OrderResponse("1", "Updated text", null));
Expand Down Expand Up @@ -183,7 +188,7 @@ void shouldDeleteOrder() throws Exception {
new OrderResponse(
orderId,
"Some text",
new CustomerDTO(customer.getId(), customer.getText()));
new CustomerResponse(customer.getId(), customer.getText()));
given(orderService.findOrderById(orderId)).willReturn(Optional.of(order));
doNothing().when(orderService).deleteOrderById(order.id());

Expand Down

0 comments on commit fb61fdd

Please sign in to comment.