-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anatoliy
authored and
Anatoliy
committed
May 6, 2018
1 parent
f402c64
commit 10e0ab2
Showing
6 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
src/test/java/com/antkorwin/statemachine/MongoPersistTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.output.ansi.enabled=always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |