Skip to content

Commit

Permalink
Added tests and documentation for REST interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Nov 11, 2018
1 parent d291029 commit ce329c2
Show file tree
Hide file tree
Showing 53 changed files with 848 additions and 174 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# DHIS2 FHIR Adapter
## Overview
This repository contains the source code of the DHIS2 FHIR Adapter. The current scope of the Adapter is to import data into DHIS2 Tracker by using FHIR Subscriptions. Event if the adapter may support more FHIR Resource types at the moment, the initial
official support is for FHIR Patient resources that are transformed to DHIS2 Tracked Entity instances.
official support is for FHIR Patient resources that are transformed to DHIS2 Tracked Entity instances.

The import works on the basis of a domain specific business rule engine that decides about transformations of medical data to questionnaire-like structures (DHIS2 Tracker Programs and their Program Stages).

![DHIS2 FHIR Adapter High Level Architecture](docs/images/DHIS2_FHIR_Adapter_High_Level_Architecture.png "DHIS2 FHIR Adapter High Level Architecture")

Expand Down
51 changes: 13 additions & 38 deletions app/src/main/java/org/dhis2/fhir/adapter/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,31 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.dhis2.fhir.adapter.jackson.SecuredPropertyFilter;
import org.dhis2.fhir.adapter.jackson.ToManyPropertyFilter;
import org.dhis2.fhir.adapter.jackson.ToOnePropertyFilter;
import org.dhis2.fhir.adapter.jackson.ZonedDateTimeDeserializer;
import org.dhis2.fhir.adapter.jackson.ZonedDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.dhis2.fhir.adapter.converter.ZonedDateTimeToDateConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.HttpMethod;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Nonnull;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.util.Date;

/**
* Common application configuration.
*
* @author volsch
*/
@Configuration
@Validated
public class AppConfig
{
@Bean
@Order( 1 )
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer()
{
return jacksonObjectMapperBuilder -> {
final ZoneId zoneId = ZoneId.systemDefault();
jacksonObjectMapperBuilder
.serializers(
new ZonedDateTimeSerializer(),
new LocalDateSerializer( DateTimeFormatter.ISO_LOCAL_DATE ) )
.deserializers(
new ZonedDateTimeDeserializer(),
new LocalDateDeserializer( DateTimeFormatter.ISO_LOCAL_DATE ) );
jacksonObjectMapperBuilder.filters( new SimpleFilterProvider()
.addFilter( SecuredPropertyFilter.FILTER_NAME, new SecuredPropertyFilter() )
.addFilter( ToManyPropertyFilter.FILTER_NAME, new SimpleBeanPropertyFilter()
{
} )
.addFilter( ToOnePropertyFilter.FILTER_NAME, new SimpleBeanPropertyFilter()
{
} ) );
};
}

@Bean
@Nonnull
public WebMvcConfigurer corsConfigurer()
public WebMvcConfigurer mvcConfigurer()
{
return new WebMvcConfigurer()
{
Expand All @@ -95,6 +64,12 @@ public void addCorsMappings( @Nonnull CorsRegistry registry )
.allowedMethods( HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.PUT.name(), HttpMethod.PATCH.name(), HttpMethod.DELETE.name(), HttpMethod.HEAD.name(), HttpMethod.OPTIONS.name() )
.allowCredentials( true );
}

@Override
public void addFormatters( @Nonnull FormatterRegistry registry )
{
registry.addConverter( ZonedDateTime.class, Date.class, new ZonedDateTimeToDateConverter() );
}
};
}
}
7 changes: 4 additions & 3 deletions app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#

spring:
jackson:
serialization:
write-dates-as-timestamps: false
data:
rest:
basePath: /api
Expand All @@ -39,6 +36,10 @@ spring:
hibernate:
ddl-auto: none
open-in-view: false
properties:
hibernate:
jdbc:
time_zone: UTC
application:
name: DHIS2 FHIR Adapter

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/resources/templates/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<title>DHIS2 FHIR Adapter - Initial Setup</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
br {
line-height : 150%;
}

