OrderMapper { public Order map(OrderDTO dto) { var order = new Order(); order.setOrderId(dto.getOrderId()); if (dto.getPet() != null) { order.setPet(petMapper.map(dto.getPet())); } order.setStatus(OrderStatus.valueOf(dto.getStatus().name())); return order; } OrderMapper(PetMapper petMapper); Order map(OrderDTO dto); OrderDTO map(Order order);  }
@Test void map_fromDTO() { var petDTO = new PetDTO(); petDTO.setPetId(PET_ID); petDTO.setName(PET_NAME); petDTO.setPetType(PET_TYPE); petDTO.setStatus(PetStatusDTO.AVAILABLE); var orderDTO = new OrderDTO(); orderDTO.setOrderId(ORDER_ID); orderDTO.setPet(petDTO); orderDTO.setStatus(OrderStatusDTO.DELIVERED); var order = ORDER_MAPPER.map(orderDTO); Assertions.assertAll(() -> { Assertions.assertNotNull(order); Assertions.assertEquals(orderDTO.getOrderId(), order.getOrderId()); Assertions.assertEquals(orderDTO.getStatus().name(), order.getStatus().name()); var pet = order.getPet(); Assertions.assertNotNull(pet); Assertions.assertEquals(petDTO.getPetId(), pet.getPetId()); Assertions.assertEquals(petDTO.getName(), pet.getName()); Assertions.assertEquals(petDTO.getPetType(), pet.getPetType()); Assertions.assertEquals(petDTO.getStatus().name(), pet.getStatus().name()); }); }
@Test void map_fromObject() { var pet = new Pet(); pet.setPetId(PET_ID); pet.setName(PET_NAME); pet.setPetType(PET_TYPE); pet.setStatus(PetStatus.AVAILABLE); var order = new Order(); order.setOrderId(ORDER_ID); order.setPet(pet); order.setStatus(OrderStatus.DELIVERED); var orderDTO = ORDER_MAPPER.map(order); Assertions.assertAll(() -> { Assertions.assertNotNull(orderDTO); Assertions.assertEquals(order.getOrderId(), orderDTO.getOrderId()); Assertions.assertEquals(order.getStatus().name(), orderDTO.getStatus().name()); var petDTO = orderDTO.getPet(); Assertions.assertNotNull(petDTO); Assertions.assertEquals(pet.getPetId(), petDTO.getPetId()); Assertions.assertEquals(pet.getName(), petDTO.getName()); Assertions.assertEquals(pet.getPetType(), petDTO.getPetType()); Assertions.assertEquals(pet.getStatus().name(), petDTO.getStatus().name()); }); }