Skip to content

Commit

Permalink
Add unit tests & improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
evaggelos99 committed May 26, 2024
1 parent 728b8d8 commit 5c0ddc1
Show file tree
Hide file tree
Showing 28 changed files with 1,295 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Ticket apply(final TicketDto ticketDto) {

return new Ticket(ticketDto.uuid(), ticketDto.createdAt().toInstant(), ticketDto.lastUpdated().toInstant(),
ticketDto.eventID(), ticketDto.ticketType(), ticketDto.price(), ticketDto.transferable(),
ticketDto.seatInfo());
ticketDto.seatInformation());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TicketDto apply(final Ticket ticket) {

return new TicketDto(ticket.getUuid(), this.convertToTimeStamp(ticket.getCreatedAt()),
this.convertToTimeStamp(ticket.getLastUpdated()), ticket.getEventID(), ticket.getTicketType(),
ticket.getPrice(), ticket.getTransferable(), ticket.getSeatInfo());
ticket.getPrice(), ticket.getTransferable(), ticket.getSeatInformation());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ public final class ContactInformation {
// TODO add regex validation for email
@NotNull
@Schema(example = "[email protected]", description = "An email address belonging to an entity")
private String email;
private final String email;
@NotNull
@Schema(example = "70493729392", description = "A phone number belonging to an entity")
private String phoneNumber;
private final String phoneNumber;
@NotNull
@Schema(example = "308 Negra Arroyo Lane, Albuquerque, New Mexico.", description = "A phone number belonging to an entity")
private String physicalAddress;

protected ContactInformation() {

}
private final String physicalAddress;

public ContactInformation(@NotNull final String email,
@NotNull final String phoneNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ public final class SeatingInformation {

@NotNull
@Schema(example = "A15", description = "The number of the seat")
private String seat;
private final String seat;
@NotNull
@Schema(example = "West", description = "Which section your seat is located in")
private String section;

protected SeatingInformation() {

}
private final String section;

public SeatingInformation(final String seat,
final String section) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Boolean getTransferable() {

}

public SeatingInformation getSeatInfo() {
public SeatingInformation getSeatInformation() {

return this.seatInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public record TicketDto(@Schema(hidden = true, description = "The UUID of the At
@NotNull @Schema(description = "What kind of ticket type it is") TicketType ticketType,
@NotNegative @Schema(example = "150", description = "The price of the ticket") Integer price,
@NotNull @Schema(example = "true", description = "If the ticket is transferable") Boolean transferable,
@NotNull @Schema(description = "The SeatingInformation of the ticket") SeatingInformation seatInfo) {
@NotNull @Schema(description = "The SeatingInformation of the ticket") SeatingInformation seatInformation) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
Expand All @@ -12,9 +11,6 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;

/**
* Exception Handler
*
Expand Down Expand Up @@ -76,18 +72,4 @@ Map<String, Object>> handleObjectNotFoundException(final ObjectNotFoundException

}

@ExceptionHandler(ConstraintViolationException.class) // does not work FIXME
protected ResponseEntity<
Map<String, Object>> handleConstraintViolationException(final ConstraintViolationException ex) {

final Set<ConstraintViolation<?>> set = ex.getConstraintViolations();

final Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("errors", set);

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 400);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
* TODO Extract stuff from the message exceptions, suppressed exceptions and the
* and make the messages more readable
*
* @author Evangelos Georgiou
*
*/
//@ControllerAdvice
@ControllerAdvice
public class PostgresExceptionHandler {

private static final String TIME_STAMP = "timeStamp";
Expand All @@ -32,10 +31,11 @@ public ResponseEntity<Map<String, Object>> handlePSQLExceptionHandler(final PSQL

final Map<String, Object> errorResponse = new HashMap<>();

errorResponse.put(MESSAGE, exc.getMessage());
errorResponse.put(MESSAGE, exc.getMessage().split(" ")[0]);
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", 400);

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 500);
return new ResponseEntity<>(errorResponse, new HttpHeaders(), 400);

}

Expand All @@ -60,11 +60,11 @@ Map<String, Object>> handleDataIntegrityViolationException(final DataIntegrityVi

final Map<String, Object> errorResponse = new HashMap<>();

errorResponse.put(MESSAGE, exc.getMessage());
errorResponse.put(MESSAGE, exc.getMessage().split(";")[2]);
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", 500);
errorResponse.put("status", 400);

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 500);
return new ResponseEntity<>(errorResponse, new HttpHeaders(), 400);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ public Collection<Attendee> findAll() {

private Attendee saveAttendee(final AttendeeDto attendee) {

final UUID attendeeUuid = attendee.uuid();
final UUID attendeeId = attendee.uuid();
final Instant now = Instant.now();
final Timestamp createdAt = Timestamp.from(now);
final Timestamp timestamp = Timestamp.from(now);
final List<UUID> ticketIds = attendee.ticketIDs() != null ? attendee.ticketIDs() : List.of();
final String firstName = attendee.firstName();
final String lastName = attendee.lastName();
final UUID[] uuids = this.convertToArray(ticketIds);
final UUID uuid = attendeeUuid != null ? attendeeUuid : UUID.randomUUID();
final UUID uuid = attendeeId != null ? attendeeId : UUID.randomUUID();

this.jdbcTemplate.update(this.attendeeQueriesProperties.getProperty(CrudQueriesOperations.SAVE.name()), uuid,
createdAt, timestamp, firstName, lastName, uuids);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private Ticket saveTicket(final TicketDto ticket) {
final TicketType ticketType = ticket.ticketType();
final Integer price = ticket.price();
final Boolean isTransferable = ticket.transferable();
final SeatingInformation seatInformation = ticket.seatInfo();
final SeatingInformation seatInformation = ticket.seatInformation();

final UUID uuid = ticketUuid != null ? ticketUuid : UUID.randomUUID();

Expand All @@ -172,7 +172,7 @@ private Ticket editTicket(final TicketDto ticket) {
final TicketType ticketType = ticket.ticketType();
final Integer price = ticket.price();
final Boolean isTransferable = ticket.transferable();
final SeatingInformation seatInformation = ticket.seatInfo();
final SeatingInformation seatInformation = ticket.seatInformation();

this.jdbcTemplate.update(this.ticketQueriesProperties.getProperty(CrudQueriesOperations.EDIT.name()), uuid,
createdAt, timestamp, eventId, ticketType.name(), price, isTransferable, seatInformation.getSeat(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

public interface IAttendeeService extends IService<Attendee, AttendeeDto> {

/**
* This method adds a ticket to the Attendee, cascades and adds the attendee to
* the {@link Event} as well.
*
* @param attendeeId
* @param ticketId
*
* @return {@link Boolean#TRUE} if the action succeeded else
* {@link Boolean#FALSE}
*/
boolean addTicket(UUID attendeeId,
UUID ticketId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

import org.com.ems.api.domainobjects.Event;
import org.com.ems.api.dto.EventDto;
import org.com.ems.services.impl.AttendeeService;

public interface IEventService extends IService<Event, EventDto> {

/**
* This method is called only from {@link AttendeeService} to add it self to the
* event.
*
* @param eventId
* @param attendeeId
*
* @return {@link Boolean#TRUE} if the action succeeded else
* {@link Boolean#FALSE}
*/
boolean addAttendee(UUID eventId,
UUID attendeeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
import java.util.Optional;
import java.util.UUID;

public interface ILookUpService<T> {
import org.com.ems.api.domainobjects.AbstractDomainObject;

public interface ILookUpService<T extends AbstractDomainObject> {

/**
* Fetches the {@link T} Domain Object
*
* @param id of the object
*
* @return {@link Optional} of the {@link T} object
*/
Optional<T> get(UUID id);

Optional<T> get(UUID uuid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,56 @@
import java.util.Optional;
import java.util.UUID;

public interface IService<T, DTO> extends ILookUpService<T> {

T add(DTO object);

import org.com.ems.api.domainobjects.AbstractDomainObject;

public interface IService<T extends AbstractDomainObject, DTO> extends ILookUpService<T> {

/**
* Adds the corresponding {@link DTO} to the service
*
* @param dto
*
* @return the {@link T} domain object
*/
T add(DTO dto);

/**
* {@inheritDoc}
*/
@Override
Optional<T> get(UUID uuid);

void delete(UUID uuid);

T edit(UUID uuid,
DTO object);

Optional<T> get(UUID id);

/**
* Deletes the {@link T} object that matches the id
*
* @param id UUID of the object that is about to be deleted
*/
void delete(UUID id);

/**
* Edits the {@link T} object
*
* @param id the existing {@link T} object's id
* @param dto the objects payload
* @return the {@link T} domain object
*/
T edit(UUID id,
DTO dto);

/**
* Fetches all the {@link T} objects available
*
* @return {@link Collection} of all the objects
*/
Collection<T> getAll();

boolean existsById(UUID objectId);
/**
* Method that tells your if the object with the specified id exists
*
* @param id of the object
* @return {@link Boolean#TRUE} if the object exists otherwise
* {@link Boolean#FALSE}
*/
boolean existsById(UUID id);

}
Loading

0 comments on commit 5c0ddc1

Please sign in to comment.