-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
d6e70c4
commit 44a9302
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
core/src/main/java/greencity/controller/CommitInfoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
core/src/test/java/greencity/controller/CommitInfoControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |