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 9e5f7bb commit 4c7550d
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<logger name="org.hibernate" level="warn" />
<logger name="org.hibernate.cfg.AnnotationBinder" level="error" />
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="off" />
<logger name="org.hibernate.internal.ExceptionMapperStandardImpl" level="off" />
<logger name="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" level="error" />
<logger name="org.springframework.data.repository.config.RepositoryConfigurationDelegate" level="warn" />
<logger name="org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport" level="warn" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import com.github.tomakehurst.wiremock.WireMockServer;
import org.dhis2.fhir.adapter.dhis.model.ReferenceType;
import org.dhis2.fhir.adapter.dhis.security.SecurityConfig;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType;
import org.dhis2.fhir.adapter.fhir.metadata.model.SubscriptionType;
Expand Down Expand Up @@ -190,6 +191,11 @@ protected void postConstruct()
setup.getOrganizationCodeSetup().setDefaultDhisCode( "OU_4567" );
setup.getOrganizationCodeSetup().setMappings( "9876 OU_1234 \n 8765, OU_2345" );

setup.getTrackedEntitySetup().getFirstName().setReferenceType( ReferenceType.CODE );
setup.getTrackedEntitySetup().getFirstName().setReferenceValue( "FIRST_NAME" );
setup.getTrackedEntitySetup().getLastName().setReferenceType( ReferenceType.NAME );
setup.getTrackedEntitySetup().getLastName().setReferenceValue( "Last Name" );

