Skip to content

Commit

Permalink
Added JAX-RS client finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Wojcik authored and Robert Wojcik committed Jun 19, 2020
1 parent 3231d7a commit 1a2a852
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ buildscript {
nexusStagingPluginVersion = '0.20.0'
nexusPublishPluginVersion = '0.2.0'
sonarqubePluginVersion = '2.7'
jaxRsVersion = '2.1.1'
}
repositories (repos)
dependencies {
Expand Down Expand Up @@ -63,6 +64,7 @@ dependencies {
compile "com.lmax:disruptor:$disruptorVersion"
compile "org.reflections:reflections:$reflectionsVersion"
compile "org.springframework.cloud:spring-cloud-starter-openfeign:$feignVersion"
compile "javax.ws.rs:javax.ws.rs-api:$jaxRsVersion"
compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
compile "uk.co.jemos.podam:podam:$podamVersion"
Expand All @@ -71,6 +73,7 @@ dependencies {
annotationProcessor "org.projectlombok:lombok:$lombokVersion"
testCompileOnly "org.projectlombok:lombok:$lombokVersion"
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
testCompile 'org.glassfish.jersey.core:jersey-common:2.22.2'

testCompile "org.codehaus.groovy:groovy-all:$groovyAllVersion"
testCompile "org.spockframework:spock-core:$spockVersion"
Expand Down
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());
}
}
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
}
}
}
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();
}
}
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();
}

0 comments on commit 1a2a852

Please sign in to comment.