Skip to content

Commit

Permalink
Merge pull request #99 from aol/micro-jackson-configuration
Browse files Browse the repository at this point in the history
Micro jackson configuration
  • Loading branch information
johnmcclean committed Dec 7, 2015
2 parents 53ee910 + b305fbc commit 4aa1ca5
Show file tree
Hide file tree
Showing 50 changed files with 508 additions and 65 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version=0.76
version=0.77
springVersion=4.2.3.RELEASE
springBootVersion=1.3.0.RELEASE
jerseyVersion=2.22.1
grizzlyVersion=2.3.23
simpleReactVersion=0.99.5
cyclopsVersion=6.2.0
cyclopsVersion=6.2.1
jacksonVersion=2.6.3
hibernateVersion=5.0.4.Final
hibernateValidator=5.2.2.Final
Expand Down
3 changes: 2 additions & 1 deletion micro-boot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ dependencies {
compile 'org.springframework.boot:spring-boot-autoconfigure:'+springBootVersion
compile project(':micro-core')
testCompile group: 'com.google.guava', name: 'guava', version:'18.0'


testCompile project(':micro-jackson-configuration')
testCompile project(':micro-grizzly')
testCompile project(':micro-swagger')
testCompile project(':micro-client')
Expand Down
1 change: 1 addition & 0 deletions micro-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
compile group: 'org.apache.httpcomponents', name:'httpasyncclient', version:apacheHttpClientVersionAsync

compile project(':micro-core')
compile project(':micro-jackson-configuration')

testCompile group: 'com.google.guava', name: 'guava', version:'18.0'
testCompile project(':micro-reactive')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.aol.micro.server.auto.discovery.Rest;
import com.aol.micro.server.auto.discovery.RestResource;
import com.aol.micro.server.config.Classes;
import com.aol.micro.server.rest.jackson.JacksonFeature;
import com.aol.micro.server.servers.model.ServerData;

public interface Module {
Expand Down Expand Up @@ -66,7 +65,6 @@ default List<String> getDefaultJaxRsPackages(){

default List<Class> getDefaultResources(){
List list = new ArrayList<>();
list.add(JacksonFeature.class);
list.addAll(SequenceM.fromStream(PluginLoader.INSTANCE.plugins.get().stream())
.filter(module -> module.servletContextListeners()!=null)
.flatMapCollection(Plugin::jaxRsResources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public void resetAllIsFalse(){
public void defaultResourcesDefault(){
Module m = ConfigurableModule.builder().build();

assertThat(m.getDefaultResources().size(),equalTo(1));
assertThat(m.getDefaultResources().size(),equalTo(0));
}
@Test
public void defaultResourcesNew(){
Module m = ConfigurableModule.builder().defaultResources(Arrays.asList(this.getClass())).build();
assertThat(m.getDefaultResources().size(),equalTo(2));
assertThat(m.getDefaultResources().size(),equalTo(1));

}
@Test
Expand Down Expand Up @@ -150,7 +150,7 @@ public void testGetDefaultResourcesReset() {
}
@Test
public void testGetDefaultResources() {
assertThat(module.getDefaultResources().size(),is(1));
assertThat(module.getDefaultResources().size(),is(0));
}
@Test
public void testGetDefaultJaxRsPackagesReset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.concurrent.TimeUnit;

import lombok.AllArgsConstructor;
import lombok.experimental.Wither;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.framework.state.ConnectionState;
Expand All @@ -11,6 +14,8 @@

import com.aol.micro.server.utility.DistributedLockService;

@Wither
@AllArgsConstructor
public class DistributedLockServiceCuratorImpl implements DistributedLockService, ConnectionStateListener {

private volatile boolean acquired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import com.aol.micro.server.testing.RestAgent;

@Microserver
public class CuratorRunnerTest {
public class PrototypeLockCuratorRunnerTest {

RestAgent rest = new RestAgent();

MicroserverApp server;
@Before
public void startServer() throws IOException{
FileUtils.deleteDirectory(new File("/tmp/zookeeper"));
FileUtils.deleteDirectory(new File("/tmp/zookeeper/pl"));
new Zookeeper().init();
server = new MicroserverApp(()->"simple-app");

Expand All @@ -45,7 +45,7 @@ public void runAppAndBasicTest() throws InterruptedException, ExecutionException


assertThat(rest.get("http://localhost:8080/simple-app/status/ping"),is("got"));
assertThat(rest.get("http://localhost:8080/simple-app/status/ping"),is("not"));
assertThat(rest.get("http://localhost:8080/simple-app/status/ping"),is("got"));


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void init(){

Properties startupProperties = new Properties();

startupProperties.put("dataDir", "/tmp/zookeeper");
startupProperties.put("dataDir", "/tmp/zookeeper/pl");
startupProperties.put("clientPort", "12181");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CuratorStatusResource(CuratorDistributedLockServiceProvider provider) {
@GET
@Path("/ping")
public String ping() {
if(service.tryLock("hello"))
if(service.tryLock("hello2"))
return "got";
return "not";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import com.aol.micro.server.testing.RestAgent;

@Microserver
public class CuratorRunnerTest {
public class KeepLockCuratorRunnerTest {

RestAgent rest = new RestAgent();

MicroserverApp server;
@Before
public void startServer() throws IOException{
FileUtils.deleteDirectory(new File("/tmp/zookeeper"));
FileUtils.deleteDirectory(new File("/tmp/zookeeper/kl"));

new Zookeeper().init();
server = new MicroserverApp(()->"simple-app");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void init(){

Properties startupProperties = new Properties();

startupProperties.put("dataDir", "/tmp/zookeeper");
startupProperties.put("dataDir", "/tmp/zookeeper/kl");
startupProperties.put("clientPort", "12181");


Expand Down
1 change: 1 addition & 0 deletions micro-general-exception-mapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ description = 'micro-general-exception-mapper'
dependencies {

compile project(':micro-jersey')
compile project(':micro-jackson-configuration')

testCompile project(':micro-grizzly-with-jersey')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public class MapOfExceptionsToErrorCodes {
private static final String UNSUPPORTED_MEDIA_TYPE = "UNSUPPORTED_MEDIA_TYPE";


private volatile static LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> mapOfExceptionsToErrorCodes = new LinkedHashMap<>();

{
mapOfExceptionsToErrorCodes.put(EOFException.class, new Tuple2<String, Status>(EMPTY_REQUEST, Status.BAD_REQUEST));
mapOfExceptionsToErrorCodes.put(JsonProcessingException.class, new Tuple2<String, Status>(JSON_PROCESSING_EXCEPTION, Status.BAD_REQUEST));

private volatile static LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> mapOfExceptionsToErrorCodes = createMap();

private static LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> createMap(){
LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> mapOfExceptionsToErrorCodes = new LinkedHashMap<>();
mapOfExceptionsToErrorCodes.put(EOFException.class, new Tuple2<String, Status>(EMPTY_REQUEST, Status.BAD_REQUEST));
mapOfExceptionsToErrorCodes.put(JsonProcessingException.class, new Tuple2<String, Status>(JSON_PROCESSING_EXCEPTION, Status.BAD_REQUEST));
return mapOfExceptionsToErrorCodes;

}
private volatile static Optional<ExtensionMapOfExceptionsToErrorCodes> extensions = Optional.empty();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.aol.micro.server.general.exception.mapper;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
Expand All @@ -14,15 +11,17 @@
import java.io.EOFException;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;

import app.custom.com.aol.micro.server.copy.MyException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

public class GeneralExceptionMapperTest {

Expand All @@ -47,12 +46,14 @@ public void whenJsonProcessingException_thenBadRequest() {

@Test
public void whenNotFoundException_thenNotFound() {
assertThat(mapper.toResponse(mock(NotFoundException.class)).getStatus(), is(Status.NOT_FOUND.getStatusCode()));
assertThat(mapper.toResponse(new NotFoundException()).getStatus(), is(Status.NOT_FOUND.getStatusCode()));
}

@Test
public void whenWebApplicationException_thenUnsupportedMediaType() {
assertThat(mapper.toResponse(mock(WebApplicationException.class)).getStatus(), is(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()));

assertThat(mapper.toResponse(new NotSupportedException(Response.status(Status.UNSUPPORTED_MEDIA_TYPE).build()))
.getStatus(), is(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()));
}

@Test
Expand All @@ -65,7 +66,7 @@ public void whenRuntimeException_thenInternalServerError() {
@Test
public void whenJacksonException_thenNotInternalServerError_NoErrorsLogged() {
mapper= new GeneralExceptionMapper(mockLogger);
assertThat(mapper.toResponse(mock(UnrecognizedPropertyException.class)).getStatus(), is(not(Status.INTERNAL_SERVER_ERROR.getStatusCode())));
assertThat(mapper.toResponse(new MyLocalException()).getStatus(), is((Status.INTERNAL_SERVER_ERROR.getStatusCode())));
verify(mockLogger, times(0)).error(any(String.class), any(Object[].class));
verify(mockLogger, times(0)).error(any(String.class), any(Throwable.class), any(Object[].class));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.aol.micro.server.general.exception.mapper;

public class MyLocalException extends Exception {

}
1 change: 1 addition & 0 deletions micro-grizzly-with-jersey/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
compile project(':micro-core')
compile project(':micro-grizzly')
compile project(':micro-jersey')
compile project(':micro-jackson-configuration')

testCompile group: 'com.aol.simplereact', name:'simple-react', version:"${simpleReactVersion}"

Expand Down
2 changes: 1 addition & 1 deletion micro-grizzly-with-jersey/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[Example Grizzly & Jersey Apps](https://github.com/aol/micro-server/tree/master/micro-grizzly/src/test/java/app)

Convenience module that packages Grizzly, Jersey and Microserver together
Convenience module that packages Grizzly, Jersey, Jackson and Microserver together

## To use

Expand Down
1 change: 1 addition & 0 deletions micro-grizzly/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {

compile project(':micro-core')
testCompile project(':micro-jersey')
testCompile project(':micro-jackson-configuration')
testCompile group: 'com.aol.simplereact', name:'simple-react', version:"${simpleReactVersion}"


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package app.jackson.com.aol.micro.server.copy;


import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.aol.micro.server.MicroserverApp;
import com.aol.micro.server.config.Microserver;
import com.aol.micro.server.module.ConfigurableModule;
import com.aol.micro.server.testing.RestAgent;

@Microserver
public class CustomRunnerTest {

RestAgent rest = new RestAgent();

MicroserverApp server;
@Before
public void startServer(){
server = new MicroserverApp(ConfigurableModule
.builder()
.context("simple-app")
.defaultResources(Arrays.asList(MultiPartFeature.class))
.build());

server.start();

}

@After
public void stopServer(){
server.stop();
}

@Test
public void runAppAndBasicTest() throws InterruptedException, ExecutionException{


assertThat(rest.postString("http://localhost:8080/simple-app/status/ping","{\"primitive\":null}").getStatus(),is(500));


}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package app.jackson.com.aol.micro.server.copy;

import java.io.EOFException;
import java.io.InputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import com.aol.cyclops.invokedynamic.ExceptionSoftener;
import com.aol.micro.server.auto.discovery.Rest;

@Rest
@Path("/status")
public class CustomStatusResource {

@POST
@Path("/ping")
public String ping(MyEntity entity) {
return "ok";

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app.jackson.com.aol.micro.server.copy;

import org.springframework.stereotype.Component;

import com.aol.micro.server.jackson.JacksonMapperConfigurator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class MapperExtension implements JacksonMapperConfigurator {

@Override
public void accept(ObjectMapper t) {
t.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

}

}
Loading

0 comments on commit 4aa1ca5

Please sign in to comment.