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

70 Add imp controllers and test for crop rotation #90

Merged
merged 6 commits into from
Nov 18, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package agroscience.fields.v2.controllers;

import agroscience.fields.v2.entities.CropRotationV2;
import agroscience.fields.v2.mappers.CropRotationMapperV2;
import agroscience.fields.v2.services.CropRotationServiceV2;
import generated.agroscience.fields.api.CropRotationsApi;
Expand All @@ -13,25 +14,27 @@

@RestController
@RequiredArgsConstructor

@PreAuthorize("hasRole('organization') or hasRole('worker')")
public class CropRotationsControllerV2 implements CropRotationsApi {

private final CropRotationServiceV2 cropRotationService;
private final CropRotationMapperV2 cropRotationMapperV2;

@Override
public void changeCropRotation(UUID cropRotationId, CropRotationDTO updateCropRotationDTO) {
// TODO в сервисный слой не передаём DTO
public void changeCropRotation(UUID cropRotationId, CropRotationDTO cropRotationDTO) {
CropRotationV2 cropRotationV2 = cropRotationMapperV2.map(cropRotationDTO);
cropRotationService.update(cropRotationId, cropRotationV2);
}

@Override
public void deleteCropRotation(UUID cropRotationId) {
// TODO Не удаляем, архивируем
cropRotationService.archive(cropRotationId);
}

@Override
public List<CropRotationDTO> getCropRotations(UUID contourID) {
return null; // TODO
return cropRotationMapperV2.map(cropRotationService.getAllByContourId(contourID));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package agroscience.fields.v2.controllers;

import agroscience.fields.v2.entities.FieldV2;
import agroscience.fields.v2.mappers.FieldMapperV2;
import agroscience.fields.v2.services.FieldsService;
import generated.agroscience.fields.api.FieldsApi;
Expand All @@ -23,7 +24,8 @@ public class FieldsController implements FieldsApi {

@Override
public void changeField(UUID fieldId, FieldBaseDTO fieldBaseDTO) {
// TODO обновлять
FieldV2 updateField = fieldMapperV2.map(fieldBaseDTO);
fieldService.update(fieldId, updateField);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import agroscience.fields.v2.entities.CropRotationV2;
import generated.agroscience.fields.api.model.CropRotationDTO;
import java.util.List;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring", uses = {CropRotationMapperV2.class})
Expand All @@ -12,4 +13,6 @@ public interface CropRotationMapperV2 {

CropRotationDTO map(CropRotationV2 cropRotationV2);

}
List<CropRotationDTO> map(List<CropRotationV2> cropRotationV2);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agroscience.fields.v2.mappers;

import agroscience.fields.v2.entities.FieldV2;
import generated.agroscience.fields.api.model.FieldBaseDTO;
import generated.agroscience.fields.api.model.FieldDTO;
import generated.agroscience.fields.api.model.FieldWithContoursAndCropRotationsDTO;
import java.util.List;
Expand All @@ -20,6 +21,8 @@ public interface FieldMapperV2 {

FieldDTO map(FieldV2 field);

FieldV2 map(FieldBaseDTO field);

List<FieldWithContoursAndCropRotationsDTO> map(List<FieldV2> fieldV2List);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.CropRotationV2;
import java.util.List;
import java.util.UUID;

public interface CropRotationRepositoryV2 extends AbstractRepository<CropRotationV2> {

List<CropRotationV2> getAllByContourIdAndArchivedIsFalse(UUID contourId);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package agroscience.fields.v2.services;

import agroscience.fields.v2.entities.Contour;
import agroscience.fields.v2.entities.CropRotationV2;
import agroscience.fields.v2.repositories.ContoursRepository;
import agroscience.fields.v2.repositories.CropRotationRepositoryV2;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -15,12 +17,26 @@ public class CropRotationServiceV2 extends DefaultService {
private final ContoursRepository contoursRepository;

public CropRotationV2 save(UUID contourId, CropRotationV2 cropRotation) {
var contour = getOrThrow(contourId, contoursRepository::findById);
Contour contour = getOrThrow(contourId, contoursRepository::findById);
cropRotation.setContour(contour);
return cropRotationRepository.save(cropRotation);
}

public CropRotationV2 getCropRotation(UUID cropRotationId) {
return cropRotationRepository.getReferenceById(cropRotationId);
public void update(UUID cropRotationId, CropRotationV2 updateCropRotation) {
CropRotationV2 cropRotation = getOrThrow(cropRotationId, cropRotationRepository::findById);
checkArchived(cropRotationId, cropRotation);
updateCropRotation.setContour(cropRotation.getContour());
updateCropRotation.setId(cropRotation.getId());
cropRotationRepository.save(updateCropRotation);
}

public void archive(UUID cropRotationId) {
CropRotationV2 cropRotation = getOrThrow(cropRotationId, cropRotationRepository::findById);
cropRotation.setArchived(true);
cropRotationRepository.save(cropRotation);
}

public List<CropRotationV2> getAllByContourId(UUID contourId) {
return cropRotationRepository.getAllByContourIdAndArchivedIsFalse(contourId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public FieldV2 save(UUID seasonId, FieldV2 field) {
return fieldsRepository.save(field);
}

public void update(UUID fieldId, FieldV2 updateField) {
FieldV2 field = getOrThrow(fieldId, fieldsRepository::findById);
field.setName(updateField.getName());
field.setDescription(updateField.getDescription());
fieldsRepository.save(field);
}

public FieldV2 findById(UUID id) {
return fieldsRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Field not found"));
}
Expand Down
132 changes: 132 additions & 0 deletions src/test/java/agroscience/fields/CropRotationV2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package agroscience.fields;

import agroscience.fields.v2.entities.CropRotationV2;
import agroscience.fields.v2.entities.FieldV2;
import agroscience.fields.v2.entities.Season;
import agroscience.fields.v2.mappers.CropRotationMapperV2;
import agroscience.fields.v2.repositories.CropRotationRepositoryV2;
import agroscience.fields.v2.repositories.FieldsRepository;
import agroscience.fields.v2.repositories.SeasonsRepository;
import generated.agroscience.fields.api.model.CropRotationDTO;
import generated.agroscience.fields.api.model.IdDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import java.util.List;
import java.util.UUID;
import static agroscience.fields.SampleObjectGenerator.createSampleCropRotation;
import static agroscience.fields.SampleObjectGenerator.createSampleFieldAndContourInside;
import static agroscience.fields.SampleObjectGenerator.createSampleSeason;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CropRotationV2Test extends AbstractTest {

@Autowired
private SeasonsRepository seasonsRepository;
@Autowired
private FieldsRepository fieldRepository;
@Autowired
private CropRotationRepositoryV2 cropRotationRepository;
@Autowired
private CropRotationMapperV2 cropRotationMapper;
@Autowired
private HttpSteps httpSteps;

@BeforeEach
public void clear() {
cropRotationRepository.deleteAll();
fieldRepository.deleteAll();
seasonsRepository.deleteAll();
}

@Test
public void createCropRotationTest() {
//Given
Season season = createSampleSeason();
seasonsRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
UUID contourId = field.getContours().get(0).getId();
CropRotationV2 cropRotation = createSampleCropRotation(field.getContours().get(0));
//When
CropRotationDTO cropRotationDTO = cropRotationMapper.map(cropRotation);
String url = "/api/v2/fields-service/contours/" + contourId + "/crop-rotation";
ResponseEntity<IdDTO> response = httpSteps.sendPostRequest(cropRotationDTO, url, IdDTO.class);
//Then
List<CropRotationV2> savedCropRotations = cropRotationRepository.findAll();
asavershin marked this conversation as resolved.
Show resolved Hide resolved
cropRotation.setId(savedCropRotations.get(0).getId());
assertEquals(200, response.getStatusCode().value());
assertNotNull(response.getBody());
assertEquals(IdDTO.class, response.getBody().getClass());
assertEquals(new IdDTO(savedCropRotations.get(0).getId()), response.getBody());
assertEquals(1, savedCropRotations.size());
assertEquals(cropRotation.getId(), savedCropRotations.get(0).getId());
asavershin marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(cropRotation, savedCropRotations.get(0));
}

@Test
public void getCropRotationTest() {
//Given
Season season = createSampleSeason();
seasonsRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
UUID contourId = field.getContours().get(0).getId();
CropRotationV2 cropRotation1 = createSampleCropRotation(field.getContours().get(0));
cropRotationRepository.save(cropRotation1);
CropRotationV2 cropRotation2 = createSampleCropRotation(field.getContours().get(0));
cropRotationRepository.save(cropRotation2);
//When
String url = "/api/v2/fields-service/contours/" + contourId + "/crop-rotations";
ResponseEntity<List<CropRotationDTO>> response = httpSteps.sendGetRequest(url, new ParameterizedTypeReference<>() {});
//Then
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));
}

@Test
public void deleteCropRotationTest() {
//Given
Season season = createSampleSeason();
seasonsRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
CropRotationV2 cropRotation = createSampleCropRotation(field.getContours().get(0));
cropRotationRepository.save(cropRotation);
//When
String url = "/api/v2/fields-service/crop-rotation";
ResponseEntity<Void> response = httpSteps.sendDeleteRequest(cropRotation.getId(), url, "id");
//Then
assertEquals(200, response.getStatusCode().value());
assertTrue(cropRotationRepository.findAll().get(0).isArchived());
assertEquals(0, cropRotationRepository.getAllByContourIdAndArchivedIsFalse(cropRotation.getContour().getId()).size());
}

@Test
public void putCropRotationTest() {
//Given
Season season = createSampleSeason();
seasonsRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
CropRotationV2 cropRotation = createSampleCropRotation(field.getContours().get(0));
cropRotationRepository.save(cropRotation);
//When
cropRotation.setDescription("Test");
KABOPOK marked this conversation as resolved.
Show resolved Hide resolved
CropRotationDTO cropRotationDTO = cropRotationMapper.map(cropRotation);
String url = "/api/v2/fields-service/crop-rotation";
ResponseEntity<Void> response = httpSteps.sendPutRequest(cropRotation.getId(), cropRotationDTO, url, "id");
//Then
assertEquals(200, response.getStatusCode().value());
assertEquals(cropRotationDTO, cropRotationMapper.map(cropRotationRepository.findAll().get(0)));
assertEquals(cropRotation, cropRotationRepository.findAll().get(0));
}

}
Loading