Skip to content

Commit

Permalink
Merge pull request #298 from josdem/feature/293
Browse files Browse the repository at this point in the history
[small]feature/293
  • Loading branch information
josdem authored Aug 20, 2024
2 parents 520ef3e + 3a9ed08 commit f52993d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ext {
}

group = 'com.josdem.vetlog'
version = '1.6.6'
version = '1.6.7'

configurations {
compileOnly {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/josdem/vetlog/command/AdoptionCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.josdem.vetlog.command;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -23,7 +22,6 @@
@Setter
public class AdoptionCommand implements Command {

@NotNull
private String uuid;

@Size(min = 1, max = 1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import com.josdem.vetlog.model.Pet;
import com.josdem.vetlog.service.AdoptionService;
import com.josdem.vetlog.service.PetService;
import com.josdem.vetlog.validator.AdoptionValidator;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -41,13 +44,19 @@ public class AdoptionController {

private final PetService petService;
private final AdoptionService adoptionService;
private final AdoptionValidator adoptionValidator;

@Value("${gcpUrl}")
private String gcpUrl;

@Value("${imageBucket}")
private String imageBucket;

@InitBinder("adoptionCommand")
private void initBinder(WebDataBinder binder) {
binder.addValidators(adoptionValidator);
}

@GetMapping(value = "/descriptionForAdoption")
public ModelAndView descriptionForAdoption(AdoptionCommand adoptionCommand) {
log.info("Adding description to pet with uuid: " + adoptionCommand.getUuid());
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/josdem/vetlog/validator/AdoptionValidator.java
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");
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ adoption.mobile=What is your phone number?
adoption.email.sent=An email was sent to the owner's pet in adoption, please wait for news
adoption.thanks=Hello, thanks for adopting:
adoption.wishes=We are glad you found a new family member, we hope you enjoy time together.
adoption.error.uuid.invalid=UUID format is not valid
vet.view.search.title=Search by username
vet.list=Search
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/i18n/messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ adoption.mobile=Proporciona tu teléfono
adoption.email.sent=Un correo electrónico fue enviado al propietario de la mascota en adopción, por favor espera por noticias
adoption.thanks=Hola, gracias por adoptar
adoption.wishes=Estamos felices que has encontrado un nuevo miembro de la familia, esperamos que disfruten de esta nueva etapa juntos.
adoption.error.uuid.invalid=El formato UUID no es válido

vet.view.search.title=Buscar por usuario
vet.list=Buscar
Expand Down
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");
}
}

0 comments on commit f52993d

Please sign in to comment.