diff --git a/src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java b/src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java index ce15ec7f..6fb82ab5 100644 --- a/src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java +++ b/src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java @@ -3,7 +3,7 @@ import eu.europeana.metis.sandbox.repository.debias.DetectRepository; import eu.europeana.metis.sandbox.service.debias.CompletedState; -import eu.europeana.metis.sandbox.service.debias.DebiasMachineService; +import eu.europeana.metis.sandbox.service.debias.DebiasDetectService; import eu.europeana.metis.sandbox.service.debias.DetectService; import eu.europeana.metis.sandbox.service.debias.ErrorState; import eu.europeana.metis.sandbox.service.debias.ProcessingState; @@ -25,55 +25,7 @@ public class DebiasConfig { */ @Bean public DetectService debiasMachine(DetectRepository detectRepository) { - return new DebiasMachineService(detectRepository); - } - - /** - * Ready state - * - * @param debiasMachine the debias machine - * @param detectRepository the detect repository - * @return the ready state - */ - @Bean - ReadyState readyState(DetectService debiasMachine, DetectRepository detectRepository) { - return new ReadyState(debiasMachine, detectRepository); - } - - /** - * Completed state - * - * @param debiasMachine the debias machine - * @param detectRepository the detect repository - * @return the completed state - */ - @Bean - CompletedState completedState(DetectService debiasMachine, DetectRepository detectRepository) { - return new CompletedState(debiasMachine, detectRepository); - } - - /** - * Processing state - * - * @param debiasMachine the debias machine - * @param detectRepository the detect repository - * @return the processing state - */ - @Bean - ProcessingState processingState(DetectService debiasMachine, DetectRepository detectRepository) { - return new ProcessingState(debiasMachine, detectRepository); - } - - /** - * Error state - * - * @param debiasMachine the debias machine - * @param detectRepository the detect repository - * @return the error state - */ - @Bean - ErrorState errorState(DetectService debiasMachine, DetectRepository detectRepository) { - return new ErrorState(debiasMachine, detectRepository); + return new DebiasDetectService(detectRepository); } } diff --git a/src/main/java/eu/europeana/metis/sandbox/controller/DatasetController.java b/src/main/java/eu/europeana/metis/sandbox/controller/DatasetController.java index 06bfff83..fcacc701 100644 --- a/src/main/java/eu/europeana/metis/sandbox/controller/DatasetController.java +++ b/src/main/java/eu/europeana/metis/sandbox/controller/DatasetController.java @@ -107,13 +107,14 @@ class DatasetController { /** * Instantiates a new Dataset controller. * - * @param datasetService the dataset service - * @param datasetLogService the dataset log service - * @param reportService the report service - * @param recordService the record service - * @param recordLogService the record log service + * @param datasetService the dataset service + * @param datasetLogService the dataset log service + * @param reportService the report service + * @param recordService the record service + * @param recordLogService the record log service * @param recordTierCalculationService the record tier calculation service - * @param harvestPublishService the harvest publish service + * @param harvestPublishService the harvest publish service + * @param debiasDetectService the debias detect service */ public DatasetController(DatasetService datasetService, DatasetLogService datasetLogService, DatasetReportService reportService, RecordService recordService, @@ -134,11 +135,11 @@ public DatasetController(DatasetService datasetService, DatasetLogService datase * POST API calls for harvesting and processing the records given a zip, tar or tar.gz file * * @param datasetName the given name of the dataset to be processed - * @param country the given country from which the records refer to - * @param language the given language that the records contain - * @param stepsize the stepsize - * @param dataset the given dataset itself to be processed as a compressed file - * @param xsltFile the xslt file used for transformation to edm external + * @param country the given country from which the records refer to + * @param language the given language that the records contain + * @param stepsize the stepsize + * @param dataset the given dataset itself to be processed as a compressed file + * @param xsltFile the xslt file used for transformation to edm external * @return 202 if it's processed correctly, 4xx or 500 otherwise */ @Operation(summary = "Harvest dataset from file", description = "Process the given dataset by HTTP providing a file") @@ -176,11 +177,11 @@ public DatasetIdDto harvestDatasetFromFile( * POST API calls for harvesting and processing the records given a URL of a compressed file * * @param datasetName the given name of the dataset to be processed - * @param country the given country from which the records refer to - * @param language the given language that the records contain - * @param stepsize the stepsize - * @param url the given dataset itself to be processed as a URL of a zip file - * @param xsltFile the xslt file used for transformation to edm external + * @param country the given country from which the records refer to + * @param language the given language that the records contain + * @param stepsize the stepsize + * @param url the given dataset itself to be processed as a URL of a zip file + * @param xsltFile the xslt file used for transformation to edm external * @return 202 if it's processed correctly, 4xx or 500 otherwise */ @Operation(summary = "Harvest dataset from url", description = "Process the given dataset by HTTP providing an URL") @@ -221,16 +222,16 @@ public DatasetIdDto harvestDatasetFromURL( /** * POST API calls for harvesting and processing the records given a URL of an OAI-PMH endpoint * - * @param datasetName the given name of the dataset to be processed - * @param country the given country from which the records refer to - * @param language the given language that the records contain - * @param stepsize the stepsize - * @param url the given URL of the OAI-PMH repository to be processed - * @param setspec forms a unique identifier for the set within the repository, it must be - * unique for each set. + * @param datasetName the given name of the dataset to be processed + * @param country the given country from which the records refer to + * @param language the given language that the records contain + * @param stepsize the stepsize + * @param url the given URL of the OAI-PMH repository to be processed + * @param setspec forms a unique identifier for the set within the repository, it must be + * unique for eac set. * @param metadataformat or metadata prefix is a string to specify the metadata format in OAI-PMH - * requests issued to the repository - * @param xsltFile the xslt file used for transformation to edm external + * requests issued to the repository + * @param xsltFile the xslt file used for transformation to edm external * @return 202 if it's processed correctly, 4xx or 500 otherwise */ @Operation(summary = "Harvest dataset from OAI-PMH protocol", description = "Process the given dataset using OAI-PMH") @@ -313,7 +314,7 @@ public DatasetInfoDto getDatasetInfo( * GET API returns the generated tier calculation view for a stored record. * * @param datasetId the dataset id - * @param recordId the record id + * @param recordId the record id * @return the record tier calculation view * @throws NoRecordFoundException if record was not found */ @@ -332,8 +333,8 @@ public RecordTierCalculationView computeRecordTierCalculation( * GET API returns the string representation of the stored record. * * @param datasetId the dataset id - * @param recordId the record id - * @param step the step name + * @param recordId the record id + * @param step the step name * @return the string representation of the stored record * @throws NoRecordFoundException if record was not found */ @@ -352,7 +353,6 @@ public String getRecord(@PathVariable("id") String datasetId, @RequestParam("rec * * @param datasetId the dataset id * @return the records tier of a given dataset - * */ @Operation(summary = "Gets a list of records tier", description = "Get list of records tiers") @ApiResponse(responseCode = "200", description = "List of records tiers") @@ -409,23 +409,35 @@ public List getAllLanguages() { return Language.getLanguageListSortedByName().stream().map(LanguageView::new).toList(); } + /** + * Process DeBias boolean. + * + * @param datasetId the dataset id + * @return the boolean + */ @Operation(description = "Process debias detection dataset") @ApiResponse(responseCode = "200", description = "Process debias detection feature", content = { @Content(mediaType = APPLICATION_JSON_VALUE)}) @ApiResponse(responseCode = "400", description = MESSAGE_FOR_400_CODE) @PostMapping(value = "{id}/debias", produces = APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public boolean processDebias(@PathVariable("id") Long datasetId) { + public boolean processDebias(@PathVariable("id") Integer datasetId) { return debiasDetectService.process(datasetId); } - @Operation(description = "Get debias detection dataset") - @ApiResponse(responseCode = "200", description = "Get detection information about debias detection", content = { + /** + * Gets DeBias detection information. + * + * @param datasetId the dataset id + * @return the DeBias detection + */ + @Operation(description = "Get DeBias detection dataset") + @ApiResponse(responseCode = "200", description = "Get detection information about DeBias detection", content = { @Content(mediaType = APPLICATION_JSON_VALUE)}) @ApiResponse(responseCode = "400", description = MESSAGE_FOR_400_CODE) @GetMapping(value = "{id}/debias", produces = APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public DetectionInfoDto getDebiasDetection(@PathVariable("id") Long datasetId) { + public DetectionInfoDto getDebiasDetection(@PathVariable("id") Integer datasetId) { return debiasDetectService.getDetectionInfo(datasetId); } diff --git a/src/main/java/eu/europeana/metis/sandbox/dto/debias/DetectionInfoDto.java b/src/main/java/eu/europeana/metis/sandbox/dto/debias/DetectionInfoDto.java index b8aec5e8..7978079b 100644 --- a/src/main/java/eu/europeana/metis/sandbox/dto/debias/DetectionInfoDto.java +++ b/src/main/java/eu/europeana/metis/sandbox/dto/debias/DetectionInfoDto.java @@ -17,13 +17,13 @@ public class DetectionInfoDto { public static final String SWAGGER_MODEL_NAME = "DebiasDetectionInfo"; @JsonProperty("dataset-id") - private final Long datasetId; + private final Integer datasetId; @JsonProperty("state") private final String state; @JsonProperty("creation-date") - @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.nZ") + @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") private final ZonedDateTime creationDate; /** @@ -33,7 +33,7 @@ public class DetectionInfoDto { * @param state the state * @param creationDate the creation date */ - public DetectionInfoDto(Long datasetId, String state, ZonedDateTime creationDate) { + public DetectionInfoDto(Integer datasetId, String state, ZonedDateTime creationDate) { this.datasetId = datasetId; this.state = state; this.creationDate = creationDate; @@ -44,7 +44,7 @@ public DetectionInfoDto(Long datasetId, String state, ZonedDateTime creationDate * * @return the dataset id */ - public Long getDatasetId() { + public Integer getDatasetId() { return datasetId; } diff --git a/src/main/java/eu/europeana/metis/sandbox/repository/debias/DetectRepository.java b/src/main/java/eu/europeana/metis/sandbox/repository/debias/DetectRepository.java index 1c402d18..fe70dfcf 100644 --- a/src/main/java/eu/europeana/metis/sandbox/repository/debias/DetectRepository.java +++ b/src/main/java/eu/europeana/metis/sandbox/repository/debias/DetectRepository.java @@ -1,20 +1,31 @@ package eu.europeana.metis.sandbox.repository.debias; import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +/** + * The interface Detect repository. + */ public interface DetectRepository extends JpaRepository { - @Query("SELECT dec.datasetId, dec.state, dec.createdDate FROM DetectionEntity dec") - List listDbEntries(); - - DetectionEntity findDetectionEntityByDatasetId_DatasetId(Long datasetId); + /** + * Find detection entity by dataset id detection entity. + * + * @param datasetId the dataset id + * @return the detection entity + */ + DetectionEntity findDetectionEntityByDatasetId_DatasetId(Integer datasetId); + /** + * Update state. + * + * @param datasetId the dataset id + * @param state the state + */ @Modifying @Query("UPDATE DetectionEntity dec SET dec.state = '?2' WHERE dec.datasetId = ?1") - void updateState(Long datasetId, String state); + void updateState(Integer datasetId, String state); } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/CompletedState.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/CompletedState.java index eafc7716..abd9ff00 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/CompletedState.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/CompletedState.java @@ -2,12 +2,24 @@ import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; import eu.europeana.metis.sandbox.repository.debias.DetectRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; +/** + * The type Completed state. + */ public class CompletedState extends State implements Stateful { + private static final Logger LOGGER = LoggerFactory.getLogger(CompletedState.class); private static final String STATE_NAME = "COMPLETED"; + /** + * Instantiates a new Completed state. + * + * @param debiasMachine the debias machine + * @param detectRepository the detect repository + */ public CompletedState(DetectService debiasMachine, DetectRepository detectRepository) { this.stateMachine = debiasMachine; this.name = STATE_NAME; @@ -16,18 +28,18 @@ public CompletedState(DetectService debiasMachine, DetectRepository detectReposi } @Override - public void fail(Long datasetId) { + public void fail(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getError()); } @Override - public void success(Long datasetId) { + public void success(Integer datasetId) { // do nothing, processing completed. } @Transactional @Override - public boolean process(Long datasetId) { + public boolean process(Integer datasetId) { LOGGER.info("{} {}", STATE_NAME, datasetId); try { DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId); @@ -49,6 +61,11 @@ public boolean process(Long datasetId) { return terminalState; } + /** + * Gets name. + * + * @return the name + */ public String getName() { return STATE_NAME; } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineService.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectService.java similarity index 69% rename from src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineService.java rename to src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectService.java index fb3ea9ce..f026871d 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineService.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectService.java @@ -3,9 +3,14 @@ import eu.europeana.metis.sandbox.dto.debias.DetectionInfoDto; import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; import eu.europeana.metis.sandbox.repository.debias.DetectRepository; +import java.time.ZonedDateTime; -public class DebiasMachineService implements DetectService { +/** + * The type DeBias detect service. + */ +public class DebiasDetectService implements DetectService { + private static final String INITIAL_STATE = "READY"; private final Stateful ready; private final Stateful processing; private final Stateful completed; @@ -13,7 +18,12 @@ public class DebiasMachineService implements DetectService { private final DetectRepository detectRepository; private Stateful state; - public DebiasMachineService(DetectRepository detectRepository) { + /** + * Instantiates a new DeBias detect service. + * + * @param detectRepository the detect repository + */ + public DebiasDetectService(DetectRepository detectRepository) { this.ready = new ReadyState(this, detectRepository); this.processing = new ProcessingState(this, detectRepository); this.completed = new CompletedState(this, detectRepository); @@ -23,17 +33,17 @@ public DebiasMachineService(DetectRepository detectRepository) { } @Override - public void fail(Long datasetId) { + public void fail(Integer datasetId) { state.fail(datasetId); } @Override - public void success(Long datasetId) { + public void success(Integer datasetId) { state.success(datasetId); } @Override - public boolean process(Long datasetId) { + public boolean process(Integer datasetId) { return state.process(datasetId); } @@ -42,26 +52,26 @@ public Stateful getState() { return state; } + public void setState(Stateful state) { + this.state = state; + } + /** - * Gets debias detection info. + * Gets DeBias detection info. * * @param datasetId the dataset id - * @return the debias detection info + * @return the DeBias detection info */ @Override - public DetectionInfoDto getDetectionInfo(Long datasetId) { + public DetectionInfoDto getDetectionInfo(Integer datasetId) { DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId); if (detectionEntity == null) { - return null; + return new DetectionInfoDto(datasetId, INITIAL_STATE, ZonedDateTime.now()); } else { return new DetectionInfoDto(datasetId, detectionEntity.getState(), detectionEntity.getCreatedDate()); } } - public void setState(Stateful state) { - this.state = state; - } - public Stateful getReady() { return ready; } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/DetectService.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/DetectService.java index 18ffa87e..44d9117e 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/DetectService.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/DetectService.java @@ -2,25 +2,80 @@ import eu.europeana.metis.sandbox.dto.debias.DetectionInfoDto; +/** + * The interface Detect service. + */ public interface DetectService { - void fail(Long datasetId); + /** + * Fail. + * + * @param datasetId the dataset id + */ + void fail(Integer datasetId); - void success(Long datasetId); + /** + * Success. + * + * @param datasetId the dataset id + */ + void success(Integer datasetId); - boolean process(Long datasetId); + /** + * Process boolean. + * + * @param datasetId the dataset id + * @return the boolean + */ + boolean process(Integer datasetId); + /** + * Gets state. + * + * @return the state + */ Stateful getState(); + /** + * Sets state. + * + * @param state the state + */ void setState(Stateful state); + /** + * Gets ready. + * + * @return the ready + */ Stateful getReady(); + /** + * Gets processing. + * + * @return the processing + */ Stateful getProcessing(); + /** + * Gets completed. + * + * @return the completed + */ Stateful getCompleted(); + /** + * Gets error. + * + * @return the error + */ Stateful getError(); - DetectionInfoDto getDetectionInfo(Long datasetId); + /** + * Gets detection info. + * + * @param datasetId the dataset id + * @return the detection info + */ + DetectionInfoDto getDetectionInfo(Integer datasetId); } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/ErrorState.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/ErrorState.java index c94c0582..83a18d24 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/ErrorState.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/ErrorState.java @@ -2,12 +2,24 @@ import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; import eu.europeana.metis.sandbox.repository.debias.DetectRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; +/** + * The type Error state. + */ public class ErrorState extends State implements Stateful { + private static final Logger LOGGER = LoggerFactory.getLogger(ErrorState.class); private static final String STATE_NAME = "ERROR"; + /** + * Instantiates a new Error state. + * + * @param debiasMachine the debias machine + * @param detectRepository the detect repository + */ public ErrorState(DetectService debiasMachine, DetectRepository detectRepository) { this.stateMachine = debiasMachine; this.name = STATE_NAME; @@ -16,18 +28,18 @@ public ErrorState(DetectService debiasMachine, DetectRepository detectRepository } @Override - public void fail(Long datasetId) { + public void fail(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getError()); } @Override - public void success(Long datasetId) { + public void success(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getReady()); } @Transactional @Override - public boolean process(Long datasetId) { + public boolean process(Integer datasetId) { LOGGER.info("{} {}", STATE_NAME, datasetId); try { DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId); @@ -48,6 +60,11 @@ public boolean process(Long datasetId) { return this.stateMachine.process(datasetId); } + /** + * Gets name. + * + * @return the name + */ public String getName() { return this.name; } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/ProcessingState.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/ProcessingState.java index 4e52bd6a..93008808 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/ProcessingState.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/ProcessingState.java @@ -2,12 +2,24 @@ import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; import eu.europeana.metis.sandbox.repository.debias.DetectRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; +/** + * The type Processing state. + */ public class ProcessingState extends State implements Stateful { + private static final Logger LOGGER = LoggerFactory.getLogger(ProcessingState.class); private static final String STATE_NAME = "PROCESSING"; + /** + * Instantiates a new Processing state. + * + * @param debiasMachine the debias machine + * @param detectRepository the detect repository + */ public ProcessingState(DetectService debiasMachine, DetectRepository detectRepository) { this.stateMachine = debiasMachine; this.name = STATE_NAME; @@ -16,18 +28,18 @@ public ProcessingState(DetectService debiasMachine, DetectRepository detectRepos } @Override - public void fail(Long datasetId) { + public void fail(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getError()); } @Override - public void success(Long datasetId) { + public void success(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getCompleted()); } @Transactional @Override - public boolean process(Long datasetId) { + public boolean process(Integer datasetId) { LOGGER.info("{} {}", STATE_NAME, datasetId); try { DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId); @@ -47,6 +59,11 @@ public boolean process(Long datasetId) { return this.stateMachine.process(datasetId); } + /** + * Gets name. + * + * @return the name + */ public String getName() { return STATE_NAME; } diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/ReadyState.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/ReadyState.java index 24e51d85..17f58c53 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/ReadyState.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/ReadyState.java @@ -1,13 +1,26 @@ package eu.europeana.metis.sandbox.service.debias; +import eu.europeana.metis.sandbox.entity.DatasetEntity; import eu.europeana.metis.sandbox.entity.debias.DetectionEntity; import eu.europeana.metis.sandbox.repository.debias.DetectRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; +/** + * The type Ready state. + */ public class ReadyState extends State implements Stateful { + private static final Logger LOGGER = LoggerFactory.getLogger(ReadyState.class); private static final String STATE_NAME = "READY"; + /** + * Instantiates a new Ready state. + * + * @param debiasMachine the debias machine + * @param detectRepository the detect repository + */ public ReadyState(DetectService debiasMachine, DetectRepository detectRepository) { this.stateMachine = debiasMachine; @@ -17,25 +30,27 @@ public ReadyState(DetectService debiasMachine, } @Override - public void fail(Long datasetId) { + public void fail(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getReady()); } @Override - public void success(Long datasetId) { + public void success(Integer datasetId) { this.stateMachine.setState(this.stateMachine.getProcessing()); } @Transactional @Override - public boolean process(Long datasetId) { + public boolean process(Integer datasetId) { LOGGER.info("{} {}", STATE_NAME, datasetId); try { DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId); if (detectionEntity == null) { detectionEntity = new DetectionEntity(); detectionEntity.setState(STATE_NAME); - detectionEntity.setId(datasetId); + DatasetEntity dataset = new DatasetEntity(); + dataset.setDatasetId(datasetId); + detectionEntity.setDatasetId(dataset); detectRepository.save(detectionEntity); } else { detectRepository.updateState(datasetId, STATE_NAME); diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/State.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/State.java index edeba6c5..d12c2cfb 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/State.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/State.java @@ -6,8 +6,6 @@ public abstract class State { - public static final Logger LOGGER = LoggerFactory.getLogger(State.class); - protected DetectService stateMachine; protected DetectRepository detectRepository; protected String name; diff --git a/src/main/java/eu/europeana/metis/sandbox/service/debias/Stateful.java b/src/main/java/eu/europeana/metis/sandbox/service/debias/Stateful.java index b9bf2ed7..a490f864 100644 --- a/src/main/java/eu/europeana/metis/sandbox/service/debias/Stateful.java +++ b/src/main/java/eu/europeana/metis/sandbox/service/debias/Stateful.java @@ -1,7 +1,7 @@ package eu.europeana.metis.sandbox.service.debias; public interface Stateful { - void fail(Long datasetId); - void success(Long datasetId); - boolean process(Long datasetId); + void fail(Integer datasetId); + void success(Integer datasetId); + boolean process(Integer datasetId); } diff --git a/src/test/java/eu/europeana/metis/sandbox/controller/DatasetControllerTest.java b/src/test/java/eu/europeana/metis/sandbox/controller/DatasetControllerTest.java index c33dbff9..2f9d0544 100644 --- a/src/test/java/eu/europeana/metis/sandbox/controller/DatasetControllerTest.java +++ b/src/test/java/eu/europeana/metis/sandbox/controller/DatasetControllerTest.java @@ -844,7 +844,7 @@ void processDatasetFromOAI_AsyncExecutionException_expectLogging() throws Except @ParameterizedTest @ValueSource(strings = {"COMPLETED", "PROCESSING", "ERROR"}) void getDebias_expectSuccess(String status) throws Exception { - final Long datasetId = 1L; + final Integer datasetId = 1; final ZonedDateTime dateTime = ZonedDateTime.now(); when(detectService.getDetectionInfo(datasetId)) @@ -852,16 +852,16 @@ void getDebias_expectSuccess(String status) throws Exception { mvc.perform(get("/dataset/{id}/debias", datasetId)) .andExpect(status().isOk()) - .andExpect(jsonPath("$.dataset-id", is(datasetId.intValue()))) + .andExpect(jsonPath("$.dataset-id", is(datasetId))) .andExpect(jsonPath("$.state", is(status))) .andExpect(jsonPath("$.creation-date", is(dateTime.toOffsetDateTime() - .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nZ"))))); + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))))); } @ParameterizedTest @ValueSource(booleans = {true, false}) void processDebias_expectSuccess(boolean process) throws Exception { - final Long datasetId = 1L; + final Integer datasetId = 1; when(detectService.process(datasetId)).thenReturn(process); mvc.perform(post("/dataset/{id}/debias", datasetId)) .andExpect(status().isOk()) diff --git a/src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineServiceTest.java b/src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectServiceTest.java similarity index 65% rename from src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineServiceTest.java rename to src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectServiceTest.java index a64da124..4391caae 100644 --- a/src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasMachineServiceTest.java +++ b/src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectServiceTest.java @@ -3,10 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -24,123 +24,122 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class DebiasMachineServiceTest { +class DebiasDetectServiceTest { @Mock DetectRepository detectRepository; @InjectMocks - DebiasMachineService debiasMachineService; + DebiasDetectService debiasDetectService; @Test void processWhenNewHappyPath_Ready_Processing_Completed_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(null) .thenReturn(detectionEntity) .thenReturn(detectionEntity); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertTrue(result); - assertInstanceOf(CompletedState.class, debiasMachineService.getState()); + assertInstanceOf(CompletedState.class, debiasDetectService.getState()); verify(detectRepository, times(3)).findDetectionEntityByDatasetId_DatasetId(datasetId); verify(detectRepository, times(1)).save(any(DetectionEntity.class)); - verify(detectRepository, times(2)).updateState(anyLong(), anyString()); + verify(detectRepository, times(2)).updateState(anyInt(), anyString()); } @Test void processWhenNewHappyPath_Ready_andError_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenThrow(new RuntimeException("Error")); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertFalse(result); - assertInstanceOf(ReadyState.class, debiasMachineService.getState()); + assertInstanceOf(ReadyState.class, debiasDetectService.getState()); verify(detectRepository, times(1)).findDetectionEntityByDatasetId_DatasetId(datasetId); verify(detectRepository, times(0)).save(any(DetectionEntity.class)); - verify(detectRepository, times(0)).updateState(anyLong(), anyString()); + verify(detectRepository, times(0)).updateState(anyInt(), anyString()); } @Test void processWhenNewHappyPath_Ready_Processing_Completed_andError_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(null) .thenReturn(detectionEntity) .thenReturn(null) .thenReturn(null); - - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertFalse(result); - assertInstanceOf(ErrorState.class, debiasMachineService.getState()); + assertInstanceOf(ErrorState.class, debiasDetectService.getState()); verify(detectRepository, times(4)).findDetectionEntityByDatasetId_DatasetId(datasetId); verify(detectRepository, times(1)).save(any(DetectionEntity.class)); - verify(detectRepository, times(1)).updateState(anyLong(), anyString()); + verify(detectRepository, times(1)).updateState(anyInt(), anyString()); } @Test void processWhenNewHappyPath_Ready_Processing_Completed_andException_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(null) .thenReturn(detectionEntity) .thenThrow(new RuntimeException("Error")) .thenReturn(null); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertFalse(result); - assertInstanceOf(ErrorState.class, debiasMachineService.getState()); + assertInstanceOf(ErrorState.class, debiasDetectService.getState()); verify(detectRepository, times(4)).findDetectionEntityByDatasetId_DatasetId(datasetId); verify(detectRepository, times(1)).save(any(DetectionEntity.class)); - verify(detectRepository, times(1)).updateState(anyLong(), anyString()); + verify(detectRepository, times(1)).updateState(anyInt(), anyString()); } @Test void processWhenNewHappyPath_Ready_Processing_Error_Ready_Processing_Completed_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(null) .thenThrow(new RuntimeException("Error")) .thenReturn(detectionEntity) @@ -148,109 +147,109 @@ void processWhenNewHappyPath_Ready_Processing_Error_Ready_Processing_Completed_e .thenReturn(detectionEntity) .thenReturn(detectionEntity); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertTrue(result); - assertInstanceOf(CompletedState.class, debiasMachineService.getState()); + assertInstanceOf(CompletedState.class, debiasDetectService.getState()); verify(detectRepository, times(6)).findDetectionEntityByDatasetId_DatasetId(datasetId); verify(detectRepository, times(1)).save(any(DetectionEntity.class)); - verify(detectRepository, times(4)).updateState(anyLong(), anyString()); + verify(detectRepository, times(4)).updateState(anyInt(), anyString()); } @Test void processWhenNewHappyPath_Ready_Processing_andError_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState("READY"); detectionEntity.setCreatedDate(ZonedDateTime.now()); final DatasetEntity datasetEntity = new DatasetEntity(); - datasetEntity.setDatasetId(datasetId.intValue()); + datasetEntity.setDatasetId(datasetId); detectionEntity.setDatasetId(datasetEntity); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(null) .thenReturn(null) .thenReturn(null); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertFalse(result); - assertInstanceOf(ErrorState.class, debiasMachineService.getState()); + assertInstanceOf(ErrorState.class, debiasDetectService.getState()); verify(detectRepository, times(1)).save(any(DetectionEntity.class)); verify(detectRepository, times(3)).findDetectionEntityByDatasetId_DatasetId(datasetId); - verify(detectRepository, times(0)).updateState(anyLong(), anyString()); + verify(detectRepository, times(0)).updateState(anyInt(), anyString()); } @Test void processWhenDatasetAlreadyExists_Ready_Processing_Completed_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final String stateName = "READY"; final DatasetEntity dataset = new DatasetEntity(); dataset.setDatasetId(1); final DetectionEntity detectionEntity = new DetectionEntity(dataset, stateName); detectionEntity.setCreatedDate(ZonedDateTime.now()); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(detectionEntity) .thenReturn(detectionEntity); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertTrue(result); - assertInstanceOf(CompletedState.class, debiasMachineService.getState()); + assertInstanceOf(CompletedState.class, debiasDetectService.getState()); verify(detectRepository, times(3)).findDetectionEntityByDatasetId_DatasetId(datasetId); - verify(detectRepository, times(3)).updateState(anyLong(), anyString()); + verify(detectRepository, times(3)).updateState(anyInt(), anyString()); } @Test void processWhenDatasetAlreadyExists_Ready_Processing_andError_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final String stateName = "READY"; final DatasetEntity dataset = new DatasetEntity(); - dataset.setDatasetId(datasetId.intValue()); + dataset.setDatasetId(datasetId); final DetectionEntity detectionEntity = new DetectionEntity(dataset, stateName); detectionEntity.setCreatedDate(ZonedDateTime.now()); - when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyLong())) + when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt())) .thenReturn(detectionEntity) .thenReturn(null) .thenReturn(null); - boolean result = debiasMachineService.process(datasetId); + boolean result = debiasDetectService.process(datasetId); assertFalse(result); - assertInstanceOf(ErrorState.class, debiasMachineService.getState()); + assertInstanceOf(ErrorState.class, debiasDetectService.getState()); verify(detectRepository, times(3)).findDetectionEntityByDatasetId_DatasetId(datasetId); - verify(detectRepository, times(1)).updateState(anyLong(), anyString()); + verify(detectRepository, times(1)).updateState(anyInt(), anyString()); } @Test void set_and_get_State() { - debiasMachineService.setState(new ReadyState(debiasMachineService, detectRepository)); - assertInstanceOf(ReadyState.class, debiasMachineService.getState()); + debiasDetectService.setState(new ReadyState(debiasDetectService, detectRepository)); + assertInstanceOf(ReadyState.class, debiasDetectService.getState()); - debiasMachineService.setState(new ProcessingState(debiasMachineService, detectRepository)); - assertInstanceOf(ProcessingState.class, debiasMachineService.getState()); + debiasDetectService.setState(new ProcessingState(debiasDetectService, detectRepository)); + assertInstanceOf(ProcessingState.class, debiasDetectService.getState()); - debiasMachineService.setState(new CompletedState(debiasMachineService, detectRepository)); - assertInstanceOf(CompletedState.class, debiasMachineService.getState()); + debiasDetectService.setState(new CompletedState(debiasDetectService, detectRepository)); + assertInstanceOf(CompletedState.class, debiasDetectService.getState()); - debiasMachineService.setState(new ErrorState(debiasMachineService, detectRepository)); - assertInstanceOf(ErrorState.class, debiasMachineService.getState()); + debiasDetectService.setState(new ErrorState(debiasDetectService, detectRepository)); + assertInstanceOf(ErrorState.class, debiasDetectService.getState()); } @Test void testSpecificStateGetters() { - assertInstanceOf(ReadyState.class, debiasMachineService.getReady()); - assertInstanceOf(ProcessingState.class, debiasMachineService.getProcessing()); - assertInstanceOf(CompletedState.class, debiasMachineService.getCompleted()); - assertInstanceOf(ErrorState.class, debiasMachineService.getError()); + assertInstanceOf(ReadyState.class, debiasDetectService.getReady()); + assertInstanceOf(ProcessingState.class, debiasDetectService.getProcessing()); + assertInstanceOf(CompletedState.class, debiasDetectService.getCompleted()); + assertInstanceOf(ErrorState.class, debiasDetectService.getError()); } @Test void testGetDetectionInfo_ObjectWhenExists_expectSuccess() { - final Long datasetId = 1L; + final Integer datasetId = 1; final String stateName = "READY"; final ZonedDateTime createdDate = ZonedDateTime.now(); DatasetEntity dataset = new DatasetEntity(); - dataset.setDatasetId(datasetId.intValue()); + dataset.setDatasetId(datasetId); DetectionEntity detectionEntity = new DetectionEntity(); detectionEntity.setState(stateName); detectionEntity.setDatasetId(dataset); @@ -258,7 +257,7 @@ void testGetDetectionInfo_ObjectWhenExists_expectSuccess() { when(detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId)) .thenReturn(detectionEntity); - DetectionInfoDto detectionInfoDto = debiasMachineService.getDetectionInfo(datasetId); + DetectionInfoDto detectionInfoDto = debiasDetectService.getDetectionInfo(datasetId); assertEquals(datasetId, detectionInfoDto.getDatasetId()); assertEquals(stateName, detectionInfoDto.getState()); @@ -266,12 +265,14 @@ void testGetDetectionInfo_ObjectWhenExists_expectSuccess() { } @Test - void testGetDetectionInfo_NullWhenNotExists_expectSuccess() { - Long datasetId = 1L; + void testGetDetectionInfo_DefaultWhenNotExists_expectSuccess() { + final Integer datasetId = 1; - DetectionInfoDto detectionInfoDto = debiasMachineService.getDetectionInfo(datasetId); + DetectionInfoDto detectionInfoDto = debiasDetectService.getDetectionInfo(datasetId); - assertNull(detectionInfoDto); + assertNotNull(detectionInfoDto); + assertEquals(datasetId, detectionInfoDto.getDatasetId()); + assertEquals("READY", detectionInfoDto.getState()); } }