Skip to content

Commit

Permalink
Merge pull request #9 from scm-spain/fix/parameter_resolver
Browse files Browse the repository at this point in the history
Formating param values error
  • Loading branch information
victuxbb committed Sep 14, 2015
2 parents 5343c9e + 4fa0002 commit 9f366ef
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ gradle-app.setting

#Ignore .gradle dir
.gradle
build_release.gradle
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
testCompile 'org.powermock:powermock-module-junit4:1.6.2'
testCompile 'com.sun.jersey:jersey-core:1.19'
testCompile 'com.netflix.karyon:karyon2-archaius:2.7.1'
testCompile 'com.tngtech.java:junit-dataprovider:1.10.0'
compile 'com.netflix.karyon:karyon2-core:2.5.1'
compile 'com.netflix.karyon:karyon2-governator:2.5.1'
compile 'org.reflections:reflections:0.9.9'
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Sep 06 02:21:04 CEST 2015
#Thu Sep 10 20:42:37 CEST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-bin.zip
6 changes: 6 additions & 0 deletions src/main/java/scmspain/karyon/restrouter/RestBasedRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import scmspain.karyon.restrouter.core.ResourceLoader;
import scmspain.karyon.restrouter.core.URIParameterParser;
import scmspain.karyon.restrouter.exception.ParamAnnotationException;
import scmspain.karyon.restrouter.exception.UnsupportedFormatException;
import scmspain.karyon.restrouter.transport.http.RestUriRouter;

