XmlNodeValueChecker implements ConditionChecker { @Override public boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameterList) { boolean result = false; List<Parameter> params = parameterList.getAllParameters(); 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 element = paramater.getName(); String value = paramater.getValue(); result = evaluateCondition(request.getBody(), element, 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(ELEMENT, ELEMENT_VALUE)); String query = "( given(queryExpressionEvaluator.evaluateXQuery(REQUEST, query)).willReturn(XML_DECLARATION + "true"); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, true); }
@Test public void testCheckConditionWhenContentIsFastinfosetShouldReturnTrue() throws SaxonApiException { parameterList.addParameter(new Parameter(ELEMENT, ELEMENT_VALUE)); String query = "( given(queryExpressionEvaluator.evaluateXQuery(REQUEST, query)).willReturn(XML_DECLARATION + "true"); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(FASTINFOSET_CONTENT); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, true); }
@Test public void testCheckConditionWhenValueDoesNotExistShouldReturnFalse() throws SaxonApiException { parameterList.addParameter(new Parameter(ELEMENT, "178")); String query = "( given(queryExpressionEvaluator.evaluateXQuery(REQUEST, query)).willReturn(XML_DECLARATION + "false"); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, false); }
@Test public void testCheckConditionWhenRequestIsNotXmlShouldReturnFalse() { parameterList.addParameter(new Parameter(ELEMENT, ELEMENT_VALUE)); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn("text/plain"); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, false); }
@Test public void testCheckConditionWhenRequestDoesNotHaveContentTypeShouldReturnFalse() { parameterList.addParameter(new Parameter(ELEMENT, ELEMENT_VALUE)); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(null); boolean actual = underTest.checkCondition(request, parameterList); assertEquals(actual, false); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterMapIsEmptyShouldThrowException() { underTest.checkCondition(request, parameterList); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterHasMultipleEntriesShouldThrowException() { parameterList.addParameter(new Parameter(ELEMENT, "178")); parameterList.addParameter(new Parameter("ns1:OTHERID", "3001")); underTest.checkCondition(request, parameterList); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenErrorDuringXqueryExecutionShouldThrowException() throws SaxonApiException { parameterList.addParameter(new Parameter(ELEMENT, ELEMENT_VALUE)); String query = "( given(queryExpressionEvaluator.evaluateXQuery(REQUEST, query)).willThrow(new SaxonApiException("exception")); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); underTest.checkCondition(request, parameterList); }