Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from palantir/qchen/serviceInterface
Browse files Browse the repository at this point in the history
code cleanup and added a service interface
  • Loading branch information
qinfchen committed Feb 27, 2016
2 parents 721a04c + 000e8bc commit 4c837f3
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 92 deletions.
28 changes: 19 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,32 @@ buildscript {

plugins {
id 'com.palantir.git-version' version '0.4.0'
id 'org.inferred.processors' version '1.1.5'
}

apply plugin: 'java'
apply plugin: 'com.palantir.baseline-config'
apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-findbugs'
apply plugin: 'com.palantir.jacoco-coverage'
apply from: "${rootDir}/gradle/publish.gradle"

repositories {
jcenter()
}

version gitVersion()
group 'com.palantir.dropwizard.version-info'
allprojects {
group 'com.palantir.dropwizard.version-info'
version gitVersion()
}

subprojects {
apply plugin: 'java'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-findbugs'
apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'com.palantir.jacoco-coverage'
apply from: "${rootDir}/gradle/publish.gradle"

sourceCompatibility = 1.7
repositories {
jcenter()
}

sourceCompatibility = 1.7
}
3 changes: 2 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ machine:

general:
artifacts:
- build/libs
- dropwizard-version-info/build/libs
- dropwizard-version-info-api/build/libs

test:
override:
Expand Down
3 changes: 3 additions & 0 deletions dropwizard-version-info-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
compile "javax.ws.rs:javax.ws.rs-api:$rsApiVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.dropwizard.versioninfo.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
* interface for obtaining the version info.
*/
@Path("/")
public interface VersionInfoService {

@GET
@Path("version")
@Produces(MediaType.TEXT_PLAIN)
String getVersion();
}
15 changes: 2 additions & 13 deletions dropwizard-version-info/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
apply plugin: 'java'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-findbugs'
apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'com.palantir.jacoco-coverage'

apply from: "${rootDir}/gradle/publish.gradle"

repositories {
jcenter()
}

dependencies {
compile project(":dropwizard-version-info-api")

compile "io.dropwizard:dropwizard-core:$dropwizardVersion"

testCompile "com.google.code.findbugs:annotations:$findBugAnnotation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import java.util.Properties;

/**
* Bundle to read product version from a .properties and expose it as a resource.
* Bundle to read product version from a file and expose it as a resource.
*
* @author sfan
*/
public final class VersionInfoBundle implements Bundle {

private static final String DEFAULT_PATH = "version.properties";
private static final String UNKNOWN = "unknown";

Expand All @@ -34,11 +35,13 @@ public VersionInfoBundle() {

public VersionInfoBundle(String path) {
checkArgument(!Strings.isNullOrEmpty(path));

this.version = readResource(path);
}

@Override
public void initialize(Bootstrap<?> bootstrap) {
// do nothing
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@
import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.base.Strings;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.palantir.dropwizard.versioninfo.api.VersionInfoService;

/**
* Resource to display version information.
*
* @author sfan
*/
@Path("/version")
@Produces(MediaType.TEXT_PLAIN)
public final class VersionInfoResource {
public final class VersionInfoResource implements VersionInfoService {

private final String version;

Expand All @@ -29,7 +24,7 @@ public VersionInfoResource(String version) {
this.version = version;
}

@GET
@Override
public String getVersion() {
return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
package com.palantir.dropwizard.versioninfo;

import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public final class TestApp extends Application<TestConfig> {
public final class TestApp extends Application<Configuration> {

@Override
public void initialize(Bootstrap<TestConfig> bootstrap) {
public void initialize(Bootstrap<Configuration> bootstrap) {
bootstrap.addBundle(new VersionInfoBundle());
}

@Override
public void run(TestConfig configuration, Environment environment) throws Exception {
public void run(Configuration configuration, Environment environment) throws Exception {
// do nothing
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@

package com.palantir.dropwizard.versioninfo;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.io.Resources;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.DropwizardAppRule;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.hamcrest.core.IsInstanceOf;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

Expand All @@ -34,33 +31,23 @@
public final class VersionInfoBundleTests {

@ClassRule
public static final DropwizardAppRule<TestConfig> RULE =
new DropwizardAppRule<TestConfig>(TestApp.class, Resources.getResource("test-app-config.yml").getPath());

private Environment environment;
private JerseyEnvironment jerseyEnvironment;
private Bootstrap<?> bootstrap;

@Before
public void setUp() {
environment = mock(Environment.class);
jerseyEnvironment = mock(JerseyEnvironment.class);
when(environment.jersey()).thenReturn(jerseyEnvironment);
bootstrap = mock(Bootstrap.class);
}
public static final DropwizardAppRule<Configuration> RULE =
new DropwizardAppRule<Configuration>(TestApp.class, Resources.getResource("test-app-config.yml").getPath());

@Test
public void testAddsVersionInfoResource() {
Environment environment = mock(Environment.class, RETURNS_DEEP_STUBS);

VersionInfoBundle versionInfoBundle = new VersionInfoBundle();
versionInfoBundle.initialize(bootstrap);
versionInfoBundle.initialize(mock(Bootstrap.class));
versionInfoBundle.run(environment);
verify(jerseyEnvironment).register(isA(VersionInfoResource.class));
verify(environment.jersey()).register(isA(VersionInfoResource.class));
}

@Test
public void testVersionPropertiesFileDoesNotExist() {
try {
assertNull(new VersionInfoBundle("filedoesntexist.properties"));
new VersionInfoBundle("filedoesntexist.properties");
fail();
} catch (RuntimeException e) {
assertThat(e.getCause(), IsInstanceOf.instanceOf(IllegalArgumentException.class));
Expand All @@ -76,13 +63,9 @@ public void testReturnsVersionFromDefaultPropertiesFile() {
assertEquals(response, "2.0.0");
}

@Test
@SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL")
@Test(expected = IllegalArgumentException.class)
public void testInvalidPath() {
try {
assertNull(new VersionInfoBundle(null));
fail();
} catch (IllegalArgumentException e) {
// expected
}
new VersionInfoBundle(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
package com.palantir.dropwizard.versioninfo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Tests for {@link VersionInfoResource}.
*
* @author sfan
*/
public final class VersionInfoResourceTests {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testVersion() {
Expand All @@ -27,9 +22,8 @@ public void testVersion() {
assertEquals(version, versionInfoResource.getVersion());
}

@Test
@Test(expected = IllegalArgumentException.class)
public void testInvalidVersion() {
thrown.expect(IllegalArgumentException.class);
assertNull(new VersionInfoResource(null));
new VersionInfoResource(null);
}
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bintrayVersion=1.4

# compile
dropwizardVersion=0.9.2
rsApiVersion=2.0.1

# testCompile
findBugAnnotation=3.0.0
Expand Down
31 changes: 27 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Using the bundle
key in the root of your classpath. It should look something like
this:

``` {.sourceCode .none}
```
productVersion: <version>
```
Expand All @@ -33,7 +33,7 @@ Using the bundle
(https://github.com/palantir/dropwizard-version-info/releases).
The dependencies section should look something like this:
``` {.sourceCode .none}
```
dependencies {
// ... unrelated dependencies omitted ...
Expand All @@ -45,7 +45,7 @@ Using the bundle
(or create one if it doesn't already exist). It should look
something like this:
``` {.sourceCode .java}
```
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new VersionInfoBundle());
Expand All @@ -56,14 +56,37 @@ Using the bundle
is defined by the `rootPath` and `applicationContextPath` in your
Dropwizard server configuration.
5. If your application uses [Feign](https://github.com/Netflix/feign)
to build clients, make sure your service interface extends
VersionInfoService interface. The dependencies section for your
`-api` project should look like this:
```
dependencies {
// ... unrelated dependencies omitted ...
compile "com.palantir.dropwizard:dropwizard-version-info-api:<VERSION>"
}
```
Your service interface should look like this:
```
public interface ExampleService extends VersionInfoService {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
String hello();
}
```
Custom resource path
--------------------
To load the version.properties file from somewhere else in the
classpath, pass the path to it as an argument to the bundle's
constructor:
``` {.sourceCode .java}
```
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new VersionInfoBundle("/properties/info.properties"));
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
rootProject.name = "dropwizard-version-info"

include "dropwizard-version-info"
include "dropwizard-version-info-api"

0 comments on commit 4c837f3

Please sign in to comment.