Skip to content

Commit

Permalink
Add CommitInfoController with endpoint and unit tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LazarenkoDmytro committed Dec 16, 2024
1 parent d6e70c4 commit 44a9302
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/greencity/constant/HttpStatuses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
61 changes: 61 additions & 0 deletions core/src/main/java/greencity/controller/CommitInfoController.java
Original file line number Diff line number Diff line change
@@ -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<CommitInfoDto> getCommitInfo() {
CommitInfoDto commitInfo = commitInfoService.getLatestCommitInfo();
if (commitInfo instanceof CommitInfoErrorDto) {
return ResponseEntity.internalServerError().body(commitInfo);
}
return ResponseEntity.ok(commitInfo);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 44a9302

Please sign in to comment.