private void assertRuleExport(RuleBaseValues oldRule, RuleBaseValues newRule) {
    assertFalse("Ids should be different.", oldRule.getId().equals(newRule.getId()));
    assertEquals(oldRule.isActive(), newRule.isActive());
    assertEquals(
        DateUtils.round(oldRule.getActivationDate(), Calendar.DATE),
        DateUtils.round(newRule.getActivationDate(), Calendar.DATE));
    assertEquals(oldRule.getName(), newRule.getName());
    assertEquals(oldRule.getCurrentInd(), newRule.getCurrentInd());
    assertEquals(oldRule.getDeactivationDate(), newRule.getDeactivationDate());
    assertEquals(oldRule.getDelegateRule(), newRule.getDelegateRule());
    assertEquals(oldRule.getDescription(), newRule.getDescription());
    assertEquals(oldRule.getDocTypeName(), newRule.getDocTypeName());

    if (oldRule.getFromDateValue() == null) {
      assertNull(newRule.getFromDateValue());
    } else {
      assertEquals(
          DateUtils.round(oldRule.getFromDateValue(), Calendar.DATE),
          DateUtils.round(newRule.getFromDateValue(), Calendar.DATE));
    }
    if (oldRule.getToDateValue() == null) {
      assertNull(newRule.getToDateValue());
    } else {
      assertEquals(
          DateUtils.round(oldRule.getToDateValue(), Calendar.DATE),
          DateUtils.round(newRule.getToDateValue(), Calendar.DATE));
    }
    assertEquals(oldRule.getFromDateString(), newRule.getFromDateString());
    assertEquals(oldRule.getToDateString(), newRule.getToDateString());

    assertEquals(oldRule.isForceAction(), newRule.isForceAction());

    if (!oldRule.getDelegateRule().booleanValue())
      assertEquals(oldRule.getPreviousVersionId(), newRule.getPreviousVersionId());

    assertEquals(oldRule.getDocumentId(), newRule.getDocumentId());

    if (oldRule.getRuleTemplate() == null) {
      assertNull(newRule.getRuleTemplate());
    } else {
      assertEquals(oldRule.getRuleTemplate().getName(), newRule.getRuleTemplate().getName());
    }
    if (oldRule.getRuleExpressionDef() == null) {
      assertNull(newRule.getRuleExpressionDef());
    } else {
      assertEquals(
          oldRule.getRuleExpressionDef().getExpression(),
          newRule.getRuleExpressionDef().getExpression());
      assertEquals(
          oldRule.getRuleExpressionDef().getType(), newRule.getRuleExpressionDef().getType());
    }
    if (!oldRule.getDelegateRule().booleanValue())
      assertEquals(oldRule.getVersionNbr(), newRule.getVersionNbr());

    assertRuleExtensions(oldRule.getRuleExtensions(), newRule.getRuleExtensions());
    assertResponsibilities(oldRule.getRuleResponsibilities(), newRule.getRuleResponsibilities());
  }
  /**
   * Note that the assertion here will fail if you have multiple rules with the same description.
   */
  protected void assertExport() throws Exception {
    // export all existing rules and their dependencies (document types, rule templates, rule
    // attributes)
    List oldRules = KEWServiceLocator.getRuleService().fetchAllRules(true);
    assertAllRulesHaveUniqueNames(oldRules);
    List oldRuleDelegations =
        KEWServiceLocator.getRuleDelegationService().findAllCurrentRuleDelegations();
    assertAllRuleDelegationsHaveUniqueNames(oldRuleDelegations);

    KewExportDataSet dataSet = new KewExportDataSet();
    dataSet.getRules().addAll(oldRules);
    dataSet.getRuleDelegations().addAll(oldRuleDelegations);
    dataSet.getDocumentTypes().addAll(KEWServiceLocator.getDocumentTypeService().findAllCurrent());
    dataSet.getRuleTemplates().addAll(KEWServiceLocator.getRuleTemplateService().findAll());
    dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
    byte[] xmlBytes =
        CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
    assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);

    // now clear the tables
    ClearDatabaseLifecycle clearLifeCycle = new ClearDatabaseLifecycle();
    clearLifeCycle.getTablesToClear().add("KREW_RULE_T");
    clearLifeCycle.getTablesToClear().add("KREW_RULE_RSP_T");
    clearLifeCycle.getTablesToClear().add("KREW_DLGN_RSP_T");
    clearLifeCycle.getTablesToClear().add("KREW_RULE_ATTR_T");
    clearLifeCycle.getTablesToClear().add("KREW_RULE_TMPL_T");
    clearLifeCycle.getTablesToClear().add("KREW_DOC_TYP_T");
    clearLifeCycle.start();
    new ClearCacheLifecycle().stop();

    // import the exported xml
    loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));

    List newRules = KEWServiceLocator.getRuleService().fetchAllRules(true);
    assertEquals("Should have same number of old and new Rules.", oldRules.size(), newRules.size());
    for (Iterator iterator = oldRules.iterator(); iterator.hasNext(); ) {
      RuleBaseValues oldRule = (RuleBaseValues) iterator.next();
      boolean foundRule = false;
      for (Iterator iterator2 = newRules.iterator(); iterator2.hasNext(); ) {
        RuleBaseValues newRule = (RuleBaseValues) iterator2.next();
        if (oldRule.getDescription().equals(newRule.getDescription())) {
          assertRuleExport(oldRule, newRule);
          foundRule = true;
        }
      }
      assertTrue(
          "Could not locate the new rule for description " + oldRule.getDescription(), foundRule);
    }

    List newRuleDelegations =
        KEWServiceLocator.getRuleDelegationService().findAllCurrentRuleDelegations();
    assertDelegations(oldRuleDelegations, newRuleDelegations);
  }
 private void assertAllRulesHaveUniqueNames(List rules) throws Exception {
   Set<String> ruleDescriptions = new HashSet<String>();
   for (Iterator iterator = rules.iterator(); iterator.hasNext(); ) {
     RuleBaseValues rule = (RuleBaseValues) iterator.next();
     assertFalse(
         "Found 2 rules with the same description '"
             + rule.getDescription()
             + "'.  "
             + "In order for this test to work, all rules in the configuration files must have unique descriptions.",
         ruleDescriptions.contains(rule.getDescription()));
     ruleDescriptions.add(rule.getDescription());
   }
 }
 private void exportDefaults(Element parent, RuleTemplateBo ruleTemplate) {
   RuleBaseValues defaultRuleValues =
       KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getId());
   if (defaultRuleValues != null) {
     RuleDelegationBo defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
     Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
     if (defaultDelegationValues != null) {
       renderer.renderTextElement(
           defaultsElement,
           DELEGATION_TYPE,
           defaultDelegationValues.getDelegationType().getCode());
     }
     renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
     if (defaultRuleValues.getFromDateValue() != null) {
       renderer.renderDateElement(
           defaultsElement, FROM_DATE, defaultRuleValues.getFromDateValue());
     }
     if (defaultRuleValues.getToDateValue() != null) {
       renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDateValue());
     }
     renderer.renderBooleanElement(
         defaultsElement, FORCE_ACTION, defaultRuleValues.isForceAction(), false);
     renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.isActive(), true);
     if (defaultDelegationValues == null) {
       RuleTemplateOptionBo defaultActionOption = ruleTemplate.getDefaultActionRequestValue();
       RuleTemplateOptionBo supportsComplete = ruleTemplate.getComplete();
       RuleTemplateOptionBo supportsApprove = ruleTemplate.getApprove();
       RuleTemplateOptionBo supportsAck = ruleTemplate.getAcknowledge();
       RuleTemplateOptionBo supportsFYI = ruleTemplate.getFyi();
       if (defaultActionOption != null) {
         String defaultActionValue =
             (defaultActionOption == null ? null : defaultActionOption.getValue());
         renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue);
       }
       if (supportsComplete != null) {
         String supportsCompleteValue = supportsComplete.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue);
       }
       if (supportsApprove != null) {
         String supportsApproveValue = supportsApprove.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue);
       }
       if (supportsAck != null) {
         String supportsAckValue = supportsAck.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue);
       }
       if (supportsFYI != null) {
         String supportsFYIValue = supportsFYI.getValue();
         renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue);
       }
     }
   }
 }