From 686f722c902d3e87b62c2d63d475899b369fe414 Mon Sep 17 00:00:00 2001 From: Hannah Holzheu <hh062@hdm-stuttgart.de> Date: Fri, 8 Dec 2023 23:09:11 +0100 Subject: [PATCH] added more Tests for Search Endpoint, focus on testing different searchRequest combinations #23 --- .../models/search/PlantSpecification.java | 98 +++++++++++-------- .../models/search/PlantSpecificationTest.java | 42 +++++++- 2 files changed, 93 insertions(+), 47 deletions(-) diff --git a/src/main/java/hdm/mi/growbros/models/search/PlantSpecification.java b/src/main/java/hdm/mi/growbros/models/search/PlantSpecification.java index a7a0780..53a562f 100644 --- a/src/main/java/hdm/mi/growbros/models/search/PlantSpecification.java +++ b/src/main/java/hdm/mi/growbros/models/search/PlantSpecification.java @@ -18,50 +18,64 @@ public class PlantSpecification { private final static Logger log = LogManager.getLogger(PlantSpecification.class); public Specification<Plant> withSearchCriteria(SearchRequest searchRequest) { - return (root, query, criteriaBuilder) -> { - Collection<Predicate> predicatesAND = new ArrayList<>(); - Collection<Predicate> plantWeekONE= new ArrayList<>(); - Collection<Predicate> plantWeekTWO= new ArrayList<>(); + return (root, query, criteriaBuilder) -> { + Collection<Predicate> predicatesAND = new ArrayList<>(); + Collection<Predicate> plantWeekONE = new ArrayList<>(); + Collection<Predicate> plantWeekTWO = new ArrayList<>(); - if (StringUtils.hasText(searchRequest.getSearchTerm())) { - predicatesAND.add(criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), - "%" + searchRequest.getSearchTerm().toLowerCase() + "%")); - } - if (searchRequest.getGroundType() != null) { - predicatesAND.add(criteriaBuilder.equal(root.get("groundType"), searchRequest.getGroundType())); - } - if(searchRequest.getNutrientDemand()!=null){ - predicatesAND.add(criteriaBuilder.equal(root.get("nutrientDemand"),searchRequest.getNutrientDemand())); - } + if (StringUtils.hasText(searchRequest.getSearchTerm())) { + predicatesAND.add(criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), + "%" + searchRequest.getSearchTerm().toLowerCase() + "%")); + } + if (searchRequest.getGroundType() != null) { + predicatesAND.add(criteriaBuilder.equal(root.get("groundType"), searchRequest.getGroundType())); + } + if (searchRequest.getNutrientDemand() != null) { + predicatesAND.add(criteriaBuilder.equal(root.get("nutrientDemand"), searchRequest.getNutrientDemand())); + } - if(searchRequest.getWaterDemand()!=null){ - predicatesAND.add(criteriaBuilder.equal(root.get("waterDemand"),searchRequest.getWaterDemand())); - } - if(searchRequest.getLightingDemand()!=null){ - predicatesAND.add(criteriaBuilder.equal(root.get("lightingDemand"),searchRequest.getLightingDemand())); - } + if (searchRequest.getWaterDemand() != null) { + predicatesAND.add(criteriaBuilder.equal(root.get("waterDemand"), searchRequest.getWaterDemand())); + } + if (searchRequest.getLightingDemand() != null) { + predicatesAND.add(criteriaBuilder.equal(root.get("lightingDemand"), searchRequest.getLightingDemand())); + } - if(searchRequest.getPlantWeekStart()!=null &searchRequest.getPlantWeekEnd()!=null) { - plantWeekONE.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), searchRequest.getPlantWeekStart())); - plantWeekONE.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), searchRequest.getPlantWeekStart())); - plantWeekTWO.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), searchRequest.getPlantWeekEnd())); - plantWeekTWO.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), searchRequest.getPlantWeekEnd())); - } - if(searchRequest.getGrowthDurationMin()!=null & searchRequest.getGrowthDurationMax()!=null ){ - predicatesAND.add(criteriaBuilder.greaterThanOrEqualTo(root.get("growthDuration"),searchRequest.getGrowthDurationMin())); - predicatesAND.add(criteriaBuilder.lessThanOrEqualTo(root.get("growthDuration"),searchRequest.getGrowthDurationMax())); - } - return criteriaBuilder.and( - criteriaBuilder.or( - criteriaBuilder.and( - plantWeekONE.toArray(new Predicate[0])), - criteriaBuilder.and( - plantWeekTWO.toArray(new Predicate[0])) - ), - criteriaBuilder.and( - predicatesAND.toArray(new Predicate[0])) - ); - }; - } + if (searchRequest.getPlantWeekStart() != null && searchRequest.getPlantWeekEnd() != null) { + int start = searchRequest.getPlantWeekStart(); + int end = searchRequest.getPlantWeekEnd(); + if(start < end) { + plantWeekONE.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), start)); + plantWeekONE.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), start)); + plantWeekTWO.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), end)); + plantWeekTWO.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), end)); + } else { + //TODO fixen + //start: 40 + //end: 10 + // -> end: 62 + plantWeekONE.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), start)); + plantWeekONE.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), start)); + plantWeekTWO.add(criteriaBuilder.lessThanOrEqualTo(root.get("plantWeekStart"), end)); + plantWeekTWO.add(criteriaBuilder.greaterThanOrEqualTo(root.get("plantWeekEnd"), end)); + } + } + + if (searchRequest.getGrowthDurationMin() != null && searchRequest.getGrowthDurationMax() != null) { + predicatesAND.add(criteriaBuilder.greaterThanOrEqualTo(root.get("growthDuration"), searchRequest.getGrowthDurationMin())); + predicatesAND.add(criteriaBuilder.lessThanOrEqualTo(root.get("growthDuration"), searchRequest.getGrowthDurationMax())); + } + return criteriaBuilder.and( + criteriaBuilder.or( + criteriaBuilder.and( + plantWeekONE.toArray(new Predicate[0])), + criteriaBuilder.and( + plantWeekTWO.toArray(new Predicate[0])) + ), + criteriaBuilder.and( + predicatesAND.toArray(new Predicate[0])) + ); + }; + } } diff --git a/src/test/java/hdm/mi/growbros/models/search/PlantSpecificationTest.java b/src/test/java/hdm/mi/growbros/models/search/PlantSpecificationTest.java index cb35c3d..02f0533 100644 --- a/src/test/java/hdm/mi/growbros/models/search/PlantSpecificationTest.java +++ b/src/test/java/hdm/mi/growbros/models/search/PlantSpecificationTest.java @@ -6,10 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import hdm.mi.growbros.models.plant.*; import hdm.mi.growbros.repositories.PlantRepository; import lombok.RequiredArgsConstructor; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; @@ -148,11 +145,46 @@ class PlantSpecificationTest { assertEquals(List.of(plant2,plant3),plantRepository.findAll(spec)); } + @Test + public void test_searchTerm_WaterDemand_1(){ + SearchRequest searchRequest= SearchRequest.builder().searchTerm("Postelein").waterDemand(WaterDemand.WET).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(plant3),plantRepository.findAll(spec)); + } + @Test + public void test_searchTerm_WaterDemand_2(){ + SearchRequest searchRequest= SearchRequest.builder().searchTerm("Salat").waterDemand(WaterDemand.WET).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(),plantRepository.findAll(spec)); + } + @Test + public void test_WaterDemand_NutrientDemand_1(){ + SearchRequest searchRequest= SearchRequest.builder().waterDemand(WaterDemand.WET).nutrientDemand(NutrientDemand.LOW).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(plant2),plantRepository.findAll(spec)); + } + @Test + public void test_WaterDemand_NutrientDemand_2(){ + SearchRequest searchRequest= SearchRequest.builder().waterDemand(WaterDemand.WET).nutrientDemand(NutrientDemand.HIGH).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(plant3),plantRepository.findAll(spec)); + } + @Test + public void test_WaterDemand_NutrientDemand_3(){ + SearchRequest searchRequest= SearchRequest.builder().waterDemand(WaterDemand.DRY).nutrientDemand(NutrientDemand.HIGH).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(),plantRepository.findAll(spec)); + } - + @Test + public void test_WaterDemand_NutrientDemand_groundType(){ + SearchRequest searchRequest= SearchRequest.builder().waterDemand(WaterDemand.DRY).nutrientDemand(NutrientDemand.LOW).groundType(GroundType.HEAVY).build(); + var spec= plantSpecification.withSearchCriteria(searchRequest); + assertEquals(List.of(plant1),plantRepository.findAll(spec)); + } } -- GitLab