Search in sources :

Example 1 with BasicInstruction

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;
}
Also used : BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) ArrayList(java.util.ArrayList) Instruction(net.solarnetwork.node.reactor.Instruction) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction)

Example 2 with BasicInstruction

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();
}
Also used : BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) Assertion(net.solarnetwork.test.Assertion) Test(org.junit.Test)

Example 3 with BasicInstruction

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();
}
Also used : BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) Instant(java.time.Instant) Assertion(net.solarnetwork.test.Assertion) Test(org.junit.Test)

Example 4 with BasicInstruction

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();
}
Also used : BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) Assertion(net.solarnetwork.test.Assertion) Test(org.junit.Test)

Example 5 with BasicInstruction

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
}
Also used : BasicInstructionStatus(net.solarnetwork.node.reactor.BasicInstructionStatus) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) SecureRandom(java.security.SecureRandom) Test(org.junit.Test)

Aggregations

BasicInstruction (net.solarnetwork.node.reactor.BasicInstruction)25 Test (org.junit.Test)22 InstructionStatus (net.solarnetwork.node.reactor.InstructionStatus)18 Instant (java.time.Instant)10 BasicInstructionStatus (net.solarnetwork.node.reactor.BasicInstructionStatus)8 Instruction (net.solarnetwork.node.reactor.Instruction)8 SimpleInstructionExecutionService (net.solarnetwork.node.reactor.SimpleInstructionExecutionService)4 Assertion (net.solarnetwork.test.Assertion)4 IOException (java.io.IOException)3 InstructionHandler (net.solarnetwork.node.reactor.InstructionHandler)3 AbstractNodeTransactionalTest (net.solarnetwork.node.test.AbstractNodeTransactionalTest)3 SecureRandom (java.security.SecureRandom)2 BitSet (java.util.BitSet)2 HttpRequesterJob (net.solarnetwork.node.control.ping.HttpRequesterJob)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StringReader (java.io.StringReader)1 URISyntaxException (java.net.URISyntaxException)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1