比较两个树状图的内容

5cg8jx4n  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(275)

我想比较两个树形图。我现在已经把它写下来了,如下所示,但我觉得这可以写得更有效。我试着寻找比较器,但我不认为这是我可以用于我的用例的东西。
这些Map是树Map,因为键必须不区分大小写。

public void theseRulesAreTheSame() {
    List<String> failures = new ArrayList<>();

    TreeMap<String, NSG> configNsgs = platformConfiguration.getAzure().nsgs();
    configNsgs.forEach((name, nsg) -> {
      assertThat(azureAdapter.doesNsgExistInAzure(name))
          .as("Unable to find network security group " + name + " in Azure.").isTrue();

      List<SecurityRulesItem> configSecurityRules = nsg.getSecurityRules();
      TreeMap<String, Object> azureSecurityRules = azureAdapter
          .getSecurityRulesForNsg(name);

      assertThat(configSecurityRules.size())
          .as("The nymber of security rules in Azure does not correspond to the number of security rules in the configuration!")
          .isEqualTo(azureSecurityRules.size());

      configSecurityRules.forEach(configSecurityRule -> {
        SecurityRuleInner azureSecurityRule = (SecurityRuleInner) azureSecurityRules
            .get(configSecurityRule.getRuleName());

        logger.info(
            "Checking security rule " + configSecurityRule.getRuleName()
                + " in network security group "
                + nsg.getName());

        if (null == azureSecurityRule) {
            logFailure(failures, null, configSecurityRule.getRuleName());
        } else {
          if (!azureSecurityRule.access().toString().equalsIgnoreCase(configSecurityRule.getAccess())) {
            logFailure(failures, configSecurityRule.getAccess(), azureSecurityRule.access());
          }
          if (!azureSecurityRule.destinationAddressPrefix().equalsIgnoreCase(configSecurityRule.getDestinationAddressPrefix())) {
            logFailure(failures, configSecurityRule.getDestinationAddressPrefix(), azureSecurityRule.destinationAddressPrefix());
          }
          if (!azureSecurityRule.destinationPortRange().equalsIgnoreCase(configSecurityRule.getDestinationPortRange())) {
            logFailure(failures, configSecurityRule.getDestinationPortRange(), azureSecurityRule.destinationPortRange());
          }
          if (!azureSecurityRule.sourceAddressPrefix().equalsIgnoreCase(configSecurityRule.getSourceAddressPrefix())) {
            logFailure(failures, configSecurityRule.getSourceAddressPrefix(), azureSecurityRule.sourceAddressPrefix());
          }
          if (!azureSecurityRule.sourcePortRange().equalsIgnoreCase(configSecurityRule.getSourcePortRange())) {
            logFailure(failures, configSecurityRule.getSourcePortRange(), azureSecurityRule.sourcePortRange());
          }
          if (!azureSecurityRule.protocol().toString().equalsIgnoreCase(configSecurityRule.getProtocol())) {
            logFailure(failures, configSecurityRule.getProtocol(), azureSecurityRule.protocol());
          }
          if (!azureSecurityRule.direction().toString().equalsIgnoreCase(configSecurityRule.getDirection())) {
            logFailure(failures, configSecurityRule.getDirection(), azureSecurityRule.direction());
          }
        }
      });
    });
    if (!failures.isEmpty()) {
      Assertions.fail(
          "Error(s) detected while comparing the network security groups between Azure and the config. Failures: "
              + failures);
    }
  }

提前谢谢

baubqpgj

baubqpgj1#

如果我们有两种类型 AzureSecurityRuleConfigSecurityRule 我们可以使比较不那么冗长,如下所示:

BiConsumer<AzureSecurityRule, ConfigSecurityRule>  compareField(Function<AzureSecurityRule,String> f1, Function<ConfigSecurityRule> f2) {
return (az, cf) -> {
    if !f1.apply(az).equalsIgnoreCase(f2.apply(cf)) {
      logFailure(failure, f2.apply(cf), f1.apply(az));
    }
  }
}
...
List.of(
  compareField(az -> az.access().toString(), cf -> cf.getAccess()),
  compareField(az -> az.destinationAddressPrefix(), cf -> cf.getDestinationAddressPrefix()),
  ...
).forEach(cf -> cf.accept(azureSecurityRule, configSecurityRule));

相关问题