-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from spals/timkral/swagger
Add Swagger
- Loading branch information
Showing
24 changed files
with
439 additions
and
19 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
55 changes: 55 additions & 0 deletions
55
app-core/src/main/java/net/spals/appbuilder/app/core/jaxrs/JaxRsDocModule.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,55 @@ | ||
package net.spals.appbuilder.app.core.jaxrs; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Module; | ||
import io.swagger.jaxrs.config.BeanConfig; | ||
import io.swagger.jaxrs.listing.AcceptHeaderApiListingResource; | ||
import io.swagger.jaxrs.listing.SwaggerSerializers; | ||
import net.spals.appbuilder.config.service.ServiceScan; | ||
import org.inferred.freebuilder.FreeBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.core.Configurable; | ||
|
||
/** | ||
* A Guice {@link Module} which automatically creates | ||
* a documentation API. | ||
* | ||
* @author tkral | ||
*/ | ||
@FreeBuilder | ||
public abstract class JaxRsDocModule extends AbstractModule { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(JaxRsDocModule.class); | ||
|
||
public abstract boolean isWebServerAutoBindingEnabled(); | ||
public abstract String getApplicationName(); | ||
public abstract Configurable<?> getConfigurable(); | ||
public abstract ServiceScan getServiceScan(); | ||
|
||
public static class Builder extends JaxRsDocModule_Builder { | ||
public Builder() { | ||
setWebServerAutoBindingEnabled(true); | ||
} | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
if (!isWebServerAutoBindingEnabled()) { | ||
return; | ||
} | ||
|
||
LOGGER.info("Registering documentation API via Swagger"); | ||
// Automatically register Swagger API documentation endpoints | ||
getConfigurable().register(AcceptHeaderApiListingResource.class); | ||
getConfigurable().register(SwaggerSerializers.class); | ||
|
||
// Automatically create and register a Swagger scanner | ||
final BeanConfig swaggerConfig = new BeanConfig(); | ||
swaggerConfig.setResourcePackage(Joiner.on(',').join(getServiceScan().getServicePackages())); | ||
swaggerConfig.setTitle(getApplicationName() + " API"); | ||
// Turn on automatic scanning. This should be the last value set in the config. | ||
swaggerConfig.setScan(); | ||
} | ||
} |
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
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
63 changes: 63 additions & 0 deletions
63
...zard-test/src/test/java/net/spals/appbuilder/app/dropwizard/DocDropwizardWebAppFTest.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,63 @@ | ||
package net.spals.appbuilder.app.dropwizard; | ||
|
||
import io.dropwizard.Configuration; | ||
import io.dropwizard.testing.DropwizardTestSupport; | ||
import net.spals.appbuilder.app.dropwizard.doc.DocDropwizardWebApp; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.GenericType; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Map; | ||
|
||
import static javax.ws.rs.core.Response.Status.OK; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
/** | ||
* Functional tests for API documentation in a {@link DropwizardWebApp} | ||
* (see {@link DocDropwizardWebApp}). | ||
* | ||
* @author tkral | ||
*/ | ||
public class DocDropwizardWebAppFTest { | ||
|
||
private final DropwizardTestSupport<Configuration> testServerWrapper = | ||
new DropwizardTestSupport<>(DocDropwizardWebApp.class, new Configuration()); | ||
|
||
private final Client webClient = ClientBuilder.newClient(); | ||
|
||
@BeforeClass | ||
void classSetup() { | ||
testServerWrapper.before(); | ||
} | ||
|
||
@AfterClass | ||
void classTearDown() { | ||
testServerWrapper.after(); | ||
} | ||
|
||
@Test | ||
public void testApiDocumentation() { | ||
final String target = "http://localhost:" + testServerWrapper.getLocalPort() + "/swagger"; | ||
final WebTarget webTarget = webClient.target(target); | ||
final Response docResponse = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get(); | ||
|
||
assertThat(docResponse.getStatus(), is(OK.getStatusCode())); | ||
final Map<String, Object> json = docResponse.readEntity(new GenericType<Map<String, Object>>() {}); | ||
|
||
assertThat(json, hasKey("info")); | ||
final Map<String, Object> info = (Map<String, Object>)json.get("info"); | ||
assertThat(info, hasEntry("title", "DocDropwizardWebApp API")); | ||
|
||
assertThat(json, hasKey("paths")); | ||
final Map<String, Object> paths = (Map<String, Object>)json.get("paths"); | ||
assertThat(paths, hasKey("/doc/get")); | ||
assertThat(paths, hasKey("/doc/get/{id}")); | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
...ard-test/src/test/java/net/spals/appbuilder/app/dropwizard/doc/DocDropwizardResource.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,37 @@ | ||
package net.spals.appbuilder.app.dropwizard.doc; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import net.spals.appbuilder.annotations.service.AutoBindSingleton; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
/** | ||
* A sample web resource. | ||
* | ||
* @author tkral | ||
*/ | ||
@AutoBindSingleton | ||
@Api(value = "doc") | ||
@Path("doc") | ||
public class DocDropwizardResource { | ||
|
||
DocDropwizardResource() { } | ||
|
||
@GET | ||
@Path("get") | ||
@ApiOperation(value = "Execute get request with no parameters") | ||
public Response getCallback() { | ||
return Response.ok().build(); | ||
} | ||
|
||
@GET | ||
@Path("get/{id}") | ||
@ApiOperation(value = "Execute get request with single id parameter") | ||
public Response getCallback(@PathParam("id") final String id) { | ||
return Response.ok().build(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...izard-test/src/test/java/net/spals/appbuilder/app/dropwizard/doc/DocDropwizardWebApp.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,38 @@ | ||
package net.spals.appbuilder.app.dropwizard.doc; | ||
|
||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.setup.Bootstrap; | ||
import io.dropwizard.setup.Environment; | ||
import net.spals.appbuilder.app.dropwizard.DropwizardWebApp; | ||
import net.spals.appbuilder.config.service.ServiceScan; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A minimally viable {@link DropwizardWebApp} | ||
* | ||
* @author tkral | ||
*/ | ||
public class DocDropwizardWebApp extends Application<Configuration> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(DocDropwizardWebApp.class); | ||
|
||
public static void main(final String[] args) throws Throwable { | ||
new DocDropwizardWebApp().run("server"); | ||
} | ||
|
||
private DropwizardWebApp.Builder webAppDelegateBuilder; | ||
|
||
@Override | ||
public void initialize(final Bootstrap<Configuration> bootstrap) { | ||
webAppDelegateBuilder = new DropwizardWebApp.Builder(bootstrap, LOGGER) | ||
.setServiceScan(new ServiceScan.Builder() | ||
.addServicePackages("net.spals.appbuilder.app.dropwizard.doc") | ||
.build()); | ||
} | ||
|
||
@Override | ||
public void run(final Configuration configuration, final Environment env) { | ||
webAppDelegateBuilder.setEnvironment(env).build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...t/src/test/java/net/spals/appbuilder/app/dropwizard/sample/SampleDropwizardCustomSet.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,8 @@ | ||
package net.spals.appbuilder.app.dropwizard.sample; | ||
|
||
/** | ||
* A multi service definition for {@link SampleDropwizardWebApp} | ||
* | ||
* @author tkral | ||
*/ | ||
public interface SampleDropwizardCustomSet { } |
12 changes: 12 additions & 0 deletions
12
...st/java/net/spals/appbuilder/app/dropwizard/sample/SampleDropwizardCustomSetInstance.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,12 @@ | ||
package net.spals.appbuilder.app.dropwizard.sample; | ||
|
||
import net.spals.appbuilder.annotations.service.AutoBindInSet; | ||
|
||
/** | ||
* An auto-bound multi service for {@link SampleDropwizardWebApp} | ||
* | ||
* @author tkral | ||
*/ | ||
@AutoBindInSet(baseClass = SampleDropwizardCustomSet.class) | ||
class SampleDropwizardCustomSetInstance implements SampleDropwizardCustomSet { | ||
} |
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
Oops, something went wrong.