Search in sources :

Example 1 with HttpRequesterJob

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()));
}
Also used : BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) HttpRequesterJob(net.solarnetwork.node.control.ping.HttpRequesterJob) Instruction(net.solarnetwork.node.reactor.Instruction) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) Test(org.junit.Test)

Example 2 with HttpRequesterJob

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)));
}
Also used : InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) HttpRequesterJob(net.solarnetwork.node.control.ping.HttpRequesterJob) Instruction(net.solarnetwork.node.reactor.Instruction) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with HttpRequesterJob

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));
}
Also used : OperationalModesService(net.solarnetwork.node.service.OperationalModesService) HttpRequesterJob(net.solarnetwork.node.control.ping.HttpRequesterJob) Test(org.junit.Test)

Example 4 with HttpRequesterJob

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)));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) Request(org.mortbay.jetty.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequesterJob(net.solarnetwork.node.control.ping.HttpRequesterJob) HttpServletResponse(javax.servlet.http.HttpServletResponse) Instruction(net.solarnetwork.node.reactor.Instruction) BasicInstruction(net.solarnetwork.node.reactor.BasicInstruction) AbstractHandler(org.mortbay.jetty.handler.AbstractHandler) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with HttpRequesterJob

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;
}
Also used : SimpleInstructionExecutionService(net.solarnetwork.node.reactor.SimpleInstructionExecutionService) HttpRequesterJob(net.solarnetwork.node.control.ping.HttpRequesterJob) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource)

Aggregations

HttpRequesterJob (net.solarnetwork.node.control.ping.HttpRequesterJob)8 Test (org.junit.Test)7 BasicInstruction (net.solarnetwork.node.reactor.BasicInstruction)5 Instruction (net.solarnetwork.node.reactor.Instruction)5 InstructionStatus (net.solarnetwork.node.reactor.InstructionStatus)5 LinkedHashMap (java.util.LinkedHashMap)3 OperationalModesService (net.solarnetwork.node.service.OperationalModesService)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SimpleInstructionExecutionService (net.solarnetwork.node.reactor.SimpleInstructionExecutionService)1 Request (org.mortbay.jetty.Request)1 AbstractHandler (org.mortbay.jetty.handler.AbstractHandler)1 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)1