From 44af7d2d80dfb35da1c2766ae9e13ac2fa9bc7ab Mon Sep 17 00:00:00 2001 From: Sasha <97806343+FergeSS@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:41:57 +0300 Subject: [PATCH] 100 add sort for crop rotations (#105) * 100 add sort for crop rotations * 100 add test for sort --- .../v2/repositories/CropRotationRepositoryV2.java | 3 ++- .../fields/v2/services/CropRotationServiceV2.java | 4 +++- .../java/agroscience/fields/CropRotationV2Test.java | 11 ++++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/agroscience/fields/v2/repositories/CropRotationRepositoryV2.java b/src/main/java/agroscience/fields/v2/repositories/CropRotationRepositoryV2.java index 23df0e8..b20b804 100644 --- a/src/main/java/agroscience/fields/v2/repositories/CropRotationRepositoryV2.java +++ b/src/main/java/agroscience/fields/v2/repositories/CropRotationRepositoryV2.java @@ -3,9 +3,10 @@ import agroscience.fields.v2.entities.CropRotationV2; import java.util.List; import java.util.UUID; +import org.springframework.data.domain.Sort; public interface CropRotationRepositoryV2 extends AbstractRepository { - List getAllByContourIdAndArchivedIsFalse(UUID contourId); + List getAllByContourIdAndArchivedIsFalse(UUID contourId, Sort sort); } \ No newline at end of file diff --git a/src/main/java/agroscience/fields/v2/services/CropRotationServiceV2.java b/src/main/java/agroscience/fields/v2/services/CropRotationServiceV2.java index 0e604cd..00e733c 100644 --- a/src/main/java/agroscience/fields/v2/services/CropRotationServiceV2.java +++ b/src/main/java/agroscience/fields/v2/services/CropRotationServiceV2.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.UUID; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @Service @@ -40,6 +41,7 @@ public void archive(UUID cropRotationId) { public List getAllByContourId(UUID contourId) { Contour contour = getOrThrow(contourId, contoursRepository::findById); checkArchived(contourId, contour); - return cropRotationRepository.getAllByContourIdAndArchivedIsFalse(contourId); + Sort sort = Sort.by(Sort.Direction.ASC, "startDate"); + return cropRotationRepository.getAllByContourIdAndArchivedIsFalse(contourId, sort); } } diff --git a/src/test/java/agroscience/fields/CropRotationV2Test.java b/src/test/java/agroscience/fields/CropRotationV2Test.java index fa0107a..a49922f 100644 --- a/src/test/java/agroscience/fields/CropRotationV2Test.java +++ b/src/test/java/agroscience/fields/CropRotationV2Test.java @@ -13,7 +13,10 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.data.domain.Sort; import org.springframework.http.ResponseEntity; + +import java.time.LocalDate; import java.util.List; import java.util.UUID; import static agroscience.fields.SampleObjectGenerator.createSampleCropRotation; @@ -79,6 +82,7 @@ public void getCropRotationTest() { CropRotationV2 cropRotation1 = createSampleCropRotation(field.getContours().get(0)); cropRotationRepository.save(cropRotation1); CropRotationV2 cropRotation2 = createSampleCropRotation(field.getContours().get(0)); + cropRotation2.setStartDate(LocalDate.of(2024, 10, 2)); cropRotationRepository.save(cropRotation2); //When String url = "/api/v2/fields-service/contours/" + contourId + "/crop-rotations"; @@ -87,8 +91,8 @@ public void getCropRotationTest() { assertEquals(200, response.getStatusCode().value()); assertNotNull(response.getBody()); assertEquals(2, response.getBody().size()); - assertEquals(response.getBody().get(0), cropRotationMapper.map(cropRotation1)); - assertEquals(response.getBody().get(1), cropRotationMapper.map(cropRotation2)); + assertEquals(cropRotationMapper.map(cropRotation1), response.getBody().get(1)); + assertEquals(cropRotationMapper.map(cropRotation2), response.getBody().get(0)); } @Test @@ -106,7 +110,8 @@ public void deleteCropRotationTest() { //Then assertEquals(200, response.getStatusCode().value()); assertTrue(cropRotationRepository.findAll().get(0).isArchived()); - assertEquals(0, cropRotationRepository.getAllByContourIdAndArchivedIsFalse(cropRotation.getContour().getId()).size()); + Sort sort = Sort.by(Sort.Direction.ASC, "startDate"); + assertEquals(0, cropRotationRepository.getAllByContourIdAndArchivedIsFalse(cropRotation.getContour().getId(), sort).size()); } @Test