HtmlProcessor extends SimpleCallback { public void rule(RuleType rule) { String id = "-"; if (rule.getRuleId() != null) { id = extractLastIdentifier(rule.getRuleId(), ":"); } htmlOut.println("<li><b>Rule ID</b>: <i>" + id + "</i></li>"); htmlOut.println("<dl>"); htmlOut.print("<p>"); htmlOut.print(rule.getEffect().value()); if (rule.getTarget() == null || rule.getTarget().getAnyOf() == null || rule.getTarget().getAnyOf().isEmpty()) { htmlOut.print(" for all requests"); } else { htmlOut.print(" for requests with attributes "); target(rule.getTarget().getAnyOf()); } if (rule.getCondition() != null) { htmlOut.print(" when "); htmlOut.println(this.stringifyCondition(rule.getCondition()) + " "); } else { htmlOut.print(" with no conditions "); } if (rule.getAdviceExpressions() != null) { advice(rule.getAdviceExpressions()); if (rule.getObligationExpressions() != null) { htmlOut.println(" and "); } } if (rule.getObligationExpressions() != null) { obligation(rule.getObligationExpressions()); } htmlOut.println("</p>"); } HtmlProcessor(File policyFile, Object policyObject); Map<String, AttributeIdentifiers> getAttributeIdentifiersMap(); @Override void onFinishScan(Object root); @Override CallbackResult onPreVisitPolicySet(PolicySetType parent, PolicySetType policySet); @Override CallbackResult onPostVisitPolicySet(PolicySetType parent, PolicySetType policySet); void policySet(PolicySetType policySet, String htmlListElement); @Override CallbackResult onPreVisitPolicy(PolicySetType parent, PolicyType policy); @Override CallbackResult onPostVisitPolicy(PolicySetType parent, PolicyType policy); void policy(PolicyType policy); @Override CallbackResult onPreVisitRule(PolicyType parent, RuleType rule); @Override CallbackResult onPostVisitRule(PolicyType parent, RuleType rule); void rule(RuleType rule); void target(List<AnyOfType> anyOfList); String html();  }
@Test public void testHtmlProcessorRule() { PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); RuleType mockRuleType = Mockito.mock(RuleType.class); ConditionType mockConditionType = Mockito.mock(ConditionType.class); ObligationExpressionsType mockOESType = Mockito.mock(ObligationExpressionsType.class); ObligationExpressionType mockOEType = Mockito.mock(ObligationExpressionType.class); EffectType effectTypePermit = EffectType.PERMIT; processor = new HtmlProcessor(temp, mockPolicySetType); List<ObligationExpressionType> oblList = new ArrayList<ObligationExpressionType>(); oblList.add(mockOEType); when(mockRuleType.getEffect()).thenReturn(effectTypePermit); when(mockRuleType.getRuleId()).thenReturn(null); when(mockRuleType.getTarget()).thenReturn(null); when(mockRuleType.getCondition()).thenReturn(mockConditionType); when(mockRuleType.getObligationExpressions()).thenReturn(mockOESType); when(mockOESType.getObligationExpression()).thenReturn(oblList); when(mockOEType.getFulfillOn()).thenReturn(effectTypePermit); processor.rule(mockRuleType); verify(mockRuleType, atLeast(1)).getRuleId(); verify(mockRuleType, atLeast(1)).getTarget(); verify(mockRuleType, atLeast(1)).getCondition(); verify(mockRuleType, atLeast(1)).getObligationExpressions(); verify(mockOESType, atLeast(1)).getObligationExpression(); verify(mockOEType, atLeast(1)).getFulfillOn(); JAXBElement<?> mockJaxBElement = Mockito.mock(JAXBElement.class); Object mockValueObject = Mockito.mock(Object.class); doReturn(mockJaxBElement).when(mockConditionType).getExpression(); doReturn(mockValueObject).when(mockJaxBElement).getValue(); try { processor.rule(mockRuleType); fail(); } catch (IllegalArgumentException e) { verify(mockConditionType, atLeast(1)).getExpression(); verify(mockJaxBElement, atLeast(1)).getValue(); } }
@Test public void testHtmlProcessorRuleNullEmptyList() { PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); RuleType mockRuleType = Mockito.mock(RuleType.class); TargetType mockTargetType = Mockito.mock(TargetType.class); EffectType effectTypePermit = EffectType.PERMIT; AdviceExpressionsType mockAdviceExsType = Mockito.mock(AdviceExpressionsType.class); AdviceExpressionType mockAdviceEx = Mockito.mock(AdviceExpressionType.class); processor = new HtmlProcessor(temp, mockPolicySetType); List<AnyOfType> anyOfList = new ArrayList<AnyOfType>(); List<AdviceExpressionType> adviceExList = new ArrayList<AdviceExpressionType>(); adviceExList.add(mockAdviceEx); when(mockRuleType.getEffect()).thenReturn(effectTypePermit); when(mockRuleType.getRuleId()).thenReturn(null); when(mockRuleType.getTarget()).thenReturn(mockTargetType); when(mockRuleType.getObligationExpressions()).thenReturn(null); when(mockRuleType.getAdviceExpressions()).thenReturn(mockAdviceExsType); when(mockTargetType.getAnyOf()).thenReturn(null); when(mockAdviceExsType.getAdviceExpression()).thenReturn(adviceExList); when(mockAdviceEx.getAttributeAssignmentExpression()).thenReturn(null); when(mockAdviceEx.getAppliesTo()).thenReturn(effectTypePermit); processor.rule(mockRuleType); verify(mockRuleType, atLeast(1)).getEffect(); verify(mockRuleType, atLeast(1)).getRuleId(); verify(mockRuleType, atLeast(1)).getTarget(); verify(mockRuleType, atLeast(1)).getCondition(); verify(mockRuleType, atLeast(1)).getObligationExpressions(); verify(mockRuleType, atLeast(1)).getAdviceExpressions(); verify(mockTargetType, atLeast(1)).getAnyOf(); verify(mockAdviceExsType, atLeast(1)).getAdviceExpression(); verify(mockAdviceEx, atLeast(1)).getAttributeAssignmentExpression(); verify(mockAdviceEx, atLeast(1)).getAppliesTo(); when(mockTargetType.getAnyOf()).thenReturn(anyOfList); processor.rule(mockRuleType); verify(mockTargetType, atLeast(1)).getAnyOf(); }
@Test public void testHtmlProcessorRuleNonNullObjects() { PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); RuleType mockRuleType = Mockito.mock(RuleType.class); TargetType mockTargetType = Mockito.mock(TargetType.class); AdviceExpressionsType mockAdvice = Mockito.mock(AdviceExpressionsType.class); ObligationExpressionsType mockObEx = Mockito.mock(ObligationExpressionsType.class); AdviceExpressionType adviceExpTypeMock = Mockito.mock(AdviceExpressionType.class); ObligationExpressionType mockObExType = Mockito.mock(ObligationExpressionType.class); EffectType effectTypePermit = EffectType.PERMIT; processor = new HtmlProcessor(temp, mockPolicySetType); List<AnyOfType> anyOfList = new ArrayList<AnyOfType>(); anyOfList.add(new AnyOfType()); List<AdviceExpressionType> adviceList = new ArrayList<AdviceExpressionType>(); adviceList.add(adviceExpTypeMock); List<AttributeAssignmentExpressionType> attrList = new ArrayList<AttributeAssignmentExpressionType>(); List<ObligationExpressionType> obExList = new ArrayList<ObligationExpressionType>(); obExList.add(mockObExType); List<Object> contentList = new ArrayList<>(); contentList.add(new Object()); when(mockRuleType.getRuleId()).thenReturn(""); when(mockRuleType.getTarget()).thenReturn(mockTargetType); when(mockRuleType.getEffect()).thenReturn(effectTypePermit); when(mockTargetType.getAnyOf()).thenReturn(anyOfList); when(mockRuleType.getAdviceExpressions()).thenReturn(mockAdvice); when(mockAdvice.getAdviceExpression()).thenReturn(adviceList); when(mockRuleType.getObligationExpressions()).thenReturn(mockObEx); when(mockObEx.getObligationExpression()).thenReturn(obExList); when(mockObExType.getAttributeAssignmentExpression()).thenReturn(null); when(mockObExType.getFulfillOn()).thenReturn(effectTypePermit); when(adviceExpTypeMock.getAdviceId()).thenReturn(""); when(adviceExpTypeMock.getAppliesTo()).thenReturn(effectTypePermit); when(adviceExpTypeMock.getAttributeAssignmentExpression()).thenReturn(attrList); processor.rule(mockRuleType); verify(mockRuleType, atLeast(1)).getRuleId(); verify(mockRuleType, atLeast(1)).getTarget(); verify(mockRuleType, atLeast(1)).getEffect(); verify(mockRuleType, atLeast(1)).getAdviceExpressions(); verify(mockRuleType, atLeast(1)).getObligationExpressions(); verify(mockTargetType, atLeast(1)).getAnyOf(); verify(mockObEx, atLeast(1)).getObligationExpression(); verify(mockObExType, atLeast(1)).getAttributeAssignmentExpression(); verify(mockObExType, atLeast(1)).getFulfillOn(); verify(mockAdvice, atLeast(1)).getAdviceExpression(); verify(adviceExpTypeMock, atLeast(1)).getAdviceId(); verify(adviceExpTypeMock, atLeast(1)).getAppliesTo(); verify(adviceExpTypeMock, atLeast(1)).getAttributeAssignmentExpression(); }
@Test public void testHtmlProcessorProcessAttributeAssignments() { PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); processor = new HtmlProcessor(temp, mockPolicySetType); RuleType mockRuleType = Mockito.mock(RuleType.class); EffectType effectTypePermit = EffectType.PERMIT; ObligationExpressionsType mockOblExsType = Mockito.mock(ObligationExpressionsType.class); ObligationExpressionType mockOblExTypeListObj = Mockito.mock(ObligationExpressionType.class); List<ObligationExpressionType> oblExTypeList = new ArrayList<ObligationExpressionType>(); oblExTypeList.add(mockOblExTypeListObj); AttributeAssignmentExpressionType mockattrAssignListObj = Mockito.mock(AttributeAssignmentExpressionType.class); List<AttributeAssignmentExpressionType> attrAssignList = new ArrayList<AttributeAssignmentExpressionType>(); attrAssignList.add(mockattrAssignListObj); JAXBElement<?> jaxbElementMock = Mockito.mock(JAXBElement.class); AttributeValueType attrValTypeMock = Mockito.mock(AttributeValueType.class); AttributeDesignatorType attrDesignTypeMock = Mockito.mock(AttributeDesignatorType.class); AttributeSelectorType attrSelTypeMock = Mockito.mock(AttributeSelectorType.class); ApplyType applyTypeMock = Mockito.mock(ApplyType.class); Object genericObjectMock = Mockito.mock(Object.class); Object mockContentListObject = Mockito.mock(Object.class); List<Object> contentList = new ArrayList<Object>(); contentList.add(mockContentListObject); contentList.add(mockContentListObject); when(mockRuleType.getEffect()).thenReturn(effectTypePermit); when(mockRuleType.getObligationExpressions()).thenReturn(mockOblExsType); when(mockOblExsType.getObligationExpression()).thenReturn(oblExTypeList); when(mockOblExTypeListObj.getAttributeAssignmentExpression()).thenReturn(attrAssignList); when(mockOblExTypeListObj.getFulfillOn()).thenReturn(effectTypePermit); when(mockattrAssignListObj.getCategory()).thenReturn(""); when(mockattrAssignListObj.getAttributeId()).thenReturn(""); doReturn(jaxbElementMock).when(mockattrAssignListObj).getExpression(); doReturn(attrValTypeMock).when(jaxbElementMock).getValue(); when(attrValTypeMock.getContent()).thenReturn(contentList); processor.rule(mockRuleType); verify(mockRuleType, atLeast(1)).getEffect(); verify(mockRuleType, atLeast(1)).getObligationExpressions(); verify(mockOblExsType, atLeast(1)).getObligationExpression(); verify(mockOblExTypeListObj, atLeast(1)).getAttributeAssignmentExpression(); verify(mockOblExTypeListObj, atLeast(1)).getFulfillOn(); verify(mockattrAssignListObj, atLeast(1)).getExpression(); verify(jaxbElementMock, atLeast(1)).getValue(); verify(attrValTypeMock, atLeast(1)).getContent(); doReturn(attrDesignTypeMock).when(jaxbElementMock).getValue(); processor.rule(mockRuleType); verify(jaxbElementMock, atLeast(1)).getValue(); doReturn(attrSelTypeMock).when(jaxbElementMock).getValue(); processor.rule(mockRuleType); verify(jaxbElementMock, atLeast(1)).getValue(); doReturn(applyTypeMock).when(jaxbElementMock).getValue(); processor.rule(mockRuleType); verify(jaxbElementMock, atLeast(1)).getValue(); doReturn(genericObjectMock).when(jaxbElementMock).getValue(); processor.rule(mockRuleType); verify(jaxbElementMock, atLeast(1)).getValue(); }
@Test public void testHtmlProcessorStringifyExpression() { PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class); processor = new HtmlProcessor(temp, mockPolicySetType); RuleType mockRuleType = Mockito.mock(RuleType.class); ConditionType mockConditionType = Mockito.mock(ConditionType.class); JAXBElement<?> mockJAXBElement = Mockito.mock(JAXBElement.class); EffectType effectTypePermit = EffectType.PERMIT; Object mockExpressObject = Mockito.mock(Object.class); AttributeDesignatorType mockAttrDesType = Mockito.mock(AttributeDesignatorType.class); AttributeSelectorType mockAttrSelType = Mockito.mock(AttributeSelectorType.class); AttributeValueType mockAttrValType = Mockito.mock(AttributeValueType.class); VariableReferenceType mockVarRefType = Mockito.mock(VariableReferenceType.class); Object mockContentObject = Mockito.mock(Object.class); List<Object> contentList = new ArrayList<Object>(); contentList.add(mockContentObject); when(mockRuleType.getEffect()).thenReturn(effectTypePermit); when(mockRuleType.getCondition()).thenReturn(mockConditionType); doReturn(mockJAXBElement).when(mockConditionType).getExpression(); doReturn(mockExpressObject).when(mockJAXBElement).getValue(); try { processor.rule(mockRuleType); fail(); } catch (IllegalArgumentException e) { verify(mockRuleType, atLeast(1)).getEffect(); verify(mockRuleType, atLeast(1)).getCondition(); verify(mockConditionType, atLeast(1)).getExpression(); verify(mockJAXBElement, atLeast(1)).getValue(); } doReturn(mockAttrDesType).when(mockJAXBElement).getValue(); when(mockAttrDesType.getCategory()).thenReturn(""); when(mockAttrDesType.getAttributeId()).thenReturn(""); when(mockAttrDesType.getDataType()).thenReturn(""); processor.rule(mockRuleType); verify(mockJAXBElement, atLeast(1)).getValue(); verify(mockAttrDesType, atLeast(1)).getCategory(); verify(mockAttrDesType, atLeast(1)).getAttributeId(); verify(mockAttrDesType, atLeast(1)).getDataType(); doReturn(mockAttrSelType).when(mockJAXBElement).getValue(); when(mockAttrSelType.getPath()).thenReturn("SamplePath/text()"); processor.rule(mockRuleType); verify(mockJAXBElement, atLeast(1)).getValue(); verify(mockAttrSelType, atLeast(1)).getPath(); when(mockAttrSelType.getPath()).thenReturn(""); processor.rule(mockRuleType); verify(mockJAXBElement, atLeast(1)).getValue(); verify(mockAttrSelType, atLeast(1)).getPath(); doReturn(mockAttrValType).when(mockJAXBElement).getValue(); when(mockAttrValType.getContent()).thenReturn(contentList); processor.rule(mockRuleType); verify(mockJAXBElement, atLeast(1)).getValue(); verify(mockAttrValType, atLeast(1)).getContent(); doReturn(mockVarRefType).when(mockJAXBElement).getValue(); processor.rule(mockRuleType); verify(mockJAXBElement, atLeast(1)).getValue(); }