ConditionDescriptorParser implements NodeParser<ConditionDescriptor> { @Override public ConditionDescriptor parseNode(final Node conDes, final Document document) { int depth = 0; Condition condition = null; if (conDes != null) { Element el = (Element) conDes; List<Condition> parsedConditions = parseConditions(el.getChildNodes(), document, depth); if (!parsedConditions.isEmpty()) { condition = parsedConditions.get(0); } } return new ConditionDescriptor(condition); }  @Override ConditionDescriptor parseNode(final Node conDes, final Document document);  }
@Test public void testParseNodeShouldReturnNullConditionWhenConditionNodeArgumentIsNull() { ConditionDescriptor actual = underTest.parseNode(null, document); assertNull(actual.getCondition()); }
@Test public void testParseNodeShouldReturnNullConditionWhenConditionNodeArgumentHasNoChildren() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(0); ConditionDescriptor actual = underTest.parseNode(node, document); assertNull(actual.getCondition()); }
@Test public void testParseNodeShouldReturnNullConditionWhenConditionNodeArgumentGetChildrenIsNull() { given(node.getChildNodes()).willReturn(null); ConditionDescriptor actual = underTest.parseNode(node, document); assertNull(actual.getCondition()); }
@Test public void testParseNodeShouldReturnWithNullConditionWhenConditionNodeArgumentHasOneChildWithUnknownTagName() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(node); given(node.getNodeType()).willReturn(Node.ELEMENT_NODE); given(node.getTagName()).willReturn("UNKNOWN_TAG_NAME"); ConditionDescriptor actual = underTest.parseNode(node, document); assertNull(actual.getCondition()); }
@Test public void testParseNodeShouldReturnWithASimpleConditionWithEmptyConditionListWhenConditionNodeArgumentHasOneNotElementChild() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(paramElement); given(paramElement.getNodeType()).willReturn(Node.TEXT_NODE); ConditionDescriptor actual = underTest.parseNode(node, document); assertNull(actual.getCondition()); }
@Test public void testParseNodeShouldReturnWithASimpleConditionWhenConditionNodeArgumentHasOneChildWithConditionTagName() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(node); given(node.getNodeType()).willReturn(Node.ELEMENT_NODE); given(node.getTagName()).willReturn(TAGNAME_CONDITION); given(node.getAttribute(CLASS_ATTRIBUTE)).willReturn(CLASS_NAME); given(conditionCheckerInitializer.getExternalClassObject(CLASS_NAME)).willReturn(conditionChecker); ConditionDescriptor actual = underTest.parseNode(node, document); SimpleCondition actCondition = (SimpleCondition) actual.getCondition(); Assert.assertEquals(actCondition.getConditionChecker(), conditionChecker); }
@Test public void testParseNodeShouldReturnWithASimpleConditionParamsWhenConditionNodeArgumentHasOneChildWithConditionTagNameWithChildParam() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(node); given(node.getNodeType()).willReturn(Node.ELEMENT_NODE); given(node.getTagName()).willReturn(TAGNAME_CONDITION); given(node.getAttribute(CLASS_ATTRIBUTE)).willReturn(CLASS_NAME); List<Node> paramList = new ArrayList<Node>(); paramList.add(paramElement); given(node.getElementsByTagName(PARAM_ATTRIBUTE)).willReturn(new MyNodeList(paramList)); given(paramElement.getAttribute("name")).willReturn(PARAM_NAME); given(paramElement.getAttribute("value")).willReturn(CLASS_NAME); ConditionDescriptor actual = underTest.parseNode(node, document); SimpleCondition actCondition = (SimpleCondition) actual.getCondition(); Assert.assertEquals(actCondition.getParameters().getAllParameters().get(0).getValue(), CLASS_NAME); }
@Test public void testParseNodeShouldReturnWithASimpleConditionWithEmptyParamsWhenConditionNodeArgumentHasOneChildWithConditionTagNameWithoutChildParam() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(node); given(node.getNodeType()).willReturn(Node.ELEMENT_NODE); given(node.getTagName()).willReturn(TAGNAME_CONDITION); given(node.getAttribute(CLASS_ATTRIBUTE)).willReturn(CLASS_NAME); List<Node> paramList = new ArrayList<Node>(); given(node.getElementsByTagName(PARAM_ATTRIBUTE)).willReturn(new MyNodeList(paramList)); given(paramElement.getAttribute("name")).willReturn(PARAM_NAME); given(paramElement.getAttribute("value")).willReturn(CLASS_NAME); ConditionDescriptor actual = underTest.parseNode(node, document); SimpleCondition actCondition = (SimpleCondition) actual.getCondition(); Assert.assertEquals(actCondition.getParameters().getAllParameters().size(), 0); }
@Test public void testParseNodeShouldReturnWithACompositeConditionWhenConditionNodeArgumentHasOneChildWithAndTagName() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(paramElement); given(paramElement.getNodeType()).willReturn(Node.ELEMENT_NODE); given(paramElement.getTagName()).willReturn(TAGNAME_AND); given(paramElement.getChildNodes()).willReturn(new MyNodeList(new ArrayList<Node>())); ConditionDescriptor actual = underTest.parseNode(node, document); CompositeCondition actCondition = (CompositeCondition) actual.getCondition(); Assert.assertEquals(actCondition.getConditionType(), ConditionType.AND); }
@Test public void testParseNodeShouldReturnWithACompositeConditionWhenConditionNodeArgumentHasOneChildWithOrTagName() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(paramElement); given(paramElement.getNodeType()).willReturn(Node.ELEMENT_NODE); given(paramElement.getTagName()).willReturn(TAGNAME_OR); given(paramElement.getChildNodes()).willReturn(new MyNodeList(new ArrayList<Node>())); ConditionDescriptor actual = underTest.parseNode(node, document); CompositeCondition actCondition = (CompositeCondition) actual.getCondition(); Assert.assertEquals(actCondition.getConditionType(), ConditionType.OR); }
@Test public void testParseNodeShouldReturnWithACompositeConditionWhenConditionNodeArgumentHasOneChildWithNotTagName() { given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(paramElement); given(paramElement.getNodeType()).willReturn(Node.ELEMENT_NODE); given(paramElement.getTagName()).willReturn(TAGNAME_NOT); given(paramElement.getChildNodes()).willReturn(new MyNodeList(new ArrayList<Node>())); ConditionDescriptor actual = underTest.parseNode(node, document); CompositeCondition actCondition = (CompositeCondition) actual.getCondition(); Assert.assertEquals(actCondition.getConditionType(), ConditionType.NOT); }
@Test public void testParseNodeShouldReturnWithACompositeConditionWhenConditionNodeArgumentHasChildWithConditionSetInvokerTagName() { given(configurationAccess.getProperties()).willReturn(properties); given(properties.getMaxDepthOfTree()).willReturn(10); given(node.getChildNodes()).willReturn(nodeList); given(nodeList.getLength()).willReturn(1); given(nodeList.item(0)).willReturn(paramElement); given(paramElement.getNodeType()).willReturn(Node.ELEMENT_NODE); given(paramElement.getTagName()).willReturn(TAGNAME_COND_SET_INVOKER); given(paramElement.getAttribute("name")).willReturn("CONDITION_SET_NAME"); given(xPathEvaluator.getElementByXPath(Mockito.anyString(), (Document) Mockito.any())).willReturn(conditionSetElement); given(conditionSetElement.getNodeType()).willReturn(Node.ELEMENT_NODE); given(conditionSetElement.getTagName()).willReturn(TAGNAME_CONDITION_SET); List<Node> conditionList = new ArrayList<Node>(); conditionList.add(paramElement2); given(conditionSetElement.getChildNodes()).willReturn(new MyNodeList(conditionList)); given(paramElement2.getNodeType()).willReturn(Node.ELEMENT_NODE); given(paramElement2.getTagName()).willReturn(TAGNAME_CONDITION); given(paramElement2.getAttribute(CLASS_ATTRIBUTE)).willReturn(CLASS_NAME); given(conditionCheckerInitializer.getExternalClassObject(CLASS_NAME)).willReturn(conditionChecker); List<Node> paramList = new ArrayList<Node>(); given(paramElement2.getElementsByTagName(PARAM_ATTRIBUTE)).willReturn(new MyNodeList(paramList)); ConditionDescriptor actual = underTest.parseNode(node, document); SimpleCondition actCondition = (SimpleCondition) actual.getCondition(); assertEquals(actCondition.getConditionChecker(), conditionChecker); }