Skip to content

Commit

Permalink
feat : fix issue with mapping child in parent
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jun 21, 2024
1 parent bd960e6 commit 0a17713
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ public Order setOrderItems(List<OrderItem> orderItems) {
return this;
}

public Order addOrderItem(OrderItem orderItem) {
this.orderItems.add(orderItem);
orderItem.setOrder(this);
return this;
}

public Order removeOrderItem(OrderItem orderItem) {
this.orderItems.remove(orderItem);
orderItem.setOrder(null);
return this;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import com.example.hibernatecache.model.request.CustomerRequest;
import com.example.hibernatecache.model.response.CustomerResponse;
import java.util.List;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
uses = OrderMapper.class,
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
suppressTimestampInGenerated = true)
public interface CustomerMapper {

Customer toEntity(CustomerRequest customerRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
suppressTimestampInGenerated = true)
public interface OrderItemMapper {

@Mapping(target = "order.id", source = "orderId")
@Mapping(target = "id", ignore = true)
OrderItem toEntity(OrderItemRequest orderItemRequest);

@Mapping(source = "id", target = "orderItemId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import com.example.hibernatecache.model.request.OrderRequest;
import com.example.hibernatecache.model.response.OrderResponse;
import java.util.List;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
uses = OrderItemMapper.class,
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
suppressTimestampInGenerated = true)
public interface OrderMapper {

@Mapping(target = "orderItems", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.example.hibernatecache.repositories.OrderItemRepository;
import com.example.hibernatecache.repositories.OrderRepository;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,26 +50,31 @@ void setUp() {
.setFirstName("firstName 1")
.setLastName("lastName 1")
.setEmail("[email protected]")
.setPhone("9876543211"));
savedOrder =
orderRepository.persist(
new Order()
.setName("First Order")
.setPrice(BigDecimal.TEN)
.setCustomer(savedCustomer));

orderItemList = new ArrayList<>();
orderItemList.add(new OrderItem().setText("First OrderItem").setOrder(savedOrder));
orderItemList.add(new OrderItem().setText("Second OrderItem").setOrder(savedOrder));
orderItemList.add(new OrderItem().setText("Third OrderItem").setOrder(savedOrder));
orderItemList = orderItemRepository.persistAll(orderItemList);
.setPhone("9876543211")
.addOrder(
new Order()
.setName("First Order")
.setPrice(BigDecimal.TEN)
.addOrderItem(
new OrderItem().setText("First OrderItem"))
.addOrderItem(
new OrderItem().setText("Second OrderItem"))
.addOrderItem(
new OrderItem()
.setText("Third OrderItem"))));
savedOrder = savedCustomer.getOrders().getFirst();
orderItemList = savedOrder.getOrderItems();
}

@Test
void shouldFetchAllOrderItems() throws Exception {
this.mockMvc
.perform(get("/api/order/items"))
.andExpect(status().isOk())
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_JSON_VALUE)))
.andExpect(jsonPath("$.data.size()", is(orderItemList.size())))
.andExpect(jsonPath("$.totalElements", is(3)))
.andExpect(jsonPath("$.pageNumber", is(1)))
Expand All @@ -89,6 +93,10 @@ void shouldFindOrderItemById() throws Exception {
this.mockMvc
.perform(get("/api/order/items/{id}", orderItemId))
.andExpect(status().isOk())
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_JSON_VALUE)))
.andExpect(jsonPath("$.orderItemId", is(orderItem.getId()), Long.class))
.andExpect(jsonPath("$.text", is(orderItem.getText())));
}
Expand All @@ -104,6 +112,10 @@ void shouldCreateNewOrderItem() throws Exception {
.content(objectMapper.writeValueAsString(orderItemRequest)))
.andExpect(status().isCreated())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_JSON_VALUE)))
.andExpect(jsonPath("$.orderItemId", notNullValue()))
.andExpect(jsonPath("$.text", is(orderItemRequest.text())));
}
Expand All @@ -118,7 +130,10 @@ void shouldReturn400WhenCreateNewOrderItemWithoutText() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(orderItemRequest)))
.andExpect(status().isBadRequest())
.andExpect(header().string("Content-Type", is("application/problem+json")))
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("about:blank")))
.andExpect(jsonPath("$.title", is("Constraint Violation")))
.andExpect(jsonPath("$.status", is(400)))
Expand All @@ -142,6 +157,10 @@ void shouldUpdateOrderItem() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(orderItemRequest)))
.andExpect(status().isOk())
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_JSON_VALUE)))
.andExpect(jsonPath("$.orderItemId", is(orderItemId), Long.class))
.andExpect(jsonPath("$.text", is(orderItemRequest.text())));
}
Expand All @@ -153,6 +172,10 @@ void shouldDeleteOrderItem() throws Exception {
this.mockMvc
.perform(delete("/api/order/items/{id}", orderItem.getId()))
.andExpect(status().isOk())
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_JSON_VALUE)))
.andExpect(jsonPath("$.orderItemId", is(orderItem.getId()), Long.class))
.andExpect(jsonPath("$.text", is(orderItem.getText())));
}
Expand Down

0 comments on commit 0a17713

Please sign in to comment.