TemplateDescriptorParser implements NodeParser<Set<ResponseFormatterDescriptor>> { @Override public Set<ResponseFormatterDescriptor> parseNode(final Node node, final Document document) { int depth = 0; return parse(node, document, depth); }  @Override Set<ResponseFormatterDescriptor> parseNode(final Node node, final Document document);  }
@Test public void testParseNodeShouldReturnTemplateFormatter() { getChildNodes(); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn(TEMPLATE_FORMATTER_TAG); given(formatterInitializer.getExternalClassObject("class")).willReturn(templateFormatter); mockTemplateFormatterAttributes(); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertEquals(((ResponseFormatterDescriptor) actual.toArray()[0]).getResponseFormatter(), templateFormatter); }
@Test public void testParseNodeShouldReturnTemplateFormatterParam() { getChildNodes(); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn(TEMPLATE_FORMATTER_TAG); mockTemplateFormatterAttributes(); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertEquals(((ResponseFormatterDescriptor) actual.toArray()[0]).getParams().getAllParameters().get(0).getValue(), "value"); }
@Test public void testParseNodeWhenNoParamsShouldReturnEmptyTemplateFormatterParams() { getChildNodes(); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn(TEMPLATE_FORMATTER_TAG); given(element.getAttribute("class")).willReturn("class"); given(element.getElementsByTagName("param")).willReturn(null); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertTrue(((ResponseFormatterDescriptor) actual.toArray()[0]).getParams().getAllParameters().isEmpty()); }
@Test public void testParseNodeShouldReturnTemplateFormatterSetInvoker() { getChildNodes(); given(configurationAccess.getProperties()).willReturn(properties); given(properties.getMaxDepthOfTree()).willReturn(10); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn(TEMPLATE_FORMATTER_SET_INVOKER_TAG, TEMPLATE_FORMATTER_SET_INVOKER_TAG, TEMPLATE_FORMATTER_TAG); mockTemplateFormatterAttributes(); given(xPathEvaluator.getElementByXPath(Mockito.anyString(), Mockito.eq(document))).willReturn(node); given(formatterInitializer.getExternalClassObject("class")).willReturn(templateFormatter); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertEquals(((ResponseFormatterDescriptor) actual.toArray()[0]).getResponseFormatter(), templateFormatter); }
@Test public void testParseNodeShouldReturnEmptySetIfNodeHasNoChildren() { given(node.getChildNodes()).willReturn(null); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertTrue(actual.isEmpty()); }
@Test public void testParseNodeShouldReturnEmptySetIfNodeTypeIsNotElementNode() { getChildNodes(); given(element.getNodeType()).willReturn(Node.COMMENT_NODE); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertTrue(actual.isEmpty()); }
@Test public void testParseNodeShouldReturnEmptySetIfNodeTypeIsNotAMatchingType() { getChildNodes(); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn("someOtherTag"); Set<ResponseFormatterDescriptor> actual = underTest.parseNode(node, document); assertTrue(actual.isEmpty()); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testParseNodeShouldThrowExceptionWhenSubTreeIsTooDeep() { Whitebox.setInternalState(underTest, "maxDepthOfXmlTree", 1); getChildNodes(); given(element.getNodeType()).willReturn(Node.ELEMENT_NODE); given(element.getTagName()).willReturn(TEMPLATE_FORMATTER_SET_INVOKER_TAG, TEMPLATE_FORMATTER_SET_INVOKER_TAG, TEMPLATE_FORMATTER_TAG); mockTemplateFormatterAttributes(); given(xPathEvaluator.getElementByXPath(Mockito.anyString(), Mockito.eq(document))).willReturn(node); given(formatterInitializer.getExternalClassObject("class")).willReturn(templateFormatter); underTest.parseNode(node, document); }