Skip to content

Commit

Permalink
Merge pull request #5 from scm-spain/fix/issues_4
Browse files Browse the repository at this point in the history
Fixed nullpointer and refactor of method getParameterValue()
  • Loading branch information
victuxbb committed Sep 8, 2015
2 parents 4663422 + d5cf7bf commit 99f08b7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package scmspain.karyon.restrouter.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import scmspain.karyon.restrouter.annotation.QueryParam;
import scmspain.karyon.restrouter.exception.ParamAnnotationException;
import scmspain.karyon.restrouter.exception.QueryParamRequiredNotFoundException;
Expand All @@ -18,20 +21,16 @@ public QueryParamAnnotation(QueryParam queryParam, Map<String, List<String>> que

@Override
public String getParameterValue() throws ParamAnnotationException {
List<String> values = queryParams.get(queryParam.value());
Optional<List<String>> values = Optional.ofNullable(queryParams.get(queryParam.value()));

String queryParamValue = null;
if (values != null && !values.isEmpty()) {
queryParamValue = values.get(0);
}
if (queryParamValue == null) {
if (!QueryParam.DEFAULT_VALUE.equals(queryParam.defaultValue())) {
queryParamValue = queryParam.defaultValue();
} else if (queryParam.required()) {
throw new QueryParamRequiredNotFoundException(queryParam.value());
}
if(queryParam.required() && !values.isPresent()) throw new QueryParamRequiredNotFoundException(queryParam.value());

if(values.isPresent()){
return values.get().stream().findFirst().get();
}

return queryParamValue;

return queryParam.defaultValue();

}
}
16 changes: 16 additions & 0 deletions src/test/java/scmspain/karyon/restrouter/KaryonRestRouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,20 @@ public void testPathNotFound() throws Exception {
.toBlocking().toFuture().get(10, TimeUnit.SECONDS);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND, status);
}

@Test
public void testEndpointDefaultQueryParamSystem() throws Exception {

String body =
RxNetty.createHttpClient("localhost", AppServer.KaryonRestRouterModuleImpl.DEFAULT_PORT)
.submit(HttpClientRequest.createGet("/example_path_default_system"))
.flatMap(response -> {
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatus().code());
return response.getContent().<String>map(content -> content.toString(Charset.defaultCharset()));
})
.toBlocking().toFuture().get(10, TimeUnit.SECONDS);
Assert.assertEquals("I'm the default queryparam", body);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,10 @@ public Observable<Void> parameterizedPathEndpoint(HttpServerRequest<ByteBuf> req
return response.writeStringAndFlush("I'm the parameterized one");
}

@Path(value = "/example_path_default_system", method = HttpMethod.GET)
public Observable<Void> defaultValueEndpoint(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response, @QueryParam(value = "Ni") String filter) {
return response.writeStringAndFlush("I'm the default queryparam"+filter);
}

}

0 comments on commit 99f08b7

Please sign in to comment.