Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[small]Feature/377 #389

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.9.4'
version = '2.0.0'

configurations {
compileOnly {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.josdem.vetlog.command.PetCommand;
import com.josdem.vetlog.enums.PetStatus;
import com.josdem.vetlog.enums.PetType;
import com.josdem.vetlog.enums.VaccinationStatus;
import com.josdem.vetlog.model.User;
import com.josdem.vetlog.service.BreedService;
import com.josdem.vetlog.service.LocaleService;
Expand Down Expand Up @@ -182,7 +183,7 @@ public ModelAndView delete(@RequestParam("uuid") String uuid) {
private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) {
var user = userService.getCurrentUser();
var pets = petService.getPetsByUser(user);
pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinationsByPet(pet)));
pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)));
modelAndView.addObject("pets", pets);
modelAndView.addObject(GCP_IMAGE_URL, gcpUrl + imageBucket + "/");
modelAndView.addObject("defaultImage", defaultImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.josdem.vetlog.controller;

import com.josdem.vetlog.command.UsernameCommand;
import com.josdem.vetlog.enums.VaccinationStatus;
import com.josdem.vetlog.service.PetService;
import com.josdem.vetlog.service.UserService;
import com.josdem.vetlog.service.VaccinationService;
Expand Down Expand Up @@ -63,7 +64,7 @@ ModelAndView search(@Valid UsernameCommand command) {
var modelAndView = new ModelAndView("vet/list");
var user = userService.getByUsername(command.getUsername());
var pets = petService.getPetsByUser(user);
pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinationsByPet(pet)));
pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)));
modelAndView.addObject("pets", pets);
modelAndView.addObject("gcpImageUrl", gcpUrl + imageBucket + "/");
modelAndView.addObject("defaultImage", defaultImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.josdem.vetlog.service;

import com.josdem.vetlog.enums.VaccinationStatus;
import com.josdem.vetlog.model.Pet;
import com.josdem.vetlog.model.Vaccination;
import java.util.List;
Expand All @@ -24,4 +25,6 @@ public interface VaccinationService {
void save(Pet pet);

List<Vaccination> getVaccinationsByPet(Pet pet);

List<Vaccination> getVaccinesByStatus(Pet pet, VaccinationStatus status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -54,4 +55,11 @@ public void save(Pet pet) {
public List<Vaccination> getVaccinationsByPet(Pet pet) {
return vaccinationRepository.findAllByPet(pet);
}

@Override
public List<Vaccination> getVaccinesByStatus(Pet pet, VaccinationStatus status) {
return getVaccinationsByPet(pet).stream()
.filter(vaccination -> vaccination.getStatus().equals(status))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package com.josdem.vetlog.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
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 static org.mockito.Mockito.when;

import com.josdem.vetlog.enums.PetType;
import com.josdem.vetlog.enums.VaccinationStatus;
Expand All @@ -36,6 +38,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -55,22 +58,16 @@ class VaccinationServiceTest {
@Mock
private VaccinationRepository vaccinationRepository;

private DogVaccinationStrategy dogVaccinationStrategy;

private CatVaccinationStrategy catVaccinationStrategy;

private Map<PetType, VaccinationStrategy> vaccinationStrategies;

private final Pet pet = new Pet();

@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);

dogVaccinationStrategy = new DogVaccinationStrategy(vaccinationRepository);
catVaccinationStrategy = new CatVaccinationStrategy(vaccinationRepository);
DogVaccinationStrategy dogVaccinationStrategy = new DogVaccinationStrategy(vaccinationRepository);
CatVaccinationStrategy catVaccinationStrategy = new CatVaccinationStrategy(vaccinationRepository);

vaccinationStrategies = new HashMap<>();
Map<PetType, VaccinationStrategy> vaccinationStrategies = new HashMap<>();
vaccinationStrategies.put(PetType.DOG, dogVaccinationStrategy);
vaccinationStrategies.put(PetType.CAT, catVaccinationStrategy);

Expand Down Expand Up @@ -108,4 +105,16 @@ void shouldNotSaveVaccinationDueToNotOldEnough(TestInfo testInfo) {
verify(vaccinationRepository, never())
.save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet));
}

@Test
@DisplayName("getting vaccines in Pending status")
void shouldGetVaccinesInPendingStatus(TestInfo testInfo) {
log.info("Test: {}", testInfo.getDisplayName());
when(vaccinationRepository.findAllByPet(pet))
.thenReturn(List.of(
new Vaccination(1L, "DA2PP", LocalDate.now(), VaccinationStatus.PENDING, pet),
new Vaccination(2L, "Deworming", LocalDate.now(), VaccinationStatus.APPLIED, pet)));
final var vaccinesInPendingStatus = vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING);
assertEquals(1, vaccinesInPendingStatus.size());
}
}
Loading