@BeforeMethod public void setup() { initializeMocksAndTestDate(); parameters.addParameter(new Parameter("type:RegexResolvable", "url:.*/urls/regex.html")); parameters.addParameter(new Parameter("type:ExactResolvable", "url:www.epam.com/urls/target.html")); parameters.addParameter(new Parameter("not_a_type", "url:www.typeless.com/urls/resolvable.html")); parameters.addParameter(new Parameter("type:NonResolvable", "not_an_url")); parameters.addParameter(new Parameter("typequery:" + XPATH_QUERY, "url:www.xpath.com/query/test")); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterHasMultipleEntriesShouldThrowException() { //GIVEN parameterList.addParameter(new Parameter(ELEMENT, "178")); parameterList.addParameter(new Parameter("ns1:OTHERID", "3001")); //WHEN underTest.checkCondition(request, parameterList); //THEN exception should be thrown }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenParameterSizeIsTwoShouldThrowException() { //GIVEN parameterList.addParameter(new Parameter("xquery", QUERY)); parameterList.addParameter(new Parameter("xquery1", QUERY)); //WHEN underTest.checkCondition(request, parameterList); //THEN exception should be thrown }
@Test(expectedExceptions = ResponseFormattingFailedException.class) public void testFormatTemplateWhenRegexpSyntaxIncorrectShouldThrowException() throws Exception { //GIVEN templateResource = "123 this is a test template 123".getBytes(); parameterList.addParameter(new Parameter("{0-2}+", "HELLO")); //WHEN underTest.formatResponse(request, response, templateResource, parameterList); //THEN }
@Test public void testValidateWhenXQueryIsSyntacticallyCorrectShouldPass() { //GIVEN parameterList.addParameter(new Parameter("check", "valid xquery")); doReturn(CUSTOM_BODY_CHECKER).when(underTest).getSimpleName(conditionChecker); //WHEN underTest.validate(conditionChecker, parameterList); //THEN it should not throw exception }
@Test public void testCheckConditionShouldReturnTrueWhenParameterListContainsMoreElement() { //GIVEN paramList.addParameter(new Parameter("key1", "aba")); paramList.addParameter(new Parameter("key2", "ba")); paramList.addParameter(new Parameter("key3", "a")); given(request.getRequestLine()).willReturn("aba"); //WHEN boolean result = underTest.checkCondition(request, paramList); //THEN Assert.assertTrue(result); }
@Test public void testCheckConditionShouldReturnFalseWhenOneParamIsEmptyString() { //GIVEN given(paramList.getAllParameters()).willReturn(params); params.add(new Parameter("", "")); //WHEN boolean actual = underTest.checkCondition(request, paramList); //THEN assertFalse(actual); }
@Test public void testCheckConditionWhenOneConditionIsFulfilledShouldReturnFalse() { //GIVEN String otherValue = "other value"; parameterList.addParameter(new Parameter(PARAMETER_NAME, MSG_ID)); parameterList.addParameter(new Parameter("OTHER_VALUE", otherValue)); given(request.getBody()).willReturn(MSG_ID, ""); //WHEN boolean actual = underTest.checkCondition(request, parameterList); //THEN assertEquals(actual, false); }
@Test public void testCheckConditionWhenNoConditionIsFulfilledShouldReturnFalse() { //GIVEN String otherValue = "other value"; parameterList.addParameter(new Parameter(PARAMETER_NAME, MSG_ID)); parameterList.addParameter(new Parameter("OTHER_VALUE", otherValue)); given(request.getBody()).willReturn("something else", "something else"); //WHEN boolean actual = underTest.checkCondition(request, parameterList); //THEN assertEquals(actual, false); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionShouldThrowExceptionWhenTooMuchParametersAreDefinedInConfig() { //GIVEN given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(HEADER_VAL_FASTINFOSET); parameterList.addParameter(new Parameter(TEXT_PLAIN, HEADER_VAL_XML)); parameterList.addParameter(new Parameter(CONTENT_TYPE_HEADER, HEADER_VAL_XML)); //WHEN underTest.checkCondition(request, parameterList); //TNEN exception is thrown }
@Test public void testCheckConditionShouldReturnFalseWhenParameterListContainsOneWrongElement() { //GIVEN paramList.addParameter(new Parameter("key1", "aba")); given(request.getRequestLine()).willReturn("cba"); //WHEN boolean result = underTest.checkCondition(request, paramList); //THEN assertFalse(result); }
@Test public void testCheckConditionShouldReturnTrueWhenParameterListContainsOneMatcherElement() { //GIVEN paramList.addParameter(new Parameter("key1", "aba")); given(request.getRequestLine()).willReturn("aba"); //WHEN boolean result = underTest.checkCondition(request, paramList); //THEN assertTrue(result); }
@Test public void testCheckConditionShouldReturnFalseWhenQueryResultFalse() throws SaxonApiException { //GIVEN given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(HEADER_VAL_XML); parameterList.addParameter(new Parameter(TEXT_PLAIN, HEADER_VAL_XML)); given(xQueryEvaluator.evaluateXQuery(anyString(), anyString())).willReturn("false"); //WHEN boolean actual = underTest.checkCondition(request, parameterList); //TNEN assertFalse(actual); }
@Test public void testCheckConditionShouldReturnTrueWhenBodyContainsParam() { //GIVEN given(request.getBody()).willReturn("baaacccacacacacaaasdfsfasfasdfafasdf"); given(paramList.getAllParameters()).willReturn(params); given(operator.checkTarget(anyString(), anyString())).willReturn(true); params.add(new Parameter("", "aa")); //WHEN boolean actual = underTest.checkCondition(request, paramList); //THEN Assert.assertTrue(actual); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionShouldThrowExceptionWhenXQueryEvaulationFailed() throws SaxonApiException { //GIVEN given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(HEADER_VAL_XML); parameterList.addParameter(new Parameter(TEXT_PLAIN, HEADER_VAL_XML)); given(xQueryEvaluator.evaluateXQuery(anyString(), anyString())).willThrow(new SaxonApiException("")); //WHEN underTest.checkCondition(request, parameterList); //TNEN exception is thrown }
@Test public void testCheckConditionWhenConditionIsFulfilledShouldReturnTrue() { //GIVEN parameterList.addParameter(new Parameter(CONTENT_TYPE_HEADER, XML_CONTENT)); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); given(request.getBody()).willReturn(XML_CONTENT); //WHEN boolean actual = underTest.checkCondition(request, parameterList); //THEN assertEquals(actual, true); }
@Test(expectedExceptions = ResponseFormattingFailedException.class) public void testFormatTemplateShouldThrowExceptionWhenXslFileResourceDoesNotExist() throws Exception { //GIVEN params.addParameter(new Parameter(XSL_PARAM_KEY, XSL_PARAM_KEY)); given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(XSL_PARAM_KEY); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willThrow(new IOException()); //WHEN underTest.formatResponse(wilmaRequest, response, templateResource, params); //THEN exception is thrown }
@Test public void testCheckConditionWhenElementExistsShouldReturnTrue() throws SaxonApiException { //GIVEN 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"); //WHEN boolean actual = underTest.checkCondition(request, parameterList); //THEN assertEquals(actual, true); }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenSyntaxExceptionShouldThrowException() throws SaxonApiException { //GIVEN 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")); //WHEN underTest.checkCondition(request, parameterList); //THEN exception should be thrown } }
@Test(expectedExceptions = ConditionEvaluationFailedException.class) public void testCheckConditionWhenErrorDuringXqueryExecutionShouldThrowException() throws SaxonApiException { //GIVEN in setUp parameterList.addParameter(new Parameter(NODE + "/@" + ATTRIBUTE, VALUE)); String query = "//*[name()='" + NODE + "']/@" + ATTRIBUTE + "='" + VALUE + "'"; given(queryExpressionEvaluator.evaluateXQuery(REQUEST, query)).willThrow(new SaxonApiException("exception")); given(request.getBody()).willReturn(REQUEST); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(XML_CONTENT); //WHEN underTest.checkCondition(request, parameterList); //THEN exception should be thrown }