CartValidator implements StorageUnitValidator { @Override public ValidationResult validate(RegisterNew registerNew) { Stream<String> notExpected = scanned(registerNew) .filter(Scanned::isViolated) .map(Scanned::violation); Stream<String> notFound = notFound(registerNew) .map(this::notFoundMessage); return new ValidationResult( Stream.concat(notFound, notExpected) .collect(Collectors.toSet()) ); } CartValidator(String refNo, List<String> requiredProducts); @Override ValidationResult validate(RegisterNew registerNew);  }
@Test public void noViolationsForValidCart() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900901", "900902", "900903", "900904"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()).isEmpty(); }
@Test public void singleProductMissingInCart() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900902", "900903", "900904"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains("refNo 900901 expected in pocket 1, but not found"); }
@Test public void multipleProductMissingInCart() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900901", "900903"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains( "refNo 900904 expected in pocket 4, but not found", "refNo 900902 expected in pocket 2, but not found"); }
@Test public void notExpectedProductInCart() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900901", "9009XXXX", "900902", "900903", "900904"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains("not expected refNo 9009XXXX"); }
@Test public void notExpectedMultipleProductsInCart() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900901", "9009XXXX", "900902", "900903", "900904", "9009YYYY"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains( "not expected refNo 9009XXXX", "not expected refNo 9009YYYY"); }
@Test public void someProductMissingSomeProductNotExpected() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("9009XXXX", "900902", "900903", "900904"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains( "not expected refNo 9009XXXX", "refNo 900901 expected in pocket 1, but not found"); }
@Test public void someProductExpectedInOtherPocket() throws Exception { requiredProductsInCart("900901", "900902", "900903", "900904"); RegisterNew command = givenCartRegistrationCommand("900902", "900901", "900903", "900904"); ValidationResult result = validator.validate(command); Assertions.assertThat(result.getViolations()) .contains( "refNo 900901 expected in pocket 1, but found in 2", "refNo 900902 expected in pocket 2, but found in 1"); }