我想比较两个树形图。我现在已经把它写下来了,如下所示,但我觉得这可以写得更有效。我试着寻找比较器,但我不认为这是我可以用于我的用例的东西。
这些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);
}
}
提前谢谢
1条答案
按热度按时间baubqpgj1#
如果我们有两种类型
AzureSecurityRule
及ConfigSecurityRule
我们可以使比较不那么冗长,如下所示: