Skip to content

Commit

Permalink
MET-5961 update unit tests and method to retrieve detection per dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
jeortizquan committed Jul 18, 2024
1 parent c2c8d81 commit a6ab975
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 64 deletions.
37 changes: 37 additions & 0 deletions src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,66 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* The type Debias config.
*/
@Configuration
public class DebiasConfig {

/**
* Debias machine detect service.
*
* @param detectRepository the detect repository
* @return the detect service
*/
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public List<LanguageView> getAllLanguages() {
@ApiResponse(responseCode = "400", description = MESSAGE_FOR_400_CODE)
@PostMapping(value = "{id}/debias", produces = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public boolean processDebias(@PathVariable("id") String datasetId) {
public boolean processDebias(@PathVariable("id") Long datasetId) {
return debiasDetectService.process(datasetId);
}

Expand All @@ -425,7 +425,7 @@ public boolean processDebias(@PathVariable("id") String datasetId) {
@ApiResponse(responseCode = "400", description = MESSAGE_FOR_400_CODE)
@GetMapping(value = "{id}/debias", produces = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public DetectionInfoDto getDebiasDetection(@PathVariable("id") String datasetId) {
public DetectionInfoDto getDebiasDetection(@PathVariable("id") Long datasetId) {
return debiasDetectService.getDetectionInfo(datasetId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DetectionInfoDto {
public static final String SWAGGER_MODEL_NAME = "DebiasDetectionInfo";

@JsonProperty("dataset-id")
private final String datasetId;
private final Long datasetId;

@JsonProperty("state")
private final String state;
Expand All @@ -19,7 +19,7 @@ public class DetectionInfoDto {
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.nZ")
private final ZonedDateTime creationDate;

public DetectionInfoDto(String datasetId, String state, ZonedDateTime creationDate) {
public DetectionInfoDto(Long datasetId, String state, ZonedDateTime creationDate) {
this.datasetId = datasetId;
this.state = state;
this.creationDate = creationDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface DetectRepository extends JpaRepository<DetectionEntity, Integer> {
public interface DetectRepository extends JpaRepository<DetectionEntity, Long> {

@Query("SELECT dec.datasetId, dec.state, dec.createdDate FROM DetectionEntity dec")
List<DetectionEntity> listDbEntries();

DetectionEntity findByDatasetId(String datasetId);
DetectionEntity findDetectionEntityByDatasetId_DatasetId(Long datasetId);

@Modifying
@Query("UPDATE DetectionEntity dec SET dec.state = '?2' WHERE dec.datasetId = ?1")
void updateState(String datasetId, String state);
void updateState(Long datasetId, String state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public CompletedState(DetectService debiasMachine, DetectRepository detectReposi
}

@Override
public void fail(String datasetId) {
public void fail(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getError());
}

@Override
public void success(String datasetId) {
public void success(Long datasetId) {
// do nothing, processing completed.
}

@Transactional
@Override
public boolean process(String datasetId) {
public boolean process(Long datasetId) {
LOGGER.info("{} {}", STATE_NAME, datasetId);
try {
DetectionEntity detectionEntity = detectRepository.findByDatasetId(datasetId);
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
if (detectionEntity == null) {
fail(datasetId);
LOGGER.warn("invalid state {} {}", STATE_NAME, datasetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public DebiasMachineService(DetectRepository detectRepository) {
}

@Override
public void fail(String datasetId) {
public void fail(Long datasetId) {
state.fail(datasetId);
}

@Override
public void success(String datasetId) {
public void success(Long datasetId) {
state.success(datasetId);
}

@Override
public boolean process(String datasetId) {
public boolean process(Long datasetId) {
return state.process(datasetId);
}

Expand All @@ -49,8 +49,8 @@ public Stateful getState() {
* @return the debias detection info
*/
@Override
public DetectionInfoDto getDetectionInfo(String datasetId) {
DetectionEntity detectionEntity = detectRepository.findByDatasetId(datasetId);
public DetectionInfoDto getDetectionInfo(Long datasetId) {
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
return new DetectionInfoDto(datasetId, detectionEntity.getState(), detectionEntity.getCreatedDate());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

public interface DetectService {

void fail(String datasetId);
void fail(Long datasetId);

void success(String datasetId);
void success(Long datasetId);

boolean process(String datasetId);
boolean process(Long datasetId);

Stateful getState();

Expand All @@ -22,5 +22,5 @@ public interface DetectService {

Stateful getError();

DetectionInfoDto getDetectionInfo(String datasetId);
DetectionInfoDto getDetectionInfo(Long datasetId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public ErrorState(DetectService debiasMachine, DetectRepository detectRepository
}

@Override
public void fail(String datasetId) {
public void fail(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getError());
}

@Override
public void success(String datasetId) {
public void success(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getReady());
}

@Transactional
@Override
public boolean process(String datasetId) {
public boolean process(Long datasetId) {
LOGGER.info("{} {}", STATE_NAME, datasetId);
try {
DetectionEntity detectionEntity = detectRepository.findByDatasetId(datasetId);
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
if (detectionEntity == null) {
fail(datasetId);
LOGGER.warn("invalid state {} {}", STATE_NAME, datasetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public ProcessingState(DetectService debiasMachine, DetectRepository detectRepos
}

@Override
public void fail(String datasetId) {
public void fail(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getError());
}

@Override
public void success(String datasetId) {
public void success(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getCompleted());
}

@Transactional
@Override
public boolean process(String datasetId) {
public boolean process(Long datasetId) {
LOGGER.info("{} {}", STATE_NAME, datasetId);
try {
DetectionEntity detectionEntity = detectRepository.findByDatasetId(datasetId);
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
if (detectionEntity == null) {
fail(datasetId);
LOGGER.warn("invalid state {} {}", STATE_NAME, datasetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ public ReadyState(DetectService debiasMachine,
}

@Override
public void fail(String datasetId) {
public void fail(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getReady());
}

@Override
public void success(String datasetId) {
public void success(Long datasetId) {
this.stateMachine.setState(this.stateMachine.getProcessing());
}

@Transactional
@Override
public boolean process(String datasetId) {
public boolean process(Long datasetId) {
LOGGER.info("{} {}", STATE_NAME, datasetId);
try {
DetectionEntity detectionEntity = detectRepository.findByDatasetId(datasetId);
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
if (detectionEntity == null) {
detectionEntity = new DetectionEntity();
detectionEntity.setState(STATE_NAME);
detectionEntity.setId(Long.parseLong(datasetId));
detectionEntity.setId(datasetId);
detectRepository.save(detectionEntity);
} else {
detectRepository.updateState(datasetId, STATE_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.metis.sandbox.service.debias;

public interface Stateful {
void fail(String datasetId);
void success(String datasetId);
boolean process(String datasetId);
void fail(Long datasetId);
void success(Long datasetId);
boolean process(Long datasetId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,15 @@ void processDatasetFromOAI_AsyncExecutionException_expectLogging() throws Except
@ParameterizedTest
@ValueSource(strings = {"COMPLETED", "PROCESSING", "ERROR"})
void getDebias_expectSuccess(String status) throws Exception {
final String datasetId = "1";
final Long datasetId = 1L;
final ZonedDateTime dateTime = ZonedDateTime.now();

when(detectService.getDetectionInfo(datasetId))
.thenReturn(new DetectionInfoDto(datasetId, status, dateTime));

mvc.perform(get("/dataset/{id}/debias", datasetId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.dataset-id", is(datasetId)))
.andExpect(jsonPath("$.dataset-id", is(datasetId.intValue())))
.andExpect(jsonPath("$.state", is(status)))
.andExpect(jsonPath("$.creation-date", is(dateTime.toOffsetDateTime()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nZ")))));
Expand All @@ -861,7 +861,7 @@ void getDebias_expectSuccess(String status) throws Exception {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void processDebias_expectSuccess(boolean process) throws Exception {
final String datasetId = "1";
final Long datasetId = 1L;
when(detectService.process(datasetId)).thenReturn(process);
mvc.perform(post("/dataset/{id}/debias", datasetId))
.andExpect(status().isOk())
Expand Down
Loading

0 comments on commit a6ab975

Please sign in to comment.