-
-
Notifications
You must be signed in to change notification settings - Fork 27
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 #340 from josdem/feature/305
[small]feature/305
- Loading branch information
Showing
8 changed files
with
282 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/josdem/vetlog/repository/VaccinationRepository.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,25 @@ | ||
/* | ||
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.repository; | ||
|
||
import com.josdem.vetlog.model.Vaccination; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface VaccinationRepository extends JpaRepository<Vaccination, Long> { | ||
|
||
Vaccination save(Vaccination vaccination); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/josdem/vetlog/service/VaccinationService.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,23 @@ | ||
/* | ||
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.service; | ||
|
||
import com.josdem.vetlog.model.Pet; | ||
|
||
public interface VaccinationService { | ||
void save(Pet pet); | ||
} |
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
87 changes: 87 additions & 0 deletions
87
src/main/java/com/josdem/vetlog/service/impl/VaccinationServiceImpl.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,87 @@ | ||
/* | ||
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.service.impl; | ||
|
||
import com.josdem.vetlog.enums.PetType; | ||
import com.josdem.vetlog.enums.VaccinationStatus; | ||
import com.josdem.vetlog.exception.BusinessException; | ||
import com.josdem.vetlog.model.Pet; | ||
import com.josdem.vetlog.model.Vaccination; | ||
import com.josdem.vetlog.repository.VaccinationRepository; | ||
import com.josdem.vetlog.service.VaccinationService; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class VaccinationServiceImpl implements VaccinationService { | ||
|
||
private static final String DA2PP = "DA2PP"; | ||
private static final String DEWORMING = "Deworming"; | ||
private static final String LEPTOSPIROSIS = "Leptospirosis"; | ||
private static final String RABIES = "Rabies"; | ||
|
||
private final VaccinationRepository vaccinationRepository; | ||
|
||
@Override | ||
public void save(Pet pet) { | ||
if (pet.getBreed().getType() != PetType.DOG) { | ||
throw new BusinessException("Only dogs are allowed"); | ||
} | ||
|
||
Long weeks = ChronoUnit.WEEKS.between(pet.getBirthDate(), LocalDateTime.now()); | ||
|
||
switch (weeks.intValue()) { | ||
case 0, 1, 2, 3, 4, 5 -> log.info("No vaccination needed"); | ||
case 6, 7, 8, 9 -> { | ||
log.info("First vaccination"); | ||
registerVaccination(DA2PP, pet); | ||
registerVaccination(DEWORMING, pet); | ||
} | ||
case 10, 11, 12, 13 -> { | ||
log.info("Second vaccination"); | ||
registerVaccination(DA2PP, pet); | ||
registerVaccination(DEWORMING, pet); | ||
registerVaccination(LEPTOSPIROSIS, pet); | ||
} | ||
case 14, 15, 16 -> { | ||
log.info("Third vaccination"); | ||
registerVaccination(DA2PP, pet); | ||
registerVaccination(DEWORMING, pet); | ||
registerVaccination(LEPTOSPIROSIS, pet); | ||
registerVaccination(RABIES, pet); | ||
} | ||
default -> { | ||
log.info("Annual vaccination"); | ||
registerVaccination(DA2PP, pet); | ||
registerVaccination(DEWORMING, pet); | ||
registerVaccination(LEPTOSPIROSIS, pet); | ||
registerVaccination(RABIES, pet); | ||
registerVaccination("Canine influenza", pet); | ||
} | ||
} | ||
} | ||
|
||
private void registerVaccination(String name, Pet pet) { | ||
vaccinationRepository.save(new Vaccination(null, name, LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V1.8__adding_pet_vaccination.sql
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,2 @@ | ||
ALTER TABLE vaccination ADD COLUMN pet_id bigint DEFAULT NULL; | ||
ALTER TABLE vaccination ADD CONSTRAINT vaccination_id_constraint FOREIGN KEY (`pet_id`) REFERENCES pet (`id`); |
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
122 changes: 122 additions & 0 deletions
122
src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.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,122 @@ | ||
/* | ||
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.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.josdem.vetlog.enums.PetType; | ||
import com.josdem.vetlog.enums.VaccinationStatus; | ||
import com.josdem.vetlog.exception.BusinessException; | ||
import com.josdem.vetlog.model.Breed; | ||
import com.josdem.vetlog.model.Pet; | ||
import com.josdem.vetlog.model.Vaccination; | ||
import com.josdem.vetlog.repository.VaccinationRepository; | ||
import com.josdem.vetlog.service.impl.VaccinationServiceImpl; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
@Slf4j | ||
class VaccinationServiceTest { | ||
|
||
private VaccinationService vaccinationService; | ||
|
||
@Mock | ||
private VaccinationRepository vaccinationRepository; | ||
|
||
private final Pet pet = new Pet(); | ||
|
||
@BeforeEach | ||
void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
vaccinationService = new VaccinationServiceImpl(vaccinationRepository); | ||
pet.setBreed(new Breed()); | ||
} | ||
|
||
@Test | ||
@DisplayName("not saving a pet if it is not a dog") | ||
void shouldNotSavePetIfItIsNotADog(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.CAT); | ||
assertThrows(BusinessException.class, () -> vaccinationService.save(pet)); | ||
} | ||
|
||
@Test | ||
@DisplayName("saving first vaccination") | ||
void shouldSaveFirstVaccination(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.DOG); | ||
pet.setBirthDate(LocalDateTime.now().minusWeeks(6)); | ||
vaccinationService.save(pet); | ||
verify(vaccinationRepository, times(2)) | ||
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
|
||
@Test | ||
@DisplayName("saving second vaccination") | ||
void shouldSaveSecondVaccination(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.DOG); | ||
pet.setBirthDate(LocalDateTime.now().minusWeeks(10)); | ||
vaccinationService.save(pet); | ||
verify(vaccinationRepository, times(3)) | ||
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
|
||
@Test | ||
@DisplayName("saving third vaccination") | ||
void shouldSaveThirdVaccination(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.DOG); | ||
pet.setBirthDate(LocalDateTime.now().minusWeeks(14)); | ||
vaccinationService.save(pet); | ||
verify(vaccinationRepository, times(4)) | ||
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
|
||
@Test | ||
@DisplayName("saving annual vaccination") | ||
void shouldSaveAnnualVaccination(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.DOG); | ||
pet.setBirthDate(LocalDateTime.now().minusWeeks(20)); | ||
vaccinationService.save(pet); | ||
verify(vaccinationRepository, times(5)) | ||
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
|
||
@Test | ||
@DisplayName("not saving vaccination due is not old enough") | ||
void shouldNotSaveVaccinationDueToNotOldEnough(TestInfo testInfo) { | ||
log.info("Test: {}", testInfo.getDisplayName()); | ||
pet.getBreed().setType(PetType.DOG); | ||
pet.setBirthDate(LocalDateTime.now().minusWeeks(1)); | ||
vaccinationService.save(pet); | ||
verify(vaccinationRepository, never()) | ||
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); | ||
} | ||
} |