Skip to content

Commit

Permalink
Merge pull request #15 from scm-spain/feature/add-multiple-package-sc…
Browse files Browse the repository at this point in the history
…anning

Feature/add multiple package scanning
  • Loading branch information
victuxbb committed Nov 19, 2015
2 parents 407632b + 8f0a17a commit 8727519
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/main/java/scmspain/karyon/restrouter/RestRouterScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -111,8 +112,7 @@ public RestRouterScanner(Injector inject,
this.parameterParser = parameterParser;
this.rmParameterInjector = rmParameterInjector;

String basePackage = ConfigurationManager.getConfigInstance().getString(BASE_PACKAGE_PROPERTY);
Set<Class<?>> annotatedTypes = resourceLoader.find(basePackage, Endpoint.class);
Set<Class<?>> annotatedTypes = getEndpointClasses(resourceLoader);

annotatedTypes.stream()
.flatMap(klass ->
Expand All @@ -130,7 +130,16 @@ public RestRouterScanner(Injector inject,
.forEach(this::configurePath);
}

private void configurePath(PathDefinition pathDefinition) {
private Set<Class<?>> getEndpointClasses(ResourceLoader resourceLoader) {
List<String> packagesList = Arrays.asList(ConfigurationManager.getConfigInstance().getStringArray(BASE_PACKAGE_PROPERTY));
Set<Class<?>> annotatedTypes = new HashSet<>();

packagesList.stream().forEach(packageName -> annotatedTypes.addAll(resourceLoader.find(packageName.trim(), Endpoint.class)));

return annotatedTypes;
}

private void configurePath(PathDefinition pathDefinition) {
Method method = pathDefinition.method;

boolean isCustomSerialization = isCustom(method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import rx.Observable;
import scmspain.karyon.restrouter.annotation.Endpoint;
Expand All @@ -16,6 +17,8 @@
import scmspain.karyon.restrouter.core.MethodParameterResolver;
import scmspain.karyon.restrouter.core.ResourceLoader;
import scmspain.karyon.restrouter.core.URIParameterParser;
import scmspain.karyon.restrouter.dummy.DummyController;
import scmspain.karyon.restrouter.endpoint.ExampleEndpointController;
import scmspain.karyon.restrouter.transport.http.RestUriRouter;

import java.util.Collection;
Expand All @@ -26,6 +29,7 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -167,6 +171,23 @@ public void givenAEndPointWithAMethodCustomSerializationItHasPreference() {
verify(restUriRouter).addUriRegex(any(), any(), any(), any(), eq(true), any());
}

@Test
public void givenScanPropertyWithTwoPackagesWhenScanningThenHitsInTwoPackages() throws Exception {
given(resourceLoader.find(eq("scmspain.karyon.restrouter.endpoint"), eq(Endpoint.class)))
.willReturn(Sets.newHashSet(ExampleEndpointController.class));

given(resourceLoader.find(eq("scmspain.karyon.restrouter.dummy"), eq(Endpoint.class)))
.willReturn(Sets.newHashSet(DummyController.class));

new RestRouterScanner(injector, parameterParser,
rmParameterInjector, resourceLoader, restUriRouter);

verify(restUriRouter, atLeastOnce()).addUriRegex(
Matchers.contains("ExampleEndpointController"), any(), any(), any(), eq(true), any());

verify(restUriRouter, atLeastOnce()).addUriRegex(
Matchers.contains("DummyController"), any(), any(), any(), eq(true), any());
}

@Endpoint
static class WithoutPathsAndMethods {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scmspain.karyon.restrouter.dummy;

import rx.Observable;
import scmspain.karyon.restrouter.annotation.Endpoint;
import scmspain.karyon.restrouter.annotation.Path;

import javax.ws.rs.HttpMethod;

/**
* Created by ruben.perez on 16/11/15.
*/
@Endpoint
public class DummyController {

@Path(value = "/dummy/{id}", method = HttpMethod.GET)
public Observable<Void> method() {

return Observable.empty();
}

}
3 changes: 2 additions & 1 deletion src/test/resources/AppServer.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
com.scmspain.karyon.rest.property.packages = scmspain.karyon.restrouter.endpoint
# Multi value (comma-separated) property
com.scmspain.karyon.rest.property.packages = scmspain.karyon.restrouter.endpoint,scmspain.karyon.restrouter.dummy

0 comments on commit 8727519

Please sign in to comment.