InterceptorDescriptorParser implements NodeParser<InterceptorDescriptor> { @Override public InterceptorDescriptor parseNode(final Node interceptorNode, final Document document) { InterceptorDescriptor interceptorDescriptor = null; if (interceptorNode != null) { Element el = (Element) interceptorNode; String name = el.getAttribute("name"); String clazz = el.getAttribute("class"); if (clazz == null) { throw new DescriptorValidationFailedException("Validation of stub descriptor failed - Class name is missing."); } if (clazz.toLowerCase().endsWith(".class")) { throw new DescriptorValidationFailedException("Validation of stub descriptor failed - Class name '" + clazz + "' should not contain 'class' at its end."); } ParameterList params = parseInterceptorParameters(el.getElementsByTagName("param")); RequestInterceptor requestInterceptor = null; ResponseInterceptor responseInterceptor = null; try { requestInterceptor = requestInterceptorInitializer.getExternalClassObject(clazz); } catch (InterfaceValidationFailedException e) { LOGGER.debug("No Request interceptor in class:" + clazz, e); } try { responseInterceptor = responseInterceptorInitializer.getExternalClassObject(clazz); } catch (InterfaceValidationFailedException e) { LOGGER.debug("No Response interceptor in class:" + clazz, e); } if (requestInterceptor == null && responseInterceptor == null) { throw new DescriptorValidationFailedException("Validation of stub descriptor failed - Class '" + clazz + "' does not implement any of the necessary interfaces."); } interceptorDescriptor = new InterceptorDescriptor(name, requestInterceptor, responseInterceptor, params); } return interceptorDescriptor; }  @Override InterceptorDescriptor parseNode(final Node interceptorNode, final Document document);  }
@Test public void testParseNodeShouldReturnInterceptorDescriptor() { NodeList paramNodeList = createParamNodeList(); RequestInterceptor requestInterceptor = new MockRequestInterceptor(); ResponseInterceptor responseInterceptor = new MockResponseInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(paramNodeList); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(requestInterceptor); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(responseInterceptor); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getName(), NAME); assertEquals(actual.getRequestInterceptor(), requestInterceptor); assertEquals(actual.getResponseInterceptor(), responseInterceptor); assertEquals(actual.getParams().getAllParameters().get(0).getName(), NAME); assertEquals(actual.getParams().getAllParameters().get(0).getValue(), VALUE); }
@Test public void testParseNodeShouldReturnInterceptorDescriptorEvenIfOnlyFirstInterfaceIsImplemented() { NodeList paramNodeList = createParamNodeList(); ResponseInterceptor responseInterceptor = new MockResponseInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(paramNodeList); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(responseInterceptor); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getName(), NAME); assertEquals(actual.getResponseInterceptor(), responseInterceptor); assertEquals(actual.getParams().getAllParameters().get(0).getName(), NAME); assertEquals(actual.getParams().getAllParameters().get(0).getValue(), VALUE); }
@Test public void testParseNodeShouldReturnInterceptorDescriptorEvenIfOnlySecondInterfaceIsImplemented() { NodeList paramNodeList = createParamNodeList(); RequestInterceptor requestInterceptor = new MockRequestInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(paramNodeList); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(requestInterceptor); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getName(), NAME); assertEquals(actual.getRequestInterceptor(), requestInterceptor); assertEquals(actual.getParams().getAllParameters().get(0).getName(), NAME); assertEquals(actual.getParams().getAllParameters().get(0).getValue(), VALUE); }
@Test public void testParseNodeWhenNoParamsShouldReturnNotSetParameters() { RequestInterceptor requestInterceptor = new MockRequestInterceptor(); ResponseInterceptor responseInterceptor = new MockResponseInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(null); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(requestInterceptor); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(responseInterceptor); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getParams().getAllParameters().isEmpty(), true); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testParseNodeWhenNoInterceptorClassShouldThrowException() { given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); underTest.parseNode(interceptorNode, document); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testParseNodeWhenInterceptorClassHasExtraClassInNameShouldThrowException() { NodeList paramNodeList = createParamNodeList(); RequestInterceptor requestInterceptor = new MockRequestInterceptor(); ResponseInterceptor responseInterceptor = new MockResponseInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(BAD_CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(paramNodeList); given(requestInterceptorInitializer.getExternalClassObject(BAD_CLASS)).willReturn(requestInterceptor); given(responseInterceptorInitializer.getExternalClassObject(BAD_CLASS)).willReturn(responseInterceptor); underTest.parseNode(interceptorNode, document); }
@Test public void testParseNodeWhenNoResponseInterceptorShouldCreateDescriptor() { RequestInterceptor requestInterceptor = new MockRequestInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(null); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(requestInterceptor); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getRequestInterceptor(), requestInterceptor); assertEquals(actual.getResponseInterceptor(), null); }
@Test public void testParseNodeWhenNoRequestInterceptorShouldCreateDescriptor() { ResponseInterceptor responseInterceptor = new MockResponseInterceptor(); given(interceptorNode.getAttribute("name")).willReturn(NAME); given(interceptorNode.getAttribute("class")).willReturn(CLASS); given(interceptorNode.getElementsByTagName("param")).willReturn(null); given(requestInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(null); given(responseInterceptorInitializer.getExternalClassObject(CLASS)).willReturn(responseInterceptor); InterceptorDescriptor actual = underTest.parseNode(interceptorNode, document); assertEquals(actual.getRequestInterceptor(), null); assertEquals(actual.getResponseInterceptor(), responseInterceptor); }
@Test public void testParseNodeWhenInterceptorNodeIsNullShouldDoNothing() { InterceptorDescriptor actual = underTest.parseNode(null, document); assertEquals(actual, null); }