StateMachinePersistenceService { @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) public StateMachine createStateMachine(String stateMachineId, StateMachineDefinition stateMachineDefinition) { final Map<EventDefinition, EventData> eventDataMap = stateMachineDefinition.getEventDataMap(); Set<Event> allEvents = createAllEvents(eventDataMap); Set<StateDefinition> stateDefinitions = stateMachineDefinition.getStates(); Set<State> states = new HashSet<>(); AtomicInteger stateId = new AtomicInteger(1); for (StateDefinition stateDefinition : stateDefinitions) { State state = convertStateDefinitionToState(stateDefinition, stateMachineId, stateId.longValue()); states.add(state); stateId.incrementAndGet(); } StateMachine stateMachine = new StateMachine(stateMachineId, stateMachineDefinition.getVersion(), stateMachineDefinition.getName(), stateMachineDefinition.getDescription(), states, stateMachineDefinition.getClientElbId()); stateMachinesDAO.create(stateMachineId, stateMachine); for (Event event : allEvents) { event.setStateMachineInstanceId(stateMachine.getId()); eventPersistenceService.persistEvent(event); } for (State state : stateMachine.getStates()) { auditDAO.create(stateMachine.getId(), new AuditRecord(stateMachine.getId(), state.getId(), 0L, Status.initialized, null, null)); } return stateMachine; } @Inject  StateMachinePersistenceService(StateMachinesDAO stateMachinesDAO, AuditDAO auditDAO,
                                          EventPersistenceService eventPersistenceService, @Named("task.maxTaskRetryCount") Integer maxRetryCount); @Transactional @SelectDataSource(type = DataSourceType.READ_WRITE, storage = Storage.SHARDED) StateMachine createStateMachine(String stateMachineId, StateMachineDefinition stateMachineDefinition);  }
@Test public void maxTaskRetryCountShouldBeTakenIfRetryCountIsHigher() { Integer maxTaskRetryCount = 10; StateMachinePersistenceService stateMachinePersistenceService = new StateMachinePersistenceService(stateMachinesDAO, auditDAO, eventPersistenceService, maxTaskRetryCount); StateDefinition stateDefinition = new StateDefinition(1L, "state1", "desc", null, "task1", null, 13L, 1000L, Collections.emptyList(), null); StateMachineDefinition stateMachineDefinition = new StateMachineDefinition("desc", "state_machine_1", 1L, Collections.singleton(stateDefinition), null, null, "client_elb_id_1"); stateMachinePersistenceService.createStateMachine("sample-state-machine-id", stateMachineDefinition); State state = new State(1L, "state1", "desc", null, "task1", null, Collections.emptyList(), 10L, 1000L, null, Status.initialized, null, 0L, "sample-state-machine-id", 1L ); verify(stateMachinesDAO).create("sample-state-machine-id", new StateMachine( "sample-state-machine-id", 1L, "state_machine_1", "desc", Collections.singleton(state), "client_elb_id_1")); }
@Test public void retryCountShouldBeTakenIfItIsLessthanMaxAllowed() { Integer maxTaskRetryCount = 10; StateMachinePersistenceService stateMachinePersistenceService = new StateMachinePersistenceService(stateMachinesDAO, auditDAO, eventPersistenceService, maxTaskRetryCount); StateDefinition stateDefinition = new StateDefinition(1L, "state1", "desc", null, "task1", null, 3L, 1000L, Collections.emptyList(), null); StateMachineDefinition stateMachineDefinition = new StateMachineDefinition("desc", "state_machine_1", 1L, Collections.singleton(stateDefinition), null, null, "client_elb_id_1"); stateMachinePersistenceService.createStateMachine("sample-state-machine-id", stateMachineDefinition); State state = new State(1L, "state1", "desc", null, "task1", null, Collections.emptyList(), 3L, 1000L, null, Status.initialized, null, 0L, "sample-state-machine-id", 1L); verify(stateMachinesDAO).create("sample-state-machine-id", new StateMachine( "sample-state-machine-id", 1L, "state_machine_1", "desc", Collections.singleton(state), "client_elb_id_1")); }