-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/ca/gc/aafc/dina/export/api/service/DataExportTemplateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ca.gc.aafc.dina.export.api.service; | ||
|
||
import java.util.UUID; | ||
import lombok.NonNull; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.validation.SmartValidator; | ||
|
||
import ca.gc.aafc.dina.export.api.entity.DataExportTemplate; | ||
import ca.gc.aafc.dina.export.api.validation.DataExportTemplateValidator; | ||
import ca.gc.aafc.dina.jpa.BaseDAO; | ||
import ca.gc.aafc.dina.service.DefaultDinaService; | ||
|
||
@Service | ||
public class DataExportTemplateService extends DefaultDinaService<DataExportTemplate> { | ||
|
||
private final DataExportTemplateValidator dataExportTemplateValidator; | ||
|
||
public DataExportTemplateService(@NonNull BaseDAO baseDAO, | ||
DataExportTemplateValidator dataExportTemplateValidator, | ||
@NonNull SmartValidator validator) { | ||
super(baseDAO, validator); | ||
this.dataExportTemplateValidator = dataExportTemplateValidator; | ||
} | ||
|
||
@Override | ||
public void preCreate(DataExportTemplate dinaExportTemplate) { | ||
|
||
if (dinaExportTemplate.getUuid() == null) { | ||
dinaExportTemplate.setUuid(UUID.randomUUID()); | ||
} | ||
} | ||
|
||
@Override | ||
public void validateBusinessRules(DataExportTemplate entity) { | ||
applyBusinessRule(entity, dataExportTemplateValidator); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/ca/gc/aafc/dina/export/api/validation/DataExportTemplateValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ca.gc.aafc.dina.export.api.validation; | ||
|
||
import org.apache.commons.lang3.BooleanUtils; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.Errors; | ||
|
||
import ca.gc.aafc.dina.export.api.entity.DataExportTemplate; | ||
import ca.gc.aafc.dina.validation.DinaBaseValidator; | ||
|
||
@Component | ||
public class DataExportTemplateValidator extends DinaBaseValidator<DataExportTemplate> { | ||
|
||
static final String INVALID_PUBLICLY_AVAILABLE_KEY = "dataExportTemplate.publiclyReleasableAndRestrictToCreatedBy.invalid"; | ||
|
||
public DataExportTemplateValidator(MessageSource messageSource) { | ||
super(DataExportTemplate.class, messageSource); | ||
} | ||
|
||
@Override | ||
public void validateTarget(DataExportTemplate target, Errors errors) { | ||
|
||
if (BooleanUtils.isTrue(target.getPubliclyReleasable()) && | ||
BooleanUtils.isTrue(target.getRestrictToCreatedBy())) { | ||
String errorMessage = getMessage(INVALID_PUBLICLY_AVAILABLE_KEY); | ||
errors.reject(INVALID_PUBLICLY_AVAILABLE_KEY, errorMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
transaction.duplicateRoleUUID=Agent roles can not have duplicated agents. | ||
description.isEmpty=description cannot be empty nor be blank | ||
dataExportTemplate.publiclyReleasableAndRestrictToCreatedBy.invalid=DataExportTemplate can be publiclyReleasable or restricted to createdBy but not both |
28 changes: 28 additions & 0 deletions
28
src/test/java/ca/gc/aafc/dina/export/api/service/DataExportTemplateServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ca.gc.aafc.dina.export.api.service; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import ca.gc.aafc.dina.export.api.BaseIntegrationTest; | ||
import ca.gc.aafc.dina.export.api.entity.DataExportTemplate; | ||
import ca.gc.aafc.dina.export.api.testsupport.factories.DataExportTemplateFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.transaction.Transactional; | ||
|
||
public class DataExportTemplateServiceIT extends BaseIntegrationTest { | ||
|
||
@Inject | ||
private DataExportTemplateService dataExportTemplateService; | ||
|
||
@Test | ||
@Transactional | ||
public void onCreate_DataExportTemplateCreated() { | ||
DataExportTemplate dataExportTemplate = DataExportTemplateFactory.newDataExportTemplate() | ||
.restrictToCreatedBy(true) | ||
.publiclyReleasable(false) | ||
.build(); | ||
|
||
dataExportTemplateService.create(dataExportTemplate); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...test/java/ca/gc/aafc/dina/export/api/testsupport/factories/DataExportTemplateFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ca.gc.aafc.dina.export.api.testsupport.factories; | ||
|
||
import ca.gc.aafc.dina.export.api.entity.DataExport; | ||
import ca.gc.aafc.dina.export.api.entity.DataExportTemplate; | ||
import ca.gc.aafc.dina.testsupport.factories.TestableEntityFactory; | ||
|
||
import java.util.UUID; | ||
|
||
public class DataExportTemplateFactory { | ||
|
||
public static DataExportTemplate.DataExportTemplateBuilder newDataExportTemplate() { | ||
return DataExportTemplate.builder() | ||
.uuid(UUID.randomUUID()) | ||
.group("aafc") | ||
.exportType(DataExport.ExportType.TABULAR_DATA) | ||
.publiclyReleasable(false) | ||
.restrictToCreatedBy(false) | ||
.name(TestableEntityFactory.generateRandomNameLettersOnly(7)) | ||
.createdBy("test user"); | ||
} | ||
|
||
} |