### Question:
BlueprintValidatorUtil { public static void validateHostGroupCardinality(Set<HostGroup> hostGroups, Map<String, InstanceCount> blueprintHostGroupCardinality) { List<String> failures = new LinkedList<>(); for (HostGroup hostGroup : hostGroups) { InstanceCount requiredCount = blueprintHostGroupCardinality.get(hostGroup.getName()); int count = hostGroup.getInstanceGroup().getNodeCount(); if (!requiredCount.isAcceptable(count)) { failures.add(String.format("(group: '%s', required: %s, actual: %d)", hostGroup.getName(), requiredCount, count)); } } if (!failures.isEmpty()) { throw new BlueprintValidationException("Node count for the following host groups does not meet requirements: " + String.join(", ", failures)); } } private  BlueprintValidatorUtil(); static void validateHostGroupsMatch(Set<HostGroup> hostGroupsInRequest, Set<String> hostGroupsInBlueprint); static void validateInstanceGroups(Collection<HostGroup> hostGroups, Collection<InstanceGroup> instanceGroups); static Map<String, HostGroup> createHostGroupMap(Iterable<HostGroup> hostGroups); static void validateHostGroupCardinality(Set<HostGroup> hostGroups, Map<String, InstanceCount> blueprintHostGroupCardinality);  }### Answer:
@Test public void rejectsTooFewNodes() { Set<HostGroup> request = Set.of( hostGroup("master", 1), hostGroup("worker", 2) ); Map<String, InstanceCount> requirements = Map.of( "master", InstanceCount.EXACTLY_ONE, "worker", InstanceCount.atLeast(3) ); Exception e = assertThrows(BlueprintValidationException.class, () -> validateHostGroupCardinality(request, requirements)); assertTrue(e.getMessage().contains("worker")); assertFalse(e.getMessage().contains("master")); }
@Test public void rejectsTooManyNodes() { Set<HostGroup> request = Set.of( hostGroup("master", 2), hostGroup("worker", 4) ); Map<String, InstanceCount> requirements = Map.of( "master", InstanceCount.EXACTLY_ONE, "worker", InstanceCount.atLeast(3) ); Exception e = assertThrows(BlueprintValidationException.class, () -> validateHostGroupCardinality(request, requirements)); assertTrue(e.getMessage().contains("master")); Assert.assertFalse(e.getMessage().contains("worker")); }
@Test public void acceptsNodeCountWithinLimits() { Set<HostGroup> request = Set.of( hostGroup("master", 2), hostGroup("worker", 4) ); Map<String, InstanceCount> requirements = Map.of( "master", InstanceCount.of(1, 2), "worker", InstanceCount.atLeast(3) ); validateHostGroupCardinality(request, requirements); }