Skip to content

Commit

Permalink
Added further test cases and minor bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Nov 28, 2018
1 parent b109f8c commit 9e5f7bb
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 23 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ More changeable settings can be found in the file [default-application.yml](app/

### Running
The adapter WAR can be run with a servlet container 3.1 or later (like Apache Tomcat 8.5 or Jetty 9.3).

After successfully building the application also Maven can be used to run the application. Enter the following command in folder app in the console:

mvn jetty:run

Since the created WAR file is an executable WAR file, also the following command can be entered in folder app/target in the console:

Expand Down
21 changes: 9 additions & 12 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,10 @@
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<compress>false</compress>
<compress>true</compress>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.12.v20180830</version>
<configuration>
<httpConnector>
<host>localhost</host>
<port>8081</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -155,6 +144,14 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.dhis2.fhir.adapter.fhir.security.SystemAuthenticationToken;
import org.dhis2.fhir.adapter.lock.LockManager;
import org.dhis2.fhir.adapter.lock.impl.EmbeddedLockManagerImpl;
import org.dhis2.fhir.adapter.script.ScriptCompiler;
import org.dhis2.fhir.adapter.script.impl.ScriptCompilerImpl;
import org.dhis2.fhir.adapter.setup.Setup;
import org.dhis2.fhir.adapter.setup.SetupResult;
import org.dhis2.fhir.adapter.setup.SetupService;
Expand Down Expand Up @@ -144,6 +146,13 @@ protected LockManager embeddedLockManager()
return new EmbeddedLockManagerImpl();
}

@Nonnull
@Bean
protected ScriptCompiler scriptCompiler( @Value( "${dhis2.fhir-adapter.transformation.script-engine-name}" ) @Nonnull String scriptEngineName )
{
return new ScriptCompilerImpl( scriptEngineName );
}

@Nonnull
@Bean
protected WireMockServer fhirMockServer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
*/

