Skip to content

Commit

Permalink
100 add sort for crop rotations (#105)
Browse files Browse the repository at this point in the history
* 100 add sort for crop rotations

* 100 add test for sort
  • Loading branch information
FergeSS authored Dec 1, 2024
1 parent 601c0ad commit 44af7d2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CropRotationV2> {

List<CropRotationV2> getAllByContourIdAndArchivedIsFalse(UUID contourId);
List<CropRotationV2> getAllByContourIdAndArchivedIsFalse(UUID contourId, Sort sort);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +41,7 @@ public void archive(UUID cropRotationId) {
public List<CropRotationV2> 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);
}
}
11 changes: 8 additions & 3 deletions src/test/java/agroscience/fields/CropRotationV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 44af7d2

Please sign in to comment.