JacksonFactory { static ObjectMapper createObjectMapper(CRestConfig crestConfig, Class<?> source){ String prefix = source.getName(); ObjectMapper mapper = crestConfig.get(prefix + JACKSON_OBJECT_MAPPER); if(mapper != null){ return mapper; } mapper = new ObjectMapper(); Map<DeserializationConfig.Feature, Boolean> deserConfig = crestConfig.get(prefix + JACKSON_DESERIALIZER_CONFIG, DEFAULT_DESERIALIZER_CONFIG); for(Map.Entry<DeserializationConfig.Feature,Boolean> feature : deserConfig.entrySet()){ mapper.configure(feature.getKey(), feature.getValue()); } Map<SerializationConfig.Feature, Boolean> serConfig = crestConfig.get(prefix + JACKSON_SERIALIZER_CONFIG, DEFAULT_SERIALIZER_CONFIG); for(Map.Entry<SerializationConfig.Feature,Boolean> feature : serConfig.entrySet()){ mapper.configure(feature.getKey(), feature.getValue()); } return mapper; } private  JacksonFactory();   }
@Test public void shouldNotCreateAnObjectMapperButUseTheCustomOn(){ ObjectMapper mockObjectMapper = mock(ObjectMapper.class); when(mockCRestConfig.get(JacksonFactoryTest.class.getName() + JacksonFactory.JACKSON_OBJECT_MAPPER)).thenReturn(mockObjectMapper); ObjectMapper actual = JacksonFactory.createObjectMapper(mockCRestConfig, getClass()); assertEquals(mockObjectMapper, actual); }
@Test public void shouldCreateAnObjectMapperWithDefaultConfig(){ ObjectMapper actual = JacksonFactory.createObjectMapper(mockCRestConfig, getClass()); assertNotNull(actual); assertFalse(actual.getDeserializationConfig().isEnabled(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES)); }
@Test public void shouldCreateAnObjectMapperWithGivenConfig(){ Map<DeserializationConfig.Feature, Boolean> deserialization = new HashMap<DeserializationConfig.Feature, Boolean>(); Map<SerializationConfig.Feature, Boolean> serialization = new HashMap<SerializationConfig.Feature, Boolean>(); deserialization.put(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true); deserialization.put(DeserializationConfig.Feature.AUTO_DETECT_SETTERS, false); serialization.put(SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS, false); serialization.put(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); when(mockCRestConfig.get(eq(JacksonFactoryTest.class.getName() + JacksonFactory.JACKSON_DESERIALIZER_CONFIG), any(Object.class))).thenReturn(deserialization); when(mockCRestConfig.get(eq(JacksonFactoryTest.class.getName() + JacksonFactory.JACKSON_SERIALIZER_CONFIG), any(Object.class))).thenReturn(serialization); ObjectMapper actual = JacksonFactory.createObjectMapper(mockCRestConfig, getClass()); assertNotNull(actual); assertTrue(actual.getDeserializationConfig().isEnabled(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(actual.getDeserializationConfig().isEnabled(DeserializationConfig.Feature.AUTO_DETECT_SETTERS)); assertFalse(actual.getSerializationConfig().isEnabled(SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS)); assertFalse(actual.getSerializationConfig().isEnabled(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)); }