From 140df69463e1a80686d6448a33afb961ebbb8fe4 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sun, 22 Nov 2020 16:52:55 +0000 Subject: [PATCH] Add test for mongo repository persist - Add test to change state and check what is in mongo after each change. - Relates #892 --- .../data/mongodb/MongoDbRepositoryTests.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java index beb4bfea7..2df576041 100644 --- a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java +++ b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java @@ -16,6 +16,7 @@ package org.springframework.statemachine.data.mongodb; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll; @@ -62,6 +63,7 @@ protected void cleanInternal() { template.dropCollection(MongoDbRepositoryGuard.class); template.dropCollection(MongoDbRepositoryState.class); template.dropCollection(MongoDbRepositoryTransition.class); + template.dropCollection(MongoDbRepositoryStateMachine.class); c.close(); } @@ -148,6 +150,35 @@ public void testStateMachinePersistWithEnums() { assertThat(stateMachine.getState().getId(), is(PersistTestStates.S1)); } + @Test + public void testStateMachinePersists() { + context.register(TestConfig.class, ConfigWithStrings.class); + context.refresh(); + + MongoTemplate template = context.getBean(MongoTemplate.class); + List findAll = template.findAll(MongoDbRepositoryStateMachine.class); + assertThat(findAll, hasSize(0)); + + StateMachine stateMachine = resolveMachine(context); + doStartAndAssert(stateMachine); + assertThat(stateMachine.getState().getId(), is("S1")); + findAll = template.findAll(MongoDbRepositoryStateMachine.class); + assertThat(findAll, hasSize(1)); + assertThat(findAll.get(0).getState(), is("S1")); + + doSendEventAndConsumeAll(stateMachine, "E1"); + assertThat(stateMachine.getState().getId(), is("S2")); + findAll = template.findAll(MongoDbRepositoryStateMachine.class); + assertThat(findAll, hasSize(1)); + assertThat(findAll.get(0).getState(), is("S2")); + + doSendEventAndConsumeAll(stateMachine, "E2"); + assertThat(stateMachine.getState().getId(), is("S1")); + findAll = template.findAll(MongoDbRepositoryStateMachine.class); + assertThat(findAll, hasSize(1)); + assertThat(findAll.get(0).getState(), is("S1")); + } + @Override protected Class[] getRegisteredClasses() { return new Class[] { TestConfig.class };