Skip to content

Commit

Permalink
Allow skipping persisting of workflow definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmokki committed Feb 13, 2015
1 parent 5662fd4 commit 5a1b4e3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.AbstractResource;
import org.springframework.stereotype.Component;

Expand All @@ -40,13 +41,15 @@ public class WorkflowDefinitionService {
private final Map<String, WorkflowDefinition<? extends WorkflowState>> workflowDefitions = new LinkedHashMap<>();
private final WorkflowInstanceDao workflowInstanceDao;
private final WorkflowDefinitionDao workflowDefinitionDao;
private final boolean persistWorkflowDefinitions;

@Inject
public WorkflowDefinitionService(@NFlow AbstractResource nflowNonSpringWorkflowsListing,
WorkflowInstanceDao workflowInstanceDao, WorkflowDefinitionDao workflowDefinitionDao) {
WorkflowInstanceDao workflowInstanceDao, WorkflowDefinitionDao workflowDefinitionDao, Environment env) {
this.nonSpringWorkflowsListing = nflowNonSpringWorkflowsListing;
this.workflowInstanceDao = workflowInstanceDao;
this.workflowDefinitionDao = workflowDefinitionDao;
this.persistWorkflowDefinitions = env.getRequiredProperty("nflow.definition.persist", Boolean.class);
}

/**
Expand Down Expand Up @@ -90,8 +93,10 @@ public void postProcessWorkflowDefinitions() throws IOException, ReflectiveOpera
} else {
initNonSpringWorkflowDefinitions();
}
for (WorkflowDefinition<?> definition : workflowDefitions.values()) {
workflowDefinitionDao.storeWorkflowDefinition(definition);
if (persistWorkflowDefinitions) {
for (WorkflowDefinition<?> definition : workflowDefitions.values()) {
workflowDefinitionDao.storeWorkflowDefinition(definition);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions nflow-engine/src/main/resources/nflow-engine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ nflow.db.postgresql.url=jdbc:postgresql://localhost/nflow
nflow.db.max_pool_size=4
nflow.db.idle_timeout_seconds=600
nflow.db.create_on_startup=true
nflow.definition.persist=true
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import com.nitorcreations.nflow.engine.internal.dao.WorkflowDefinitionDao;
Expand All @@ -35,14 +36,17 @@ public class WorkflowDefinitionServiceTest extends BaseNflowTest {
private WorkflowInstanceDao workflowInstanceDao;
@Mock
private WorkflowDefinitionDao workflowDefinitionDao;
@Mock
private Environment env;
private WorkflowDefinitionService service;

@Before
public void setup() throws Exception {
when(env.getRequiredProperty("nflow.definition.persist", Boolean.class)).thenReturn(true);
String dummyTestClassname = DummyTestWorkflow.class.getName();
ByteArrayInputStream bis = new ByteArrayInputStream(dummyTestClassname.getBytes(UTF_8));
when(nonSpringWorkflowListing.getInputStream()).thenReturn(bis);
service = new WorkflowDefinitionService(nonSpringWorkflowListing, workflowInstanceDao, workflowDefinitionDao);
service = new WorkflowDefinitionService(nonSpringWorkflowListing, workflowInstanceDao, workflowDefinitionDao, env);
assertThat(service.getWorkflowDefinitions().size(), is(equalTo(0)));
service.postProcessWorkflowDefinitions();
assertThat(service.getWorkflowDefinitions().size(), is(equalTo(1)));
Expand All @@ -67,7 +71,7 @@ public void demoWorkflowLoadedSuccessfully() {

@Test
public void nonSpringWorkflowsAreOptional() throws Exception {
service = new WorkflowDefinitionService(null, workflowInstanceDao, workflowDefinitionDao);
service = new WorkflowDefinitionService(null, workflowInstanceDao, workflowDefinitionDao, env);
service.postProcessWorkflowDefinitions();
assertEquals(0, service.getWorkflowDefinitions().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.AbstractResource;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
Expand All @@ -33,6 +35,10 @@ public class WorkflowDefinitionServiceWithSpringTest {
@Configuration
@ComponentScan(basePackageClasses = SpringDummyTestWorkflow.class)
static class ContextConfiguration {
@Bean
public Environment env() {
return new MockEnvironment().withProperty("nflow.definition.persist", "true");
}

@Bean
@NFlow
Expand Down

0 comments on commit 5a1b4e3

Please sign in to comment.