From 7348659e5308b421d1233df96ca611caeefdc622 Mon Sep 17 00:00:00 2001 From: Hugo Marcellin Date: Thu, 21 Sep 2023 11:16:46 +0200 Subject: [PATCH] Add supervision controller to get results count (#29) Signed-off-by: Hugo Marcellin --- .../server/SupervisionController.java | 39 +++++++++++++++ .../server/service/SupervisionService.java | 26 ++++++++++ .../server/SupervisionControllerTest.java | 48 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/main/java/org/gridsuite/shortcircuit/server/SupervisionController.java create mode 100644 src/main/java/org/gridsuite/shortcircuit/server/service/SupervisionService.java create mode 100644 src/test/java/org/gridsuite/shortcircuit/server/SupervisionControllerTest.java diff --git a/src/main/java/org/gridsuite/shortcircuit/server/SupervisionController.java b/src/main/java/org/gridsuite/shortcircuit/server/SupervisionController.java new file mode 100644 index 00000000..b3c97391 --- /dev/null +++ b/src/main/java/org/gridsuite/shortcircuit/server/SupervisionController.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.shortcircuit.server; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.gridsuite.shortcircuit.server.service.SupervisionService; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Hugo Marcellin + */ +@RestController +@RequestMapping(value = "/" + ShortCircuitApi.API_VERSION + "/supervision") +@Tag(name = "Short circuit server - Supervision") +public class SupervisionController { + private final SupervisionService supervisionService; + + public SupervisionController(SupervisionService supervisionService) { + this.supervisionService = supervisionService; + } + + @GetMapping(value = "/results-count") + @Operation(summary = "Get results count") + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The count of all results")}) + public ResponseEntity getResultsCount() { + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(supervisionService.getResultsCount()); + } +} diff --git a/src/main/java/org/gridsuite/shortcircuit/server/service/SupervisionService.java b/src/main/java/org/gridsuite/shortcircuit/server/service/SupervisionService.java new file mode 100644 index 00000000..4706ab9a --- /dev/null +++ b/src/main/java/org/gridsuite/shortcircuit/server/service/SupervisionService.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.shortcircuit.server.service; + +import org.gridsuite.shortcircuit.server.repositories.ResultRepository; +import org.springframework.stereotype.Service; + +/** + * @author Hugo Marcellin + */ +@Service +public class SupervisionService { + private final ResultRepository resultRepository; + + public SupervisionService(ResultRepository resultRepository) { + this.resultRepository = resultRepository; + } + + public Integer getResultsCount() { + return (int) resultRepository.count(); + } +} diff --git a/src/test/java/org/gridsuite/shortcircuit/server/SupervisionControllerTest.java b/src/test/java/org/gridsuite/shortcircuit/server/SupervisionControllerTest.java new file mode 100644 index 00000000..6c4848c6 --- /dev/null +++ b/src/test/java/org/gridsuite/shortcircuit/server/SupervisionControllerTest.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.shortcircuit.server; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.junit.Assert.assertEquals; + +/** + * @author Hugo Marcellin + */ +@RunWith(SpringRunner.class) +@AutoConfigureMockMvc +@SpringBootTest +public class SupervisionControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void testResultCount() throws Exception { + //get the result timeline uuid of the calculation + MvcResult mvcResult = mockMvc.perform(get("/v1/supervision/results-count")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + + String resultCount = mvcResult.getResponse().getContentAsString(); + assertEquals("0", resultCount); + + } +}