InstanceFSM { @OnTransit public void onInstanceRenaming(@Event InstanceRenamingEvent event) { Instance instance = event.getInstance(); String cloudId = instance.getCloudId(); Cloud cloud = cloudConfigurationProvider.getCloud(cloudId); Optional<Instance> instanceCandidate = instancesAware.getInstance(instance.getId()); instanceCandidate.ifPresent(i -> { LOG.info("Renaming instance {} ({}) to {}", i.getName(), i.getId(), instance.getName()); if (operationProcessor.supply(cloud, OperationType.RENAME_INSTANCE, () -> instance)) { instancesAware.saveInstance(instance); } else { throw new RuntimeException(String.format( "Can't rename instance %s (%s) to %s", i.getName(), i.getId(), instance.getName() )); } }); } @Autowired  InstanceFSM(OperationProcessor operationProcessor, InstancesAware instancesAware, CloudConfigurationProvider cloudConfigurationProvider, EventBus eventBus, MailSender mailSender); @BeforeTransit void beforeTransit(@Event InstanceEvent instanceEvent); @OnTransit void onInstanceQueued(@Event InstanceQueuedEvent event); @OnTransit void onInstanceLaunching(@Event InstanceLaunchingEvent event); @OnTransit void onInstanceLaunched(@Event InstanceLaunchedEvent event); @OnTransit void onInstanceRebooting(@Event InstanceRebootingEvent event); @OnTransit void onInstanceHardRebooting(@Event InstanceHardRebootingEvent event); @OnTransit void onInstanceShuttingDown(@Event InstanceShuttingDownEvent event); @OnTransit void onInstanceShutoff(@Event InstanceShutOffEvent event); @OnTransit void onInstancePausing(@Event InstancePausingEvent event); @OnTransit void onInstancePaused(@Event InstancePausedEvent event); @OnTransit void onInstanceResuming(@Event InstanceResumingEvent event); @OnTransit void onInstanceRebuilding(@Event InstanceRebuildingEvent event); @OnTransit void onInstanceResizing(@Event InstanceResizingEvent event); @OnTransit void onInstanceStarting(@Event InstanceStartingEvent event); @OnTransit void onInstanceSuspending(@Event InstanceSuspendingEvent event); @OnTransit void onInstanceSuspended(@Event InstanceSuspendedEvent event); @OnTransit void onInstanceMigrating(@Event InstanceMigratingEvent event); @OnTransit void onInstanceDeleting(@Event InstanceDeletingEvent event); @OnTransit void onInstanceRenaming(@Event InstanceRenamingEvent event); @OnTransit void onInstanceSnapshotting(@Event InstanceSnapshottingEvent event); @OnTransit void onInstanceError(@FromState InstanceEvent from, @Event InstanceErrorEvent event); @OnException void onUnsupportedOperationException(UnsupportedOperationException e);  }
@Test public void testOnInstanceRenaming() { Instance instance = EntityGenerator.getInstance(); String instanceId = instance.getId(); instancesAware.saveInstance(instance); Optional<Instance> instanceBeforeRenaming = instancesAware.getInstance(instanceId); assertThat(instanceBeforeRenaming.isPresent(), is(true)); assertThat(instanceBeforeRenaming.get().getName(), equalTo(EntityGenerator.getInstance().getName())); Instance newInstance = EntityGenerator.getInstance(); newInstance.setName("new-name"); fireEvent(InstanceRenamingEvent.class, newInstance, false); Optional<Instance> instanceAfterRenaming = instancesAware.getInstance(instanceId); assertThat(instanceAfterRenaming.isPresent(), is(true)); assertThat(instanceAfterRenaming.get().getName(), equalTo("new-name")); }