diff --git a/app/src/main/resources/logback-spring.xml b/app/src/main/resources/logback-spring.xml
index fde41679..011572aa 100644
--- a/app/src/main/resources/logback-spring.xml
+++ b/app/src/main/resources/logback-spring.xml
@@ -41,6 +41,7 @@
+
diff --git a/app/src/test/java/org/dhis2/fhir/adapter/TestConfiguration.java b/app/src/test/java/org/dhis2/fhir/adapter/TestConfiguration.java
index c74fa483..19d58081 100644
--- a/app/src/test/java/org/dhis2/fhir/adapter/TestConfiguration.java
+++ b/app/src/test/java/org/dhis2/fhir/adapter/TestConfiguration.java
@@ -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;
@@ -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
diff --git a/app/src/test/resources/logback-test.xml b/app/src/test/resources/logback-test.xml
index a1235f90..1a761b50 100644
--- a/app/src/test/resources/logback-test.xml
+++ b/app/src/test/resources/logback-test.xml
@@ -34,6 +34,7 @@
+
diff --git a/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-attributes.json b/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-attributes.json
index c9f7501b..5e3f5f82 100644
--- a/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-attributes.json
+++ b/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-attributes.json
@@ -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 },
diff --git a/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-type.json b/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-type.json
index c63bdaf7..19533457 100644
--- a/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-type.json
+++ b/app/src/test/resources/org/dhis2/fhir/adapter/dhis/test/default-tracked-entity-type.json
@@ -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" } ] }
diff --git a/common/pom.xml b/common/pom.xml
index c6c3dfbd..438f7279 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -54,6 +54,14 @@
org.springframework.boot
spring-boot-starter-data-redis
+
+ org.springframework.data
+ spring-data-rest-core
+
+
+ org.springframework.data
+ spring-data-rest-webmvc
+
org.apache.commons
diff --git a/common/src/main/java/org/dhis2/fhir/adapter/rest/DataIntegrityViolationExceptionHandler.java b/common/src/main/java/org/dhis2/fhir/adapter/rest/DataIntegrityViolationExceptionHandler.java
new file mode 100644
index 00000000..cf6b45ad
--- /dev/null
+++ b/common/src/main/java/org/dhis2/fhir/adapter/rest/DataIntegrityViolationExceptionHandler.java
@@ -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 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 );
+ }
+}
diff --git a/common/src/main/java/org/dhis2/fhir/adapter/rest/RestBasePackage.java b/common/src/main/java/org/dhis2/fhir/adapter/rest/RestBasePackage.java
new file mode 100644
index 00000000..eb16422d
--- /dev/null
+++ b/common/src/main/java/org/dhis2/fhir/adapter/rest/RestBasePackage.java
@@ -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
+{
+}
diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/SqlExceptionUtils.java b/common/src/main/java/org/dhis2/fhir/adapter/util/SqlExceptionUtils.java
similarity index 97%
rename from fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/SqlExceptionUtils.java
rename to common/src/main/java/org/dhis2/fhir/adapter/util/SqlExceptionUtils.java
index ead861e0..b9e1ff8d 100644
--- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/SqlExceptionUtils.java
+++ b/common/src/main/java/org/dhis2/fhir/adapter/util/SqlExceptionUtils.java
@@ -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
diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteFhirResourceRepositoryImpl.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteFhirResourceRepositoryImpl.java
index 2adef87b..d8db0e7c 100644
--- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteFhirResourceRepositoryImpl.java
+++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteFhirResourceRepositoryImpl.java
@@ -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;
@@ -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) " +
diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteSubscriptionRequestRepositoryImpl.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteSubscriptionRequestRepositoryImpl.java
index 2fab3a65..f77f18ff 100644
--- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteSubscriptionRequestRepositoryImpl.java
+++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/data/repository/impl/CustomQueuedRemoteSubscriptionRequestRepositoryImpl.java
@@ -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;
@@ -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" +
diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/Constant.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/Constant.java
index 71e077af..484780e8 100644
--- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/Constant.java
+++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/Constant.java
@@ -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;
@@ -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;
@@ -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;
@@ -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;
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/MockMvcTestConfig.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/MockMvcTestConfig.java
index f8efbcca..4400681e 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/MockMvcTestConfig.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/MockMvcTestConfig.java
@@ -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;
@@ -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
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/ConstantRepositoryTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/ConstantRepositoryTest.java
new file mode 100644
index 00000000..25650e9c
--- /dev/null
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/ConstantRepositoryTest.java
@@ -0,0 +1,85 @@
+package org.dhis2.fhir.adapter.fhir.metadata.repository;
+
+/*
+ * 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.fhir.AbstractJpaRepositoryTest;
+import org.dhis2.fhir.adapter.fhir.metadata.model.Constant;
+import org.dhis2.fhir.adapter.fhir.metadata.model.ConstantCategory;
+import org.dhis2.fhir.adapter.fhir.metadata.model.DataType;
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+/**
+ * Tests for {@link ConstantRepository}.
+ *
+ * @author volsch
+ */
+public class ConstantRepositoryTest extends AbstractJpaRepositoryTest
+{
+ public static final String RESOURCE_PATH = "/api/constants";
+
+ public static final String AUTHORIZATION_HEADER_VALUE = CODE_MAPPING_AUTHORIZATION_HEADER_VALUE;
+
+ private Constant entity;
+
+ @Before
+ public void before()
+ {
+ entity = new Constant();
+ entity.setName( createUnique() );
+ entity.setCode( createUnique() );
+ entity.setValue( createUnique() );
+ entity.setDataType( DataType.STRING );
+ entity.setCategory( ConstantCategory.OTHER );
+ entity.setDescription( createUnique() );
+ }
+
+ @Test
+ public void testDuplicateCode() throws Exception
+ {
+ entity.setCode( "GENDER_MALE" );
+ mockMvc.perform( MockMvcRequestBuilders.post( RESOURCE_PATH ).header( AUTHORIZATION_HEADER_NAME, AUTHORIZATION_HEADER_VALUE )
+ .contentType( MediaType.APPLICATION_JSON ).content( objectMapper.writeValueAsString( entity ) ) )
+ .andExpect( status().isConflict() ).andExpect( jsonPath( "message", Matchers.is( "The specified code does already exist." ) ) );
+ }
+
+ @Test
+ public void testDuplicateName() throws Exception
+ {
+ entity.setName( "Gender Male" );
+ mockMvc.perform( MockMvcRequestBuilders.post( RESOURCE_PATH ).header( AUTHORIZATION_HEADER_NAME, AUTHORIZATION_HEADER_VALUE )
+ .contentType( MediaType.APPLICATION_JSON ).content( objectMapper.writeValueAsString( entity ) ) )
+ .andExpect( status().isConflict() ).andExpect( jsonPath( "message", Matchers.is( "The specified name does already exist." ) ) );
+ }
+}
\ No newline at end of file
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeSetValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeSetValidatorTest.java
index a31a02d4..37df4870 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeSetValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeSetValidatorTest.java
@@ -50,7 +50,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveCodeSetValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/codeSets";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeValidatorTest.java
index 26472779..f202dc92 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveCodeValidatorTest.java
@@ -45,7 +45,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveCodeValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/codes";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveExecutableScriptValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveExecutableScriptValidatorTest.java
index 5da92444..2a14f664 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveExecutableScriptValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveExecutableScriptValidatorTest.java
@@ -55,7 +55,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveExecutableScriptValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/executableScripts";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionResourceValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionResourceValidatorTest.java
index 932db0be..e3fc0e7f 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionResourceValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionResourceValidatorTest.java
@@ -46,7 +46,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveRemoteSubscriptionResourceValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/remoteSubscriptionResources";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionSystemValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionSystemValidatorTest.java
index e7cf4fd0..8eeff965 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionSystemValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveRemoteSubscriptionSystemValidatorTest.java
@@ -47,7 +47,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveRemoteSubscriptionSystemValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/remoteSubscriptionSystems";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptArgValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptArgValidatorTest.java
index 439860f6..9eae3575 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptArgValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptArgValidatorTest.java
@@ -46,7 +46,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveScriptArgValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/scriptArgs";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptSourceValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptSourceValidatorTest.java
index 1ce7d8cd..9d2e355f 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptSourceValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveScriptSourceValidatorTest.java
@@ -48,7 +48,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveScriptSourceValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/scriptSources";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveSystemCodeValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveSystemCodeValidatorTest.java
index 732452ea..867277c8 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveSystemCodeValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveSystemCodeValidatorTest.java
@@ -46,7 +46,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveSystemCodeValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/systemCodes";
diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveTrackedEntityRuleValidatorTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveTrackedEntityRuleValidatorTest.java
index 38e26009..03d89bbb 100644
--- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveTrackedEntityRuleValidatorTest.java
+++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/metadata/repository/validator/BeforeCreateSaveTrackedEntityRuleValidatorTest.java
@@ -48,7 +48,6 @@
*
* @author volsch
*/
-@SuppressWarnings( "JpaQlInspection" )
public class BeforeCreateSaveTrackedEntityRuleValidatorTest extends AbstractJpaRepositoryTest
{
public static final String RESOURCE_PATH = "/api/trackedEntityRules";