Skip to content

Commit

Permalink
Add GotErrorsResource (#24)
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
sleberknight authored Oct 28, 2020
1 parent 7807c08 commit cfa0535
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
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();
}
}
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())
);
}
}

0 comments on commit cfa0535

Please sign in to comment.