比较两个树状图的内容

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

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

  1. public void theseRulesAreTheSame() {
  2. List<String> failures = new ArrayList<>();
  3. TreeMap<String, NSG> configNsgs = platformConfiguration.getAzure().nsgs();
  4. configNsgs.forEach((name, nsg) -> {
  5. assertThat(azureAdapter.doesNsgExistInAzure(name))
  6. .as("Unable to find network security group " + name + " in Azure.").isTrue();
  7. List<SecurityRulesItem> configSecurityRules = nsg.getSecurityRules();
  8. TreeMap<String, Object> azureSecurityRules = azureAdapter
  9. .getSecurityRulesForNsg(name);
  10. assertThat(configSecurityRules.size())
  11. .as("The nymber of security rules in Azure does not correspond to the number of security rules in the configuration!")
  12. .isEqualTo(azureSecurityRules.size());
  13. configSecurityRules.forEach(configSecurityRule -> {
  14. SecurityRuleInner azureSecurityRule = (SecurityRuleInner) azureSecurityRules
  15. .get(configSecurityRule.getRuleName());
  16. logger.info(
  17. "Checking security rule " + configSecurityRule.getRuleName()
  18. + " in network security group "
  19. + nsg.getName());
  20. if (null == azureSecurityRule) {
  21. logFailure(failures, null, configSecurityRule.getRuleName());
  22. } else {
  23. if (!azureSecurityRule.access().toString().equalsIgnoreCase(configSecurityRule.getAccess())) {
  24. logFailure(failures, configSecurityRule.getAccess(), azureSecurityRule.access());
  25. }
  26. if (!azureSecurityRule.destinationAddressPrefix().equalsIgnoreCase(configSecurityRule.getDestinationAddressPrefix())) {
  27. logFailure(failures, configSecurityRule.getDestinationAddressPrefix(), azureSecurityRule.destinationAddressPrefix());
  28. }
  29. if (!azureSecurityRule.destinationPortRange().equalsIgnoreCase(configSecurityRule.getDestinationPortRange())) {
  30. logFailure(failures, configSecurityRule.getDestinationPortRange(), azureSecurityRule.destinationPortRange());
  31. }
  32. if (!azureSecurityRule.sourceAddressPrefix().equalsIgnoreCase(configSecurityRule.getSourceAddressPrefix())) {
  33. logFailure(failures, configSecurityRule.getSourceAddressPrefix(), azureSecurityRule.sourceAddressPrefix());
  34. }
  35. if (!azureSecurityRule.sourcePortRange().equalsIgnoreCase(configSecurityRule.getSourcePortRange())) {
  36. logFailure(failures, configSecurityRule.getSourcePortRange(), azureSecurityRule.sourcePortRange());
  37. }
  38. if (!azureSecurityRule.protocol().toString().equalsIgnoreCase(configSecurityRule.getProtocol())) {
  39. logFailure(failures, configSecurityRule.getProtocol(), azureSecurityRule.protocol());
  40. }
  41. if (!azureSecurityRule.direction().toString().equalsIgnoreCase(configSecurityRule.getDirection())) {
  42. logFailure(failures, configSecurityRule.getDirection(), azureSecurityRule.direction());
  43. }
  44. }
  45. });
  46. });
  47. if (!failures.isEmpty()) {
  48. Assertions.fail(
  49. "Error(s) detected while comparing the network security groups between Azure and the config. Failures: "
  50. + failures);
  51. }
  52. }

提前谢谢

baubqpgj

baubqpgj1#

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

  1. BiConsumer<AzureSecurityRule, ConfigSecurityRule> compareField(Function<AzureSecurityRule,String> f1, Function<ConfigSecurityRule> f2) {
  2. return (az, cf) -> {
  3. if !f1.apply(az).equalsIgnoreCase(f2.apply(cf)) {
  4. logFailure(failure, f2.apply(cf), f1.apply(az));
  5. }
  6. }
  7. }
  8. ...
  9. List.of(
  10. compareField(az -> az.access().toString(), cf -> cf.getAccess()),
  11. compareField(az -> az.destinationAddressPrefix(), cf -> cf.getDestinationAddressPrefix()),
  12. ...
  13. ).forEach(cf -> cf.accept(azureSecurityRule, configSecurityRule));

相关问题