use of net.solarnetwork.node.control.ping.HttpRequesterJob in project solarnetwork-node by SolarNetwork.
the class HttpRequesterJobTest method pingFailure.
@Test
public void pingFailure() throws Exception {
final TestHandler httpHandler = new TestHandler();
server.stop();
Capture<BasicInstruction> instructions = Capture.newInstance(CaptureType.ALL);
expect(handler.handlesTopic(InstructionHandler.TOPIC_SET_CONTROL_PARAMETER)).andReturn(Boolean.TRUE);
expect(handler.processInstruction(capture(instructions))).andAnswer(new IAnswer<InstructionStatus>() {
@Override
public InstructionStatus answer() throws Throwable {
int size = instructions.getValues().size();
return InstructionUtils.createStatus(instructions.getValues().get(size - 1), InstructionState.Completed);
}
});
expect(handler.handlesTopic(InstructionHandler.TOPIC_SET_CONTROL_PARAMETER)).andReturn(Boolean.TRUE);
expect(handler.processInstruction(capture(instructions))).andAnswer(new IAnswer<InstructionStatus>() {
@Override
public InstructionStatus answer() throws Throwable {
int size = instructions.getValues().size();
return InstructionUtils.createStatus(instructions.getValues().get(size - 1), InstructionState.Completed);
}
});
replay(handler);
HttpRequesterJob job = newJobInstance();
job.executeJobService();
verify(handler);
assertThat("No requests", httpHandler.getCount(), is(0));
assertThat("Instructions executed", instructions.getValues(), hasSize(2));
Instruction instr1 = instructions.getValues().get(0);
assertThat("Control set ON", instr1.getParameterValue(TEST_CONTROL_ID), is(Boolean.TRUE.toString()));
Instruction instr2 = instructions.getValues().get(1);
assertThat("Control set OFF", instr2.getParameterValue(TEST_CONTROL_ID), is(Boolean.FALSE.toString()));
}
use of net.solarnetwork.node.control.ping.HttpRequesterJob in project solarnetwork-node by SolarNetwork.
the class HttpRequesterJobTest method systemConfigure_success.
@Test
public void systemConfigure_success() throws Exception {
// GIVEN
final TestHandler httpHandler = new TestHandler();
server.setHandler(httpHandler);
HttpRequesterJob job = newJobInstance();
// WHEN
replay(handler);
Map<String, String> instrParams = new LinkedHashMap<>(4);
instrParams.put(InstructionHandler.PARAM_SERVICE, HttpRequesterJob.PING_SERVICE_NAME);
Instruction instr = InstructionUtils.createLocalInstruction(InstructionHandler.TOPIC_SYSTEM_CONFIGURE, instrParams);
InstructionStatus result = job.processInstruction(instr);
// THEN
verify(handler);
assertThat("Request count", httpHandler.getCount(), is(1));
assertThat("Instruction returned result", result, is(notNullValue()));
assertThat("Completed ping", result.getInstructionState(), is(InstructionState.Completed));
assertThat("Result parameters provided", result.getResultParameters(), is(notNullValue()));
log.debug("Got ping result message: {}", result.getResultParameters().get(InstructionHandler.PARAM_MESSAGE));
assertThat("Result parameter message provided", result.getResultParameters().get(InstructionHandler.PARAM_MESSAGE), is(instanceOf(String.class)));
}
use of net.solarnetwork.node.control.ping.HttpRequesterJob in project solarnetwork-node by SolarNetwork.
the class HttpRequesterJobTest method pingSuccessful_opModes.
@Test
public void pingSuccessful_opModes() throws Exception {
// GIVEN
final TestHandler httpHandler = new TestHandler();
server.setHandler(httpHandler);
final OperationalModesService opModesService = EasyMock.createMock(OperationalModesService.class);
expect(opModesService.enableOperationalModes(singleton(HttpRequesterJob.DEFAULT_SUCCESS_OP_MODE))).andReturn(singleton(HttpRequesterJob.DEFAULT_SUCCESS_OP_MODE));
expect(opModesService.disableOperationalModes(singleton(HttpRequesterJob.DEFAULT_FAILURE_OP_MODE))).andReturn(singleton(HttpRequesterJob.DEFAULT_SUCCESS_OP_MODE));
// WHEN
replay(handler, opModesService);
HttpRequesterJob job = newJobInstance();
job.setOpModesService(new StaticOptionalService<>(opModesService));
job.executeJobService();
// THEN
verify(handler, opModesService);
assertThat("Request count", httpHandler.getCount(), is(1));
}
use of net.solarnetwork.node.control.ping.HttpRequesterJob in project solarnetwork-node by SolarNetwork.
the class HttpRequesterJobTest method systemConfigure_statusError.
@Test
public void systemConfigure_statusError() throws Exception {
// GIVEN
final AbstractHandler httpHandler = new AbstractHandler() {
@Override
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
((Request) request).setHandled(true);
}
};
server.setHandler(httpHandler);
HttpRequesterJob job = newJobInstance();
// WHEN
replay(handler);
Map<String, String> instrParams = new LinkedHashMap<>(4);
instrParams.put(InstructionHandler.PARAM_SERVICE, HttpRequesterJob.PING_SERVICE_NAME);
Instruction instr = InstructionUtils.createLocalInstruction(InstructionHandler.TOPIC_SYSTEM_CONFIGURE, instrParams);
InstructionStatus result = job.processInstruction(instr);
// THEN
verify(handler);
assertThat("Instruction returned result", result, is(notNullValue()));
assertThat("Completed ping", result.getInstructionState(), is(InstructionState.Completed));
assertThat("Result parameters provided", result.getResultParameters(), is(notNullValue()));
log.debug("Got ping result message: {}", result.getResultParameters().get(InstructionHandler.PARAM_MESSAGE));
assertThat("Result parameter message provided", result.getResultParameters().get(InstructionHandler.PARAM_MESSAGE), is(instanceOf(String.class)));
assertThat("Result parameter result is status code", result.getResultParameters().get(InstructionHandler.PARAM_SERVICE_RESULT), is(equalTo(500)));
}
use of net.solarnetwork.node.control.ping.HttpRequesterJob in project solarnetwork-node by SolarNetwork.
the class HttpRequesterJobTest method newJobInstance.
private HttpRequesterJob newJobInstance() {
HttpRequesterJob job = new HttpRequesterJob();
job.setControlId(TEST_CONTROL_ID);
job.setUrl("http://localhost:8988/");
job.setInstructionExecutionService(new StaticOptionalService<>(new SimpleInstructionExecutionService(singletonList(handler))));
job.setSleepSeconds(0);
ResourceBundleMessageSource msgSource = new ResourceBundleMessageSource();
msgSource.setBasenames(HttpRequesterJob.class.getName());
job.setMessageSource(msgSource);
return job;
}
Aggregations