Skip to content

Commit

Permalink
mongodb persist
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy authored and Anatoliy committed May 6, 2018
1 parent f402c64 commit 10e0ab2
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 3 deletions.
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-data-mongodb</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- MongoDB -->

<!-- TestContainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.4.3</version>
</dependency>
<!-- TestContainers -->


</dependencies>

<dependencyManagement>
Expand All @@ -61,6 +83,18 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateMachinePersist;
Expand All @@ -17,11 +16,14 @@
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.data.mongodb.MongoDbPersistingStateMachineInterceptor;
import org.springframework.statemachine.data.mongodb.MongoDbStateMachineRepository;
import org.springframework.statemachine.guard.Guard;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.statemachine.persist.StateMachineRuntimePersister;
import org.springframework.statemachine.transition.Transition;

import java.util.Optional;
Expand Down Expand Up @@ -151,7 +153,7 @@ private Action<States, Events> deployPreProd() {
}

@Bean
@Profile({"in-memory","default"})
@Profile({"in-memory", "default"})
public StateMachinePersist<States, Events, UUID> inMemoryPersist() {
return new InMemoryStateMachinePersist();
}
Expand All @@ -162,11 +164,17 @@ public StateMachinePersist<States, Events, UUID> customPersist() {
return new CustomStateMachinePersist();
}

@Bean
@Profile("mongo")
public StateMachineRuntimePersister<States, Events, UUID> mongoRuntimePersist(
MongoDbStateMachineRepository repository) {
return new MongoDbPersistingStateMachineInterceptor<>(repository);
}

@Bean
public StateMachinePersister<States, Events, UUID> persister(
StateMachinePersist<States, Events, UUID> defaultPersist) {

return new DefaultStateMachinePersister<>(defaultPersist);
}

}
35 changes: 35 additions & 0 deletions src/test/java/com/antkorwin/statemachine/BaseMongoIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.antkorwin.statemachine;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.GenericContainer;

/**
* Created by Korovin A. on 28.03.2018.
* <p>
* Base Integration Test with
* MongoDb started in TestContainers
*
* @author Korovin Anatoliy
* @version 1.0
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public abstract class BaseMongoIT {

private static final Integer MONGO_PORT = 27017;
private static GenericContainer mongo = new GenericContainer("mongo:latest")
.withExposedPorts(MONGO_PORT);

static {
mongo.start();
System.setProperty("spring.data.mongodb.host", mongo.getContainerIpAddress());
System.setProperty("spring.data.mongodb.port", mongo.getMappedPort(MONGO_PORT).toString());
}

@Autowired
protected MongoTemplate mongoTemplate;
}
90 changes: 90 additions & 0 deletions src/test/java/com/antkorwin/statemachine/MongoPersistTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.antkorwin.statemachine;

import com.antkorwin.statemachine.statemachine.Events;
import com.antkorwin.statemachine.statemachine.States;
import org.assertj.core.api.Assertions;
import org.bson.Document;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
* Created by Korovin Anatolii on 06.05.2018.
*
* @author Korovin Anatolii
* @version 1.0
*/
@ActiveProfiles("mongo")
public class MongoPersistTest extends BaseMongoIT {

@Autowired
private StateMachinePersister<States, Events, UUID> persister;
@Autowired
private StateMachineFactory<States, Events> stateMachineFactory;

@Before
public void setUp() throws Exception {
mongoTemplate.getCollectionNames()
.forEach(mongoTemplate::dropCollection);
}

@Test
public void firstTest() {
// Arrange
// Act
Set<String> collectionNames = mongoTemplate.getCollectionNames();
// Asserts
Assertions.assertThat(collectionNames).isEmpty();
}

@Test
public void testMongoPersist() throws Exception {
// Arrange
StateMachine<States, Events> firstStateMachine = stateMachineFactory.getStateMachine();
firstStateMachine.sendEvent(Events.START_FEATURE);
firstStateMachine.sendEvent(Events.DEPLOY);

StateMachine<States, Events> secondStateMachine = stateMachineFactory.getStateMachine();

// Check Precondition
Assertions.assertThat(firstStateMachine.getState().getId()).isEqualTo(States.IN_PROGRESS);
Assertions.assertThat((boolean) firstStateMachine.getExtendedState().getVariables().get("deployed"))
.isEqualTo(true);
Assertions.assertThat(secondStateMachine.getState().getId()).isEqualTo(States.BACKLOG);
Assertions.assertThat(secondStateMachine.getExtendedState().getVariables().get("deployed")).isNull();

// Act
persister.persist(firstStateMachine, firstStateMachine.getUuid());
persister.persist(secondStateMachine, secondStateMachine.getUuid());
persister.restore(secondStateMachine, firstStateMachine.getUuid());

// Asserts
Assertions.assertThat(secondStateMachine.getState().getId())
.isEqualTo(States.IN_PROGRESS);

Assertions.assertThat((boolean) secondStateMachine.getExtendedState().getVariables().get("deployed"))
.isEqualTo(true);

// Mongo specific asserts:
Assertions.assertThat(mongoTemplate.getCollectionNames())
.isNotEmpty();
List<Document> documents = mongoTemplate.findAll(Document.class,
"MongoDbRepositoryStateMachine");

Assertions.assertThat(documents).hasSize(2);
Assertions.assertThat(documents)
.flatExtracting(Document::values)
.contains(firstStateMachine.getUuid().toString(),
secondStateMachine.getUuid().toString())
.contains(firstStateMachine.getState().getId().toString(),
secondStateMachine.getState().getId().toString());
}
}
1 change: 1 addition & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.output.ansi.enabled=always
17 changes: 17 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<logger name="org.apache.http" level="WARN"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
<logger name="org.dbunit" level="ERROR"/>
</configuration>

0 comments on commit 10e0ab2

Please sign in to comment.