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

69 add api endpoints and ContourTest #94

Merged
merged 3 commits into from
Nov 19, 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.Contour;
import agroscience.fields.v2.mappers.ContourMapper;
import agroscience.fields.v2.services.ContoursService;
import generated.agroscience.fields.api.ContoursApi;
Expand All @@ -24,17 +25,19 @@ public class ContoursController implements ContoursApi {

@Override
public void changeContour(UUID contourId, UpdateContourDTO updateContourDTO) {
// TODO в сервисный слой не передаём DTO
Contour updatedContour = contourMapper.map(updateContourDTO);
contourService.update(contourId, updatedContour);
}

@Override
public void deleteContour(UUID contourId) {
// TODO Помечаем как архивированно
contourService.archive(contourId);
}

@Override
public List<ContourBaseDTO> findContours(UUID fieldId) {
return null; // TODO не возвращаем архивированные
List<Contour> contourList = contourService.findAllByFieldId(fieldId);
return contourMapper.map(contourList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import agroscience.fields.v2.entities.Contour;
import generated.agroscience.fields.api.model.ContourBaseDTO;
import generated.agroscience.fields.api.model.CoordinatesDTO;
import generated.agroscience.fields.api.model.UpdateContourDTO;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -20,6 +21,13 @@ public interface ContourMapper {
@Mapping(target = "geom", source = "request.coordinates", qualifiedByName = "toGeom")
Contour map(ContourBaseDTO request);

Contour map(UpdateContourDTO updateContourDTO);

@Mapping(target = "coordinates", source = "geom", qualifiedByName = "toCoordinates")
ContourBaseDTO map(Contour contour);

List<ContourBaseDTO> map(List<Contour> contourList);

@Named("toGeom")
default Geometry mapGeom(List<CoordinatesDTO> coordinates) {
GeometryFactory geometryFactory = new GeometryFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.Contour;
import agroscience.fields.v2.entities.FieldV2;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public interface ContoursRepository extends AbstractRepository<Contour> {

List<Contour> findAllByFieldAndArchivedIsFalse(FieldV2 fieldV2);

}
22 changes: 22 additions & 0 deletions src/main/java/agroscience/fields/v2/services/ContoursService.java
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.FieldV2;
import agroscience.fields.v2.repositories.ContoursRepository;
import agroscience.fields.v2.repositories.FieldsRepository;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -24,4 +26,24 @@ public UUID save(UUID fieldId, Contour contour) {
return contoursRepository.save(contour).getId();
}

public void archive(UUID contourId) {
Contour contour = getOrThrow(contourId, contoursRepository::findById);
contour.archive();
contoursRepository.save(contour);
}

public void update(UUID contourId, Contour updatedContour) {
Contour contour = getOrThrow(contourId, contoursRepository::findById);
checkArchived(contourId, contour);
contour.setName(updatedContour.getName());
contour.setColor(updatedContour.getColor());
contoursRepository.save(contour);
}

public List<Contour> findAllByFieldId(UUID fieldId) {
FieldV2 field = getOrThrow(fieldId, fieldsRepository::findById);
checkArchived(fieldId, field);
return contoursRepository.findAllByFieldAndArchivedIsFalse(field);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected static <T> T getOrThrow(UUID id, Function<UUID, Optional<T>> function

protected static <T extends AbstractEntity> void checkArchived(UUID id, T entity) {
if (entity.isArchived()) {
throw new EntityNotFoundException(format(NOT_FOUND + "because archived", id));
throw new EntityNotFoundException(format(NOT_FOUND + " because archived", id));
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,6 @@ components:
- $ref: '#/components/schemas/ContourWithoutCoordinatesDTO'
type: object
properties:
id:
type: string
format: uuid
readOnly: true
color:
type: string
pattern: "^([A-Fa-f0-9]{6})$"
Expand Down
130 changes: 130 additions & 0 deletions src/test/java/agroscience/fields/ContourTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package agroscience.fields;

import agroscience.fields.v2.entities.Contour;
import agroscience.fields.v2.entities.FieldV2;
import agroscience.fields.v2.entities.Season;
import agroscience.fields.v2.mappers.ContourMapper;
import agroscience.fields.v2.repositories.ContoursRepository;
import agroscience.fields.v2.repositories.FieldsRepository;
import agroscience.fields.v2.repositories.SeasonsRepository;
import agroscience.fields.v2.services.ContoursService;
import generated.agroscience.fields.api.model.ContourBaseDTO;
import generated.agroscience.fields.api.model.IdDTO;
import jakarta.persistence.EntityNotFoundException;
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 static agroscience.fields.SampleObjectGenerator.createSampleContour;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ContourTest extends AbstractTest {

@Autowired
private SeasonsRepository seasonRepository;
@Autowired
private ContoursRepository contourRepository;
@Autowired
private FieldsRepository fieldRepository;
@Autowired
private ContoursService contourService;
@Autowired
private ContourMapper contourMapper;
@Autowired
private HttpSteps httpSteps;

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

@Test
public void CreateContourTest(){
// Given
Season season = createSampleSeason();
seasonRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
Contour contour = createSampleContour(field);
// When
ContourBaseDTO contourBaseDTO = contourMapper.map(contour);
String url = "/api/v2/fields-service/fields/" + field.getId().toString() + "/contour";
ResponseEntity<IdDTO> response = httpSteps.sendPostRequest(contourBaseDTO,url,IdDTO.class);
// Then
List<Contour> contourList = contourRepository.findAllByFieldAndArchivedIsFalse(field);
assertEquals(200, response.getStatusCode().value());
assertNotNull(response.getBody());
assertEquals(IdDTO.class, response.getBody().getClass());
assertEquals(2,contourList.size());
contour.setId(contourList.get(1).getId());
assertEquals(contourList.get(1), contour);
}

@Test
public void getContourTest(){
// Given
Season season = createSampleSeason();
seasonRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
Contour contour = createSampleContour(field);
contourRepository.save(contour);
// When
String url = "/api/v2/fields-service/fields/" + field.getId().toString() + "/contours";
ResponseEntity<List<ContourBaseDTO>> response = httpSteps.sendGetRequest(url, new ParameterizedTypeReference<>() {});
//Then
assertEquals(200, response.getStatusCode().value());
List<ContourBaseDTO> responseDto = response.getBody();
assertNotNull(responseDto);
assertEquals(2,responseDto.size());
assertEquals(responseDto.get(0),contourMapper.map(field.getContours().get(0)));
assertEquals(responseDto.get(1),contourMapper.map(contour));
}

@Test
public void deleteContourTest(){
// Given
Season season = createSampleSeason();
seasonRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
//When
String url = "/api/v2/fields-service/contour";
ResponseEntity<Void> response = httpSteps.sendDeleteRequest(field.getContours().get(0).getId(),url, "id");
//Then
assertEquals(200, response.getStatusCode().value());
List<Contour> savedContours = contourRepository.findAll();
assertTrue(savedContours.get(0).isArchived());
field.setId(savedContours.get(0).getField().getId());
assertThrows(EntityNotFoundException.class, () -> contourService.findAllByFieldId(field.getId()));
}

@Test
public void putContourTest(){
// Given
Season season = createSampleSeason();
seasonRepository.save(season);
FieldV2 field = createSampleFieldAndContourInside(season);
fieldRepository.save(field);
Contour contour = contourRepository.findAll().get(0);
//When
contour.setName("sasedskiy");
ContourBaseDTO contourBaseDTO = contourMapper.map(contour);
String url = "/api/v2/fields-service/contour";
ResponseEntity<Void> response = httpSteps.sendPutRequest(contour.getId(), contourBaseDTO, url, "id");
//Then
assertEquals(200, response.getStatusCode().value());
List<Contour> contourList = contourRepository.findAll();
assertEquals(contourBaseDTO, contourMapper.map(contourList.get(0)));
assertEquals(contour, contourList.get(0));
}

}
Loading