JsonWellFormedChecker extends FixedForwardPipe { @Override public PipeRunResult doPipe(Message message, IPipeLineSession session) throws PipeRunException { PipeForward forward = findForward("success"); String input; try { input = message.asString(); } catch (IOException e) { throw new PipeRunException(this, getLogPrefix(session)+"cannot open stream", e); } if (input.isEmpty()) { forward = findForward("failure"); } else { try { new JSONObject(input); } catch (JSONException ex) { try { new JSONArray(input); } catch (JSONException ex1) { forward = findForward("failure"); } } } return new PipeRunResult(forward, message); }  @Override PipeRunResult doPipe(Message message, IPipeLineSession session);  }
@Test public void doFailureEmptyInputTest() throws Exception { pipe.registerForward(createPipeFailureForward()); pipe.configure(); pipe.start(); PipeRunResult pipeRunResult = doPipe(pipe, "", session); assertEquals("failure", pipeRunResult.getPipeForward().getName()); }
@Test public void doSuccessTest() throws Exception { pipe.registerForward(createPipeFailureForward()); pipe.configure(); pipe.start(); PipeRunResult pipeRunResult = doPipe(pipe, "{\"input\":\"ok\"}", session); assertEquals("success", pipeRunResult.getPipeForward().getName()); }
@Test public void doFailureNonWellFormedInputTest() throws Exception { pipe.registerForward(createPipeFailureForward()); pipe.configure(); pipe.start(); PipeRunResult pipeRunResult = doPipe(pipe, "input=ok", session); assertEquals("failure", pipeRunResult.getPipeForward().getName()); }