-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/org/kiwiproject/dropwizard/error/resource/GotErrorsResource.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,53 @@ | ||
package org.kiwiproject.dropwizard.error.resource; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
import org.kiwiproject.dropwizard.error.model.DataStoreType; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Map; | ||
|
||
/** | ||
* A simple JAX-RS resource for clients to determine if application errors are available or not. | ||
* <p> | ||
* Clients can check if application errors are available using a HTTP GET request that returns a | ||
* {@code 200 OK} response if available. The response entity contains information on whether the | ||
* underlying data store is shared or not. A shared database means it is shared between separate | ||
* service/application instances, whereas a database that is <em>not</em> shared is local to the | ||
* specific service/application instance being queried. | ||
* <p> | ||
* For example, an in-memory H2 database is not shared, whereas a Postgres database used by all | ||
* instances of one specific service/application (e.g. an "Order Service" that has 3 running | ||
* instances that all use the same Postgres database). | ||
* <p> | ||
* Obviously, a {@code 404 Not Found} is returned if this resource is not registered and available. | ||
*/ | ||
@Path("/kiwi/got-errors") | ||
@Produces(APPLICATION_JSON) | ||
public class GotErrorsResource { | ||
|
||
private final DataStoreType dataStoreType; | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param dataStoreType the DataStoreType of the service/application this resource is registered with | ||
*/ | ||
public GotErrorsResource(DataStoreType dataStoreType) { | ||
this.dataStoreType = dataStoreType; | ||
} | ||
|
||
/** | ||
* Check if application errors are available. Clients always receive a 200 response from this endpoint. | ||
* | ||
* @return the Response | ||
*/ | ||
@GET | ||
public Response gotErrors() { | ||
var entity = Map.of("shared", dataStoreType.shared()); | ||
return Response.ok(entity).build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/org/kiwiproject/dropwizard/error/resource/GotErrorsResourceTest.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,40 @@ | ||
package org.kiwiproject.dropwizard.error.resource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
import static org.kiwiproject.jaxrs.KiwiGenericTypes.MAP_OF_STRING_TO_OBJECT_GENERIC_TYPE; | ||
import static org.kiwiproject.test.jaxrs.JaxrsTestHelper.assertOkResponse; | ||
|
||
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; | ||
import io.dropwizard.testing.junit5.ResourceExtension; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.kiwiproject.dropwizard.error.model.DataStoreType; | ||
|
||
@DisplayName("GotErrorsResource") | ||
@ExtendWith(DropwizardExtensionsSupport.class) | ||
class GotErrorsResourceTest { | ||
|
||
private static final DataStoreType DATA_STORE_TYPE = DataStoreType.NOT_SHARED; | ||
|
||
private static final ResourceExtension RESOURCES = ResourceExtension.builder() | ||
.addResource(new GotErrorsResource(DATA_STORE_TYPE)) | ||
.build(); | ||
|
||
@Test | ||
void shouldReturnOkResponse() { | ||
var response = RESOURCES.client() | ||
.target("/kiwi/got-errors?") | ||
.request() | ||
.get(); | ||
|
||
assertOkResponse(response); | ||
|
||
var entity = response.readEntity(MAP_OF_STRING_TO_OBJECT_GENERIC_TYPE); | ||
|
||
assertThat(entity).containsOnly( | ||
entry("shared", DATA_STORE_TYPE.shared()) | ||
); | ||
} | ||
} |