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

Refactoring

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