Search in sources :

Example 1 with CapturingExecutorService

use of net.solarnetwork.node.test.CapturingExecutorService in project solarnetwork-node by SolarNetwork.

the class OutstationServceTests method handleAnalogInt32WithTaskExecutor.

@Test
public void handleAnalogInt32WithTaskExecutor() throws InterruptedException {
    // given
    TestOutstationService service = createOutstationService();
    CapturingExecutorService executor = new CapturingExecutorService(newSingleThreadExecutor());
    service.setTaskExecutor(new TaskExecutorAdapter(executor));
    final String controlId = "/foo/limiter";
    ControlConfig cConfig = new ControlConfig(null, controlId, ControlType.Analog);
    service.setControlConfigs(new ControlConfig[] { cConfig });
    Capture<Instruction> instrCaptor = Capture.newInstance();
    expect(instructionService.executeInstruction(capture(instrCaptor))).andAnswer(new IAnswer<InstructionStatus>() {

        @Override
        public InstructionStatus answer() throws Throwable {
            return createStatus(instrCaptor.getValue(), Completed);
        }
    });
    // when
    replayAll();
    AnalogOutputInt32 cmd = new AnalogOutputInt32(321456, CommandStatus.SUCCESS);
    CommandStatus status = service.getCommandHandler().operateAOI32(cmd, 0, OperateType.DirectOperate);
    // wait for bg task
    executor.shutdown();
    executor.awaitTermination(2, TimeUnit.SECONDS);
    // then
    assertThat("Command OK", status, equalTo(CommandStatus.SUCCESS));
    Instruction instr = instrCaptor.getValue();
    assertThat("Instruction", instr.getTopic(), equalTo(InstructionHandler.TOPIC_SET_CONTROL_PARAMETER));
    assertThat("Control ID param", instr.getParameterValue(controlId), equalTo(String.valueOf(cmd.value)));
    assertThat("Instruction handled in background", executor.getCapturedFutures(), hasSize(1));
}
Also used : ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) AnalogOutputInt32(com.automatak.dnp3.AnalogOutputInt32) CapturingExecutorService(net.solarnetwork.node.test.CapturingExecutorService) Instruction(net.solarnetwork.node.reactor.Instruction) TaskExecutorAdapter(org.springframework.core.task.support.TaskExecutorAdapter) CommandStatus(com.automatak.dnp3.enums.CommandStatus) Test(org.junit.Test)

Example 2 with CapturingExecutorService

use of net.solarnetwork.node.test.CapturingExecutorService in project solarnetwork-node by SolarNetwork.

the class OutstationServceTests method handleCROBLatchOnWithTaskExecutor.

@Test
public void handleCROBLatchOnWithTaskExecutor() throws InterruptedException {
    // given
    TestOutstationService service = createOutstationService();
    CapturingExecutorService executor = new CapturingExecutorService(newSingleThreadExecutor());
    service.setTaskExecutor(new TaskExecutorAdapter(executor));
    final String controlId = "/foo/switch";
    ControlConfig cConfig = new ControlConfig(null, controlId, ControlType.Binary);
    service.setControlConfigs(new ControlConfig[] { cConfig });
    Capture<Instruction> instrCaptor = Capture.newInstance();
    expect(instructionService.executeInstruction(capture(instrCaptor))).andAnswer(new IAnswer<InstructionStatus>() {

        @Override
        public InstructionStatus answer() throws Throwable {
            return createStatus(instrCaptor.getValue(), Completed);
        }
    });
    // when
    replayAll();
    ControlRelayOutputBlock crob = new ControlRelayOutputBlock(ControlCode.LATCH_ON, (short) 1, 0, 0, CommandStatus.SUCCESS);
    CommandStatus status = service.getCommandHandler().operateCROB(crob, 0, OperateType.DirectOperate);
    // wait for bg task
    executor.shutdown();
    executor.awaitTermination(2, TimeUnit.SECONDS);
    // then
    assertThat("Command OK", status, equalTo(CommandStatus.SUCCESS));
    Instruction instr = instrCaptor.getValue();
    assertThat("Instruction", instr.getTopic(), equalTo(InstructionHandler.TOPIC_SET_CONTROL_PARAMETER));
    assertThat("Control ID param", instr.getParameterValue(controlId), equalTo(Boolean.TRUE.toString()));
    assertThat("Instruction handled in background", executor.getCapturedFutures(), hasSize(1));
}
Also used : ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) CapturingExecutorService(net.solarnetwork.node.test.CapturingExecutorService) ControlRelayOutputBlock(com.automatak.dnp3.ControlRelayOutputBlock) Instruction(net.solarnetwork.node.reactor.Instruction) TaskExecutorAdapter(org.springframework.core.task.support.TaskExecutorAdapter) CommandStatus(com.automatak.dnp3.enums.CommandStatus) Test(org.junit.Test)

Aggregations

CommandStatus (com.automatak.dnp3.enums.CommandStatus)2 ControlConfig (net.solarnetwork.node.io.dnp3.domain.ControlConfig)2 Instruction (net.solarnetwork.node.reactor.Instruction)2 InstructionStatus (net.solarnetwork.node.reactor.InstructionStatus)2 CapturingExecutorService (net.solarnetwork.node.test.CapturingExecutorService)2 Test (org.junit.Test)2 TaskExecutorAdapter (org.springframework.core.task.support.TaskExecutorAdapter)2 AnalogOutputInt32 (com.automatak.dnp3.AnalogOutputInt32)1 ControlRelayOutputBlock (com.automatak.dnp3.ControlRelayOutputBlock)1