diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbPersistingStateMachineInterceptor.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbPersistingStateMachineInterceptor.java new file mode 100644 index 00000000..3b6906b3 --- /dev/null +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbPersistingStateMachineInterceptor.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.mongodb; + +import org.springframework.statemachine.StateMachineContext; +import org.springframework.statemachine.data.mongodb.MongoDbRepositoryStateMachinePersist; +import org.springframework.statemachine.data.mongodb.MongoDbStateMachineRepository; +import org.springframework.statemachine.persist.AbstractPersistingStateMachineInterceptor; +import org.springframework.statemachine.persist.StateMachineRuntimePersister; +import org.springframework.statemachine.support.StateMachineInterceptor; +import org.springframework.util.Assert; + +/** + * {@code MongoDb} implementation of a {@link AbstractPersistingStateMachineInterceptor}. + * + * @author Janne Valkealahti + * + * @param the type of state + * @param the type of event + * @param the type of persister context object + */ +public class MongoDbPersistingStateMachineInterceptor extends AbstractPersistingStateMachineInterceptor + implements StateMachineRuntimePersister { + + private final MongoDbRepositoryStateMachinePersist persist; + + /** + * Instantiates a new mongodb persisting state machine interceptor. + * + * @param mongodbStateMachineRepository the mongodb state machine repository + */ + public MongoDbPersistingStateMachineInterceptor(MongoDbStateMachineRepository mongodbStateMachineRepository) { + Assert.notNull(mongodbStateMachineRepository, "'mongodbStateMachineRepository' must be set"); + this.persist = new MongoDbRepositoryStateMachinePersist(mongodbStateMachineRepository); + } + + /** + * Instantiates a new mongodb persisting state machine interceptor. + * + * @param persist the persist + */ + public MongoDbPersistingStateMachineInterceptor(MongoDbRepositoryStateMachinePersist persist) { + Assert.notNull(persist, "'persist' must be set"); + this.persist = persist; + } + + @Override + public StateMachineInterceptor getInterceptor() { + return this; + } + + @Override + public void write(StateMachineContext context, T contextObj) throws Exception { + persist.write(context, contextObj); + } + + @Override + public StateMachineContext read(Object contextObj) throws Exception { + return persist.read(contextObj); + } +} diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java new file mode 100644 index 00000000..f90d479b --- /dev/null +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java @@ -0,0 +1,64 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.mongodb; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.statemachine.data.RepositoryStateMachine; + +/** + * A {@link RepositoryStateMachine} interface for Redis used for states machines. + * + * @author Janne Valkealahti + * + */ +@Document(collection = "MongoDbRepositoryStateMachine") +public class MongoDbRepositoryStateMachine extends RepositoryStateMachine { + + @Id + private String id; + + private String machineId; + private String state; + private byte[] stateMachineContext; + + @Override + public String getMachineId() { + return machineId; + } + + public void setMachineId(String machineId) { + this.machineId = machineId; + } + + @Override + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @Override + public byte[] getStateMachineContext() { + return stateMachineContext; + } + + public void setStateMachineContext(byte[] stateMachineContext) { + this.stateMachineContext = stateMachineContext; + } +} diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java new file mode 100644 index 00000000..f0c92f31 --- /dev/null +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.mongodb; + +import org.springframework.statemachine.StateMachineContext; +import org.springframework.statemachine.data.RepositoryStateMachinePersist; +import org.springframework.statemachine.data.StateMachineRepository; +import org.springframework.statemachine.service.StateMachineSerialisationService; + +/** + * {@code MongoDb} based implementation of a {@link RepositoryStateMachinePersist}. + * + * @author Janne Valkealahti + * + * @param the type of state + * @param the type of event + */ +public class MongoDbRepositoryStateMachinePersist extends RepositoryStateMachinePersist { + + private final MongoDbStateMachineRepository mongodbStateMachineRepository; + + /** + * Instantiates a new mongodb repository state machine persist. + * + * @param mongodbStateMachineRepository the mongodb state machine repository + */ + public MongoDbRepositoryStateMachinePersist(MongoDbStateMachineRepository mongodbStateMachineRepository) { + super(); + this.mongodbStateMachineRepository = mongodbStateMachineRepository; + } + + /** + * Instantiates a new mongodb repository state machine persist. + * + * @param mongodbStateMachineRepository the mongodb state machine repository + * @param serialisationService the serialisation service + */ + public MongoDbRepositoryStateMachinePersist(MongoDbStateMachineRepository mongodbStateMachineRepository, + StateMachineSerialisationService serialisationService) { + super(serialisationService); + this.mongodbStateMachineRepository = mongodbStateMachineRepository; + } + + @Override + protected StateMachineRepository getRepository() { + return mongodbStateMachineRepository; + } + + @Override + protected MongoDbRepositoryStateMachine build(StateMachineContext context, byte[] serialisedContext) { + MongoDbRepositoryStateMachine mongodbRepositoryStateMachine = new MongoDbRepositoryStateMachine(); + mongodbRepositoryStateMachine.setMachineId(context.getId()); + mongodbRepositoryStateMachine.setState(context.getState().toString()); + mongodbRepositoryStateMachine.setStateMachineContext(serialisedContext); + return mongodbRepositoryStateMachine; + } +} diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbStateMachineRepository.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbStateMachineRepository.java new file mode 100644 index 00000000..2c376cc0 --- /dev/null +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbStateMachineRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.mongodb; + +import org.springframework.statemachine.data.StateMachineRepository; + +/** + * A {@link StateMachineRepository} interface for MongoDb used for states machines. + * + * @author Janne Valkealahti + * + */ +public interface MongoDbStateMachineRepository extends StateMachineRepository{ +} 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 7e33782c..2328a82b 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 @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,11 +24,22 @@ import java.util.List; import org.junit.Rule; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoTemplate; -//import org.springframework.data.keyvalue.core.KeyValueTemplate; +import org.springframework.statemachine.StateMachine; +import org.springframework.statemachine.config.EnableStateMachine; +import org.springframework.statemachine.config.StateMachineConfigurerAdapter; +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.AbstractRepositoryTests; +import org.springframework.statemachine.data.mongodb.MongoDbPersistingStateMachineInterceptor; +import org.springframework.statemachine.data.mongodb.MongoDbStateMachineRepository; +import org.springframework.statemachine.persist.StateMachineRuntimePersister; import org.springframework.statemachine.transition.TransitionKind; /** @@ -109,6 +120,36 @@ public class MongoDbRepositoryTests extends AbstractRepositoryTests { context.close(); } + @Test + @SuppressWarnings("unchecked") + public void testStateMachinePersistWithStrings() { + context.register(TestConfig.class, ConfigWithStrings.class); + context.refresh(); + + StateMachine stateMachine = context.getBean(StateMachine.class); + stateMachine.start(); + assertThat(stateMachine.getState().getId(), is("S1")); + stateMachine.sendEvent("E1"); + assertThat(stateMachine.getState().getId(), is("S2")); + stateMachine.sendEvent("E2"); + assertThat(stateMachine.getState().getId(), is("S1")); + } + + @Test + @SuppressWarnings("unchecked") + public void testStateMachinePersistWithEnums() { + context.register(TestConfig.class, ConfigWithEnums.class); + context.refresh(); + + StateMachine stateMachine = context.getBean(StateMachine.class); + stateMachine.start(); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S1)); + stateMachine.sendEvent(PersistTestEvents.E1); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S2)); + stateMachine.sendEvent(PersistTestEvents.E2); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S1)); + } + @Override protected Class[] getRegisteredClasses() { return new Class[] { TestConfig.class }; @@ -122,4 +163,102 @@ public class MongoDbRepositoryTests extends AbstractRepositoryTests { @EnableAutoConfiguration static class TestConfig { } + + @Configuration + @EnableStateMachine + public static class ConfigWithStrings extends StateMachineConfigurerAdapter { + + @Autowired + private MongoDbStateMachineRepository mongodbStateMachineRepository; + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config + .withConfiguration() + .machineId("xxx1") + .and() + .withPersistence() + .runtimePersister(stateMachineRuntimePersister()); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial("S1") + .state("S2"); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source("S1") + .target("S2") + .event("E1") + .and() + .withExternal() + .source("S2") + .target("S1") + .event("E2"); + } + + @Bean + public StateMachineRuntimePersister stateMachineRuntimePersister() { + return new MongoDbPersistingStateMachineInterceptor<>(mongodbStateMachineRepository); + } + } + + @Configuration + @EnableStateMachine + public static class ConfigWithEnums extends StateMachineConfigurerAdapter { + + @Autowired + private MongoDbStateMachineRepository mongodbStateMachineRepository; + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config + .withConfiguration() + .machineId("xxx2") + .and() + .withPersistence() + .runtimePersister(stateMachineRuntimePersister()); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial(PersistTestStates.S1) + .state(PersistTestStates.S2); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(PersistTestStates.S1) + .target(PersistTestStates.S2) + .event(PersistTestEvents.E1) + .and() + .withExternal() + .source(PersistTestStates.S2) + .target(PersistTestStates.S1) + .event(PersistTestEvents.E2); + } + + @Bean + public StateMachineRuntimePersister stateMachineRuntimePersister() { + return new MongoDbPersistingStateMachineInterceptor<>(mongodbStateMachineRepository); + } + } + + public enum PersistTestStates { + S1, S2; + } + + public enum PersistTestEvents { + E1, E2; + } } diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisPersistingStateMachineInterceptor.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisPersistingStateMachineInterceptor.java new file mode 100644 index 00000000..a337770a --- /dev/null +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisPersistingStateMachineInterceptor.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.redis; + +import org.springframework.statemachine.StateMachineContext; +import org.springframework.statemachine.data.redis.RedisRepositoryStateMachinePersist; +import org.springframework.statemachine.data.redis.RedisStateMachineRepository; +import org.springframework.statemachine.persist.AbstractPersistingStateMachineInterceptor; +import org.springframework.statemachine.persist.StateMachineRuntimePersister; +import org.springframework.statemachine.support.StateMachineInterceptor; +import org.springframework.util.Assert; + +/** + * {@code JPA} implementation of a {@link AbstractPersistingStateMachineInterceptor}. + * + * @author Janne Valkealahti + * + * @param the type of state + * @param the type of event + * @param the type of persister context object + */ +public class RedisPersistingStateMachineInterceptor extends AbstractPersistingStateMachineInterceptor + implements StateMachineRuntimePersister { + + private final RedisRepositoryStateMachinePersist persist; + + /** + * Instantiates a new redis persisting state machine interceptor. + * + * @param redisStateMachineRepository the redis state machine repository + */ + public RedisPersistingStateMachineInterceptor(RedisStateMachineRepository redisStateMachineRepository) { + Assert.notNull(redisStateMachineRepository, "'redisStateMachineRepository' must be set"); + this.persist = new RedisRepositoryStateMachinePersist(redisStateMachineRepository); + } + + /** + * Instantiates a new redis persisting state machine interceptor. + * + * @param persist the persist + */ + public RedisPersistingStateMachineInterceptor(RedisRepositoryStateMachinePersist persist) { + Assert.notNull(persist, "'persist' must be set"); + this.persist = persist; + } + + @Override + public StateMachineInterceptor getInterceptor() { + return this; + } + + @Override + public void write(StateMachineContext context, T contextObj) throws Exception { + persist.write(context, contextObj); + } + + @Override + public StateMachineContext read(Object contextObj) throws Exception { + return persist.read(contextObj); + } +} diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java new file mode 100644 index 00000000..f0b33b30 --- /dev/null +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java @@ -0,0 +1,64 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.redis; + +import org.springframework.data.annotation.Id; +import org.springframework.data.redis.core.RedisHash; +import org.springframework.statemachine.data.RepositoryStateMachine; + +/** + * A {@link RepositoryStateMachine} interface for Redis used for states machines. + * + * @author Janne Valkealahti + * + */ +@RedisHash("RedisRepositoryStateMachine") +public class RedisRepositoryStateMachine extends RepositoryStateMachine { + + @Id + private String id; + + private String machineId; + private String state; + private byte[] stateMachineContext; + + @Override + public String getMachineId() { + return machineId; + } + + public void setMachineId(String machineId) { + this.machineId = machineId; + } + + @Override + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @Override + public byte[] getStateMachineContext() { + return stateMachineContext; + } + + public void setStateMachineContext(byte[] stateMachineContext) { + this.stateMachineContext = stateMachineContext; + } +} diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java new file mode 100644 index 00000000..9e5336c1 --- /dev/null +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.redis; + +import org.springframework.statemachine.StateMachineContext; +import org.springframework.statemachine.data.RepositoryStateMachinePersist; +import org.springframework.statemachine.data.StateMachineRepository; +import org.springframework.statemachine.service.StateMachineSerialisationService; + +/** + * {@code Redis} based implementation of a {@link RepositoryStateMachinePersist}. + * + * @author Janne Valkealahti + * + * @param the type of state + * @param the type of event + */ +public class RedisRepositoryStateMachinePersist extends RepositoryStateMachinePersist { + + private final RedisStateMachineRepository redisStateMachineRepository; + + /** + * Instantiates a new redis repository state machine persist. + * + * @param redisStateMachineRepository the redis state machine repository + */ + public RedisRepositoryStateMachinePersist(RedisStateMachineRepository redisStateMachineRepository) { + super(); + this.redisStateMachineRepository = redisStateMachineRepository; + } + + /** + * Instantiates a new redis repository state machine persist. + * + * @param redisStateMachineRepository the redis state machine repository + * @param serialisationService the serialisation service + */ + public RedisRepositoryStateMachinePersist(RedisStateMachineRepository redisStateMachineRepository, + StateMachineSerialisationService serialisationService) { + super(serialisationService); + this.redisStateMachineRepository = redisStateMachineRepository; + } + + @Override + protected StateMachineRepository getRepository() { + return redisStateMachineRepository; + } + + @Override + protected RedisRepositoryStateMachine build(StateMachineContext context, byte[] serialisedContext) { + RedisRepositoryStateMachine redisRepositoryStateMachine = new RedisRepositoryStateMachine(); + redisRepositoryStateMachine.setMachineId(context.getId()); + redisRepositoryStateMachine.setState(context.getState().toString()); + redisRepositoryStateMachine.setStateMachineContext(serialisedContext); + return redisRepositoryStateMachine; + } +} diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisStateMachineRepository.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisStateMachineRepository.java new file mode 100644 index 00000000..6b2df0f8 --- /dev/null +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisStateMachineRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine.data.redis; + +import org.springframework.statemachine.data.StateMachineRepository; + +/** + * A {@link StateMachineRepository} interface for Redis used for states machines. + * + * @author Janne Valkealahti + * + */ +public interface RedisStateMachineRepository extends StateMachineRepository{ +} diff --git a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java index 46adb7fb..dca0cf9a 100644 --- a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java +++ b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +24,20 @@ import java.util.List; import org.junit.Rule; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.keyvalue.core.KeyValueTemplate; +import org.springframework.statemachine.StateMachine; +import org.springframework.statemachine.config.EnableStateMachine; +import org.springframework.statemachine.config.StateMachineConfigurerAdapter; +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.AbstractRepositoryTests; +import org.springframework.statemachine.persist.StateMachineRuntimePersister; import org.springframework.statemachine.transition.TransitionKind; /** @@ -108,6 +118,36 @@ public class RedisRepositoryTests extends AbstractRepositoryTests { context.close(); } + @Test + @SuppressWarnings("unchecked") + public void testStateMachinePersistWithStrings() { + context.register(TestConfig.class, ConfigWithStrings.class); + context.refresh(); + + StateMachine stateMachine = context.getBean(StateMachine.class); + stateMachine.start(); + assertThat(stateMachine.getState().getId(), is("S1")); + stateMachine.sendEvent("E1"); + assertThat(stateMachine.getState().getId(), is("S2")); + stateMachine.sendEvent("E2"); + assertThat(stateMachine.getState().getId(), is("S1")); + } + + @Test + @SuppressWarnings("unchecked") + public void testStateMachinePersistWithEnums() { + context.register(TestConfig.class, ConfigWithEnums.class); + context.refresh(); + + StateMachine stateMachine = context.getBean(StateMachine.class); + stateMachine.start(); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S1)); + stateMachine.sendEvent(PersistTestEvents.E1); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S2)); + stateMachine.sendEvent(PersistTestEvents.E2); + assertThat(stateMachine.getState().getId(), is(PersistTestStates.S1)); + } + @Override protected Class[] getRegisteredClasses() { return new Class[] { TestConfig.class }; @@ -121,4 +161,102 @@ public class RedisRepositoryTests extends AbstractRepositoryTests { @EnableAutoConfiguration static class TestConfig { } + + @Configuration + @EnableStateMachine + public static class ConfigWithStrings extends StateMachineConfigurerAdapter { + + @Autowired + private RedisStateMachineRepository redisStateMachineRepository; + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config + .withConfiguration() + .machineId("xxx1") + .and() + .withPersistence() + .runtimePersister(stateMachineRuntimePersister()); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial("S1") + .state("S2"); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source("S1") + .target("S2") + .event("E1") + .and() + .withExternal() + .source("S2") + .target("S1") + .event("E2"); + } + + @Bean + public StateMachineRuntimePersister stateMachineRuntimePersister() { + return new RedisPersistingStateMachineInterceptor<>(redisStateMachineRepository); + } + } + + @Configuration + @EnableStateMachine + public static class ConfigWithEnums extends StateMachineConfigurerAdapter { + + @Autowired + private RedisStateMachineRepository redisStateMachineRepository; + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config + .withConfiguration() + .machineId("xxx2") + .and() + .withPersistence() + .runtimePersister(stateMachineRuntimePersister()); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial(PersistTestStates.S1) + .state(PersistTestStates.S2); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(PersistTestStates.S1) + .target(PersistTestStates.S2) + .event(PersistTestEvents.E1) + .and() + .withExternal() + .source(PersistTestStates.S2) + .target(PersistTestStates.S1) + .event(PersistTestEvents.E2); + } + + @Bean + public StateMachineRuntimePersister stateMachineRuntimePersister() { + return new RedisPersistingStateMachineInterceptor<>(redisStateMachineRepository); + } + } + + public enum PersistTestStates { + S1, S2; + } + + public enum PersistTestEvents { + E1, E2; + } }