-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from josdem/feature/293
[small]feature/293
- Loading branch information
Showing
7 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/josdem/vetlog/validator/AdoptionValidator.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,41 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
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 | ||
http://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 com.josdem.vetlog.validator; | ||
|
||
import com.josdem.vetlog.command.AdoptionCommand; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.Validator; | ||
|
||
@Component | ||
public class AdoptionValidator implements Validator { | ||
|
||
private static final String REGEX = "[a-f0-9]{8}(?:-[a-f0-9]{4}){4}[a-f0-9]{8}"; | ||
|
||
@Override | ||
public boolean supports(Class<?> clazz) { | ||
return AdoptionCommand.class.equals(clazz); | ||
} | ||
|
||
@Override | ||
public void validate(Object target, Errors errors) { | ||
AdoptionCommand adoptionCommand = (AdoptionCommand) target; | ||
if (!adoptionCommand.getUuid().matches(REGEX)) { | ||
errors.rejectValue("uuid", "adoption.error.uuid.invalid"); | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/test/java/com/josdem/vetlog/validator/AdoptionValidatorTest.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,57 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
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 | ||
http://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 com.josdem.vetlog.validator; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.josdem.vetlog.command.AdoptionCommand; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
import org.springframework.validation.Errors; | ||
|
||
@Slf4j | ||
class AdoptionValidatorTest { | ||
|
||
private static final String UUID = "21740c48-13f9-4bf4-a8a2-ef61b7d3cdc3"; | ||
private final AdoptionValidator adoptionValidator = new AdoptionValidator(); | ||
|
||
@Test | ||
@DisplayName("rejecting adoption command with invalid uuid") | ||
void shouldRejectDueToInvalidUuid(TestInfo testInfo) { | ||
log.info("Running: {}", testInfo.getDisplayName()); | ||
AdoptionCommand adoptionCommand = new AdoptionCommand(); | ||
Errors errors = mock(Errors.class); | ||
adoptionCommand.setUuid("uuid"); | ||
adoptionValidator.validate(adoptionCommand, errors); | ||
verify(errors).rejectValue("uuid", "adoption.error.uuid.invalid"); | ||
} | ||
|
||
@Test | ||
@DisplayName("validating adoption command with valid uuid") | ||
void shouldValidateWithValidUuid(TestInfo testInfo) { | ||
log.info("Running: {}", testInfo.getDisplayName()); | ||
AdoptionCommand adoptionCommand = new AdoptionCommand(); | ||
Errors errors = mock(Errors.class); | ||
adoptionCommand.setUuid(UUID); | ||
adoptionValidator.validate(adoptionCommand, errors); | ||
verify(errors, never()).rejectValue("uuid", "adoption.error.uuid.invalid"); | ||
} | ||
} |