Skip to content

Implemented builder pattern in Passenger and Payment IT classes. #23 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.projects.aeroplannerrestapi.dto.request;

import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import org.hibernate.validator.constraints.CreditCardNumber;

import java.math.BigDecimal;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PaymentRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.projects.aeroplannerrestapi.enums.PaymentStatusEnum;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.math.BigDecimal;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
import java.util.List;
import java.util.Set;

import static com.projects.aeroplannerrestapi.constants.PathConstants.API_V1_PASSENGERS;
import static com.projects.aeroplannerrestapi.constants.PathConstants.ID;
import static com.projects.aeroplannerrestapi.constants.SecurityRoleConstants.ADMIN;
import static com.projects.aeroplannerrestapi.constants.SortingAndPaginationConstants.*;
import static com.projects.aeroplannerrestapi.util.TestConstants.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@ActiveProfiles("integration")
@WithMockUser(roles = "ADMIN")
@ActiveProfiles(INTEGRATION)
@WithMockUser(roles = ADMIN)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PassengerControllerIT extends AbstractContainerBaseTest {

Expand All @@ -54,35 +59,38 @@ public void setup() {
userRepository.deleteAll();
roleRepository.deleteAll();

Role role = new Role();
role.setName(RoleEnum.USER);
role.setDescription("Default user role");
Role role = Role.builder()
.name(RoleEnum.USER)
.description(DEFAULT_USER_ROLE)
.build();
savedRole = roleRepository.save(role);
}

@Test
public void givenListOfPassengers_whenGetPassengers_thenReturnPaginatedAndSortedPassengers() throws Exception {
// given
User user1 = new User();
user1.setFullName("Full Name 1");
user1.setEmail("sample1@email.com");
user1.setPassword(passwordEncoder.encode("password1"));
user1.setRoles(Set.of(savedRole));

User user2 = new User();
user2.setFullName("Full Name 2");
user2.setEmail("sample2@email.com");
user2.setPassword(passwordEncoder.encode("password 2"));
user2.setRoles(Set.of(savedRole));
User user1 = User.builder()
.fullName(FULL_NAME.concat(ONE))
.email(VALID_EMAIL_ADDRESS.concat(ONE))
.password(passwordEncoder.encode(VALID_PASSWORD.concat(ONE)))
.roles(Set.of(savedRole))
.build();

User user2 = User.builder()
.fullName(FULL_NAME.concat(TWO))
.email(VALID_EMAIL_ADDRESS.concat(TWO))
.password(passwordEncoder.encode(VALID_PASSWORD.concat(TWO)))
.roles(Set.of(savedRole))
.build();

List<User> savedUsers = userRepository.saveAll(List.of(user1, user2));

// when
ResultActions resultActions = mockMvc.perform(get("/api/v1/passengers")
.param("pageNum", "1")
.param("pageSize", "10")
.param("sortBy", "id")
.param("sortDir", "asc"));
ResultActions resultActions = mockMvc.perform(get(API_V1_PASSENGERS)
.param(PAGE_NUM, DEFAULT_PAGE_NUM)
.param(PAGE_SIZE, DEFAULT_PAGE_SIZE)
.param(SORT_BY, DEFAULT_SORT_BY)
.param(SORT_DIR, DEFAULT_SORT_DIR));

// then
resultActions.andDo(print())
Expand All @@ -98,37 +106,39 @@ public void givenListOfPassengers_whenGetPassengers_thenReturnPaginatedAndSorted
@Test
public void givenPassengerId_whenGetPassenger_thenReturnPassenger() throws Exception {
// given
User user = new User();
user.setFullName("Full Name");
user.setEmail("sample@email.com");
user.setPassword(passwordEncoder.encode("password"));
user.setRoles(Set.of(savedRole));
User user = User.builder()
.fullName(FULL_NAME)
.email(VALID_EMAIL_ADDRESS)
.password(passwordEncoder.encode(VALID_PASSWORD))
.roles(Set.of(savedRole))
.build();

User savedUser = userRepository.save(user);

// when
ResultActions resultActions = mockMvc.perform(get("/api/v1/passengers/{id}", savedUser.getId()));
ResultActions resultActions = mockMvc.perform(get(API_V1_PASSENGERS.concat(ID), savedUser.getId()));

// then
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.fullName").value(user.getFullName()))
.andExpect(jsonPath("$.email").value(user.getEmail()));
.andExpect(jsonPath(FULL_NAME_PATH).value(user.getFullName()))
.andExpect(jsonPath(EMAIL_PATH).value(user.getEmail()));
}

@Test
public void givenPassengerId_whenDeletePassenger_thenReturnNothing() throws Exception {
// given
User user = new User();
user.setFullName("Full Name");
user.setEmail("sample@email.com");
user.setPassword(passwordEncoder.encode("password"));
user.setRoles(Set.of(savedRole));
User user = User.builder()
.fullName(FULL_NAME)
.email(VALID_EMAIL_ADDRESS)
.password(passwordEncoder.encode(VALID_PASSWORD))
.roles(Set.of(savedRole))
.build();

User savedUser = userRepository.save(user);

// when
ResultActions resultActions = mockMvc.perform(delete("/api/v1/passengers/{id}", savedUser.getId()));
ResultActions resultActions = mockMvc.perform(delete(API_V1_PASSENGERS.concat(ID), savedUser.getId()));

// then
resultActions.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Set;

import static com.projects.aeroplannerrestapi.constants.PathConstants.*;
import static com.projects.aeroplannerrestapi.constants.SecurityRoleConstants.ADMIN;
import static com.projects.aeroplannerrestapi.constants.SecurityRoleConstants.USER;
import static com.projects.aeroplannerrestapi.util.TestConstants.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@ActiveProfiles("integration")
@ActiveProfiles(INTEGRATION)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PaymentControllerIT extends AbstractContainerBaseTest {

Expand Down Expand Up @@ -67,96 +70,107 @@ public void setup() {
}

@Test
@WithMockUser(roles = "USER")
@WithMockUser(roles = USER)
public void givenPaymentRequest_whenMakePayment_thenReturnPaymentResponse() throws Exception {
// given
User user = new User();
user.setFullName("Full Name");
user.setEmail("sample@email.com");
user.setPassword(passwordEncoder.encode("password"));
Role role = new Role();
role.setName(RoleEnum.USER);
role.setDescription("Default user role");
Role role = Role.builder()
.name(RoleEnum.USER)
.description(DEFAULT_USER_ROLE)
.build();

Role savedRole = roleRepository.save(role);
user.setRoles(Set.of(savedRole));

User user = User.builder()
.fullName(FULL_NAME)
.email(VALID_EMAIL_ADDRESS)
.password(passwordEncoder.encode(VALID_PASSWORD))
.roles(Set.of(savedRole))
.build();

User savedUser = userRepository.save(user);

Flight flight = new Flight();
flight.setAirline("Airline");
flight.setFlightNumber("Flight Number");
flight.setDepartureTime("2023-04-19T15:30:00");
flight.setArrivalTime("2024-04-19T15:30:00");
flight.setDuration(Duration.between(LocalDateTime.parse("2023-04-19T15:30:00"),
LocalDateTime.parse("2024-04-19T15:30:00")));
flight.setPrice(BigDecimal.valueOf(100.00));
flight.setAircraftType("Aircraft Type");
flight.setSeatAvailability(0);
flight.setCurrentAvailableSeat(0);
flight.setStatus(FlightStatusEnum.UNKNOWN);
Flight flight = Flight.builder()
.airline(AIRLINE)
.flightNumber(FLIGHT_NUMBER)
.departureTime(VALID_DEPARTURE_TIME)
.arrivalTime(VALID_ARRIVAL_TIME)
.duration(Duration.between(LocalDateTime.parse(VALID_DEPARTURE_TIME), LocalDateTime.parse(VALID_ARRIVAL_TIME)))
.price(VALID_AMOUNT)
.aircraftType(AIRCRAFT_TYPE)
.seatAvailability(ZERO)
.currentAvailableSeat(ZERO)
.status(FlightStatusEnum.UNKNOWN)
.build();

Flight savedFlight = flightRepository.save(flight);

Reservation reservation = new Reservation();
reservation.setPassengerId(savedUser.getId());
reservation.setFlightId(savedFlight.getId());
reservation.setSeatNumber(1);
reservation.setReservationDate("2024-05-19T15:30:00");
reservation.setReservationStatus(ReservationStatusEnum.CONFIRMED);
Reservation reservation = Reservation.builder()
.passengerId(savedUser.getId())
.flightId(savedFlight.getId())
.seatNumber(Integer.parseInt(ONE))
.reservationDate(VALID_RESERVATION_DATE)
.reservationStatus(ReservationStatusEnum.CONFIRMED)
.build();

Reservation savedReservation = reservationRepository.save(reservation);

PaymentRequest paymentRequest = new PaymentRequest();
paymentRequest.setPassengerId(savedUser.getId());
paymentRequest.setFlightId(savedFlight.getId());
paymentRequest.setSeatNumber(savedReservation.getSeatNumber());
paymentRequest.setCardNumber("4532280979380570");
paymentRequest.setCardHolderName("Card Holder Name");
paymentRequest.setExpiryDate("11/30");
paymentRequest.setCvv("4431");
paymentRequest.setAmount(savedFlight.getPrice());
PaymentRequest paymentRequest = PaymentRequest.builder()
.passengerId(savedUser.getId())
.flightId(savedFlight.getId())
.seatNumber(savedReservation.getSeatNumber())
.cardNumber(VALID_CARD_NUMBER)
.cardHolderName(CARD_HOLDER_NAME)
.expiryDate(VALID_EXPIRY_DATE)
.cvv(VALID_CVV)
.amount(savedFlight.getPrice())
.build();

// when
ResultActions resultActions = mockMvc.perform(post("/api/v1/payments/payment")
ResultActions resultActions = mockMvc.perform(post(API_V1_PAYMENTS.concat(PAYMENT))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(paymentRequest)));

// then
resultActions.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.amount").value(paymentRequest.getAmount()))
.andExpect(jsonPath("$.status").value(PaymentStatusEnum.PAID.name()))
.andExpect(jsonPath("$.message").value(PaymentStatusEnum.PAID.name()));
.andExpect(jsonPath(AMOUNT_PATH).value(paymentRequest.getAmount().intValue()))
.andExpect(jsonPath(STATUS_PATH).value(PaymentStatusEnum.PAID.name()))
.andExpect(jsonPath(MESSAGE_PATH).value(PaymentStatusEnum.PAID.name()));
}

@Test
@WithMockUser(roles = {"USER", "ADMIN"})
@WithMockUser(roles = {USER, ADMIN})
public void givenPaymentId_whenGetPaymentDetails_thenReturnPaymentObject() throws Exception {
// given
Payment payment = new Payment();
payment.setPassengerId(1L);
payment.setFlightId(1L);
payment.setStatus(PaymentStatusEnum.PAID);
payment.setTransactionId("transaction id");
payment.setCvv("4431");
payment.setAmount(BigDecimal.valueOf(100.00));
payment.setCardNumber("4532280979380570");
payment.setCardHolderName("Card Holder Name");
payment.setExpiryDate("11/30");
Payment payment = Payment.builder()
.passengerId(VALID_PASSENGER_ID)
.flightId(VALID_FLIGHT_ID)
.status(PaymentStatusEnum.PAID)
.transactionId(TRANSACTION_ID)
.cvv(VALID_CVV)
.amount(VALID_AMOUNT)
.cardNumber(VALID_CARD_NUMBER)
.cardHolderName(CARD_HOLDER_NAME)
.expiryDate(VALID_EXPIRY_DATE)
.build();

Payment savedPayment = paymentRepository.save(payment);

// when
ResultActions resultActions = mockMvc.perform(get("/api/v1/payments/{id}", savedPayment.getId()));
ResultActions resultActions = mockMvc.perform(get(API_V1_PAYMENTS.concat(ID), savedPayment.getId()));

// then
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(savedPayment.getId()))
.andExpect(jsonPath("$.passengerId").value(savedPayment.getPassengerId()))
.andExpect(jsonPath("$.flightId").value(savedPayment.getFlightId()))
.andExpect(jsonPath("$.status").value(savedPayment.getStatus().name()))
.andExpect(jsonPath("$.transactionId").value(savedPayment.getTransactionId()))
.andExpect(jsonPath("$.cvv").value(savedPayment.getCvv()))
.andExpect(jsonPath("$.amount").value(savedPayment.getAmount()))
.andExpect(jsonPath("$.cardNumber").value(savedPayment.getCardNumber()))
.andExpect(jsonPath("$.cardHolderName").value(savedPayment.getCardHolderName()))
.andExpect(jsonPath("$.expiryDate").value(savedPayment.getExpiryDate()));
.andExpect(jsonPath(ID_PATH).value(savedPayment.getId()))
.andExpect(jsonPath(PASSENGER_ID_PATH).value(savedPayment.getPassengerId()))
.andExpect(jsonPath(FLIGHT_ID_PATH).value(savedPayment.getFlightId()))
.andExpect(jsonPath(STATUS_PATH).value(savedPayment.getStatus().name()))
.andExpect(jsonPath(TRANSACTION_ID_PATH).value(savedPayment.getTransactionId()))
.andExpect(jsonPath(CVV_PATH).value(savedPayment.getCvv()))
.andExpect(jsonPath(AMOUNT_PATH).value(savedPayment.getAmount().intValue()))
.andExpect(jsonPath(CARD_NUMBER_PATH).value(savedPayment.getCardNumber()))
.andExpect(jsonPath(CARD_HOLDER_NAME_PATH).value(savedPayment.getCardHolderName()))
.andExpect(jsonPath(EXPIRY_DATE_PATH).value(savedPayment.getExpiryDate()));
}
}
Loading
Loading