import org.dhis2.fhir.adapter.AbstractAppTest;
import org.dhis2.fhir.adapter.script.ScriptCompiler;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand All @@ -43,12 +45,16 @@
*/
public class JavaScriptGeneratorControllerAppTest extends AbstractAppTest
{
@Autowired
private ScriptCompiler scriptCompiler;

@Test
public void scriptAvailable() throws Exception
{
mockMvc.perform( get( "/scripts/to-dhis2-all-mapping.js" ) )
final String script = mockMvc.perform( get( "/scripts/to-dhis2-all-mapping.js" ) )
.andExpect( status().isOk() ).andExpect( header().string( "Content-Type", Matchers.containsString( "application/javascript" ) ) )
.andExpect( content().string( containsString( "var trackedEntityInstance = new TrackedEntityInstance();" ) ) )
.andExpect( content().string( containsString( "Copyright (c)" ) ) );
.andExpect( content().string( containsString( "Copyright (c)" ) ) ).andReturn().getResponse().getContentAsString();
scriptCompiler.compile( script );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.dhis2.fhir.adapter.scriptable.ScriptMethod;
import org.dhis2.fhir.adapter.scriptable.ScriptMethodArg;
import org.dhis2.fhir.adapter.scriptable.ScriptType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ResourceLoader;
Expand All @@ -48,6 +49,7 @@
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -75,10 +77,11 @@
*
* @author volsch
*/
@ConditionalOnBean( JavaScriptGeneratorConfig.class )
@Controller
public class JavaScriptGeneratorController
{
private final Logger logger = LoggerFactory.getLogger( getClass() );

public static final String LICENSE_FILE = "classpath:/dhis2-license.txt";

public static final String LINE_ENDING = "\r\n";
Expand All @@ -95,7 +98,7 @@ public class JavaScriptGeneratorController

private String eTag;

public JavaScriptGeneratorController( @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" ) @Nonnull JavaScriptGeneratorConfig config, @Nonnull ResourceLoader resourceLoader )
public JavaScriptGeneratorController( @Nullable JavaScriptGeneratorConfig config, @Nonnull ResourceLoader resourceLoader )
{
this.config = config;
this.resourceLoader = resourceLoader;
Expand All @@ -104,6 +107,11 @@ public JavaScriptGeneratorController( @SuppressWarnings( "SpringJavaInjectionPoi
@RequestMapping( path = "/scripts/to-dhis2-all-mapping.js", method = RequestMethod.GET, produces = "application/javascript;charset=UTF-8" )
public ResponseEntity<String> getScript( @Nonnull WebRequest request )
{
if ( lastModified == null )
{
return new ResponseEntity<>( HttpStatus.NOT_FOUND );
}

if ( request.checkNotModified( eTag, lastModified.toEpochMilli() ) )
{
return null;
Expand All @@ -118,6 +126,12 @@ public ResponseEntity<String> getScript( @Nonnull WebRequest request )
@PostConstruct
protected void init()
{
if ( config == null )
{
logger.info( "JavaScript generator configuration could not be found. Generator is not available." );
return;
}

lastModified = Instant.now();

final SortedMap<String, Class<?>> orderedClasses = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public void setVariables( SortedSet<ScriptVariable> variables )

@OneToMany( mappedBy = "script", orphanRemoval = true, cascade = CascadeType.ALL )
@OrderBy( "id" )
@JsonIgnore
public List<ScriptSource> getSources()
{
return sources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ protected void validate( Object target, @Nonnull TransformDataType transformData
{
errors.rejectValue( "name", "AbstractRule.name.length", new Object[]{ AbstractRule.MAX_NAME_LENGTH }, "Name must not be longer than {0} characters." );
}
if ( rule.getTransformInScript() == null )
{
errors.rejectValue( "transformInScript", "AbstractRule.transformInScript.null", "Transformation input script is mandatory." );
}
if ( rule.getFhirResourceType() == null )
{
errors.rejectValue( "fhirResourceType", "AbstractRule.fhirResourceType.null", "FHIR resource type is mandatory." );
}
else
{

checkValidApplicableInScript( errors, "applicableInScript", rule.getFhirResourceType(), rule.getApplicableInScript() );
checkValidTransformInScript( errors, "transformInScript", rule.getFhirResourceType(), transformDataType, rule.getTransformInScript() );
}
Expand All @@ -92,6 +97,26 @@ protected static void checkValidApplicableInScript( @NonNull Errors errors, @Non
}
}

protected static void checkValidTeiLookupScript( @NonNull Errors errors, @Nonnull String field, @Nonnull FhirResourceType fhirResourceType, @Nullable ExecutableScript executableScript )
{
if ( executableScript == null )
{
return;
}
if ( executableScript.getScript().getScriptType() != ScriptType.EVALUATE )
{
errors.rejectValue( field, "AbstractRule." + field + ".scriptType", "Assigned script type for TEI evaluation must be EVALUATE." );
}
if ( executableScript.getScript().getReturnType() != DataType.FHIR_RESOURCE )
{
errors.rejectValue( field, "AbstractRule." + field + ".returnType", "Assigned return type for TEI evaluation script must be FHIR_RESOURCE." );
}
if ( (executableScript.getScript().getInputType() != null) && (executableScript.getScript().getInputType().getFhirResourceType() != fhirResourceType) )
{
errors.rejectValue( field, "AbstractRule." + field + ".inputType", new Object[]{ fhirResourceType }, "Assigned input type for TEI evaluation script must be the same as for the rule {0}." );
}
}

protected static void checkValidTransformInScript( @NonNull Errors errors, @Nonnull String field, @Nonnull FhirResourceType fhirResourceType, @Nonnull TransformDataType transformDataType, @Nullable ExecutableScript executableScript )
{
if ( executableScript == null )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public void validate( Object target, @Nonnull Errors errors )
}
BeforeCreateSaveFhirResourceMappingValidator.checkValidOrgLookupScript( errors, "TrackedEntityRule.", "orgUnitLookupScript", rule.getFhirResourceType(), rule.getOrgUnitLookupScript() );
BeforeCreateSaveFhirResourceMappingValidator.checkValidLocationLookupScript( errors, "TrackedEntityRule.", "locationLookupScript", rule.getFhirResourceType(), rule.getLocationLookupScript() );
checkValidTeiLookupScript( errors, "teiLookupScript", rule.getFhirResourceType(), rule.getTeiLookupScript() );
}
}
Loading

0 comments on commit 9e5f7bb

Please sign in to comment.