-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add last timestamp in a day constraint
- Loading branch information
Mihael Cacko
committed
Oct 24, 2023
1 parent
a7f51b3
commit e10c405
Showing
8 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...dation-api/src/main/java/net/croz/nrich/validation/api/constraint/LastTimestampInDay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.api.constraint; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* The annotated element must be before end of the day | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Repeatable(LastTimestampInDay.List.class) | ||
@Documented | ||
@Constraint(validatedBy = {}) | ||
public @interface LastTimestampInDay { | ||
|
||
String message() default "{nrich.constraint.lastTimestampInDay.invalid.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* Defines several {@link LastTimestampInDay.List} annotations on the same element. | ||
* | ||
* @see LastTimestampInDay.List | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
LastTimestampInDay[] value(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...main/java/net/croz/nrich/validation/constraint/validator/LastTimestampInDayValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.validator; | ||
|
||
import net.croz.nrich.validation.api.constraint.LastTimestampInDay; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.temporal.ChronoField; | ||
|
||
public class LastTimestampInDayValidator implements ConstraintValidator<LastTimestampInDay, LocalDate> { | ||
|
||
private LocalDateTime lastTimestampInDay; | ||
|
||
@Override | ||
public void initialize(LastTimestampInDay constraintAnnotation) { | ||
lastTimestampInDay = LocalDateTime.now().with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(LocalDate value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return value.atTime(LocalTime.now()).isBefore(lastTimestampInDay); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...rc/test/java/net/croz/nrich/validation/constraint/stub/LastTimestampInDayTestRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.croz.nrich.validation.constraint.stub; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import net.croz.nrich.validation.api.constraint.LastTimestampInDay; | ||
|
||
import java.time.LocalDate; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class LastTimestampInDayTestRequest { | ||
|
||
@LastTimestampInDay | ||
private final LocalDate date; | ||
} |
58 changes: 58 additions & 0 deletions
58
.../java/net/croz/nrich/validation/constraint/validator/LastTimestampInDayValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.croz.nrich.validation.constraint.validator; | ||
|
||
import net.croz.nrich.validation.ValidationTestConfiguration; | ||
import net.croz.nrich.validation.constraint.stub.LastTimestampInDayTestRequest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import java.time.LocalDate; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringJUnitConfig(ValidationTestConfiguration.class) | ||
class LastTimestampInDayValidatorTest { | ||
|
||
@Autowired | ||
private Validator validator; | ||
|
||
@Test | ||
void shouldNotReportErrorForNullValue() { | ||
// given | ||
LastTimestampInDayTestRequest request = new LastTimestampInDayTestRequest(null); | ||
|
||
// when | ||
Set<ConstraintViolation<LastTimestampInDayTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldNotReportErrorDateIsAfterMinDate() { | ||
// given | ||
LastTimestampInDayTestRequest request = new LastTimestampInDayTestRequest(LocalDate.of(2023, 10, 23)); | ||
|
||
// when | ||
Set<ConstraintViolation<LastTimestampInDayTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldReportErrorWhenDateIsBeforeMinDate() { | ||
// given | ||
LastTimestampInDayTestRequest request = new LastTimestampInDayTestRequest(LocalDate.now().plusDays(1)); | ||
|
||
// when | ||
Set<ConstraintViolation<LastTimestampInDayTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isNotEmpty(); | ||
} | ||
|
||
} |