-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Wojcik
authored and
Robert Wojcik
committed
Jun 19, 2020
1 parent
3231d7a
commit 1a2a852
Showing
5 changed files
with
93 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/hltech/pact/gen/domain/client/jaxrs/JaxRsClientsFinder.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,33 @@ | ||
package com.hltech.pact.gen.domain.client.jaxrs; | ||
|
||
import org.reflections.Reflections; | ||
import org.reflections.scanners.MethodAnnotationsScanner; | ||
|
||
import javax.ws.rs.Path; | ||
import java.lang.reflect.Method; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class JaxRsClientsFinder { | ||
|
||
public Set<Class<?>> findJaxRsClients(String packageRoot) { | ||
Set<Class<?>> jaxRsClients = new HashSet<>(); | ||
jaxRsClients.addAll(classAnnotatedClients(packageRoot)); | ||
jaxRsClients.addAll(methodAnnotatedClients(packageRoot)); | ||
|
||
return jaxRsClients; | ||
} | ||
|
||
private Set<Class<?>> classAnnotatedClients(String packageRoot) { | ||
return new Reflections(packageRoot) | ||
.getTypesAnnotatedWith(Path.class); | ||
} | ||
|
||
private Set<Class<?>> methodAnnotatedClients(String packageRoot) { | ||
return new Reflections(packageRoot, new MethodAnnotationsScanner()) | ||
.getMethodsAnnotatedWith(Path.class).stream() | ||
.map(Method::getDeclaringClass) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/unit/com/hltech/pact/gen/domain/client/jaxrs/JaxRsClientsFinderUT.groovy
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,34 @@ | ||
package com.hltech.pact.gen.domain.client.jaxrs | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class JaxRsClientsFinderUT extends Specification { | ||
|
||
@Subject | ||
private JaxRsClientsFinder finder = new JaxRsClientsFinder() | ||
|
||
def "should find jax rs client"() { | ||
when: | ||
Set<Class<?>> jaxRsClients = finder.findJaxRsClients('com.hltech.pact.gen.domain.client.jaxrs.sample') | ||
|
||
then: | ||
jaxRsClients.size() == 2 | ||
verifyJaxRsClient(jaxRsClients, 'PathJaxRsClassAnnotatedClient') | ||
verifyJaxRsClient(jaxRsClients, 'PathJaxRsMethodAnnotatedClient') | ||
} | ||
|
||
def "should not find feign clients"() { | ||
when: | ||
Set<Class<?>> feignClients = finder.findJaxRsClients('some.not.existing.package') | ||
|
||
then: | ||
feignClients.size() == 0 | ||
} | ||
|
||
private static boolean verifyJaxRsClient(Set<Class<?>> jaxRsClients, String name) { | ||
jaxRsClients.any { client -> | ||
client.simpleName == name | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...st/unit/com/hltech/pact/gen/domain/client/jaxrs/sample/PathJaxRsClassAnnotatedClient.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,14 @@ | ||
package com.hltech.pact.gen.domain.client.jaxrs.sample; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path(value = "/testPathGet") | ||
public class PathJaxRsClassAnnotatedClient { | ||
|
||
@GET | ||
public Response httpGetResponseAccepted() { | ||
return Response.accepted().build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...t/unit/com/hltech/pact/gen/domain/client/jaxrs/sample/PathJaxRsMethodAnnotatedClient.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,9 @@ | ||
package com.hltech.pact.gen.domain.client.jaxrs.sample; | ||
|
||
import javax.ws.rs.Path; | ||
|
||
public interface PathJaxRsMethodAnnotatedClient { | ||
|
||
@Path(value = "/testPath") | ||
String getTestObject(); | ||
} |