Commit 7c7f55d1 authored by Vladimir Trubachoff's avatar Vladimir Trubachoff

Refactoring

parent f3eab262
...@@ -4,14 +4,16 @@ import com.fasterxml.jackson.annotation.JsonIgnore; ...@@ -4,14 +4,16 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import org.springframework.format.annotation.NumberFormat; import org.springframework.format.annotation.NumberFormat;
@Getter @Getter
@Setter @Setter
@RequiredArgsConstructor @AllArgsConstructor
@NoArgsConstructor
@Entity @Entity
@Table(name = "order_line") @Table(name = "order_line")
public class OrderLine { public class OrderLine {
......
...@@ -8,7 +8,7 @@ import com.example.testj.service.dto.OrderLineRequestDto; ...@@ -8,7 +8,7 @@ import com.example.testj.service.dto.OrderLineRequestDto;
public interface OrderLineService { public interface OrderLineService {
OrderLineDto getOrderLine(Long id) throws ResourceNotFoundException; OrderLineDto getOrderLine(Long id) throws ResourceNotFoundException;
OrderLineDto createOrderLine(OrderLineRequestDto orderLineRequest); OrderLineDto createOrderLine(OrderLineRequestDto request);
OrderLineDto updateOrderLine(OrderLine orderLine); OrderLineDto updateOrderLine(OrderLine orderLine);
......
package com.example.testj.service.dto; package com.example.testj.service.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.*; import jakarta.validation.constraints.*;
import lombok.*; import lombok.*;
...@@ -13,7 +12,6 @@ import java.io.Serializable; ...@@ -13,7 +12,6 @@ import java.io.Serializable;
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GoodsDto implements Serializable { public class GoodsDto implements Serializable {
private Long id; private Long id;
......
...@@ -13,7 +13,6 @@ import java.util.List; ...@@ -13,7 +13,6 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
@EqualsAndHashCode(exclude = {"orderLines"})
public class OrderDto implements Serializable { public class OrderDto implements Serializable {
private Long id; private Long id;
......
...@@ -20,9 +20,6 @@ public class OrderLineDto implements Serializable { ...@@ -20,9 +20,6 @@ public class OrderLineDto implements Serializable {
private GoodsDto goods; private GoodsDto goods;
// @JsonIgnore
// private OrderDto order;
@Positive @Positive
private int count; private int count;
} }
...@@ -2,11 +2,15 @@ package com.example.testj.service.dto; ...@@ -2,11 +2,15 @@ package com.example.testj.service.dto;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive; import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@Getter @Getter
@Setter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderLineRequestDto { public class OrderLineRequestDto {
@NotNull @NotNull
private Long orderId; private Long orderId;
......
package com.example.testj.service.dto;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderLineUpdateRequestDto {
@Positive
private int count;
}
package com.example.testj.service.impl; package com.example.testj.service.impl;
import com.example.testj.domain.Goods;
import com.example.testj.domain.Order;
import com.example.testj.domain.OrderLine;
import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.repository.OrderLineRepository;
import com.example.testj.service.GoodsService; import com.example.testj.service.GoodsService;
import com.example.testj.service.OrderLineService; import com.example.testj.service.OrderLineService;
import com.example.testj.service.OrderService; import com.example.testj.service.OrderService;
import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.service.mapper.GoodsMapper; import com.example.testj.service.mapper.GoodsMapper;
import com.example.testj.service.mapper.OrderLineMapper; import com.example.testj.service.mapper.OrderLineMapper;
import com.example.testj.service.mapper.OrderMapper; import com.example.testj.service.mapper.OrderMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.testj.domain.OrderLine;
import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.repository.OrderLineRepository;
import com.example.testj.service.dto.GoodsDto;
import com.example.testj.service.dto.OrderDto;
import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class OrderLineServiceImpl implements OrderLineService { public class OrderLineServiceImpl implements OrderLineService {
private final OrderLineRepository orderLineRepository; private final OrderLineRepository orderLineRepository;
private final OrderLineMapper orderLineMapper;
private final OrderService orderService; private final OrderService orderService;
private final GoodsService goodsService; private final GoodsService goodsService;
private final OrderLineMapper orderLineMapper;
private final OrderMapper orderMapper; private final OrderMapper orderMapper;
private final GoodsMapper goodsMapper; private final GoodsMapper goodsMapper;
@Override @Override
public OrderLineDto createOrderLine(OrderLineRequestDto orderLineRequest) { public OrderLineDto createOrderLine(OrderLineRequestDto request) {
OrderDto order = orderService.getOrder(orderLineRequest.getOrderId()); Order order = orderMapper.toEntity(orderService.getOrder(request.getOrderId()));
GoodsDto goods = goodsService.getGoods(orderLineRequest.getGoodsId()); Goods goods = goodsMapper.toEntity(goodsService.getGoods(request.getGoodsId()));
OrderLine orderLine = new OrderLine(); OrderLine orderLine = new OrderLine();
orderLine.setOrder(orderMapper.toEntity(order)); orderLine.setOrder(order);
orderLine.setGoods(goodsMapper.toEntity(goods)); orderLine.setGoods(goods);
orderLine.setCount(orderLineRequest.getCount()); orderLine.setCount(request.getCount());
return orderLineMapper.toDto(orderLineRepository.save(orderLine)); return orderLineMapper.toDto(orderLineRepository.save(orderLine));
} }
@Override @Override
public OrderLineDto updateOrderLine(OrderLine orderLine) { public OrderLineDto updateOrderLine(OrderLine orderLine) {
OrderLine updatedOrderLine = orderLineRepository.findById(orderLine.getId()).orElseThrow(ResourceNotFoundException::new); orderLineRepository.findById(orderLine.getId()).orElseThrow(ResourceNotFoundException::new);
updatedOrderLine.setCount(orderLine.getCount()); return orderLineMapper.toDto(orderLineRepository.save(orderLine));
return orderLineMapper.toDto(orderLineRepository.save(updatedOrderLine));
} }
@Override @Override
......
package com.example.testj.web.rest; package com.example.testj.web.rest;
import com.example.testj.domain.Order;
import com.example.testj.exception.ResourceNotFoundException; import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.service.OrderService; import com.example.testj.service.OrderService;
import com.example.testj.service.dto.OrderDto; import com.example.testj.service.dto.OrderDto;
...@@ -38,18 +37,17 @@ public class OrderController { ...@@ -38,18 +37,17 @@ public class OrderController {
@PostMapping @PostMapping
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public OrderDto create(@Valid @RequestBody Order order) { public OrderDto create(@Valid @RequestBody OrderDto orderDto) {
return orderService.createOrUpdateOrder(order); return orderService.createOrUpdateOrder(orderMapper.toEntity(orderDto));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public OrderDto update(@PathVariable Long id, @RequestBody OrderDto order) { public OrderDto update(@PathVariable Long id, @Valid @RequestBody OrderDto orderDto) {
OrderDto updatedOrder = orderService.getOrder(id); OrderDto updatedOrder = orderService.getOrder(id);
updatedOrder.setClient(order.getClient()); updatedOrder.setClient(orderDto.getClient());
updatedOrder.setAddress(order.getAddress()); updatedOrder.setAddress(orderDto.getAddress());
updatedOrder.setDate(order.getDate()); updatedOrder.setDate(orderDto.getDate());
return orderService.createOrUpdateOrder(orderMapper.toEntity(updatedOrder)); return orderService.createOrUpdateOrder(orderMapper.toEntity(updatedOrder));
} }
......
package com.example.testj.web.rest; package com.example.testj.web.rest;
import com.example.testj.exception.ResourceNotFoundException; import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.service.GoodsService;
import com.example.testj.service.OrderLineService; import com.example.testj.service.OrderLineService;
import com.example.testj.service.OrderService;
import com.example.testj.service.dto.OrderLineDto; import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto; import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.service.dto.OrderLineUpdateRequestDto;
import com.example.testj.service.mapper.OrderLineMapper; import com.example.testj.service.mapper.OrderLineMapper;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -18,6 +21,8 @@ import org.springframework.web.bind.annotation.*; ...@@ -18,6 +21,8 @@ import org.springframework.web.bind.annotation.*;
public class OrderLineController { public class OrderLineController {
private final OrderLineService orderLineService; private final OrderLineService orderLineService;
private final OrderLineMapper orderLineMapper; private final OrderLineMapper orderLineMapper;
private OrderService orderService;
private GoodsService goodsService;
@GetMapping("/{id}") @GetMapping("/{id}")
public OrderLineDto get(@PathVariable Long id) { public OrderLineDto get(@PathVariable Long id) {
...@@ -26,13 +31,15 @@ public class OrderLineController { ...@@ -26,13 +31,15 @@ public class OrderLineController {
@PostMapping @PostMapping
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public OrderLineDto create(@Valid @RequestBody OrderLineRequestDto orderLineRequest) { public OrderLineDto create(@Valid @RequestBody OrderLineRequestDto request) {
return orderLineService.createOrderLine(orderLineRequest); return orderLineService.createOrderLine(request);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public OrderLineDto update(@PathVariable Long id, @Valid @RequestBody OrderLineDto orderLine) { public OrderLineDto update(@PathVariable Long id, @Valid @RequestBody OrderLineUpdateRequestDto request) {
return orderLineService.updateOrderLine(orderLineMapper.toEntity(orderLine)); OrderLineDto orderLineDto = orderLineService.getOrderLine(id);
orderLineDto.setCount(request.getCount());
return orderLineService.updateOrderLine(orderLineMapper.toEntity(orderLineDto));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
......
...@@ -63,17 +63,15 @@ public class OrderServiceTest { ...@@ -63,17 +63,15 @@ public class OrderServiceTest {
@Test @Test
void testCreateOrUpdateOrder() { void testCreateOrUpdateOrder() {
// given // given
Order order = new Order(); Order order = new Order(null, "client new", "01-01-2021", "address new", null);
order.setClient("person 2");
order.setAddress("address 2"); Order result = new Order(
order.setDate("01-01-2020"); ordersList.getLast().getId() + 1,
order.getClient(),
Order result = new Order(); order.getDate(),
Long nextId = ordersList.getLast().getId() + 1; order.getAddress(),
result.setId(nextId); null
result.setClient(order.getClient()); );
result.setAddress(order.getAddress());
result.setDate(order.getDate());
// when // when
when(orderRepository.save(order)).thenReturn(result); when(orderRepository.save(order)).thenReturn(result);
OrderDto created = orderService.createOrUpdateOrder(order); OrderDto created = orderService.createOrUpdateOrder(order);
...@@ -86,10 +84,7 @@ public class OrderServiceTest { ...@@ -86,10 +84,7 @@ public class OrderServiceTest {
@Disabled @Disabled
void testSaveOrderWithEmptyClient() { void testSaveOrderWithEmptyClient() {
// given // given
Order order = new Order(); Order order = new Order(null, "", "01-01-2020", "address new", null);
order.setClient("");
order.setAddress("address 2");
order.setDate("01-01-2020");
// when // when
assertThrows(ConstraintViolationException.class, () -> orderService.createOrUpdateOrder(order)); assertThrows(ConstraintViolationException.class, () -> orderService.createOrUpdateOrder(order));
// then // then
...@@ -100,10 +95,7 @@ public class OrderServiceTest { ...@@ -100,10 +95,7 @@ public class OrderServiceTest {
@Disabled @Disabled
void testSaveOrderWithNullName() { void testSaveOrderWithNullName() {
// given // given
Order order = new Order(); Order order = new Order(null, null, "01-01-2020", "address", null);
order.setClient(null);
order.setAddress("address 2");
order.setDate("01-01-2020");
// when // when
assertThrows(ConstraintViolationException.class, () -> orderService.createOrUpdateOrder(order)); assertThrows(ConstraintViolationException.class, () -> orderService.createOrUpdateOrder(order));
// then // then
......
...@@ -9,7 +9,8 @@ import org.json.JSONObject; ...@@ -9,7 +9,8 @@ import org.json.JSONObject;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
...@@ -26,9 +27,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder ...@@ -26,9 +27,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//@SpringBootTest @SpringBootTest
//@AutoConfigureMockMvc @AutoConfigureMockMvc
@WebMvcTest(GoodsController.class)
public class GoodsControllerTest { public class GoodsControllerTest {
@Autowired @Autowired
...@@ -85,9 +85,11 @@ public class GoodsControllerTest { ...@@ -85,9 +85,11 @@ public class GoodsControllerTest {
@Test @Test
@DisplayName("POST /api/goods [201 Created]") @DisplayName("POST /api/goods [201 Created]")
void testCreateGoods() throws Exception { void testCreateGoods() throws Exception {
GoodsDto goodsDto = new GoodsDto(); GoodsDto goodsDto = new GoodsDto(
goodsDto.setName("new product"); null,
goodsDto.setPrice(199.99f); "product new",
199.99
);
String body = new ObjectMapper().writeValueAsString(goodsDto); String body = new ObjectMapper().writeValueAsString(goodsDto);
Long nextId = goodsList.getLast().getId() + 1; Long nextId = goodsList.getLast().getId() + 1;
......
...@@ -30,7 +30,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -30,7 +30,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
//@WebMvcTest(OrderController.class)
public class OrderControllerTest { public class OrderControllerTest {
@Autowired @Autowired
private MockMvc mvc; private MockMvc mvc;
...@@ -45,21 +44,13 @@ public class OrderControllerTest { ...@@ -45,21 +44,13 @@ public class OrderControllerTest {
@BeforeAll @BeforeAll
static void init() { static void init() {
OrderDto orderDto = new OrderDto();
orderDto.setId(1L);
orderDto.setClient("client 1");
orderDto.setAddress("address 1");
orderDto.setDate("20-20-2020");
OrderDto orderDto1 = new OrderDto();
orderDto1.setId(2L);
orderDto1.setClient("client 2");
orderDto1.setAddress("address 2");
orderDto1.setDate("10-10-2021");
ordersList = Stream.of( ordersList = Stream.of(
orderDto, new OrderDto(
orderDto1 1L, "client 1", "20-20-2020", "address 1", null
),
new OrderDto(
2L, "client 2", "20-20-2020", "address 2", null
)
).toList(); ).toList();
} }
...@@ -91,36 +82,32 @@ public class OrderControllerTest { ...@@ -91,36 +82,32 @@ public class OrderControllerTest {
@Test @Test
@DisplayName("POST /api/orders [201 Created]") @DisplayName("POST /api/orders [201 Created]")
void testCreateOrder() throws Exception { void testCreateOrder() throws Exception {
OrderDto orderDto = new OrderDto(); OrderDto orderDto = new OrderDto(
orderDto.setClient("new client"); null, "client new", "21-21-2021", "address new", null
orderDto.setAddress("new address"); );
orderDto.setDate("20-20-2020");
String body = new ObjectMapper().writeValueAsString(orderDto); String body = new ObjectMapper().writeValueAsString(orderDto);
Long nextId = (ordersList.getLast().getId() + 1); Long nextId = (ordersList.getLast().getId() + 1);
orderDto.setId(nextId); orderDto.setId(nextId);
when(orderService.createOrUpdateOrder(orderMapper.toEntity(orderDto))).thenReturn(orderDto); when(orderService.createOrUpdateOrder(orderMapper.toEntity(orderDto))).thenReturn(orderDto);
mvc.perform(post("/api/orders").contentType(MediaType.APPLICATION_JSON).content(body)) mvc.perform(post("/api/orders").contentType(MediaType.APPLICATION_JSON).content(body))
.andExpect(status().isCreated()); .andExpect(status().isCreated())
// .andExpect(jsonPath("$.id").isNumber()) .andExpect(jsonPath("$.id").isNumber())
// .andExpect(jsonPath("$.client").value(orderDto.getClient())) .andExpect(jsonPath("$.client").value(orderDto.getClient()))
// .andExpect(jsonPath("$.address").value(orderDto.getAddress())) .andExpect(jsonPath("$.address").value(orderDto.getAddress()))
// .andExpect(jsonPath("$.date").value(orderDto.getDate())); .andExpect(jsonPath("$.date").value(orderDto.getDate())).andReturn();
} }
@Test @Test
@DisplayName("PUT /api/orders/:id") @DisplayName("PUT /api/orders/:id")
void testUpdateOrder() throws Exception { void testUpdateOrder() throws Exception {
Long id = 1L; Long id = 1L;
OrderDto orderDto = new OrderDto(); OrderDto orderDto = new OrderDto(
orderDto.setClient("new client"); id, "client new", "21-21-2021", "address new", null
orderDto.setAddress("new address"); );
orderDto.setDate("21-21-2021");
String body = new ObjectMapper().writeValueAsString(orderDto); String body = new ObjectMapper().writeValueAsString(orderDto);
orderDto.setId(id);
when(orderService.getOrder(id)).thenReturn(ordersList.getFirst()); when(orderService.getOrder(id)).thenReturn(ordersList.getFirst());
when(orderService.createOrUpdateOrder(orderMapper.toEntity(orderDto))).thenReturn(orderDto); when(orderService.createOrUpdateOrder(orderMapper.toEntity(orderDto))).thenReturn(orderDto);
......
package com.example.testj.web.rest; package com.example.testj.web.rest;
import com.example.testj.domain.Goods;
import com.example.testj.domain.Order;
import com.example.testj.domain.OrderLine; import com.example.testj.domain.OrderLine;
import com.example.testj.exception.ResourceNotFoundException; import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.service.OrderLineService; import com.example.testj.service.OrderLineService;
import com.example.testj.service.dto.GoodsDto; import com.example.testj.service.dto.GoodsDto;
import com.example.testj.service.dto.OrderLineDto; import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto; import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.service.mapper.OrderLineMapper; import com.example.testj.service.dto.OrderLineUpdateRequestDto;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -19,6 +17,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; ...@@ -19,6 +17,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
...@@ -27,7 +26,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -27,7 +26,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
//@WebMvcTest(OrderLineController.class)
public class OrderLineControllerTest { public class OrderLineControllerTest {
@Autowired @Autowired
private MockMvc mvc; private MockMvc mvc;
...@@ -35,24 +33,18 @@ public class OrderLineControllerTest { ...@@ -35,24 +33,18 @@ public class OrderLineControllerTest {
@MockBean @MockBean
private OrderLineService orderLineService; private OrderLineService orderLineService;
@MockBean
private OrderLineMapper orderLineMapper;
@Test @Test
@DisplayName("GET /api/order-line/:id [200 OK]") @DisplayName("GET /api/order-line/:id [200 OK]")
void testGetOrderLine() throws Exception { void testGetOrderLine() throws Exception {
Long id = 1L; Long id = 1L;
GoodsDto goodsDto = new GoodsDto(1L, "product 1", 100.00f); OrderLineDto orderLineDto = new OrderLineDto(id, new GoodsDto(1L, "product 1", 100.00), 10);
OrderLineDto orderLineDto = new OrderLineDto();
orderLineDto.setId(id);
orderLineDto.setCount(10);
orderLineDto.setGoods(goodsDto);
when(orderLineService.getOrderLine(id)).thenReturn(orderLineDto); when(orderLineService.getOrderLine(id)).thenReturn(orderLineDto);
mvc.perform(get("/api/order-line/{id}", id).contentType(MediaType.APPLICATION_JSON)) mvc.perform(get("/api/order-line/{id}", id).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(id)) .andExpectAll(
.andExpect(jsonPath("$.count").value(orderLineDto.getCount())) jsonPath("$.id").value(id),
.andExpect(jsonPath("$.goods.id").value(goodsDto.getId())); jsonPath("$.count").value(orderLineDto.getCount()),
jsonPath("$.goods.id").value(orderLineDto.getGoods().getId()));
} }
@Test @Test
...@@ -67,54 +59,36 @@ public class OrderLineControllerTest { ...@@ -67,54 +59,36 @@ public class OrderLineControllerTest {
@Test @Test
@DisplayName("POST /api/order-line [201 Created]") @DisplayName("POST /api/order-line [201 Created]")
void testCreateOrderLine() throws Exception { void testCreateOrderLine() throws Exception {
Goods goods = new Goods(); Long nextId = 1L;
goods.setId(1L); OrderLineDto orderLineDto = new OrderLineDto(
goods.setName("product 1"); nextId,
goods.setPrice(100.00); new GoodsDto(1L, "product 1", 100.00),
Order order = new Order(); 10);
order.setId(1L); OrderLineRequestDto requestDto = new OrderLineRequestDto(1L, 1L, orderLineDto.getCount());
OrderLine orderLine = new OrderLine();
orderLine.setOrder(order);
orderLine.setGoods(goods);
orderLine.setCount(10);
OrderLineRequestDto requestDto = new OrderLineRequestDto();
requestDto.setGoodsId(orderLine.getGoods().getId());
requestDto.setOrderId(orderLine.getOrder().getId());
requestDto.setCount(orderLine.getCount());
String body = new ObjectMapper().writeValueAsString(requestDto); String body = new ObjectMapper().writeValueAsString(requestDto);
Long nextId = 2L; when(orderLineService.createOrderLine(any(OrderLineRequestDto.class))).thenReturn(orderLineDto);
orderLine.setId(nextId);
OrderLineDto orderLineDto = orderLineMapper.toDto(orderLine);
// OrderLineDto orderLineDto = new OrderLineDto(nextId, new GoodsDto(1L, "product 1", 100.00), 10);
when(orderLineService.createOrderLine(requestDto)).thenReturn(orderLineDto);
mvc.perform(post("/api/order-line").contentType(MediaType.APPLICATION_JSON).content(body)) mvc.perform(post("/api/order-line").contentType(MediaType.APPLICATION_JSON).content(body))
.andExpect(status().isCreated()) .andExpect(status().isCreated())
.andReturn(); .andExpectAll(
// .andExpectAll( jsonPath("$.id").value(orderLineDto.getId()),
// content().contentType(MediaType. APPLICATION_JSON), jsonPath("$.count").value(orderLineDto.getCount()));
// jsonPath("$.id").value(orderLineDto.getId()),
// jsonPath("$.count").value(orderLineDto.getCount()));
} }
@Test @Test
@DisplayName("PUT /api/order-line/:id [200 OK]") @DisplayName("PUT /api/order-line/:id [200 OK]")
void testUpdateOrderLine() throws Exception { void testUpdateOrderLine() throws Exception {
Long id = 1L; Long id = 1L;
OrderLineDto orderLineDto = new OrderLineDto(); OrderLineDto orderLineDto = new OrderLineDto(
orderLineDto.setId(id); id,
orderLineDto.setCount(100); new GoodsDto(1L, "product 1", 100.00),
10);
OrderLineUpdateRequestDto request = new OrderLineUpdateRequestDto(orderLineDto.getCount());
OrderLineRequestDto orderLineRequest = new OrderLineRequestDto(); String body = new ObjectMapper().writeValueAsString(request);
orderLineRequest.setCount(orderLineDto.getCount()); when(orderLineService.getOrderLine(id)).thenReturn(orderLineDto);
String body = new ObjectMapper().writeValueAsString(orderLineRequest); when(orderLineService.updateOrderLine(any(OrderLine.class))).thenReturn(orderLineDto);
when(orderLineService.updateOrderLine(orderLineMapper.toEntity(orderLineDto))).thenReturn(orderLineDto);
mvc.perform(put("/api/order-line/{id}", id) mvc.perform(put("/api/order-line/{id}", id)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment