1 | package io.github.marcopaglio.booking.validator.restricted; | |
2 | ||
3 | import java.time.LocalDate; | |
4 | import java.time.format.DateTimeParseException; | |
5 | import java.util.UUID; | |
6 | import java.util.regex.Pattern; | |
7 | ||
8 | import io.github.marcopaglio.booking.validator.ReservationValidator; | |
9 | ||
10 | /** | |
11 | * An implementation of validator for reservation entities that verifies if parameters are not | |
12 | * null neither date string contains non-valid characters, or are in a non-valid format | |
13 | * or out of range. | |
14 | * | |
15 | * @see <a href="../model/Reservation.html">Reservation</a> | |
16 | */ | |
17 | public class RestrictedReservationValidator implements ReservationValidator{ | |
18 | /** | |
19 | * Regular expression for stating other characters except the numeric and the dash ones. | |
20 | */ | |
21 | private static final Pattern notOnlyNumeric = Pattern.compile("[^\\d\\-]"); | |
22 | ||
23 | /** | |
24 | * Checks if clientId is a not null identifier for the creation of a reservation entity, | |
25 | * and returns it. | |
26 | * | |
27 | * @param clientId the associated client identifier to evaluate. | |
28 | * @return a valid {@code UUID} of {@code clientId}. | |
29 | * @throws IllegalArgumentException if {@code clientId} is null. | |
30 | */ | |
31 | @Override | |
32 | public UUID validateClientId(UUID clientId) throws IllegalArgumentException { | |
33 |
1
1. validateClientId : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkNotNull → KILLED |
checkNotNull(clientId, "client identifier"); |
34 |
1
1. validateClientId : replaced return value with null for io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::validateClientId → KILLED |
return clientId; |
35 | } | |
36 | ||
37 | /** | |
38 | * Checks if stringDate is valid as date for a reservation entity, and returns it. | |
39 | * | |
40 | * @param stringDate the string date to evaluate. | |
41 | * @return a valid {@code LocalDate} of {@code date}. | |
42 | * @throws IllegalArgumentException if {@code stringDate} is null, | |
43 | * or it contains non-valid characters, | |
44 | * or its format is not valid, | |
45 | * or it is out of range. | |
46 | */ | |
47 | @Override | |
48 | public LocalDate validateDate(String stringDate) throws IllegalArgumentException { | |
49 |
1
1. validateDate : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkDateValidity → KILLED |
checkDateValidity(stringDate, "date"); |
50 |
1
1. validateDate : replaced return value with null for io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::validateDate → KILLED |
return LocalDate.parse(stringDate); |
51 | } | |
52 | ||
53 | /** | |
54 | * Checks if the string is a valid date that is not null, and contains | |
55 | * only numeric and dash characters in the right format. | |
56 | * | |
57 | * @param stringDate the string to evaluate. | |
58 | * @param inputName the role of the string in the reservation's context. | |
59 | * @throws IllegalArgumentException if {@code stringDate} is a null string | |
60 | * or {@code stringDate} contains non-numeric characters | |
61 | * or {@code stringDate} has a wrong format | |
62 | * or {@code stringDate} is out of range. | |
63 | */ | |
64 | private void checkDateValidity(String stringDate, String inputName) | |
65 | throws IllegalArgumentException { | |
66 |
1
1. checkDateValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkNotNull → KILLED |
checkNotNull(stringDate, inputName); |
67 |
1
1. checkDateValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkOnlyNumeric → KILLED |
checkOnlyNumeric(stringDate, inputName); |
68 |
1
1. checkDateValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkDateFormat → KILLED |
checkDateFormat(stringDate, inputName); |
69 |
1
1. checkDateValidity : removed call to io/github/marcopaglio/booking/validator/restricted/RestrictedReservationValidator::checkDateInRange → KILLED |
checkDateInRange(stringDate); |
70 | } | |
71 | ||
72 | /** | |
73 | * Checks if the object is not null. | |
74 | * | |
75 | * @param o the object to evaluate. | |
76 | * @param inputName the role of the object in the reservation's context. | |
77 | * @throws IllegalArgumentException if {@code o} is null. | |
78 | */ | |
79 | private void checkNotNull(Object o, String inputName) throws IllegalArgumentException { | |
80 |
1
1. checkNotNull : removed conditional - replaced equality check with false → KILLED |
if (o == null) |
81 | throw new IllegalArgumentException( | |
82 | "Reservation needs a not null " + inputName + "."); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Checks if the string contains only accepted characters that is | |
87 | * numeric digits and dash separators {@code -}. | |
88 | * | |
89 | * @param stringDate the string to evaluate. | |
90 | * @param inputName the role of the string in the reservation's context. | |
91 | * @throws IllegalArgumentException if {@code stringDate} contains non-valid characters. | |
92 | */ | |
93 | private void checkOnlyNumeric(String stringDate, String inputName) | |
94 | throws IllegalArgumentException { | |
95 |
1
1. checkOnlyNumeric : removed conditional - replaced equality check with false → KILLED |
if (notOnlyNumeric.matcher(stringDate).find()) |
96 | throw new IllegalArgumentException( | |
97 | "Reservation's " + inputName + " can contain only numeric characters."); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Check if the string has the format aaaa-mm-dd. | |
102 | * | |
103 | * @param stringDate the string to evaluate. | |
104 | * @param inputName the role of the string in the reservation's context. | |
105 | * @throws IllegalArgumentException if {@code stringDate} has a different format from aaaa-mm-gg. | |
106 | */ | |
107 | private void checkDateFormat(String stringDate, String inputName) | |
108 | throws IllegalArgumentException { | |
109 | String[] arrOfStr = stringDate.split("-"); | |
110 |
1
1. checkDateFormat : removed conditional - replaced equality check with false → KILLED |
if (arrOfStr.length != 3 |
111 |
1
1. checkDateFormat : removed conditional - replaced equality check with false → KILLED |
|| arrOfStr[0].length() != 4 |
112 |
1
1. checkDateFormat : removed conditional - replaced equality check with false → KILLED |
|| arrOfStr[1].length() != 2 |
113 |
1
1. checkDateFormat : removed conditional - replaced equality check with false → KILLED |
|| arrOfStr[2].length() != 2) |
114 | throw new IllegalArgumentException( | |
115 | "Reservation needs a " + inputName + " in format aaaa-mm-dd."); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Check if the string has data values in the ranges. | |
120 | * | |
121 | * @param stringDate the date string to evaluate. | |
122 | * @throws IllegalArgumentException if {@code stringDate} is out of ranges. | |
123 | */ | |
124 | private void checkDateInRange(String stringDate) throws IllegalArgumentException { | |
125 | try { | |
126 | LocalDate.parse(stringDate); | |
127 | } catch (DateTimeParseException e) { | |
128 | throw new IllegalArgumentException(e.getMessage()); | |
129 | } | |
130 | } | |
131 | } | |
Mutations | ||
33 |
1.1 |
|
34 |
1.1 |
|
49 |
1.1 |
|
50 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 |
|
69 |
1.1 |
|
80 |
1.1 |
|
95 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
112 |
1.1 |
|
113 |
1.1 |