Skip to content

Commit

Permalink
Pull request #265: Implement healthcheck endpoint
Browse files Browse the repository at this point in the history
Merge in ITB/itb-commons from feature/ITB-1624-validator-healthcheck-endpoint-for-validators to development

* commit '7872999857647d0b49ce83ec4cd351ef3a5d6956':
  Implement healthcheck endpoint
  • Loading branch information
Ivo Carbajo authored and costas80 committed Sep 16, 2024
2 parents f5929ed + 7872999 commit 881f904
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.print.attribute.standard.Media;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -100,6 +102,32 @@ public ApiInfo info(
return ApiInfo.fromDomainConfig(domainConfig);
}

/**
* Check if the validator is responding to requests.
*
* @param domain The domain.
* @return A 200 status code.
*/
@Operation(summary = "Check if the validator is responding to requests",
description = "Check if the API for the given domain is available to respond to requests. " +
"Can also be used as a general healthcheck, by not specifying a domain." +
"This request does not return a body. Only a 200 status code if the healthcheck passes.")
@ApiResponse(responseCode = "200", description = "Success", content = { @Content(schema = @Schema()) })
@ApiResponse(responseCode = "500", description = "Error (If a problem occurred with processing the request)", content = @Content)
@ApiResponse(responseCode = "404", description = "Not found (for an invalid domain value)", content = @Content)
@GetMapping(value = {"/api/healthcheck", "/{domain}/api/healthcheck"}, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity healthcheck(
@Parameter(name = "domain", description = "A fixed value corresponding to the specific validation domain.",
examples = {
@ExampleObject(name="order", summary="Sample 'order' configuration", value="order", description = "The domain value to use for the demo 'order' validator."),
@ExampleObject(name="any", summary="Generic 'any' configuration", value = "any", description = "The domain value to use for the generic 'any' validator used to validate content with user-provided validation artefacts.")
})
@PathVariable(value = "domain", required = false) String domain
) {
if (domain != null && !domain.isEmpty()) validateDomain(domain);
return ResponseEntity.ok().build();
}

/**
* Validates that the domain exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.junit.jupiter.api.Test;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatusCode;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -100,6 +102,12 @@ void testInfo() {
assertEquals("Description of type2", result.getValidationTypes().get(1).getDescription());
}

@Test
void testHealthcheck() {
assertEquals(HttpStatusCode.valueOf(200), controller.healthcheck("domain1Name").getStatusCode());
assertEquals(HttpStatusCode.valueOf(200), controller.healthcheck("").getStatusCode());
}

@Test
void testNotFound() {
assertThrows(NotFoundException.class, () -> controller.info("domainXName"));
Expand Down

0 comments on commit 881f904

Please sign in to comment.