From 44a93026c620809101ce017b8f07725fb24f2def Mon Sep 17 00:00:00 2001 From: LazarenkoDmytro Date: Mon, 16 Dec 2024 21:10:12 +0200 Subject: [PATCH] Add CommitInfoController with endpoint and unit tests Introduced a new REST controller `CommitInfoController` to fetch the latest Git commit hash and date. Added corresponding unit tests in `CommitInfoControllerTest` and updated `HttpStatuses` with `INTERNAL_SERVER_ERROR` for response descriptions. --- .../java/greencity/constant/HttpStatuses.java | 1 + .../controller/CommitInfoController.java | 61 +++++++++++++++++ .../controller/CommitInfoControllerTest.java | 66 +++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 core/src/main/java/greencity/controller/CommitInfoController.java create mode 100644 core/src/test/java/greencity/controller/CommitInfoControllerTest.java diff --git a/core/src/main/java/greencity/constant/HttpStatuses.java b/core/src/main/java/greencity/constant/HttpStatuses.java index f0ddc595b2..08a3d97eea 100644 --- a/core/src/main/java/greencity/constant/HttpStatuses.java +++ b/core/src/main/java/greencity/constant/HttpStatuses.java @@ -12,4 +12,5 @@ public class HttpStatuses { public static final String NOT_FOUND = "Not Found"; public static final String SEE_OTHER = "See Other"; public static final String FOUND = "Found"; + public static final String INTERNAL_SERVER_ERROR = "Internal Server Error"; } diff --git a/core/src/main/java/greencity/controller/CommitInfoController.java b/core/src/main/java/greencity/controller/CommitInfoController.java new file mode 100644 index 0000000000..b9ce99d102 --- /dev/null +++ b/core/src/main/java/greencity/controller/CommitInfoController.java @@ -0,0 +1,61 @@ +package greencity.controller; + +import greencity.constant.HttpStatuses; +import greencity.dto.commitinfo.CommitInfoDto; +import greencity.dto.commitinfo.CommitInfoErrorDto; +import greencity.service.CommitInfoService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import lombok.RequiredArgsConstructor; +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; + +/** + * Controller for fetching Git commit information. + */ +@RestController +@RequestMapping("/commit-info") +@RequiredArgsConstructor +public class CommitInfoController { + private final CommitInfoService commitInfoService; + + /** + * Endpoint to fetch the latest Git commit hash and date. + * + * @return {@link CommitInfoDto}, either success or error + */ + @Operation(summary = "Get the latest commit hash and date.") + @ApiResponse( + responseCode = "200", + description = HttpStatuses.OK, + content = @Content( + mediaType = "application/json", + examples = @ExampleObject( + value = """ + { + "commitHash": "d6e70c46b39857846f3f13ca9756c39448ab3d6f", + "commitDate": "16/12/2024 10:55:00" + }"""))) + @ApiResponse( + responseCode = "500", + description = HttpStatuses.INTERNAL_SERVER_ERROR, + content = @Content( + mediaType = "application/json", + examples = @ExampleObject( + value = """ + { + "error": "Failed to fetch commit info: Repository not found" + }"""))) + @GetMapping + public ResponseEntity getCommitInfo() { + CommitInfoDto commitInfo = commitInfoService.getLatestCommitInfo(); + if (commitInfo instanceof CommitInfoErrorDto) { + return ResponseEntity.internalServerError().body(commitInfo); + } + return ResponseEntity.ok(commitInfo); + } +} diff --git a/core/src/test/java/greencity/controller/CommitInfoControllerTest.java b/core/src/test/java/greencity/controller/CommitInfoControllerTest.java new file mode 100644 index 0000000000..5fb8d0bb3e --- /dev/null +++ b/core/src/test/java/greencity/controller/CommitInfoControllerTest.java @@ -0,0 +1,66 @@ +package greencity.controller; + +import greencity.dto.commitinfo.CommitInfoDto; +import greencity.dto.commitinfo.CommitInfoErrorDto; +import greencity.dto.commitinfo.CommitInfoSuccessDto; +import greencity.service.CommitInfoService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import static org.mockito.Mockito.*; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + +@ExtendWith(MockitoExtension.class) +class CommitInfoControllerTest { + @InjectMocks + private CommitInfoController commitInfoController; + + @Mock + private CommitInfoService commitInfoService; + + private MockMvc mockMvc; + + @BeforeEach + void setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(commitInfoController).build(); + } + + private static final String COMMIT_INFO_URL = "/commit-info"; + private static final String COMMIT_HASH = "abc123"; + private static final String COMMIT_DATE = "16/12/2024 12:06:32"; + private static final String ERROR_MESSAGE = "Failed to fetch commit info due to I/O error: Missing object"; + + @Test + void getCommitInfoReturnsSuccessTest() throws Exception { + CommitInfoDto commitInfoDto = new CommitInfoSuccessDto(COMMIT_HASH, COMMIT_DATE); + when(commitInfoService.getLatestCommitInfo()).thenReturn(commitInfoDto); + + mockMvc.perform(get(COMMIT_INFO_URL).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.commitHash").value(COMMIT_HASH)) + .andExpect(jsonPath("$.commitDate").value(COMMIT_DATE)); + + verify(commitInfoService, times(1)).getLatestCommitInfo(); + } + + @Test + void getCommitInfoReturnsErrorTest() throws Exception { + CommitInfoDto commitInfoDto = new CommitInfoErrorDto(ERROR_MESSAGE); + when(commitInfoService.getLatestCommitInfo()).thenReturn(commitInfoDto); + + mockMvc.perform(get(COMMIT_INFO_URL).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.error").value(ERROR_MESSAGE)); + + verify(commitInfoService, times(1)).getLatestCommitInfo(); + } +}