label {
display : inline-block;
width : 150px;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.dhis2.fhir.adapter.converter;

/*
* Copyright (c) 2004-2018, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
* Converts a zoned date time to a date with system time zone.
*
* @author volsch
*/
public class ZonedDateTimeToDateConverter extends TypedConverter<ZonedDateTime, Date>
{
private final ZoneId zoneId = ZoneId.systemDefault();

public ZonedDateTimeToDateConverter()
{
super( ZonedDateTime.class, Date.class );
}

@Override
@Nullable
public Date doConvert( @Nonnull ZonedDateTime source )
{
return Date.from( source.toInstant() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.dhis2.fhir.adapter.jackson;

/*
* Copyright (c) 2004-2018, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.validation.annotation.Validated;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
* Configures Jackson for the application.
*
* @author volsch
*/
@Configuration
@Validated
public class JacksonConfig
{
@Bean
@Order( 1 )
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer()
{
return jacksonObjectMapperBuilder -> {
final ZoneId zoneId = ZoneId.systemDefault();
jacksonObjectMapperBuilder
.serializers(
new ZonedDateTimeSerializer(),
new LocalDateSerializer( DateTimeFormatter.ISO_LOCAL_DATE ) )
.deserializers(
new ZonedDateTimeDeserializer(),
new LocalDateDeserializer( DateTimeFormatter.ISO_LOCAL_DATE ) );
jacksonObjectMapperBuilder.filters( new SimpleFilterProvider()
.addFilter( SecuredPropertyFilter.FILTER_NAME, new SecuredPropertyFilter() )
.addFilter( ToManyPropertyFilter.FILTER_NAME, new SimpleBeanPropertyFilter()
{
} )
.addFilter( ToOnePropertyFilter.FILTER_NAME, new SimpleBeanPropertyFilter()
{
} ) );
jacksonObjectMapperBuilder.featuresToDisable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.dhis2.fhir.adapter.converter;

/*
* Copyright (c) 2004-2018, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import org.junit.Assert;
import org.junit.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* Unit tests for {@link ZonedDateTimeToDateConverter}.
*
* @author volsch
*/
public class ZonedDateTimeToDateConverterTest
{
private ZonedDateTimeToDateConverter converter = new ZonedDateTimeToDateConverter();

@Test
public void docConvert()
{
final Date date = new GregorianCalendar( 2018, 7, 15, 17, 24, 23 ).getTime();
Assert.assertEquals( date, converter.convert( ZonedDateTime.of( 2018, 8, 15, 17, 24, 23, 0, ZoneId.systemDefault() ) ) );
}

@Test
public void doConvertNull()
{
Assert.assertNull( converter.convert( null ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.dhis2.fhir.adapter.auth.AuthorizationContext;
Expand All @@ -51,6 +53,7 @@
import org.springframework.web.client.RestTemplate;

import javax.annotation.Nonnull;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -121,9 +124,11 @@ public GenericJackson2JsonRedisSerializer cacheRedisSerializer()
mapper.disable( WRITE_DATES_AS_TIMESTAMPS );

final SimpleModule module = new SimpleModule();
module.addSerializer( InstantSerializer.INSTANCE );
module.addSerializer( LocalDateTimeSerializer.INSTANCE );
module.addSerializer( new ZonedDateTimeSerializer() );
module.addSerializer( new LocalDateSerializer( DateTimeFormatter.ISO_LOCAL_DATE ) );
module.addDeserializer( Instant.class, InstantDeserializer.INSTANT );
module.addDeserializer( LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE );
module.addDeserializer( ZonedDateTime.class, new ZonedDateTimeDeserializer() );
module.addDeserializer( LocalDate.class, new LocalDateDeserializer( DateTimeFormatter.ISO_LOCAL_DATE ) );
Expand Down
15 changes: 15 additions & 0 deletions fhir/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit ce329c2

Please sign in to comment.