Skip to content

Commit

Permalink
Making a function to allow free tickets (#551)
Browse files Browse the repository at this point in the history
* Making a function to allow free tickets

* Set amount of tickets to int instead of float

* Use the ref url instead of hardcoding

* Missing the "?order=" added
  • Loading branch information
ArdyZ authored Dec 24, 2021
1 parent d1406e9 commit 5b7ba3b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public float getAmount() {
return price;
}

@JsonView(View.OrderOverview.class)
public int getAmountTickets() {
return tickets.size();
}

public void setUser(User user) {
this.user = user;
if (user != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class OrderServiceImpl implements OrderService {
private final PaymentService paymentService;
private final MailService mailService;

@Value("${a5l.paymentReturnUrl}")
private String RETURN_URL;

@Value("${a5l.orderLimit}")
private int ORDER_LIMIT;

Expand Down Expand Up @@ -206,12 +209,16 @@ public Order removeTicketFromOrder(Long orderId, Long ticketId) {
@Override
public String requestPayment(Long orderId) {
Order order = getOrderById(orderId);
if (order.getAmount() == 0) {
if (order.getAmountTickets() == 0) {
throw new IllegalStateException("Order can not be empty");
}
if (order.getStatus() != OrderStatus.ASSIGNED) {
throw new UnassignedOrderException(order.getId());
}
if (order.getAmount() == 0) {
order.setStatus(OrderStatus.PAID);
return RETURN_URL + "?order=" + orderId;
}

return paymentService.registerOrder(order);
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/ch/wisv/areafiftylan/unit/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@

import ch.wisv.areafiftylan.exception.*;
import ch.wisv.areafiftylan.products.model.Ticket;
import ch.wisv.areafiftylan.products.model.TicketType;
import ch.wisv.areafiftylan.products.model.order.Order;
import ch.wisv.areafiftylan.products.model.order.OrderStatus;
import ch.wisv.areafiftylan.users.model.User;
import ch.wisv.areafiftylan.utils.mail.MailService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -44,6 +47,9 @@ public class OrderServiceTest extends ServiceTest {
@Autowired
private MailService mailService;

@Value("${a5l.paymentReturnUrl}")
private String RETURN_URL;

@Test
public void getOrderById() {
Long id = testEntityManager.persistAndGetId(new Order(), Long.class);
Expand Down Expand Up @@ -493,6 +499,21 @@ public void requestPayment() {
reset(paymentService);
}

@Test
public void requestPaymentFree() {
User user = persistUser();
Order order = new Order(user);
TicketType ticketType = new TicketType("testPaymentFree", "testPaymentFree", 0.0f, 15, LocalDateTime.of(3021, 6, 3, 0, 0), true);
Ticket ticket = new Ticket(ticketType);
assertEquals(0.0, ticket.getPrice(), 0.0001);
order.addTicket(ticket);

Long id = testEntityManager.persistAndGetId(order, Long.class);
assertEquals(RETURN_URL + "?order=" + order.getId(), orderService.requestPayment(id));
verify(paymentService, never()).registerOrder(Mockito.any(Order.class));
reset(paymentService);
}

@Test
public void requestPaymentEmptyOrder() {
User user = persistUser();
Expand Down

0 comments on commit 5b7ba3b

Please sign in to comment.