use of net.solarnetwork.node.reactor.BasicInstruction in project solarnetwork-node by SolarNetwork.
the class JdbcInstructionDao method extractInstructions.
private List<Instruction> extractInstructions(ResultSet rs) throws SQLException {
List<Instruction> results = new ArrayList<Instruction>(5);
BasicInstruction bi = null;
while (rs.next()) {
Long currId = rs.getLong(1);
String currInstructorId = rs.getString(2);
if (bi == null || currInstructorId == null || !bi.getId().equals(currId) || !bi.getInstructorId().equals(currInstructorId)) {
InstructionStatus status = new BasicInstructionStatus(currId, InstructionState.valueOf(rs.getString(5)), rs.getTimestamp(6).toInstant(), (rs.getString(8) == null ? null : InstructionState.valueOf(rs.getString(8))), JsonUtils.getStringMap(rs.getString(7)));
bi = new BasicInstruction(currId, rs.getString(3), rs.getTimestamp(4).toInstant(), currInstructorId, status);
results.add(bi);
}
String pName = rs.getString(9);
String pValue = rs.getString(10);
if (pName != null) {
bi.addParameter(pName, pValue);
}
}
return results;
}
use of net.solarnetwork.node.reactor.BasicInstruction in project solarnetwork-node by SolarNetwork.
the class InstructionExecutionJobTests method noHandler.
@Test
public void noHandler() throws Exception {
// GIVEN
BasicInstruction instr = new BasicInstruction(1L, "foo", Instant.now(), TEST_INSTRUCTOR_ID, new BasicInstructionStatus(1L, Received, Instant.now()));
expect(instructionDao.findInstructionsForState(Received)).andReturn(Collections.singletonList(instr));
// update to Executing
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Received), assertWith(new Assertion<InstructionStatus>() {
@Override
public void check(InstructionStatus status) throws Throwable {
assertThat("Status provided", status, is(notNullValue()));
assertThat("State is Executing", status.getInstructionState(), is(Executing));
}
}))).andReturn(true);
expect(instructionExecutionService.executeInstruction(instr)).andReturn(null);
// roll back to Received
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Executing), same(instr.getStatus()))).andReturn(true);
expect(instructionDao.findInstructionsForState(Executing)).andReturn(Collections.emptyList());
// WHEN
replayAll();
job.executeJobService();
}
use of net.solarnetwork.node.reactor.BasicInstruction in project solarnetwork-node by SolarNetwork.
the class InstructionExecutionJobTests method noHandler_expired.
@Test
public void noHandler_expired() throws Exception {
// GIVEN
// instruction is older than maximumIncompleteHours
Instant ts = Instant.now().truncatedTo(ChronoUnit.HOURS).minus(job.getMaximumIncompleteHours() + 1, ChronoUnit.HOURS);
BasicInstruction instr = new BasicInstruction(1L, "foo", ts, TEST_INSTRUCTOR_ID, new BasicInstructionStatus(1L, Received, ts));
expect(instructionDao.findInstructionsForState(Received)).andReturn(Collections.singletonList(instr));
// update to Executing
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Received), assertWith(new Assertion<InstructionStatus>() {
@Override
public void check(InstructionStatus status) throws Throwable {
assertThat("Status provided", status, is(notNullValue()));
assertThat("State is Executing", status.getInstructionState(), is(Executing));
}
}))).andReturn(true);
expect(instructionExecutionService.executeInstruction(instr)).andReturn(null);
// jump to Declined
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Executing), assertWith(new Assertion<InstructionStatus>() {
@Override
public void check(InstructionStatus status) throws Throwable {
assertThat("Status provided", status, is(notNullValue()));
assertThat("State is Declined", status.getInstructionState(), is(Declined));
assertThat("Error code defined", status.getResultParameters(), hasEntry(InstructionStatus.ERROR_CODE_RESULT_PARAM, InstructionExecutionJob.ERROR_CODE_INSTRUCTION_EXPIRED));
assertThat("Error message provided", status.getResultParameters(), hasEntry(is(InstructionStatus.MESSAGE_RESULT_PARAM), is(notNullValue())));
}
}))).andReturn(true);
expect(instructionDao.findInstructionsForState(Executing)).andReturn(Collections.emptyList());
// WHEN
replayAll();
job.executeJobService();
}
use of net.solarnetwork.node.reactor.BasicInstruction in project solarnetwork-node by SolarNetwork.
the class InstructionExecutionJobTests method handled_completed.
@Test
public void handled_completed() throws Exception {
// GIVEN
BasicInstruction instr = new BasicInstruction(1L, "foo", Instant.now(), TEST_INSTRUCTOR_ID, new BasicInstructionStatus(1L, Received, Instant.now()));
expect(instructionDao.findInstructionsForState(Received)).andReturn(Collections.singletonList(instr));
// update to Executing
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Received), assertWith(new Assertion<InstructionStatus>() {
@Override
public void check(InstructionStatus status) throws Throwable {
assertThat("Status provided", status, is(notNullValue()));
assertThat("State is Executing", status.getInstructionState(), is(Executing));
}
}))).andReturn(true);
InstructionStatus handledStatus = InstructionUtils.createStatus(instr, Completed);
expect(instructionExecutionService.executeInstruction(instr)).andReturn(handledStatus);
// update to handled status
expect(instructionDao.compareAndStoreInstructionStatus(eq(instr.getId()), eq(instr.getInstructorId()), eq(Executing), same(handledStatus))).andReturn(true);
expect(instructionDao.findInstructionsForState(Executing)).andReturn(Collections.emptyList());
// WHEN
replayAll();
job.executeJobService();
}
use of net.solarnetwork.node.reactor.BasicInstruction in project solarnetwork-node by SolarNetwork.
the class SimpleReactorServiceTests method store_withStatus.
@Test
public void store_withStatus() {
// GIVEN
Long instrId = new SecureRandom().nextLong();
BasicInstructionStatus status = new BasicInstructionStatus(instrId, InstructionState.Executing, Instant.now());
BasicInstruction instr = new BasicInstruction(instrId, TEST_TOPIC, Instant.now().minusSeconds(1), TEST_INSTRUCTOR_ID, status);
instructionDao.storeInstructionStatus(instrId, TEST_INSTRUCTOR_ID, status);
// WHEN
replayAll();
service.storeInstruction(instr);
// THEN
}
Aggregations