CustomXQueryBodyChecker implements ConditionChecker { @Override public boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameterList) { List<Parameter> params = parameterList.getAllParameters(); boolean result = false; if (params.size() == 1) { String contentType = request.getHeader(CONTENT_TYPE_HEADER); if (contentType != null && (contentType.contains(XML_CONTENT) || FASTINFOSET_CONTENT.equals(contentType))) { try { Parameter paramater = params.iterator().next(); String value = paramater.getValue(); result = evaluateCondition(request.getBody(), value); } catch (SaxonApiException e) { throw new ConditionEvaluationFailedException("XQuery evaluation failed at request: " + request.getWilmaMessageLoggerId(), e); } } } else { throw new ConditionEvaluationFailedException("Please provide exactly one parameter in Stub Configuration!"); } return result; }  @Override boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameterList);  }
@Test public void testCheckConditionWhenElementExistsShouldReturnTrue() throws SaxonApiException { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); given(request.getBody()).willReturn(BODY); given(queryExpressionEvaluator.evaluateXQuery(BODY, QUERY)).willReturn(XML_DECLARATION + "true"); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, true); }
@Test public void testCheckConditionWhenContentTypeIsFastinfosetShouldReturnTrue() throws SaxonApiException { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(FASTINFOSET_CONTENT); given(request.getBody()).willReturn(BODY); given(queryExpressionEvaluator.evaluateXQuery(BODY, QUERY)).willReturn(XML_DECLARATION + "true"); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, true); }
@Test public void testCheckConditionWhenResultIsANodeShouldReturnTrue() throws SaxonApiException { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); given(request.getBody()).willReturn(BODY); given(queryExpressionEvaluator.evaluateXQuery(BODY, QUERY)).willReturn(XML_DECLARATION + "<ns5:HotelID >179</ns5:HotelID>"); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, true); }
@Test public void testCheckConditionWhenResultIsANodeShouldLogResult() throws SaxonApiException { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); given(request.getBody()).willReturn(BODY); given(queryExpressionEvaluator.evaluateXQuery(BODY, QUERY)).willReturn(XML_DECLARATION + "<ns5:HotelID >179</ns5:HotelID>"); Whitebox.setInternalState(underTest, "logger", logger); underTest.checkCondition(request, parameterList); verify(logger).debug(Mockito.anyString(), Mockito.any(SaxonApiException.class)); }
@Test public void testCheckConditionWhenContentTypeIsNullShouldReturnFalse() { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(null); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, false); }
@Test public void testCheckConditionWhenContentTypeIsTextShouldReturnFalse() { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn("text/plain"); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, false); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterSizeIsZeroShouldThrowException() { underTest.checkCondition(request, parameterList); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterSizeIsTwoShouldThrowException() { parameterList.addParameter(new Parameter("xquery", QUERY)); parameterList.addParameter(new Parameter("xquery1", QUERY)); underTest.checkCondition(request, parameterList); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenSyntaxExceptionShouldThrowException() throws SaxonApiException { parameterList.addParameter(new Parameter("xquery", QUERY)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(FASTINFOSET_CONTENT); given(request.getBody()).willReturn(BODY); given(queryExpressionEvaluator.evaluateXQuery(BODY, QUERY)).willThrow(new SaxonApiException("exception")); underTest.checkCondition(request, parameterList); }