JsonPathChecker implements ConditionChecker { @Override public boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameters) { String expected = parameters.get(EXPECTED_KEY); String path = parameters.get(JSONPATH_KEY); boolean equals; try { String actualValue = JsonPath.read(request.getBody(), path); equals = evaluate(expected, actualValue); } catch (Exception e) { logger.debug("Request body isn't of JSON format, message:" + request.getWilmaMessageLoggerId(), e); equals = false; } return equals; }  @Override boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameters);  }
@Test public void testCheckConditionShouldPassWhenJsonHasProperParameter() throws Exception { givenExpectations("Wilma", "$.name"); givenWilmaRequest("{\"name\":\"Wilma\",\"age\":\"20\"}"); boolean matches = underTest.checkCondition(request, parameters); assertTrue(matches); }
@Test public void testCheckConditionShouldFailWhenJsonHasDiffernetParameter() throws Exception { givenExpectations("Wilma", "$.name"); givenWilmaRequest("{\"name\":\"Irma\",\"age\":\"20\"}"); boolean matches = underTest.checkCondition(request, parameters); assertFalse(matches); }
@Test public void testCheckConditionShouldFailWhenJsonHasNoSuchParameter() throws Exception { givenExpectations("Wilma", "$.name"); givenWilmaRequest("{\"firstName\":\"Wilma\",\"age\":\"20\"}"); boolean matches = underTest.checkCondition(request, parameters); assertFalse(matches); }
@Test public void testCheckConditionShouldFailWhenRequestIsNotJson() throws Exception { givenExpectations("Wilma", "$.name"); givenWilmaRequest("<request><name>Wilma</name><age>2></age></request>"); boolean matches = underTest.checkCondition(request, parameters); assertFalse(matches); }