Skip to content

Commit

Permalink
MET-5697 add unit tests evaluating code bifurcations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeortizquan committed Jan 16, 2024
1 parent 772bb99 commit 10507df
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private ExecutablePluginFactory() {
* new plugins's id and data status.
*
* @param metadata The metadata for which to create a plugin. Cannot be null.
* @return A new pluing instance.
* @return A new plugin instance.
*/
public static AbstractExecutablePlugin createPlugin(ExecutablePluginMetadata metadata) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import eu.europeana.metis.core.service.Authorizer;
import eu.europeana.metis.core.service.OrchestratorService;
import eu.europeana.metis.core.service.ProxiesService;
import eu.europeana.metis.core.service.RedirectionInferrer;
import eu.europeana.metis.core.service.ScheduleWorkflowService;
import eu.europeana.metis.core.service.WorkflowExecutionFactory;
import eu.europeana.metis.core.workflow.ValidationProperties;
Expand Down Expand Up @@ -158,11 +159,11 @@ public ValidationProperties getValidationInternalProperties(
public WorkflowExecutionFactory getWorkflowExecutionFactory(
@Qualifier("validationExternalProperties") ValidationProperties validationExternalProperties,
@Qualifier("validationInternalProperties") ValidationProperties validationInternalProperties,
WorkflowExecutionDao workflowExecutionDao, DataEvolutionUtils dataEvolutionUtils,
RedirectionInferrer redirectionInferrer,
DatasetXsltDao datasetXsltDao, DepublishRecordIdDao depublishRecordIdDao,
MetisCoreConfigurationProperties metisCoreConfigurationProperties) {
WorkflowExecutionFactory workflowExecutionFactory = new WorkflowExecutionFactory(datasetXsltDao,
depublishRecordIdDao, workflowExecutionDao, dataEvolutionUtils);
depublishRecordIdDao, redirectionInferrer);
workflowExecutionFactory
.setValidationExternalProperties(validationExternalProperties);
workflowExecutionFactory
Expand All @@ -172,6 +173,12 @@ public WorkflowExecutionFactory getWorkflowExecutionFactory(
return workflowExecutionFactory;
}

@Bean
public RedirectionInferrer getRedirectionInferrer(WorkflowExecutionDao workflowExecutionDao,
DataEvolutionUtils dataEvolutionUtils) {
return new RedirectionInferrer(workflowExecutionDao, dataEvolutionUtils);
}

@Bean
public ScheduleWorkflowService getScheduleWorkflowService(
ScheduledWorkflowDao scheduledWorkflowDao, WorkflowDao workflowDao, DatasetDao datasetDao,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.function.BooleanSupplier;
import org.springframework.util.CollectionUtils;

public class RedirectionService {
public class RedirectionInferrer {

private final WorkflowExecutionDao workflowExecutionDao;
private final DataEvolutionUtils dataEvolutionUtils;

public RedirectionService(WorkflowExecutionDao workflowExecutionDao, DataEvolutionUtils dataEvolutionUtils) {
public RedirectionInferrer(WorkflowExecutionDao workflowExecutionDao, DataEvolutionUtils dataEvolutionUtils) {
this.workflowExecutionDao = workflowExecutionDao;
this.dataEvolutionUtils = dataEvolutionUtils;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package eu.europeana.metis.core.service;

import eu.europeana.metis.core.common.TransformationParameters;
import eu.europeana.metis.core.dao.DataEvolutionUtils;
import eu.europeana.metis.core.dao.DatasetXsltDao;
import eu.europeana.metis.core.dao.DepublishRecordIdDao;
import eu.europeana.metis.core.dao.PluginWithExecutionId;
import eu.europeana.metis.core.dao.WorkflowExecutionDao;
import eu.europeana.metis.core.dataset.Dataset;
import eu.europeana.metis.core.dataset.DatasetXslt;
import eu.europeana.metis.core.dataset.DepublishRecordId.DepublicationStatus;
Expand All @@ -28,12 +26,9 @@
import eu.europeana.metis.core.workflow.plugins.ValidationInternalPluginMetadata;
import eu.europeana.metis.exception.BadContentException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.BooleanSupplier;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

/**
* Class that contains various functionality for "helping" the {@link OrchestratorService}.
Expand All @@ -45,9 +40,7 @@ public class WorkflowExecutionFactory {

private final DatasetXsltDao datasetXsltDao;
private final DepublishRecordIdDao depublishRecordIdDao;
private final WorkflowExecutionDao workflowExecutionDao;
private final DataEvolutionUtils dataEvolutionUtils;
private final RedirectionService redirectionService;
private final RedirectionInferrer redirectionInferrer;

private ValidationProperties validationExternalProperties; // Use getter and setter!
private ValidationProperties validationInternalProperties; // Use getter and setter!
Expand All @@ -58,17 +51,13 @@ public class WorkflowExecutionFactory {
*
* @param datasetXsltDao the Dao instance to access the dataset xslts
* @param depublishRecordIdDao The Dao instance to access depublish records.
* @param workflowExecutionDao the Dao instance to access the workflow executions
* @param dataEvolutionUtils the utilities class for workflow operations
* @param redirectionInferrer the service instance to access redirection logic
*/
public WorkflowExecutionFactory(DatasetXsltDao datasetXsltDao,
DepublishRecordIdDao depublishRecordIdDao, WorkflowExecutionDao workflowExecutionDao,
DataEvolutionUtils dataEvolutionUtils) {
DepublishRecordIdDao depublishRecordIdDao, RedirectionInferrer redirectionInferrer) {
this.datasetXsltDao = datasetXsltDao;
this.depublishRecordIdDao = depublishRecordIdDao;
this.workflowExecutionDao = workflowExecutionDao;
this.dataEvolutionUtils = dataEvolutionUtils;
this.redirectionService = new RedirectionService(workflowExecutionDao,dataEvolutionUtils);
this.redirectionInferrer = redirectionInferrer;
}

// Expect the dataset to be synced with eCloud.
Expand Down Expand Up @@ -112,12 +101,12 @@ private AbstractExecutablePlugin<?> createWorkflowExecutionPlugin(Dataset datase
this.setupValidationInternalForPluginMetadata(validationInternalPluginMetadata, getValidationInternalProperties());
} else if (pluginMetadata instanceof IndexToPreviewPluginMetadata indexToPreviewPluginMetadata) {
indexToPreviewPluginMetadata.setDatasetIdsToRedirectFrom(dataset.getDatasetIdsToRedirectFrom());
boolean performRedirects = redirectionService.shouldRedirectsBePerformed(dataset, workflowPredecessor,
boolean performRedirects = redirectionInferrer.shouldRedirectsBePerformed(dataset, workflowPredecessor,
ExecutablePluginType.PREVIEW, typesInWorkflowBeforeThisPlugin);
indexToPreviewPluginMetadata.setPerformRedirects(performRedirects);
} else if (pluginMetadata instanceof IndexToPublishPluginMetadata indexToPublishPluginMetadata) {
indexToPublishPluginMetadata.setDatasetIdsToRedirectFrom(dataset.getDatasetIdsToRedirectFrom());
boolean performRedirects = redirectionService.shouldRedirectsBePerformed(dataset, workflowPredecessor,
boolean performRedirects = redirectionInferrer.shouldRedirectsBePerformed(dataset, workflowPredecessor,
ExecutablePluginType.PUBLISH, typesInWorkflowBeforeThisPlugin);
indexToPublishPluginMetadata.setPerformRedirects(performRedirects);
} else if (pluginMetadata instanceof DepublishPluginMetadata depublishPluginMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class TestOrchestratorService {
private static final int SOLR_COMMIT_PERIOD_IN_MINS = 15;
private static WorkflowExecutionDao workflowExecutionDao;
private static DataEvolutionUtils dataEvolutionUtils;
private static RedirectionInferrer redirectionInferrer;
private static WorkflowValidationUtils validationUtils;
private static WorkflowDao workflowDao;
private static DatasetDao datasetDao;
Expand All @@ -140,8 +141,9 @@ static void prepare() {
redissonClient = mock(RedissonClient.class);
authorizer = mock(Authorizer.class);

redirectionInferrer = new RedirectionInferrer(workflowExecutionDao, dataEvolutionUtils);
workflowExecutionFactory = spy(new WorkflowExecutionFactory(datasetXsltDao,
depublishRecordIdDao, workflowExecutionDao, dataEvolutionUtils));
depublishRecordIdDao, redirectionInferrer));
workflowExecutionFactory.setValidationExternalProperties(
new ValidationProperties("url-ext", "schema-ext", "schematron-ext"));
workflowExecutionFactory.setValidationInternalProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.europeana.metis.core.workflow.Workflow;
import eu.europeana.metis.core.workflow.plugins.DataStatus;
import eu.europeana.metis.core.workflow.plugins.ExecutablePluginFactory;
import eu.europeana.metis.core.workflow.plugins.ExecutablePluginType;
import eu.europeana.metis.core.workflow.plugins.ExecutionProgress;
import eu.europeana.metis.core.workflow.plugins.HTTPHarvestPlugin;
import eu.europeana.metis.core.workflow.plugins.HTTPHarvestPluginMetadata;
Expand All @@ -17,6 +18,7 @@
import eu.europeana.metis.core.workflow.plugins.PluginStatus;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
Expand Down Expand Up @@ -142,4 +144,19 @@ static IndexToPublishPlugin getIndexToPublishPlugin(IndexToPublishPluginMetadata
indexToPublishPlugin.setDataStatus(DataStatus.VALID);
return indexToPublishPlugin;
}

@NotNull
static List<ExecutablePluginType> getExecutablePluginTypes() {
final List<ExecutablePluginType> typesInWorkflow = new ArrayList<>();
typesInWorkflow.add(ExecutablePluginType.HTTP_HARVEST);
typesInWorkflow.add(ExecutablePluginType.VALIDATION_EXTERNAL);
typesInWorkflow.add(ExecutablePluginType.TRANSFORMATION);
typesInWorkflow.add(ExecutablePluginType.VALIDATION_INTERNAL);
typesInWorkflow.add(ExecutablePluginType.NORMALIZATION);
typesInWorkflow.add(ExecutablePluginType.ENRICHMENT);
typesInWorkflow.add(ExecutablePluginType.MEDIA_PROCESS);
typesInWorkflow.add(ExecutablePluginType.PREVIEW);
typesInWorkflow.add(ExecutablePluginType.PUBLISH);
return typesInWorkflow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package eu.europeana.metis.core.service;

import static eu.europeana.metis.core.service.TestRedirectionBase.getExecutablePluginTypes;
import static eu.europeana.metis.core.service.TestRedirectionBase.getExecutionProgress;
import static eu.europeana.metis.core.service.TestRedirectionBase.getIndexToPreviewPlugin;
import static eu.europeana.metis.core.service.TestRedirectionBase.getIndexToPreviewPluginMetadata;
import static eu.europeana.metis.core.service.TestRedirectionBase.getIndexToPublishPlugin;
import static eu.europeana.metis.core.service.TestRedirectionBase.getIndexToPublishPluginMetadata;
import static eu.europeana.metis.core.service.TestRedirectionBase.getTestDataset;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import eu.europeana.metis.core.dao.DataEvolutionUtils;
import eu.europeana.metis.core.dao.PluginWithExecutionId;
import eu.europeana.metis.core.dao.WorkflowExecutionDao;
import eu.europeana.metis.core.dataset.Dataset;
import eu.europeana.metis.core.workflow.plugins.ExecutablePlugin;
import eu.europeana.metis.core.workflow.plugins.ExecutablePluginType;
import eu.europeana.metis.core.workflow.plugins.IndexToPreviewPlugin;
import eu.europeana.metis.core.workflow.plugins.IndexToPreviewPluginMetadata;
import eu.europeana.metis.core.workflow.plugins.IndexToPublishPlugin;
import eu.europeana.metis.core.workflow.plugins.IndexToPublishPluginMetadata;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class TestRedirectionInferrer {

@Mock
WorkflowExecutionDao workflowExecutionDao;

@Mock
DataEvolutionUtils dataEvolutionUtils;

@InjectMocks
RedirectionInferrer redirectionInferrer;

@Test
void shouldRedirectsBePerformed_whenRootAncestorDifferent_expectRedirect() {
final Dataset dataset = getTestDataset();
final IndexToPreviewPluginMetadata indexToPreviewPluginMetadata = getIndexToPreviewPluginMetadata();
final IndexToPreviewPlugin indexToPreviewPlugin = getIndexToPreviewPlugin(indexToPreviewPluginMetadata,
getExecutionProgress());
final IndexToPublishPluginMetadata indexToPublishPluginMetadata = getIndexToPublishPluginMetadata(indexToPreviewPlugin);
final IndexToPublishPlugin indexToPublishPlugin = getIndexToPublishPlugin(indexToPublishPluginMetadata,
getExecutionProgress());
final IndexToPublishPlugin indexToPreviewPlugin2 = getIndexToPublishPlugin(indexToPublishPluginMetadata,
getExecutionProgress());
final PluginWithExecutionId<ExecutablePlugin> indexToPublishPluginWithExecutionId =
new PluginWithExecutionId<>("executionId", indexToPublishPlugin);
final PluginWithExecutionId<ExecutablePlugin> indexToPreviewPluginWithExecutionId2 =
new PluginWithExecutionId<>("executionId2", indexToPreviewPlugin2);
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(indexToPublishPluginWithExecutionId);

when(dataEvolutionUtils.getRootAncestor(any()))
.thenReturn(indexToPublishPluginWithExecutionId)
.thenReturn(indexToPreviewPluginWithExecutionId2);

final PluginWithExecutionId<ExecutablePlugin> predecessor = new PluginWithExecutionId<>("executionId",
indexToPreviewPlugin);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, predecessor,
ExecutablePluginType.PREVIEW, new ArrayList<>());

assertTrue(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenRootAncestorTheSame_expectNoRedirect() {
final Dataset dataset = getTestDataset();
final IndexToPreviewPluginMetadata indexToPreviewPluginMetadata = getIndexToPreviewPluginMetadata();
final IndexToPreviewPlugin indexToPreviewPlugin = getIndexToPreviewPlugin(indexToPreviewPluginMetadata,
getExecutionProgress());
final IndexToPublishPluginMetadata indexToPublishPluginMetadata = getIndexToPublishPluginMetadata(indexToPreviewPlugin);
final IndexToPublishPlugin indexToPublishPlugin = getIndexToPublishPlugin(indexToPublishPluginMetadata,
getExecutionProgress());

final PluginWithExecutionId<ExecutablePlugin> indexToPublishPluginWithExecutionId =
new PluginWithExecutionId<>("executionId", indexToPublishPlugin);
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(indexToPublishPluginWithExecutionId);

when(dataEvolutionUtils.getRootAncestor(any()))
.thenReturn(indexToPublishPluginWithExecutionId)
.thenReturn(indexToPublishPluginWithExecutionId);

final PluginWithExecutionId<ExecutablePlugin> predecessor = new PluginWithExecutionId<>("executionId",
indexToPreviewPlugin);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, predecessor,
ExecutablePluginType.PREVIEW, new ArrayList<>());

assertFalse(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenLatestSuccessfulPluginButDatasetUpdate_expectRedirect() {
final Dataset dataset = getTestDataset();
dataset.setUpdatedDate(Date.from(Instant.now()));
dataset.setDatasetIdsToRedirectFrom(List.of("258"));
final IndexToPreviewPluginMetadata indexToPreviewPluginMetadata = getIndexToPreviewPluginMetadata();
final IndexToPreviewPlugin indexToPreviewPlugin = getIndexToPreviewPlugin(indexToPreviewPluginMetadata,
getExecutionProgress());
final PluginWithExecutionId<ExecutablePlugin> indexToPreviewPluginWithExecutionId =
new PluginWithExecutionId<>("executionId", indexToPreviewPlugin);
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(indexToPreviewPluginWithExecutionId);
final PluginWithExecutionId<ExecutablePlugin> predecessor = new PluginWithExecutionId<>("executionId",
indexToPreviewPlugin);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, predecessor,
ExecutablePluginType.PREVIEW, new ArrayList<>());

assertTrue(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenLatestSuccessfulPluginButNoDatasetUpdate_expectNoRedirect() {
final Dataset dataset = getTestDataset();
final IndexToPreviewPluginMetadata indexToPreviewPluginMetadata = getIndexToPreviewPluginMetadata();
final IndexToPreviewPlugin indexToPreviewPlugin = getIndexToPreviewPlugin(indexToPreviewPluginMetadata,
getExecutionProgress());
final PluginWithExecutionId<ExecutablePlugin> indexToPreviewPluginWithExecutionId =
new PluginWithExecutionId<>("executionId", indexToPreviewPlugin);
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(indexToPreviewPluginWithExecutionId);
when(dataEvolutionUtils.getRootAncestor(any()))
.thenReturn(indexToPreviewPluginWithExecutionId)
.thenReturn(indexToPreviewPluginWithExecutionId);
final PluginWithExecutionId<ExecutablePlugin> predecessor = new PluginWithExecutionId<>("executionId",
indexToPreviewPlugin);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, predecessor,
ExecutablePluginType.PREVIEW, new ArrayList<>());

assertFalse(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenNoLatestSuccessfulPluginButDatasetRedirects_expectRedirect() {
final Dataset dataset = getTestDataset();
dataset.setDatasetIdsToRedirectFrom(List.of("258"));
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(null);
boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, null,
ExecutablePluginType.PREVIEW,
new ArrayList<>());

assertTrue(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenNoLatestSuccessfulPluginAndNoDatasetRedirects_expectNoRedirect() {
final Dataset dataset = getTestDataset();
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(null);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, null,
ExecutablePluginType.PREVIEW,
new ArrayList<>());

assertFalse(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenTypesInWorkflowSameAsExecutablePluginType_expectNoRedirect() {
final Dataset dataset = getTestDataset();
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(null);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, null, ExecutablePluginType.PREVIEW,
getExecutablePluginTypes());

assertFalse(redirectsToBePerformed);
}

@Test
void shouldRedirectsBePerformed_whenTypesInWorkflowLatestHarvestAndDatasetRedirect_expectRedirect() {
final Dataset dataset = getTestDataset();
dataset.setDatasetIdsToRedirectFrom(List.of("258"));
when(workflowExecutionDao.getLatestSuccessfulExecutablePlugin(anyString(), any(), eq(Boolean.FALSE)))
.thenReturn(null);

boolean redirectsToBePerformed = redirectionInferrer.shouldRedirectsBePerformed(dataset, null,
ExecutablePluginType.HTTP_HARVEST,
getExecutablePluginTypes());

assertTrue(redirectsToBePerformed);
}
}
Loading

0 comments on commit 10507df

Please sign in to comment.