org.springframework.statemachine.config.StateMachineFactory Java Examples

The following examples show how to use org.springframework.statemachine.config.StateMachineFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testFactory() {
	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	assertThat(factory).isNotNull();
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testFactory");
	assertThat(stateMachine).isNotNull();
}
 
Example #2
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleInstallShouldNotError() throws Exception {
	Mockito.when(packageService.downloadPackage(any()))
			.thenReturn(new org.springframework.cloud.skipper.domain.Package());
	Mockito.when(releaseService.install(any(), any())).thenReturn(new Release());

	Message<SkipperEvents> message = MessageBuilder
		.withPayload(SkipperEvents.INSTALL)
		.setHeader(SkipperEventHeaders.PACKAGE_METADATA, new PackageMetadata())
		.setHeader(SkipperEventHeaders.INSTALL_PROPERTIES, new InstallProperties())
		.setHeader(SkipperEventHeaders.VERSION, 1)
		.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testInstall");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(3)
					.expectStateEntered(SkipperStates.INSTALL,
							SkipperStates.INSTALL_INSTALL,
							SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #3
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleUpgradeShouldNotError() throws Exception {
	Manifest manifest = new Manifest();
	Release release = new Release();
	release.setManifest(manifest);
	Mockito.when(releaseReportService.createReport(any(), any(), any(boolean.class))).thenReturn(new ReleaseAnalysisReport(
			new ArrayList<>(), new ReleaseDifference(), release, release));
	Mockito.when(upgradeStrategy.checkStatus(any()))
			.thenReturn(true);
	Mockito.when(upgradeStrategyFactory.getUpgradeStrategy(any())).thenReturn(upgradeStrategy);

	UpgradeRequest upgradeRequest = new UpgradeRequest();

	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE)
			.setHeader(SkipperEventHeaders.UPGRADE_REQUEST, upgradeRequest)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testSimpleUpgradeShouldNotError");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(9)
					.and()
				.build();
	plan.test();
	Mockito.verify(upgradeCancelAction, never()).execute(any());
	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #4
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteSucceed() throws Exception {
	Mockito.when(releaseService.delete(any(String.class), any(boolean.class))).thenReturn(new Release());
	DeleteProperties deleteProperties = new DeleteProperties();
	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.DELETE)
			.setHeader(SkipperEventHeaders.RELEASE_NAME, "testDeleteSucceed")
			.setHeader(SkipperEventHeaders.RELEASE_DELETE_PROPERTIES, deleteProperties)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testDeleteSucceed");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(3)
					.expectStateEntered(SkipperStates.DELETE,
							SkipperStates.DELETE_DELETE,
							SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #5
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testScaleSucceed() throws Exception {
	Mockito.when(releaseService.scale(any(String.class), any(ScaleRequest.class))).thenReturn(new Release());
	ScaleRequest scaleRequest = new ScaleRequest();
	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.SCALE)
			.setHeader(SkipperEventHeaders.RELEASE_NAME, "testScaleSucceed")
			.setHeader(SkipperEventHeaders.SCALE_REQUEST, scaleRequest)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testScaleSucceed");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(3)
					.expectStateEntered(SkipperStates.SCALE,
							SkipperStates.SCALE_SCALE,
							SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #6
Source File: RedisPersistConfig.java    From spring-statemachine-learning with Apache License 2.0 4 votes vote down vote up
@Bean
public StateMachineAdapter<StateEnum, EventEnum, String> stateMachineAdapter(
        StateMachineFactory<StateEnum, EventEnum> stateMachineFactory,
        StateMachinePersister<StateEnum, EventEnum, String> stateMachinePersister) {
    return new StateMachineAdapter(stateMachineFactory, stateMachinePersister);
}
 
Example #7
Source File: Application.java    From spring-statemachine-learning with Apache License 2.0 4 votes vote down vote up
@Bean
public StateMachineAdapter<StateEnum, EventEnum, StateMachineContextEntity<StateEnum, EventEnum>> stateMachineAdapter(
        StateMachineFactory<StateEnum, EventEnum> stateMachineFactory,
        StateMachinePersister<StateEnum, EventEnum, StateMachineContextEntity<StateEnum, EventEnum>> stateMachinePersister) {
    return new StateMachineAdapter(stateMachineFactory, stateMachinePersister);
}
 
Example #8
Source File: StateMachineConfiguration.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Bean
public StateMachineService<SkipperStates, SkipperEvents> stateMachineService(
		StateMachineFactory<SkipperStates, SkipperEvents> stateMachineFactory,
		StateMachinePersist<SkipperStates, SkipperEvents, String> stateMachinePersist) {
	return new DefaultStateMachineService<>(stateMachineFactory, stateMachinePersist);
}
 
Example #9
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpgradeFailsNewAppFailToDeploy() throws Exception {
	Manifest manifest = new Manifest();
	Release release = new Release();
	release.setManifest(manifest);
	Mockito.when(releaseReportService.createReport(any(), any(), any(boolean.class))).thenReturn(new ReleaseAnalysisReport(
			new ArrayList<>(), new ReleaseDifference(), release, release));
	Mockito.when(upgradeStrategy.checkStatus(any()))
			.thenReturn(false);
	Mockito.when(upgradeStrategyFactory.getUpgradeStrategy(any())).thenReturn(upgradeStrategy);

	UpgradeRequest upgradeRequest = new UpgradeRequest();

	// timeout 0 for things to fail immediately
	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE)
			.setHeader(SkipperEventHeaders.UPGRADE_REQUEST, upgradeRequest)
			.setHeader(SkipperEventHeaders.UPGRADE_TIMEOUT, 0L)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testUpgradeFailsNewAppFailToDeploy");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(9)
					.expectStateEntered(SkipperStates.UPGRADE,
							SkipperStates.UPGRADE_START,
							SkipperStates.UPGRADE_DEPLOY_TARGET_APPS,
							SkipperStates.UPGRADE_WAIT_TARGET_APPS,
							SkipperStates.UPGRADE_CHECK_TARGET_APPS,
							SkipperStates.UPGRADE_WAIT_TARGET_APPS,
							SkipperStates.UPGRADE_DEPLOY_TARGET_APPS_FAILED,
							SkipperStates.UPGRADE_CANCEL,
							SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	Mockito.verify(upgradeCancelAction).execute(any());
	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #10
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Ignore("Flaky, what it tests not actually used yet")
@Test
public void testUpgradeCancelWhileCheckingApps() throws Exception {
	Manifest manifest = new Manifest();
	Release release = new Release();
	release.setManifest(manifest);
	Mockito.when(releaseReportService.createReport(any(), any(), any(boolean.class))).thenReturn(new ReleaseAnalysisReport(
			new ArrayList<>(), new ReleaseDifference(), release, release));
	Mockito.when(upgradeStrategy.checkStatus(any()))
			.thenReturn(false);
	Mockito.when(upgradeStrategyFactory.getUpgradeStrategy(any())).thenReturn(upgradeStrategy);

	UpgradeRequest upgradeRequest = new UpgradeRequest();

	// timeout 60s giving time to try cancel
	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE)
			.setHeader(SkipperEventHeaders.UPGRADE_REQUEST, upgradeRequest)
			.setHeader(SkipperEventHeaders.UPGRADE_TIMEOUT, 60000L)
			.build();

	Message<SkipperEvents> message2 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE_CANCEL)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testUpgradeCancelWhileCheckingApps");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStateChanged(4)
					.expectStateEntered(SkipperStates.UPGRADE,
							SkipperStates.UPGRADE_START,
							SkipperStates.UPGRADE_DEPLOY_TARGET_APPS,
							SkipperStates.UPGRADE_WAIT_TARGET_APPS)
					.and()
				.step()
					.sendEvent(message2)
					.expectStateChanged(2)
					// for now need to do a trick to wait this later
					//.expectStateEntered(SkipperStates.UPGRADE_CANCEL,
					//		SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	SkipperStates result = null;
	for (int i = 0; i < 10; i++) {
		SkipperStates s = stateMachine.getState().getId();
		if (s == SkipperStates.INITIAL) {
			result = s;
			break;
		}
		Thread.sleep(200);
	}
	assertThat(result).isEqualTo(SkipperStates.INITIAL);

	Mockito.verify(upgradeCancelAction).execute(any());
	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #11
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Test
public void testRollbackInstall() throws Exception {
	Release release = new Release();
	Status status = new Status();
	status.setStatusCode(StatusCode.DELETED);
	Info info = Info.createNewInfo("xxx");
	info.setStatus(status);
	release.setPkg(createPkg());
	release.setInfo(info);
	Mockito.when(releaseRepository.findLatestReleaseForUpdate(any())).thenReturn(release);
	Mockito.when(releaseRepository.findReleaseToRollback(any())).thenReturn(release);
	Mockito.when(releaseService.install(any(InstallRequest.class))).thenReturn(release);


	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.ROLLBACK)
			.setHeader(SkipperEventHeaders.RELEASE_NAME, "testRollbackInstall")
			.setHeader(SkipperEventHeaders.ROLLBACK_VERSION, 0)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testRollbackInstall");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStates(SkipperStates.INITIAL)
					.expectStateChanged(5)
					.expectStateEntered(SkipperStates.ROLLBACK,
							SkipperStates.ROLLBACK_START,
							SkipperStates.INSTALL,
							SkipperStates.INSTALL_INSTALL,
							SkipperStates.INITIAL)
					.and()
				.build();
	plan.test();

	Mockito.verify(errorAction, never()).execute(any());
}
 
Example #12
Source File: StateMachineTests.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Test
public void testInstallDeniedWhileUpgrading() throws Exception {
	Manifest manifest = new Manifest();
	Release release = new Release();
	release.setManifest(manifest);
	Mockito.when(releaseReportService.createReport(any(), any(), any(boolean.class))).thenReturn(new ReleaseAnalysisReport(
			new ArrayList<>(), new ReleaseDifference(), release, release));
	Mockito.when(upgradeStrategy.checkStatus(any()))
			.thenReturn(false);
	Mockito.when(upgradeStrategyFactory.getUpgradeStrategy(any())).thenReturn(upgradeStrategy);

	UpgradeRequest upgradeRequest = new UpgradeRequest();

	Message<SkipperEvents> message1 = MessageBuilder
			.withPayload(SkipperEvents.UPGRADE)
			.setHeader(SkipperEventHeaders.UPGRADE_REQUEST, upgradeRequest)
			.build();

	Message<SkipperEvents> message2 = MessageBuilder
			.withPayload(SkipperEvents.INSTALL)
			.setHeader(SkipperEventHeaders.PACKAGE_METADATA, new PackageMetadata())
			.setHeader(SkipperEventHeaders.INSTALL_PROPERTIES, new InstallProperties())
			.setHeader(SkipperEventHeaders.VERSION, 1)
			.build();

	StateMachineFactory<SkipperStates, SkipperEvents> factory = context.getBean(StateMachineFactory.class);
	StateMachine<SkipperStates, SkipperEvents> stateMachine = factory.getStateMachine("testInstallDeniedWhileUpgrading");

	StateMachineTestPlan<SkipperStates, SkipperEvents> plan =
			StateMachineTestPlanBuilder.<SkipperStates, SkipperEvents>builder()
				.defaultAwaitTime(10)
				.stateMachine(stateMachine)
				.step()
					.expectStateMachineStarted(1)
					.expectStates(SkipperStates.INITIAL)
					.and()
				.step()
					.sendEvent(message1)
					.expectStateChanged(6)
					.and()
				.build();
	plan.test();

	// install event is not accepted
	boolean accepted = stateMachine.sendEvent(message2);
	assertThat(accepted).isFalse();
}
 
Example #13
Source File: AbstractFlowConfiguration.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
protected StateMachineFactory<S, E> getStateMachineFactory() {
    return stateMachineFactory;
}