001package de.cuioss.test.jsf.validator; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.stream.Collectors; 006 007import javax.faces.application.FacesMessage; 008import javax.faces.application.FacesMessage.Severity; 009import javax.faces.validator.ValidatorException; 010 011/** 012 * TestData Store for Test Items which will be used by 013 * {@link AbstractValidatorTest}-<br> 014 * Class is prepared to be used as Fluent Interface 015 * 016 * @param <T> type of Test Item value 017 */ 018public class TestItems<T> { 019 020 private final List<TestItem<T>> localItems = new ArrayList<>(); 021 022 /** 023 * Add to TestData Store a Test item which must fail with 024 * {@link ValidatorException} 025 * 026 * @param value T invalid value which should cause a {@link ValidatorException} 027 * @return TestItems reference to this object 028 */ 029 public TestItems<T> addInvalid(final T value) { 030 return this.addItem(false, value, FacesMessage.SEVERITY_ERROR, null); 031 } 032 033 /** 034 * Add to TestData Store a Test item which must fail with 035 * {@link ValidatorException} 036 * 037 * @param value T invalid value which should cause a 038 * {@link ValidatorException} 039 * @param message which should be set within the {@link ValidatorException} 040 * @return TestItems reference to this object 041 */ 042 public TestItems<T> addInvalidWithMessage(final T value, final String message) { 043 return this.addItem(false, value, FacesMessage.SEVERITY_ERROR, message); 044 } 045 046 /** 047 * Add to TestData Store a Test item which must fail with 048 * {@link ValidatorException} 049 * 050 * @param value T invalid value which should cause a {@link ValidatorException} 051 * @return TestItems reference to this object 052 */ 053 public TestItems<T> addValid(final T value) { 054 return this.addItem(true, value, null, null); 055 } 056 057 /** 058 * Add to TestData Store a Test item 059 * 060 * @param valid indicating whether it is a valid or invalid item 061 * @param value T value to be validated 062 * @param level {@link Severity} represent message severity, usually 063 * {@link FacesMessage#SEVERITY_ERROR} 064 * @param message which should be set within the {@link ValidatorException} 065 * @return TestItems reference to this object 066 */ 067 private TestItems<T> addItem(final boolean valid, final T value, final Severity level, final String message) { 068 final var item = new TestItem<T>(); 069 item.setTestValue(value); 070 item.setValid(valid); 071 item.setErrorMessage(message); 072 item.setSeverity(level); 073 localItems.add(item); 074 return this; 075 } 076 077 /** 078 * @return all valid test-items 079 */ 080 List<TestItem<T>> allValid() { 081 return localItems.stream().filter(TestItem::isValid).collect(Collectors.toList()); 082 } 083 084 /** 085 * @return all invalid test-items 086 */ 087 List<TestItem<T>> allInvalid() { 088 return localItems.stream().filter(item -> !item.isValid()).collect(Collectors.toList()); 089 } 090}