Skip to content

Commit

Permalink
Merge pull request #22 from AntaresSimulatorTeam/feature/ANT-2627_fin…
Browse files Browse the repository at this point in the history
…d_trajectories

find trajectories by trajectory type and study ids
  • Loading branch information
melazaar authored Jan 11, 2025
2 parents 801b2fd + ecb30ee commit 5d655fb
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ public ResponseEntity<TrajectoryDTO> uploadTrajectory(@RequestParam("trajectoryT
throws IOException {
return new ResponseEntity<>(toTrajectoryDTO(trajectoryService.processTrajectory(trajectoryType, trajectoryToUse, horizon)), HttpStatus.CREATED);
}

@GetMapping
public List<TrajectoryDTO> getTrajectoriesByStudyIdAndType(@RequestParam("studyIds") List<Integer> trajectoryIds,
@RequestParam("trajectoryType") String trajectoryType) {
return trajectoryService.findTrajectoriesByTypeAndIds(trajectoryType, trajectoryIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder(toBuilder = true)
Expand All @@ -33,7 +32,4 @@ public class TrajectoryDTO {
@JsonProperty("creation_date")
LocalDateTime creationDate;

List<String> tags;

String projet;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public interface TrajectoryRepository extends JpaRepository<TrajectoryEntity, In
@Query("SELECT t FROM Trajectory t WHERE t.type = :type AND t.horizon= :horizon AND (t.fileName LIKE CONCAT('%', CONCAT(:fileNameStartsWith, '%')) OR :fileNameStartsWith IS NULL)")
List<TrajectoryEntity> findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith(@Param("type") String type,@Param("horizon") String horizon, @Param("fileNameStartsWith") String fileNameStartsWith);

List<TrajectoryEntity> findByTypeAndIdIn(String trajectoryType, List<Integer> trajectoryIds);

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rte_france.antares.datamanager_back.service;

import com.rte_france.antares.datamanager_back.dto.TrajectoryDTO;
import com.rte_france.antares.datamanager_back.dto.TrajectoryType;
import com.rte_france.antares.datamanager_back.repository.model.TrajectoryEntity;

Expand All @@ -14,5 +15,6 @@ public interface TrajectoryService {

List<String> findTrajectoriesByTypeAndFileNameStartWithFromFS(TrajectoryType trajectoryType);

List<TrajectoryDTO> findTrajectoriesByTypeAndIds(String trajectoryType, List<Integer> trajectoryIds);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.rte_france.antares.datamanager_back.service.impl;

import com.rte_france.antares.datamanager_back.configuration.AntaressDataManagerProperties;
import com.rte_france.antares.datamanager_back.dto.TrajectoryDTO;
import com.rte_france.antares.datamanager_back.dto.TrajectoryType;
import com.rte_france.antares.datamanager_back.mapper.TrajectoryMapper;
import com.rte_france.antares.datamanager_back.repository.TrajectoryRepository;
import com.rte_france.antares.datamanager_back.repository.model.TrajectoryEntity;
import com.rte_france.antares.datamanager_back.service.AreaFileProcessorService;
Expand All @@ -19,6 +21,8 @@
import java.util.List;
import java.util.Objects;

import static com.rte_france.antares.datamanager_back.mapper.TrajectoryMapper.toTrajectoryDTO;


@Slf4j
@Service
Expand Down Expand Up @@ -83,5 +87,11 @@ public List<String> findTrajectoriesByTypeAndFileNameStartWithFromFS(TrajectoryT
return Collections.emptyList();
}

@Override
public List<TrajectoryDTO> findTrajectoriesByTypeAndIds(String trajectoryType, List<Integer> trajectoryIds) {
return trajectoryRepository.findByTypeAndIdIn(trajectoryType, trajectoryIds).stream()
.map(TrajectoryMapper::toTrajectoryDTO)
.toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.rte_france.antares.datamanager_back.controller;


import com.rte_france.antares.datamanager_back.service.impl.SftpDownloadService;
import com.rte_france.antares.datamanager_back.dto.TrajectoryDTO;
import com.rte_france.antares.datamanager_back.repository.model.TrajectoryEntity;
import com.rte_france.antares.datamanager_back.service.impl.SftpDownloadService;
import com.rte_france.antares.datamanager_back.service.impl.TrajectoryServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -23,11 +24,12 @@
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TrajectoryControllerTest {
class TrajectoryControllerTest {

@Autowired
protected WebApplicationContext wac;
Expand All @@ -49,7 +51,7 @@ public void setup() {

@Test
void uploadTrajectory_returnsCreatedTrajectory() throws Exception {
when(trajectoryServiceImpl.processTrajectory(any(),any(), any())).thenReturn(TrajectoryEntity.builder().build());
when(trajectoryServiceImpl.processTrajectory(any(), any(), any())).thenReturn(TrajectoryEntity.builder().build());

this.mockMvc.perform(post("/v1/trajectory")
.contentType(MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -62,12 +64,12 @@ void uploadTrajectory_returnsCreatedTrajectory() throws Exception {
.andExpect(status().isCreated())
.andDo(MockMvcResultHandlers.print())
.andReturn();
verify(trajectoryServiceImpl, times(1)).processTrajectory(any(), any(),any());
verify(trajectoryServiceImpl, times(1)).processTrajectory(any(), any(), any());
}

@Test
void findTrajectoriesByTypeFromDb_returnsTrajectories() throws Exception {
when(trajectoryServiceImpl.findTrajectoriesByTypeAndFileNameStartWithFromDB(any(),any(),any())).thenReturn(List.of(TrajectoryEntity.builder().build()));
when(trajectoryServiceImpl.findTrajectoriesByTypeAndFileNameStartWithFromDB(any(), any(), any())).thenReturn(List.of(TrajectoryEntity.builder().build()));

this.mockMvc.perform(get("/v1/trajectory/db")
.contentType(MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -80,7 +82,7 @@ void findTrajectoriesByTypeFromDb_returnsTrajectories() throws Exception {
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
verify(trajectoryServiceImpl, times(1)).findTrajectoriesByTypeAndFileNameStartWithFromDB(any(), any(),any());
verify(trajectoryServiceImpl, times(1)).findTrajectoriesByTypeAndFileNameStartWithFromDB(any(), any(), any());
}

@Test
Expand All @@ -99,4 +101,52 @@ void findTrajectoriesByTypeFromFileSystem_returnsFileNames() throws Exception {
verify(sftpDownloadService, times(1)).listFsTrajectoryByType(any(), any());
}

@Test
void getTrajectoriesByStudyIdAndType_returnsEmptyListForNonExistentType() throws Exception {
when(trajectoryServiceImpl.findTrajectoriesByTypeAndIds("nonExistentType", List.of(1, 2, 3))).thenReturn(List.of());
this.mockMvc.perform(get("/v1/trajectory")
.param("trajectoryType", "nonExistentType")
.param("studyIds", "1,2,3")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isEmpty());
}

@Test
void getTrajectoriesByStudyIdAndType_returnsEmptyListForNonExistentIds() throws Exception {
when(trajectoryServiceImpl.findTrajectoriesByTypeAndIds("AREA", List.of(999, 1000))).thenReturn(List.of());
this.mockMvc.perform(get("/v1/trajectory")
.param("trajectoryType", "AREA")
.param("studyIds", "999,1000")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isEmpty());
}

@Test
void getTrajectoriesByStudyIdAndType_returnsNonEmptyListForExistentTypeAndIds() throws Exception {
TrajectoryDTO dto = new TrajectoryDTO();
dto.setType("AREA");
dto.setId(1);
when(trajectoryServiceImpl.findTrajectoriesByTypeAndIds("AREA", List.of(1, 2))).thenReturn(List.of(dto));
this.mockMvc.perform(get("/v1/trajectory")
.param("trajectoryType", "AREA")
.param("studyIds", "1,2")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isNotEmpty())
.andExpect(jsonPath("$[0].type").value("AREA"))
.andExpect(jsonPath("$[0].id").value(1));
}

@Test
void getTrajectoriesByStudyIdAndType_returnsEmptyListForEmptyIds() throws Exception {
when(trajectoryServiceImpl.findTrajectoriesByTypeAndIds("AREA", List.of())).thenReturn(List.of());
this.mockMvc.perform(get("/v1/trajectory")
.param("trajectoryType", "AREA")
.param("studyIds", "")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,58 @@ void findFirstByFileNameOrderByVersionDesc_returnsEmptyOptionalForNonExistentFil

@Test
void findTrajectoriesByTypeAndFileNameStartsWith_returnsEmptyListForNonExistentType() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("nonExistentType", "2023-2024","test");
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("nonExistentType", "2023-2024", "test");
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findTrajectoriesByTypeAndFileNameStartsWith_returnsEmptyListForNonExistentFileNameStartsWith() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("AREA", "2023-2024","nonExistentStart");
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("AREA", "2023-2024", "nonExistentStart");
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findTrajectoriesByTypeAndFileNameStartsWith_returnsNonEmptyListForExistentTypeAndFileNameStartsWith() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("AREA","2023-2024", "test");
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith("AREA", "2023-2024", "test");
assertThat(trajectoryEntities).isNotEmpty();
assertThat(trajectoryEntities.get(0).getFileName()).startsWith("test");
}

@Test
void findTrajectoriesByTypeAndFileNameStartsWith_returnsEmptyListForNullType() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith(null, "2023-2024","test");
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findTrajectoriesFileNameByTypeAAndHorizonAndFileNameStartsWith(null, "2023-2024", "test");
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findByTypeAndIdIn_returnsEmptyListForNonExistentType() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findByTypeAndIdIn("nonExistentType", List.of(1, 2, 3));
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findByTypeAndIdIn_returnsEmptyListForNonExistentIds() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findByTypeAndIdIn("AREA", List.of(999, 1000));
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findByTypeAndIdIn_returnsNonEmptyListForExistentTypeAndIds() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findByTypeAndIdIn("AREA", List.of(1, 2));
assertThat(trajectoryEntities).isNotEmpty();
assertThat(trajectoryEntities.get(0).getType()).isEqualTo("AREA");
assertThat(trajectoryEntities.get(0).getId()).isIn(1, 2);
}

@Test
void findByTypeAndIdIn_returnsEmptyListForNullType() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findByTypeAndIdIn(null, List.of(1, 2, 3));
assertThat(trajectoryEntities).isEmpty();
}

@Test
void findByTypeAndIdIn_returnsEmptyListForEmptyIds() {
List<TrajectoryEntity> trajectoryEntities = trajectoryRepository.findByTypeAndIdIn("AREA", List.of());
assertThat(trajectoryEntities).isEmpty();
}

Expand Down
Loading

0 comments on commit 5d655fb

Please sign in to comment.