import static org.reflections.ReflectionUtils.*;
Expand Down Expand Up @@ -95,6 +96,11 @@ public RestBasedRouter(Injector inject, URIParameterParser parameterParser,
} catch (ParamAnnotationException e) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return Observable.empty();
} catch (UnsupportedFormatException e) {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
throw new RuntimeException(
String.format("Impossible to resolve params in method \"%s\" ",
method.toString()), e);
} catch (InvocationTargetException e) {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
throw new RuntimeException("Exception invoking method " + method.toString(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import scmspain.karyon.restrouter.annotation.PathParam;
import scmspain.karyon.restrouter.annotation.QueryParam;
import scmspain.karyon.restrouter.exception.ParamAnnotationException;
import scmspain.karyon.restrouter.exception.UnsupportedFormatException;

@Singleton
public class MethodParameterResolver {
Expand All @@ -22,7 +23,14 @@ public MethodParameterResolver(ParamTypeResolver paramTypeResolver){
this.paramTypeResolver = paramTypeResolver;
}

public Object[] resolveParameters(Method method, HttpServerRequest request, HttpServerResponse response, Map<String, String> pathParams, Map<String, List<String>> queryParams) throws ParamAnnotationException {
public Object[] resolveParameters(
Method method,
HttpServerRequest request,
HttpServerResponse response,
Map<String, String> pathParams,
Map<String, List<String>> queryParams
) throws ParamAnnotationException, UnsupportedFormatException
{

Annotation[][] parametersAnnotations = method.getParameterAnnotations();
Class[] parametersTypes = method.getParameterTypes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package scmspain.karyon.restrouter.core;

import com.google.inject.Singleton;
import scmspain.karyon.restrouter.exception.UnsupportedFormatException;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Singleton
public class ParamTypeResolver<T> {

private Map<Class, Function<String, T>> resolver = new HashMap<Class, Function<String, T>>() {{
put(int.class, (val) -> (T) Integer.valueOf(val));
put(double.class, (val) -> (T) Double.valueOf(val));
put(boolean.class, (val) -> (T) Boolean.valueOf(val));
put(long.class, (val) -> (T) Long.valueOf(val));
put(short.class, (val) -> (T) Short.valueOf(val));
put(float.class, (val) -> (T) Float.valueOf(val));

put(String.class, (val) -> (T) val);
put(Integer.class, (val) -> (T) Integer.valueOf(val));
put(Double.class, (val) -> (T) Double.valueOf(val));
put(Boolean.class, (val) -> (T) Boolean.valueOf(val));
put(Long.class, (val) -> (T) Long.valueOf(val));
put(Short.class, (val) -> (T) Short.valueOf(val));
put(Float.class, (val) -> (T) Float.valueOf(val));
}};

public T resolveValueType(Class<T> classType, String value) throws UnsupportedFormatException {

public T resolveValueType(Class<T> classType, String value){
if (classType.isAssignableFrom(String.class)) {
return (T)value;
} else if (classType.isAssignableFrom(int.class) || classType.isAssignableFrom(Integer.class)) {
return (T)Integer.valueOf(value);
} else if (classType.isAssignableFrom(double.class) || classType.isAssignableFrom(Double.class)) {
return (T)Double.valueOf(value);
} else if (classType.isAssignableFrom(boolean.class) || classType.isAssignableFrom(Boolean.class)) {
return (T)Boolean.valueOf(value);
} else if (classType.isAssignableFrom(long.class) || classType.isAssignableFrom(Long.class)) {
return (T)Boolean.valueOf(value);
} else if (classType.isAssignableFrom(short.class) || classType.isAssignableFrom(Short.class)) {
return (T)Boolean.valueOf(value);
} else if (classType.isAssignableFrom(float.class) || classType.isAssignableFrom(Float.class)) {
return (T)Boolean.valueOf(value);
}
return null;
return Optional
.ofNullable(resolver.get(classType))
.map(casting -> casting.apply(value))
.orElseThrow(() -> new UnsupportedFormatException(classType.getName()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public class PathParamAnnotation implements ParamAnnotation {
private final PathParam annotation;
private Map<String, String> pathParams;

public PathParamAnnotation(PathParam annotation,Map<String, String> pathParams) {
public PathParamAnnotation(
PathParam annotation,Map<String,
String> pathParams
) {
this.annotation = annotation;
this.pathParams = pathParams;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package scmspain.karyon.restrouter.exception;


public class UnsupportedFormatException extends Exception {
public UnsupportedFormatException(String classTryingToFormat) {
super(String.format("Class \"%s\" is not supported.", classTryingToFormat ));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package scmspain.karyon.restrouter.core;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import scmspain.karyon.restrouter.exception.UnsupportedFormatException;

import static org.junit.Assert.*;

@RunWith(DataProviderRunner.class)
public class ParamTypeResolverTest<T> {

ParamTypeResolver<T> parameterResolver;

@Before
public void setUp() {
parameterResolver = new ParamTypeResolver<>();
}

@DataProvider
public static Object[][] dataProviderAdd() {
// @formatter:off
return new Object[][] {
{ String.class, "lorem ipsum", String.class },
{ Boolean.class, "true", Boolean.class },
{ Long.class, "27", Long.class },
{ Integer.class, "42", Integer.class },
{ Short.class, "2", Short.class },
{ Float.class, "27.42", Float.class },
{ Double.class, "27.42", Double.class },

{ boolean.class, "true", Boolean.class },
{ long.class, "27", Long.class },
{ int.class, "42", Integer.class },
{ short.class, "2", Short.class },
{ float.class, "27.42", Float.class },
{ double.class, "42.42", Double.class },
};
// @formatter:on
}


@Test
@UseDataProvider("dataProviderAdd")
public void itShouldResolveValueType(
Class<T> type,
String value,
Class<T> expectedClass
) throws Exception {
assertEquals(expectedClass, parameterResolver.resolveValueType(type, value).getClass());
}

@Test(expected = UnsupportedFormatException.class)
public void itShouldReturnsErrorIfCanNotResolveValueType() throws Exception {
Class anyUnsupportedClass = Byte.class;
parameterResolver.resolveValueType(anyUnsupportedClass, "byte");
}
}

0 comments on commit 9f366ef

Please sign in to comment.