### Question:
PolicyValidator implements RabbitClusterValidator { @Override public List<String> validate(final ClusterSpec clusterSpec) { final List<PolicySpec> policies = clusterSpec.getPolicies(); final List<String> errors = new ArrayList<>(); policies.forEach(policy -> { try { Pattern.compile(policy.getPattern()); } catch (final PatternSyntaxException ex) { errors.add(String.format("Invalid pattern for policy %s: %s", policy.getName(), ex.getMessage())); } try { Policy.ApplyTo.fromValue(policy.getApplyTo()); } catch (final IllegalArgumentException ex) { errors.add(String.format("Invalid applyTo for policy %s: %s", policy.getName(), ex.getMessage())); } }); return errors; }  @Override List<String> validate(final ClusterSpec clusterSpec);  }### Answer:
@Test public void testValidate_validSpec() { final List<PolicySpec> policies = Lists.newArrayList( new PolicySpec("vhost1", "name1", "pattern", "queues", POLICY_DEFINITION, 1L), new PolicySpec(" new PolicySpec(" ); final List<String> errors = validator.validate(buildClusterSpec(policies)); assertTrue(errors.isEmpty()); }
@Test public void testValidate_invalidPattern() { final List<PolicySpec> policies = Lists.newArrayList( new PolicySpec("vhost1", "name1", "[[[[[[", "queues", POLICY_DEFINITION, 1L) ); final List<String> errors = validator.validate(buildClusterSpec(policies)); assertEquals(1, errors.size()); assertTrue(errors.get(0).contains("pattern")); }
@Test public void testValidate_invalidApplyTo() { final List<PolicySpec> policies = Lists.newArrayList( new PolicySpec("vhost1", "name1", "pattern", "blah", POLICY_DEFINITION, 1L) ); final List<String> errors = validator.validate(buildClusterSpec(policies)); assertEquals(1, errors.size()); assertTrue(errors.get(0).contains("applyTo")); }
@Test public void testValidate_multipleInvalid() { final List<PolicySpec> policies = Lists.newArrayList( new PolicySpec("vhost1", "name1", "[[[[[[", "queues", POLICY_DEFINITION, 1L), new PolicySpec("vhost1", "name1", "pattern", "blah", POLICY_DEFINITION, 1L) ); final List<String> errors = validator.validate(buildClusterSpec(policies)); assertEquals(2, errors.size()); }