Skip to content

Commit

Permalink
OrderService testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed May 12, 2024
1 parent 62762ac commit 2313233
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.ibrahimokic.ordermanagement.service.ProductService;
import com.ibrahimokic.ordermanagement.service.UserService;
import com.ibrahimokic.ordermanagement.utils.Utils;
import jakarta.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;
Expand Down Expand Up @@ -250,7 +251,15 @@ private void createUserAccountForm() {
.build();

saveUserAndReturnToMenu(newUserAccount);
} catch (Exception e) {
}
catch (ConstraintViolationException e) {
e.getConstraintViolations().forEach(violation ->
System.out.println(violation.getMessage())
);
Utils.returnBackToTheMainMenu(scanner);
adminUserManagementOptions();
}
catch (Exception e) {
System.out.println("ERROR: "+ e.getMessage());
Utils.returnBackToTheMainMenu(scanner);
adminUserManagementOptions();
Expand Down Expand Up @@ -306,7 +315,13 @@ private void saveUserAndReturnToMenu(User user) {
try {
userService.createUser(user);
System.out.println("OM-APP: New account '" + user.getFirstName() + " " + user.getLastName() + "' with role '"+ user.getRole() +"' has been successfully created.");
} catch (Exception e) {
}
catch (ConstraintViolationException e) {
e.getConstraintViolations().forEach(violation ->
System.out.println(violation.getMessage())
);
}
catch (Exception e) {
System.out.println("ERROR: An error occurred while creating an user, please check your inputs.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ public class OrderItemDto {

@JsonProperty("quantity")
@NotNull
@NotBlank(message = "Quantity is required")
private Integer quantity;

@JsonProperty("item_price")
@NotNull
@NotBlank(message = "Item price is required")
private BigDecimal itemPrice;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ProductDto {

@JsonProperty("price")
@NotNull
@NotBlank(message = "Price is required")
private BigDecimal price;

@JsonProperty("available_from")
Expand All @@ -45,6 +44,6 @@ public class ProductDto {
private LocalDate availableUntil;

@JsonProperty("available_quantity")
@NotBlank(message = "Available quantity is required")
@NotNull
private int availableQuantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -62,5 +63,6 @@ public class UserDto {

@JsonProperty("address")
@NotNull
@Valid
private AddressDto address;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ibrahimokic.ordermanagement.domain.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -19,15 +21,23 @@ public class Address {
private Long addressId;

@Column(name = "street")
@NotNull
@NotBlank(message = "Street is required")
private String street;

@Column(name = "zip")
@NotNull
@NotBlank(message = "Zip is required")
private String zip;

@Column(name = "city")
@NotNull
@NotBlank(message = "City is required")
private String city;

@Column(name = "country")
@NotNull
@NotBlank(message = "Country is required")
private String country;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ibrahimokic.ordermanagement.domain.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ibrahimokic.ordermanagement.domain.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -29,8 +30,10 @@ public class OrderItem {
private Product product;

@Column(nullable = false)
@NotNull
private int quantity;

@Column(name = "item_price", nullable = false, precision = 10, scale = 2)
@NotNull
private BigDecimal itemPrice;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class Product {
private String productName;

@Column(nullable = false)
@NotBlank(message = "Price is required")
@NotNull
private BigDecimal price;

Expand All @@ -47,7 +46,6 @@ public class Product {
private LocalDate availableUntil;

@Column(name = "available_quantity", nullable = false)
@NotBlank(message = "Available quantity is required")
@NotNull
private int availableQuantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public class User {
@Column(name = "password", nullable = false)
@JsonIgnore
@NotBlank(message = "Password is required")
@Size(
min = 5,
message = "The password '${validatedValue}' must be greater then {min}"
)
@Size( min = 5, message = "The password '${validatedValue}' must be greater then {min}")
private String password;

@Column(name = "email", nullable = false)
Expand Down Expand Up @@ -67,5 +64,4 @@ public class User {
public boolean checkUserPassword(String password) {
return password.equals(this.password);
}
public boolean checkEmailAlreadyExists(String email) { return email.equals(this.email); }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ibrahimokic.ordermanagement.mapper;

import com.ibrahimokic.ordermanagement.domain.dto.OrderItemDto;

import java.util.Collections;
import java.util.List;

Expand All @@ -13,8 +11,4 @@ public interface Mapper<A, B> {
default List<A> mapListToEntityList(List<B> b) {
return Collections.emptyList();
}

default List<B> mapDtoListToEntityList(List<OrderItemDto> a) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,4 @@ public List<OrderItem> mapListToEntityList(List<OrderItemDto> orderItemDtos) {
.map(this::mapFrom)
.collect(Collectors.toList());
}

@Override
public List<OrderItemDto> mapDtoListToEntityList(@Valid List<OrderItemDto> orderItems) {
return orderItems.stream()
.map(orderItemDto -> mapTo(mapFrom(orderItemDto)))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public OrderMapperImpl(ModelMapper modelMapper) {
}
@Override
public OrderDto mapTo(Order order) {
OrderDto orderDto;
orderDto = modelMapper.map(order, OrderDto.class);
OrderDto orderDto = modelMapper.map(order, OrderDto.class);
System.out.println(orderDto);
orderDto.setUserId(order.getUser().getUserId());
return orderDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -109,7 +110,7 @@ public Optional<OrderDto> createNewOrder(@Valid OrderDto orderDto) {
.sourceAddress(addressMapper.mapFrom(orderDto.getSourceAddress()))
.orderDate(orderDto.getOrderDate())
.orderItems(null)
.totalAmount(new BigDecimal(0))
.totalAmount(BigDecimal.ZERO)
.build();

Optional<User> optionalUser = userRepository.findById(orderDto.getUserId());
Expand Down Expand Up @@ -161,10 +162,10 @@ public Optional<OrderDto> createNewOrder(@Valid OrderDto orderDto) {
order.setTotalAmount(Utils.calculateTotalProductsPriceAmount(orderItems));

orderRepository.save(order);

return Optional.ofNullable(orderMapper.mapTo(order));
OrderDto mappedOrderDto = orderMapper.mapTo(order);
return Optional.of(Objects.requireNonNullElse(mappedOrderDto, orderDto));
} catch (Exception e) {
throw new RuntimeException("Error creating order: " + e.getMessage());
throw new RuntimeException("Error occurred while creating an order: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public User createUser(User user) {
try {
User userExists = userRepository.findByEmail(user.getEmail());

if(userExists != null)
{
if(userExists != null) {
throw new RuntimeException("User with that email already exists in the database.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class OrderMapperImplTest {

@Test
void testMapToOrderDto() {
// Given
User user = User.builder().userId(101L).build();
Address deliveryAddress = Address.builder().addressId(1L).build();
Address sourceAddress = Address.builder().addressId(2L).build();
Expand All @@ -56,16 +55,12 @@ void testMapToOrderDto() {

when(modelMapper.map(order, OrderDto.class)).thenReturn(expectedOrderDto);

// When
OrderDto result = orderMapper.mapTo(order);

// Then
assertEquals(expectedOrderDto, result);
}

@Test
void testMapFromOrderDto() {
// Given
AddressDto deliveryAddressDto = AddressDto.builder().addressId(1L).build();
AddressDto sourceAddressDto = AddressDto.builder().addressId(2L).build();
OrderDto orderDto = OrderDto.builder()
Expand All @@ -91,10 +86,7 @@ void testMapFromOrderDto() {

when(modelMapper.map(orderDto, Order.class)).thenReturn(expectedOrder);

// When
Order result = orderMapper.mapFrom(orderDto);

// Then
assertEquals(expectedOrder, result);
}
}
Loading

0 comments on commit 2313233

Please sign in to comment.