Skip to content

Commit bf9d292

Browse files
authored
Testing feign-example-github jar (OpenFeign#953)
* Running GitHubExample as unit test * Testing feign-example-github jar * Rename packages to cross module boundary * Adding github token
1 parent 87dfff8 commit bf9d292

8 files changed

Lines changed: 141 additions & 21 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ env:
5959
- secure: "ONAU76S0WBGcQGf0mr7KxKQjFvhhu73GNuQG8j47pxhJojNlNpWBbu+EGkgaInWKMtO89iBtpicVlXZc06HtbSqv7L93gbMo+xgp5daLlQg4gocDixjB1I2oPPITFFoztu76nOA1IBWRLTKu+w+Y2tKOmzWm+5v2UKD6fz7SYoo="
6060
# Ex. travis encrypt SONATYPE_PASSWORD=your_sonatype_password
6161
- secure: "UaVTxnw8klS36WLAdcmubqrHIgS4o5NcIqQMPIihk0tv3VEvCJSGvc2b7EPyQZMvm5TR3mXq5IJUAHp8j3seAHfYWmLIZWzvn7Y5mLRw8Kh9up7GzXl8Idui0AEHAAL2mfvE9smlOKPS5D13LKc6tOGFER66itHW3Jg1QoijDmQ="
62-
62+
# Ex. travis encrypt GITHUB_TOKEN=token_for_tests
63+
- secure: "H4PuppuPE3lkvVQ1osulhgWeZmpIkDKj/z74lx4MUeDPNtcuqpwmTVWtL5Zyjf8CxlALX2djx4RIBshaQAu4GtKarPLONinNLZ/TCtoK8dF08/ESxLEiLQzwGkS+geWoEFiZncB5Px2T7ZbUfVFO3crVY9CLn35znR8k1uidocL0JlyVPGwCwuBxFmDhs3BZh3JvbwSikAVRvlCRU6BbREFQbSK1EamuUju/rlo+dx7W5tiiuEJJ50c8vpgatTFyy821YP82fMRrhuBDpS4/rsL9DmLhQTEbCjZW+22DhEFPRlo0XIfidC7APybXnu3oO+jFuGaFKiQdy7sjB03g/Bz5H7jAIAkbl8UpbjN+IoeUU/OgMuBYf5wJjPDYUEdI3CXqywPn0xYZwVsOcSg+UkQGYdW9ux/U+nKsYLXLWWhst2QMFzbmO94KCrpgCW4mshr/5WP4XU6cEJwDsKMAUPWuOk0KMMjIufSgvPvteWZwT9akZwzEMuGaUQ5kLr1X6xTPv1cKXTreitaoOLQs28kmPVfTwVEdareaSVXcRqeflJJBSXkAgBqGhV5CAEUaUgt9/QD0Jj5RGyRPllFcydXVLTPeg62X/L5COswlvJhPkvfNnkbMpDQZYojKKPmAf+UqZJmVYPpOoNEXygldueKeunWkna/wYkMj0YnOkM8="

example-github/pom.xml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<version>10.3.0-SNAPSHOT</version>
2424
</parent>
2525

26-
<groupId>io.github.openfeign</groupId>
2726
<artifactId>feign-example-github</artifactId>
2827
<packaging>jar</packaging>
2928
<name>GitHub Example</name>
@@ -36,12 +35,16 @@
3635
<dependency>
3736
<groupId>io.github.openfeign</groupId>
3837
<artifactId>feign-core</artifactId>
39-
<version>${project.version}</version>
4038
</dependency>
4139
<dependency>
4240
<groupId>io.github.openfeign</groupId>
4341
<artifactId>feign-gson</artifactId>
44-
<version>${project.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.commons</groupId>
45+
<artifactId>commons-exec</artifactId>
46+
<version>1.3</version>
47+
<scope>test</scope>
4548
</dependency>
4649
</dependencies>
4750

@@ -61,7 +64,7 @@
6164
<configuration>
6265
<transformers>
6366
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
64-
<mainClass>feign.example.github.GitHubExample</mainClass>
67+
<mainClass>example.github.GitHubExample</mainClass>
6568
</transformer>
6669
</transformers>
6770
<createDependencyReducedPom>false</createDependencyReducedPom>
@@ -87,11 +90,16 @@
8790
</plugin>
8891
<plugin>
8992
<groupId>org.apache.maven.plugins</groupId>
90-
<artifactId>maven-compiler-plugin</artifactId>
91-
<configuration>
92-
<source>1.8</source>
93-
<target>1.8</target>
94-
</configuration>
93+
<artifactId>maven-failsafe-plugin</artifactId>
94+
<version>${maven-surefire-plugin.version}</version>
95+
<executions>
96+
<execution>
97+
<goals>
98+
<goal>integration-test</goal>
99+
<goal>verify</goal>
100+
</goals>
101+
</execution>
102+
</executions>
95103
</plugin>
96104
</plugins>
97105
</build>

example-github/src/main/java/feign/example/github/GitHubExample.java renamed to example-github/src/main/java/example/github/GitHubExample.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* or implied. See the License for the specific language governing permissions and limitations under
1212
* the License.
1313
*/
14-
package feign.example.github;
14+
package example.github;
1515

1616
import feign.Feign;
1717
import feign.Logger;
@@ -30,6 +30,8 @@
3030
*/
3131
public class GitHubExample {
3232

33+
private static final String GITHUB_TOKEN = "GITHUB_TOKEN";
34+
3335
interface GitHub {
3436

3537
class Repository {
@@ -62,6 +64,14 @@ static GitHub connect() {
6264
.errorDecoder(new GitHubErrorDecoder(decoder))
6365
.logger(new Logger.ErrorLogger())
6466
.logLevel(Logger.Level.BASIC)
67+
.requestInterceptor(template -> {
68+
if (System.getenv().containsKey(GITHUB_TOKEN)) {
69+
System.out.println("Detected Authorization token from environment variable");
70+
template.header(
71+
"Authorization",
72+
"token " + System.getenv(GITHUB_TOKEN));
73+
}
74+
})
6575
.target(GitHub.class, "https://api.github.com");
6676
}
6777
}
@@ -105,6 +115,8 @@ static class GitHubErrorDecoder implements ErrorDecoder {
105115
@Override
106116
public Exception decode(String methodKey, Response response) {
107117
try {
118+
// must replace status by 200 other GSONDecoder returns null
119+
response = response.toBuilder().status(200).build();
108120
return (Exception) decoder.decode(response, GitHubClientError.class);
109121
} catch (IOException fallbackToDefault) {
110122
return defaultDecoder.decode(methodKey, response);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2012-2019 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.example.github;
15+
16+
import static org.junit.Assert.assertThat;
17+
import org.apache.commons.exec.CommandLine;
18+
import org.apache.commons.exec.DefaultExecutor;
19+
import org.hamcrest.CoreMatchers;
20+
import org.junit.Test;
21+
import java.io.File;
22+
import java.util.Arrays;
23+
24+
/**
25+
* Run main for {@link GitHubExampleIT}
26+
*/
27+
public class GitHubExampleIT {
28+
29+
@Test
30+
public void runMain() throws Exception {
31+
final String jar = Arrays.stream(new File("target").listFiles())
32+
.filter(file -> file.getName().startsWith("feign-example-github")
33+
&& file.getName().endsWith(".jar"))
34+
.findFirst()
35+
.map(File::getAbsolutePath)
36+
.get();
37+
38+
final String line = "java -jar " + jar;
39+
final CommandLine cmdLine = CommandLine.parse(line);
40+
final int exitValue = new DefaultExecutor().execute(cmdLine);
41+
42+
assertThat(exitValue, CoreMatchers.equalTo(0));
43+
}
44+
45+
}

example-wikipedia/pom.xml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
<dependency>
3737
<groupId>io.github.openfeign</groupId>
3838
<artifactId>feign-core</artifactId>
39-
<version>${project.version}</version>
4039
</dependency>
4140
<dependency>
4241
<groupId>io.github.openfeign</groupId>
4342
<artifactId>feign-gson</artifactId>
44-
<version>${project.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.commons</groupId>
46+
<artifactId>commons-exec</artifactId>
47+
<version>1.3</version>
48+
<scope>test</scope>
4549
</dependency>
4650
</dependencies>
4751

@@ -61,7 +65,7 @@
6165
<configuration>
6266
<transformers>
6367
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
64-
<mainClass>feign.example.wikipedia.WikipediaExample</mainClass>
68+
<mainClass>example.wikipedia.WikipediaExample</mainClass>
6569
</transformer>
6670
</transformers>
6771
<createDependencyReducedPom>false</createDependencyReducedPom>
@@ -87,11 +91,16 @@
8791
</plugin>
8892
<plugin>
8993
<groupId>org.apache.maven.plugins</groupId>
90-
<artifactId>maven-compiler-plugin</artifactId>
91-
<configuration>
92-
<source>6</source>
93-
<target>6</target>
94-
</configuration>
94+
<artifactId>maven-failsafe-plugin</artifactId>
95+
<version>${maven-surefire-plugin.version}</version>
96+
<executions>
97+
<execution>
98+
<goals>
99+
<goal>integration-test</goal>
100+
<goal>verify</goal>
101+
</goals>
102+
</execution>
103+
</executions>
95104
</plugin>
96105
</plugins>
97106
</build>

example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java renamed to example-wikipedia/src/main/java/example/wikipedia/ResponseAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* or implied. See the License for the specific language governing permissions and limitations under
1212
* the License.
1313
*/
14-
package feign.example.wikipedia;
14+
package example.wikipedia;
1515

1616
import com.google.gson.TypeAdapter;
1717
import com.google.gson.stream.JsonReader;

example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java renamed to example-wikipedia/src/main/java/example/wikipedia/WikipediaExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* or implied. See the License for the specific language governing permissions and limitations under
1212
* the License.
1313
*/
14-
package feign.example.wikipedia;
14+
package example.wikipedia;
1515

1616
import com.google.gson.Gson;
1717
import com.google.gson.GsonBuilder;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2012-2019 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.example.wikipedia;
15+
16+
import static org.junit.Assert.assertThat;
17+
import org.apache.commons.exec.CommandLine;
18+
import org.apache.commons.exec.DefaultExecutor;
19+
import org.hamcrest.CoreMatchers;
20+
import org.junit.Test;
21+
import java.io.File;
22+
import java.util.Arrays;
23+
24+
/**
25+
* Run main for {@link WikipediaExampleIT}
26+
*/
27+
public class WikipediaExampleIT {
28+
29+
@Test
30+
public void runMain() throws Exception {
31+
final String jar = Arrays.stream(new File("target").listFiles())
32+
.filter(file -> file.getName().startsWith("feign-example-wikipedia")
33+
&& file.getName().endsWith(".jar"))
34+
.findFirst()
35+
.map(File::getAbsolutePath)
36+
.get();
37+
38+
final String line = "java -jar " + jar;
39+
final CommandLine cmdLine = CommandLine.parse(line);
40+
final int exitValue = new DefaultExecutor().execute(cmdLine);
41+
42+
assertThat(exitValue, CoreMatchers.equalTo(0));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)