Skip to content

Commit

Permalink
Merge pull request #340 from josdem/feature/305
Browse files Browse the repository at this point in the history
[small]feature/305
  • Loading branch information
josdem authored Sep 20, 2024
2 parents 23cce57 + 9463acf commit 4e86cf6
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/com/josdem/vetlog/model/Vaccination.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -34,6 +38,7 @@
@Getter
@Setter
@ToString
@AllArgsConstructor
public class Vaccination {

@Id
Expand All @@ -49,4 +54,8 @@ public class Vaccination {
@Enumerated(STRING)
@Column(nullable = false)
private VaccinationStatus status;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pet_id")
private Pet pet;
}
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 src/main/java/com/josdem/vetlog/service/VaccinationService.java
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.josdem.vetlog.service.LocaleService;
import com.josdem.vetlog.service.PetImageService;
import com.josdem.vetlog.service.PetService;
import com.josdem.vetlog.service.VaccinationService;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
Expand All @@ -47,13 +48,15 @@ public class PetServiceImpl implements PetService {
private final UserRepository userRepository;
private final AdoptionRepository adoptionRepository;
private final LocaleService localeService;
private final VaccinationService vaccinationService;

@Transactional
public Pet save(Command command, User user) throws IOException {
var pet = petBinder.bindPet(command);
pet.setUser(user);
petImageService.attachFile(command);
petRepository.save(pet);
vaccinationService.save(pet);
return pet;
}

Expand Down
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));
}
}
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`);
12 changes: 11 additions & 1 deletion src/test/java/com/josdem/vetlog/service/PetServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class PetServiceTest {
@Mock
private LocaleService localeService;

@Mock
private VaccinationService vaccinationService;

private User user;
private User adopter;
private Pet pet;
Expand All @@ -85,7 +88,13 @@ void setup() {
adopter = new User();
pet = new Pet();
service = new PetServiceImpl(
petBinder, petRepository, petImageService, userRepository, adoptionRepository, localeService);
petBinder,
petRepository,
petImageService,
userRepository,
adoptionRepository,
localeService,
vaccinationService);
}

@Test
Expand All @@ -96,6 +105,7 @@ void shouldSavePet(TestInfo testInfo) throws IOException {
when(petBinder.bindPet(command)).thenReturn(pet);
service.save(command, user);
verify(petRepository).save(Mockito.isA(Pet.class));
verify(vaccinationService).save(Mockito.isA(Pet.class));
}

@Test
Expand Down
122 changes: 122 additions & 0 deletions src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.java
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));
}
}

0 comments on commit 4e86cf6

Please sign in to comment.