From be6ee233c5255ba110d9d3a560ef1df8d45a1a5f Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 11 May 2019 10:05:16 +0100 Subject: [PATCH] Reactive changes for docs - Relates #742 - Relates #744 --- .../docs/DocsConfigurationSampleTests.java | 11 +++-- .../docs/DocsConfigurationSampleTests10.java | 17 ++++--- .../docs/DocsConfigurationSampleTests11.java | 10 ++-- .../docs/DocsConfigurationSampleTests2.java | 49 ++++++++++--------- .../docs/DocsConfigurationSampleTests5.java | 12 +++-- .../docs/DocsConfigurationSampleTests6.java | 8 +-- .../statemachine/docs/IntroSample.java | 25 +++++++--- 7 files changed, 84 insertions(+), 48 deletions(-) diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java index 27a70aa5..60b62e63 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java @@ -64,6 +64,8 @@ import org.springframework.statemachine.support.StateMachineInterceptorAdapter; import org.springframework.statemachine.transition.Transition; import org.springframework.statemachine.transition.TransitionConflictPolicy; +import reactor.core.publisher.Mono; + /** * Tests for state machine configuration. * @@ -547,7 +549,7 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests { void method() { StateMachine stateMachine = factory.getStateMachine(); - stateMachine.start(); + stateMachine.startReactively().subscribe(); } } // end::snippetL[] @@ -589,13 +591,16 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests { StateMachine stateMachine; void signalMachine() { - stateMachine.sendEvent(Events.E1); + stateMachine + .sendEvent(Mono.just(MessageBuilder + .withPayload(Events.E1).build())) + .subscribe(); Message message = MessageBuilder .withPayload(Events.E2) .setHeader("foo", "bar") .build(); - stateMachine.sendEvent(message); + stateMachine.sendEvent(Mono.just(message)).subscribe(); } // end::snippetO[] diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests10.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests10.java index a7996efd..3661ca86 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests10.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests10.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -18,13 +18,15 @@ package org.springframework.statemachine.docs; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll; +import static org.springframework.statemachine.TestUtils.doStartAndAssert; +import static org.springframework.statemachine.TestUtils.resolveMachine; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.AbstractStateMachineTests; import org.springframework.statemachine.StateMachine; -import org.springframework.statemachine.StateMachineSystemConstants; import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.StateMachineConfigurerAdapter; @@ -39,12 +41,11 @@ public class DocsConfigurationSampleTests10 extends AbstractStateMachineTests { public void testConfig1() throws Exception { context.register(Config1.class); context.refresh(); - @SuppressWarnings("unchecked") - StateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class); - machine.start(); + StateMachine machine = resolveMachine(context); + doStartAndAssert(machine); assertThat(machine.getState().getIds(), containsInAnyOrder("S1")); assertThat(machine.getId(), is("mymachine")); - machine.sendEvent("E1"); + doSendEventAndConsumeAll(machine, "E1"); assertThat(machine.getState().getIds(), containsInAnyOrder("S2")); } @@ -57,10 +58,10 @@ public class DocsConfigurationSampleTests10 extends AbstractStateMachineTests { StateMachineFactory factory = context.getBean(StateMachineFactory.class); StateMachine machine = factory.getStateMachine("mymachine"); // end::snippetB[] - machine.start(); + doStartAndAssert(machine); assertThat(machine.getState().getIds(), containsInAnyOrder("S1")); assertThat(machine.getId(), is("mymachine")); - machine.sendEvent("E1"); + doSendEventAndConsumeAll(machine, "E1"); assertThat(machine.getState().getIds(), containsInAnyOrder("S2")); } diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests11.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests11.java index 8db0a8b6..026ffef2 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests11.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests11.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2019 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. @@ -30,6 +30,8 @@ import org.springframework.statemachine.config.builders.StateMachineConfiguratio import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; +import reactor.core.publisher.Mono; + public class DocsConfigurationSampleTests11 extends AbstractStateMachineTests { // tag::snippetA[] @@ -114,10 +116,12 @@ public class DocsConfigurationSampleTests11 extends AbstractStateMachineTests { StateMachine stateMachine; void sendEventUsingTimeout() { - stateMachine.sendEvent(MessageBuilder + stateMachine + .sendEvent(Mono.just(MessageBuilder .withPayload("E1") .setHeader(StateMachineMessageHeaders.HEADER_DO_ACTION_TIMEOUT, 5000) - .build()); + .build())) + .subscribe(); } // end::snippetC[] diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests2.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests2.java index 0859c659..7a34353e 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests2.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests2.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2019 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. @@ -17,6 +17,9 @@ package org.springframework.statemachine.docs; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll; +import static org.springframework.statemachine.TestUtils.doStartAndAssert; +import static org.springframework.statemachine.TestUtils.resolveMachine; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -33,6 +36,7 @@ import org.springframework.core.task.TaskExecutor; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.messaging.support.MessageBuilder; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.statemachine.AbstractStateMachineTests; import org.springframework.statemachine.StateContext; @@ -54,6 +58,8 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import reactor.core.publisher.Mono; + public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { @Override @@ -182,7 +188,10 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { @RequestMapping(path="/state", method=RequestMethod.POST) public HttpEntity setState(@RequestParam("event") String event) { - stateMachine.sendEvent(event); + stateMachine + .sendEvent(Mono.just(MessageBuilder + .withPayload(event).build())) + .subscribe(); return new ResponseEntity(HttpStatus.ACCEPTED); } @@ -198,17 +207,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { public void testConfig51() throws Exception { context.register(Config5.class, ExecutorConfig.class); context.refresh(); - @SuppressWarnings("unchecked") - StateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class); + StateMachine machine = resolveMachine(context); TestListener listener = new TestListener(); machine.addStateListener(listener); - machine.start(); + doStartAndAssert(machine); assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(1)); listener.reset(0, 0, 2); - machine.sendEvent("DEPLOY"); - machine.sendEvent("DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(2)); } @@ -217,17 +225,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { public void testConfig52() throws Exception { context.register(Config5.class); context.refresh(); - @SuppressWarnings("unchecked") - StateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class); + StateMachine machine = resolveMachine(context); TestListener listener = new TestListener(); machine.addStateListener(listener); - machine.start(); + doStartAndAssert(machine); assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(1)); listener.reset(0, 0, 2); - machine.sendEvent("DEPLOY"); - machine.sendEvent("DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(2)); } @@ -236,17 +243,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { public void testConfig61() throws Exception { context.register(Config6.class, ExecutorConfig.class); context.refresh(); - @SuppressWarnings("unchecked") - StateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class); + StateMachine machine = resolveMachine(context); TestListener listener = new TestListener(); machine.addStateListener(listener); - machine.start(); + doStartAndAssert(machine); assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(1)); listener.reset(0, 0, 2); - machine.sendEvent("DEPLOY"); - machine.sendEvent("DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(2)); } @@ -255,17 +261,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests { public void testConfig62() throws Exception { context.register(Config6.class); context.refresh(); - @SuppressWarnings("unchecked") - StateMachine machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class); + StateMachine machine = resolveMachine(context); TestListener listener = new TestListener(); machine.addStateListener(listener); - machine.start(); + doStartAndAssert(machine); assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(1)); listener.reset(0, 0, 2); - machine.sendEvent("DEPLOY"); - machine.sendEvent("DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); + doSendEventAndConsumeAll(machine, "DEPLOY"); assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true)); assertThat(listener.readyStateEnteredCount, is(2)); } diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests5.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests5.java index 7b9d489e..c712ff83 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests5.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests5.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -23,6 +23,7 @@ import java.util.HashMap; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.support.MessageBuilder; import org.springframework.statemachine.AbstractStateMachineTests; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachineContext; @@ -34,6 +35,8 @@ import org.springframework.statemachine.config.builders.StateMachineTransitionCo import org.springframework.statemachine.persist.DefaultStateMachinePersister; import org.springframework.statemachine.persist.StateMachinePersister; +import reactor.core.publisher.Mono; + public class DocsConfigurationSampleTests5 extends AbstractStateMachineTests { @Override @@ -54,9 +57,12 @@ public class DocsConfigurationSampleTests5 extends AbstractStateMachineTests { StateMachine stateMachine1 = context.getBean("machine1", StateMachine.class); StateMachine stateMachine2 = context.getBean("machine2", StateMachine.class); - stateMachine1.start(); + stateMachine1.startReactively().block(); - stateMachine1.sendEvent("E1"); + stateMachine1 + .sendEvent(Mono.just(MessageBuilder + .withPayload("E1").build())) + .blockLast(); assertThat(stateMachine1.getState().getIds(), contains("S2")); persister.persist(stateMachine1, "myid"); diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests6.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests6.java index 3dfd91d8..a0eaa132 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests6.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests6.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -17,6 +17,8 @@ package org.springframework.statemachine.docs; import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertThat; +import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll; +import static org.springframework.statemachine.TestUtils.doStartAndAssert; import java.util.ArrayList; import java.util.Collection; @@ -59,9 +61,9 @@ public class DocsConfigurationSampleTests6 { ObjectStateMachineFactory factory = new ObjectStateMachineFactory<>(stateMachineModel); StateMachine stateMachine = factory.getStateMachine(); // end::snippetA[] - stateMachine.start(); + doStartAndAssert(stateMachine); assertThat(stateMachine.getState().getIds(), contains("S1")); - stateMachine.sendEvent("E1"); + doSendEventAndConsumeAll(stateMachine, "E1"); assertThat(stateMachine.getState().getIds(), contains("S2")); } diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/IntroSample.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/IntroSample.java index 89e29e12..36594546 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/IntroSample.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/IntroSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2019 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. @@ -17,11 +17,15 @@ package org.springframework.statemachine.docs; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; +import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll; +import static org.springframework.statemachine.TestUtils.doStartAndAssert; import java.util.EnumSet; +import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.support.MessageBuilder; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.annotation.OnTransition; import org.springframework.statemachine.annotation.WithStateMachine; @@ -32,6 +36,8 @@ import org.springframework.statemachine.config.StateMachineBuilder.Builder; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; +import reactor.core.publisher.Mono; + public class IntroSample { // tag::snippetA[] @@ -94,19 +100,26 @@ public class IntroSample { StateMachine stateMachine; void doSignals() { - stateMachine.sendEvent(Events.EVENT1); - stateMachine.sendEvent(Events.EVENT2); + stateMachine + .sendEvent(Mono.just(MessageBuilder + .withPayload(Events.EVENT1).build())) + .subscribe(); + stateMachine + .sendEvent(Mono.just(MessageBuilder + .withPayload(Events.EVENT2).build())) + .subscribe(); } } // end::snippetD[] + @Test public void testManual() throws Exception { StateMachine stateMachine = buildMachine(); - stateMachine.start(); + doStartAndAssert(stateMachine); assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE1)); - stateMachine.sendEvent(Events.EVENT1); + doSendEventAndConsumeAll(stateMachine, Events.EVENT1); assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE2)); - stateMachine.sendEvent(Events.EVENT2); + doSendEventAndConsumeAll(stateMachine, Events.EVENT2); assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE1)); }