final SetupResult setupResult;
SecurityContextHolder.getContext().setAuthentication( new SystemAuthenticationToken() );
try
Expand Down
1 change: 1 addition & 0 deletions app/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<logger name="org.hibernate" level="warn" />
<logger name="org.hibernate.cfg.AnnotationBinder" level="error" />
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="off" />
<logger name="org.hibernate.internal.ExceptionMapperStandardImpl" level="off" />
<logger name="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" level="error" />
<logger name="org.springframework.data.repository.config.RepositoryConfigurationDelegate" level="warn" />
<logger name="org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport" level="warn" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"trackedEntityAttributes": [
{ "name": "Address", "id": "VCtm2pySeEV", "valueType": "TEXT", "optionSetValue": false }, { "name": "Date of birth", "id": "BiTsLcJQ95V", "valueType": "DATE", "optionSetValue": false },
{ "name": "First Name", "id": "TfdH5KvFmMy", "valueType": "TEXT", "optionSetValue": false },
{ "name": "First Name", "id": "TfdH5KvFmMy", "code": "FIRST_NAME", "valueType": "TEXT", "optionSetValue": false },
{ "name": "Gender", "id": "CklPZdOd6H1", "valueType": "TEXT", "optionSetValue": true, "optionSet": { "name": "Sex", "id": "hiQ3QFheQ3O", "options": [ { "code": "MALE", "name": "Male" }, { "code": "FEMALE", "name": "Female" } ] } },
{ "name": "Last Name", "id": "aW66s2QSosT", "valueType": "TEXT", "optionSetValue": false }, { "name": "Mother/Caregiver's contact number", "id": "pjexi5YaAPa", "valueType": "PHONE_NUMBER", "optionSetValue": false },
{ "name": "Mother/Caregiver's first name", "id": "ftFBu8mHZ4H", "valueType": "TEXT", "optionSetValue": false }, { "name": "Mother/Caregiver's last name", "id": "EpbquVl5OD6", "valueType": "TEXT", "optionSetValue": false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Person", "id": "MCPQUTHX1Ze", "trackedEntityTypeAttributes": [
{ "name": "Person Unique ID", "id": "yFfy1y9vvZ3", "valueType": "TEXT", "mandatory": false, "trackedEntityAttribute": { "code": "UID", "name": "Unique ID", "id": "KSr2yTdu1AI", "valueType": "TEXT", "optionSetValue": false } },
{ "name": "Person Patient ID", "id": "eUP78IY4rUF", "valueType": "TEXT", "trackedEntityAttribute": { "name": "Patient ID", "id": "Ewi7FUfcHAD", "valueType": "TEXT", "optionSetValue": false } },
{ "name": "Person First Name", "id": "XznTxIRztFR", "valueType": "TEXT", "trackedEntityAttribute": { "name": "First Name", "id": "TfdH5KvFmMy", "valueType": "TEXT", "optionSetValue": false } },
{ "name": "Person First Name", "id": "XznTxIRztFR", "valueType": "TEXT", "trackedEntityAttribute": { "name": "First Name", "id": "TfdH5KvFmMy", "code": "FIRST_NAME", "valueType": "TEXT", "optionSetValue": false } },
{ "name": "Person Last Name", "id": "aobAjew2sJt", "valueType": "TEXT", "trackedEntityAttribute": { "name": "Last Name", "id": "aW66s2QSosT", "valueType": "TEXT", "optionSetValue": false } }, {
"name": "Person Gender", "id": "D011up7xrF1", "valueType": "TEXT", "trackedEntityAttribute": {
"name": "Gender", "id": "CklPZdOd6H1", "valueType": "TEXT", "optionSetValue": true, "optionSet": { "name": "Sex", "id": "hiQ3QFheQ3O", "options": [ { "code": "MALE", "name": "Male" }, { "code": "FEMALE", "name": "Female" } ] }
Expand Down
8 changes: 8 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.dhis2.fhir.adapter.rest;

/*
* 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.dhis2.fhir.adapter.util.SqlExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.annotation.Nonnull;
import java.util.regex.Pattern;

/**
* Handles exception instances of {@link org.springframework.dao.DataIntegrityViolationException}.
*
* @author volsch
*/
@ControllerAdvice( annotations = RepositoryRestController.class )
@Order( value = 0 )
public class DataIntegrityViolationExceptionHandler extends ResponseEntityExceptionHandler
{
private final Logger logger = LoggerFactory.getLogger( getClass() );

private final Pattern CODE_PATTERN = Pattern.compile( "[a-z0-9]_uk_code", Pattern.CASE_INSENSITIVE );

private final Pattern NAME_PATTERN = Pattern.compile( "[a-z0-9]_uk_name", Pattern.CASE_INSENSITIVE );

private final MessageSourceAccessor messageSourceAccessor;

public DataIntegrityViolationExceptionHandler( @Nonnull MessageSource messageSource )
{
this.messageSourceAccessor = new MessageSourceAccessor( messageSource );
}

@ExceptionHandler
public ResponseEntity<RepositoryConstraintViolationExceptionMessage> handleRepositoryConstraintViolationException(
RepositoryConstraintViolationException e )
{
return new ResponseEntity<>( new RepositoryConstraintViolationExceptionMessage( e, messageSourceAccessor ), HttpStatus.BAD_REQUEST );
}

@ExceptionHandler( value = { DataIntegrityViolationException.class } )
@ResponseBody
public ResponseEntity<?> handleResponseEntityException( DataIntegrityViolationException e, WebRequest request )
{
logger.debug( "Handling data integrity constraint violation.", e );

final Throwable rootException = e.getMostSpecificCause();
final String rootExceptionMessage = rootException.getMessage();
String message = null;
if ( rootExceptionMessage != null )
{
if ( CODE_PATTERN.matcher( rootExceptionMessage ).find() )
{
message = "The specified code does already exist.";
}
else if ( NAME_PATTERN.matcher( rootExceptionMessage ).find() )
{
message = "The specified name does already exist.";
}
else if ( SqlExceptionUtils.isUniqueKeyViolation( rootException ) )
{
message = "The entity data contains duplicate values.";
}
}
if ( message == null )
{
logger.error( "Unhandled data integrity constraint violation.", e );
message = "The data cannot be processed because of a data integrity violation.";
}
return new ResponseEntity<>( new RestError( message ), HttpStatus.CONFLICT );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.dhis2.fhir.adapter.rest;

/*
* 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.
*/

/**
* Marker interface that is used to reference package as base package.
*
* @author volsch
*/
public interface RestBasePackage
{
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dhis2.fhir.adapter.fhir.data.repository.impl;
package org.dhis2.fhir.adapter.util;

/*
* Copyright (c) 2004-2018, University of Oslo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.dhis2.fhir.adapter.fhir.data.repository.AlreadyQueuedException;
import org.dhis2.fhir.adapter.fhir.data.repository.CustomQueuedRemoteFhirResourceRepository;
import org.dhis2.fhir.adapter.fhir.data.repository.IgnoredSubscriptionResourceException;
import org.dhis2.fhir.adapter.util.SqlExceptionUtils;
import org.hibernate.query.NativeQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -80,7 +81,6 @@ public CustomQueuedRemoteFhirResourceRepositoryImpl( @Nonnull EntityManager enti

@Transactional( rollbackFor = AlreadyQueuedException.class )
@Override
@SuppressWarnings( "unchecked" )
public void enqueue( @Nonnull UUID subscriptionResourceId, @Nonnull String fhirResourceId, @Nonnull String requestId ) throws AlreadyQueuedException
{
final Query query = entityManager.createNativeQuery( "INSERT INTO fhir_queued_remote_resource(remote_subscription_resource_id,fhir_resource_id,request_id,queued_at) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.dhis2.fhir.adapter.fhir.data.repository.AlreadyQueuedException;
import org.dhis2.fhir.adapter.fhir.data.repository.CustomQueuedRemoteSubscriptionRequestRepository;
import org.dhis2.fhir.adapter.fhir.data.repository.IgnoredSubscriptionResourceException;
import org.dhis2.fhir.adapter.util.SqlExceptionUtils;
import org.hibernate.query.NativeQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -80,7 +81,6 @@ public CustomQueuedRemoteSubscriptionRequestRepositoryImpl( @Nonnull EntityManag

@Transactional( rollbackFor = AlreadyQueuedException.class )
@Override
@SuppressWarnings( "unchecked" )
public void enqueue( @Nonnull UUID subscriptionResourceId, @Nonnull String requestId ) throws AlreadyQueuedException
{
final Query query = entityManager.createNativeQuery( "INSERT INTO fhir_queued_remote_subscription_request" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
Expand All @@ -48,7 +49,10 @@
* @author volsch
*/
@Entity
@Table( name = "fhir_constant" )
@Table( name = "fhir_constant", uniqueConstraints = {
@UniqueConstraint( name = "fhir_constant_uk_name", columnNames = "name" ),
@UniqueConstraint( name = "fhir_constant_uk_code", columnNames = "code" )
} )
public class Constant extends VersionedBaseMetadata implements Serializable
{
private static final long serialVersionUID = -4219974054617859678L;
Expand Down Expand Up @@ -81,7 +85,7 @@ public class Constant extends VersionedBaseMetadata implements Serializable
private String value;

@Basic
@Column( name = "name", nullable = false, length = 230 )
@Column( name = "name", nullable = false, length = MAX_NAME_LENGTH )
public String getName()
{
return name;
Expand Down Expand Up @@ -118,7 +122,7 @@ public void setCategory( ConstantCategory category )
}

@Basic
@Column( name = "code", nullable = false, length = 50, unique = true )
@Column( name = "code", nullable = false, length = MAX_CODE_LENGTH )
public String getCode()
{
return code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.dhis2.fhir.adapter.converter.ZonedDateTimeToDateConverter;
import org.dhis2.fhir.adapter.fhir.data.DataBasePackage;
import org.dhis2.fhir.adapter.fhir.metadata.MetadataBasePackage;
import org.dhis2.fhir.adapter.rest.RestBasePackage;
import org.dhis2.fhir.adapter.script.ScriptCompiler;
import org.dhis2.fhir.adapter.script.impl.ScriptCompilerImpl;
import org.dhis2.fhir.adapter.validator.EnumValue;
Expand Down Expand Up @@ -67,7 +68,7 @@
*/
@Configuration
@EnableAutoConfiguration( exclude = { RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class, ArtemisAutoConfiguration.class, HystrixAutoConfiguration.class } )
@ComponentScan( basePackageClasses = { DataBasePackage.class, MetadataBasePackage.class } )
@ComponentScan( basePackageClasses = { RestBasePackage.class, DataBasePackage.class, MetadataBasePackage.class } )
public class MockMvcTestConfig
{
@Nonnull
Expand Down
Loading

0 comments on commit 4c7550d

Please sign in to comment.