Skip to content
Snippets Groups Projects
Commit 679c23e8 authored by Lukas Karsch's avatar Lukas Karsch
Browse files

#3

Started work on unit tests for Garden. Fix custom queries (implementation still not finished). Exclude createdAt date for equals and hashcode in GardenEntry
parent 4f08eda2
No related branches found
No related tags found
1 merge request!12#3 merge fully implemented garden branch into main
...@@ -3,16 +3,19 @@ package hdm.mi.growbros.models; ...@@ -3,16 +3,19 @@ package hdm.mi.growbros.models;
import hdm.mi.growbros.models.plant.Plant; import hdm.mi.growbros.models.plant.Plant;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.util.Date; import java.util.Date;
import java.util.Objects;
@Entity @Entity
@EntityListeners(AuditingEntityListener.class) @EntityListeners(AuditingEntityListener.class)
@Data @Getter
@Setter
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class GardenEntry { public class GardenEntry {
...@@ -28,4 +31,17 @@ public class GardenEntry { ...@@ -28,4 +31,17 @@ public class GardenEntry {
@CreatedDate @CreatedDate
private Date createdAt; private Date createdAt;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GardenEntry that = (GardenEntry) o;
return Objects.equals(id, that.id) && Objects.equals(plant, that.plant);
}
@Override
public int hashCode() {
return Objects.hash(id, plant);
}
} }
...@@ -8,12 +8,14 @@ import jakarta.validation.constraints.Max; ...@@ -8,12 +8,14 @@ import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.URL; import org.hibernate.validator.constraints.URL;
//careful! weeks are indexed at 0 -> max value is 51. //careful! weeks are indexed at 0 -> max value is 51.
@Data @Data
@Builder
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Entity @Entity
......
...@@ -14,25 +14,25 @@ public interface GardenRepository extends JpaRepository<GardenEntry, Long> { ...@@ -14,25 +14,25 @@ public interface GardenRepository extends JpaRepository<GardenEntry, Long> {
SELECT ge FROM GardenEntry ge SELECT ge FROM GardenEntry ge
ORDER BY ORDER BY
CASE CASE
WHEN :week >= ge.plant.harvestWeekStart WHEN :week <= ge.plant.harvestWeekStart
THEN ABS(:week - ge.plant.harvestWeekStart) THEN ABS(:week - ge.plant.harvestWeekStart)
ELSE ABS(52 + :week - ge.plant.harvestWeekStart) ELSE ABS(52 + :week - ge.plant.harvestWeekStart)
END, END,
ge.plant.name ge.plant.name
ASC ASC
""") """)
List<GardenEntry> findNearestHarvestWeek(@Param("week") int currentWeek); List<GardenEntry> findAllByNearestHarvestWeek(@Param("week") int currentWeek);
@Query(""" @Query("""
SELECT ge FROM GardenEntry ge SELECT ge FROM GardenEntry ge
ORDER BY ORDER BY
CASE CASE
WHEN :week >= ge.plant.plantWeekStart WHEN :week <= ge.plant.plantWeekStart
THEN ABS(:week - ge.plant.plantWeekStart) THEN ABS(:week - ge.plant.plantWeekStart)
ELSE ABS(52 + :week - ge.plant.plantWeekStart) ELSE ABS(52 + :week - ge.plant.plantWeekStart)
END, END,
ge.plant.name ge.plant.name
ASC ASC
""") """)
List<GardenEntry> findNearestPlantingWeek(@Param("week") int currentWeek); List<GardenEntry> findAllByNearestPlantingWeek(@Param("week") int currentWeek);
} }
...@@ -63,13 +63,13 @@ public class GardenService { ...@@ -63,13 +63,13 @@ public class GardenService {
private List<Plant> getAllPlantsOrderedByNextPossiblePlantDate() { private List<Plant> getAllPlantsOrderedByNextPossiblePlantDate() {
return mapGardenEntriesToPlants( return mapGardenEntriesToPlants(
gardenRepository.findNearestPlantingWeek(DateTimeUtils.getCurrentWeekForToday()) gardenRepository.findAllByNearestPlantingWeek(DateTimeUtils.getCurrentWeekForToday())
); );
} }
private List<Plant> getAllPlantsOrderedByNextPossibleHarvest() { private List<Plant> getAllPlantsOrderedByNextPossibleHarvest() {
return mapGardenEntriesToPlants( return mapGardenEntriesToPlants(
gardenRepository.findNearestHarvestWeek(DateTimeUtils.getCurrentWeekForToday()) gardenRepository.findAllByNearestHarvestWeek(DateTimeUtils.getCurrentWeekForToday())
); );
} }
......
package hdm.mi.growbros.repositories;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.models.plant.Plant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
//TODO: Test for when the current week is in between plant/harvest start and end date
@SpringBootTest
class GardenRepositoryTest {
@Autowired
private GardenRepository underTest;
private static GardenEntry ge1, ge2, ge3;
@BeforeAll
public static void setupAll(@Autowired GardenRepository underTest, @Autowired PlantRepository plantRepository) {
Plant p1 = Plant.builder()
.name("Early plant")
.plantWeekStart(1)
.plantWeekEnd(3)
.harvestWeekStart(10)
.harvestWeekEnd(15)
.build();
Plant p2 = Plant.builder()
.name("Medium late plant")
.plantWeekStart(4)
.plantWeekEnd(6)
.harvestWeekStart(18)
.harvestWeekEnd(20)
.build();
Plant p3 = Plant.builder()
.name("Late plant")
.plantWeekStart(10)
.plantWeekEnd(15)
.harvestWeekStart(25)
.harvestWeekEnd(30)
.build();
ge1 = new GardenEntry(0L, p1, null);
ge2 = new GardenEntry(0L, p2, null);
ge3 = new GardenEntry(0L, p3, null);
plantRepository.saveAll(List.of(p1, p2, p3));
underTest.saveAll(List.of(ge1, ge2, ge3));
}
@Test
void testNearestHarvestWeek() {
//when
var result = underTest.findAllByNearestHarvestWeek(1);
//then
assertEquals(List.of(ge1, ge2, ge3), result);
}
@Test
void findNearestPlantingWeek() {
//when
var result = underTest.findAllByNearestPlantingWeek(1);
//then
assertEquals(List.of(ge1, ge2, ge3), result);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment