Refactor: Enhance healthcheck response with version info and DTO (#2378)

* Refactor: Enhance healthcheck response with version info and DTO

* test: Enhance healthcheck response with version info and DTO
This commit is contained in:
BAE
2026-01-21 22:38:44 -07:00
committed by GitHub
parent c42f62c338
commit 48b615d529
3 changed files with 136 additions and 3 deletions
@@ -1,21 +1,36 @@
package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.model.dto.HealthcheckResponse;
import com.adityachandel.booklore.model.dto.response.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@RestController
@RequestMapping("/api/v1/healthcheck")
@Tag(name = "Healthcheck", description = "Endpoints for checking the healch of the application")
public class HealthcheckController {
@Operation(summary = "Get a ping response", description = "Check if the application is responding to requests")
@Value("${app.version}")
private String appVersion;
@Operation(summary = "Get a ping response", description = "Check if the application is responding")
@ApiResponse(responseCode = "200", description = "Health status returned successfully")
@GetMapping
public ResponseEntity<?> getPing() {
return ResponseEntity.ok(new SuccessResponse<>(200, "Pong"));
public ResponseEntity<SuccessResponse<HealthcheckResponse>> getPing() {
HealthcheckResponse healthData = HealthcheckResponse.builder()
.status("UP")
.message("Application is running smoothly.")
.version(appVersion) // ex) 'development' Insert
.timestamp(LocalDateTime.now())
.build();
return ResponseEntity.ok(new SuccessResponse<>(200, "Pong", healthData));
}
}
@@ -0,0 +1,15 @@
package com.adityachandel.booklore.model.dto;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Builder
public class HealthcheckResponse {
private String status;
private String message;
private LocalDateTime timestamp;
private String version;
}
@@ -0,0 +1,103 @@
package com.adityachandel.booklore.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.test.context.TestPropertySource; // Added this import
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(
controllers = HealthcheckController.class,
useDefaultFilters = false, // Set to false to disable default component filters
includeFilters = @ComponentScan.Filter( // Explicitly include HealthcheckController
type = FilterType.ASSIGNABLE_TYPE,
classes = HealthcheckController.class
)
)
@TestPropertySource(properties = {"app.version=1.0.0"}) // Added this line
public class HealthcheckControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WebApplicationContext webApplicationContext;
@BeforeEach
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void getPing_ShouldReturnHealthcheckResponse() throws Exception {
mockMvc.perform(get("/api/v1/healthcheck"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.status").value("UP"))
.andExpect(jsonPath("$.data.message").value("Application is running smoothly."))
.andExpect(jsonPath("$.data.timestamp").exists())
.andExpect(jsonPath("$.data.version").value("1.0.0"));
}
}