001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.converter;
003
004import java.util.ArrayList;
005import java.util.Arrays;
006import java.util.HashSet;
007import java.util.List;
008import java.util.Set;
009import javax.faces.application.FacesMessage;
010import javax.faces.application.FacesMessage.Severity;
011import javax.faces.convert.Converter;
012import javax.faces.convert.ConverterException;
013
014/**
015 * TestData Store for Test Items which will be used by
016 * {@link AbstractConverterTest}<br>
017 * Class is prepared to be used as Fluent Interface
018 *
019 * @param <T> type of Test Item value
020 */
021public class TestItems<T> {
022    /**
023     * Items that are used for testing
024     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
025     */
026    private final List<ConverterTestItem<T>> validObjectTestItems = new ArrayList<>();
027    /**
028     * Items that are used for testing
029     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
030     * but are invalid {@link Object}s
031     */
032    private final List<ConverterTestItem<T>> invalidObjectTestItems = new ArrayList<>();
033    /**
034     * Items that are used for testing
035     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
036     */
037    private final List<ConverterTestItem<T>> validStringTestItems = new ArrayList<>();
038    /**
039     * Items that are used for testing
040     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
041     */
042    private final List<ConverterTestItem<T>> invalidStringTestItems = new ArrayList<>();
043    private final Set<String> roundtripValues = new HashSet<>();
044
045    /**
046     * Adds roundtrip String values to be tested. See
047     * {@link AbstractConverterTest#shouldRoundTripValidData()}
048     *
049     * @param roundtripValue the values to be roundtrip converted
050     * @return TestItems reference to this object
051     */
052    public TestItems<T> addRoundtripValues(final String... roundtripValue) {
053        roundtripValues.addAll(Arrays.asList(roundtripValue));
054        return this;
055    }
056
057    /**
058     * Adds testData to be used for testing
059     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
060     * Test item must fail with {@link ConverterException}
061     *
062     * @param value T invalid value which should cause a {@link ConverterException}
063     * @return TestItems reference to this object
064     */
065    public TestItems<T> addInvalidObject(final T value) {
066        return this.addinValidObjectTestItem(value, null, FacesMessage.SEVERITY_ERROR, null);
067    }
068
069    /**
070     * Adds testData to be used for testing
071     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
072     * Test item must fail with {@link ConverterException}
073     *
074     * @param value   T invalid value which should cause a
075     *                {@link ConverterException}
076     * @param message which should be set within the {@link ConverterException}
077     * @return TestItems reference to this object
078     */
079    public TestItems<T> addInvalidObjectWithMessage(final T value, final String message) {
080        return this.addinValidObjectTestItem(value, null, FacesMessage.SEVERITY_ERROR, message);
081    }
082
083    /**
084     * Adds testData to be used for testing
085     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
086     * Test item should pass without {@link ConverterException}
087     *
088     * @param value valid value which should cause a {@link ConverterException}
089     * @return TestItems reference to this object
090     */
091    public TestItems<T> addValidObject(final T value) {
092        return this.addValidObjectTestItem(value, null, null, null);
093    }
094
095    /**
096     * Adds testData to be used for testing
097     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
098     * Test item should pass without {@link ConverterException} and the result
099     * should be the same as the given converterResult
100     *
101     * @param value           valid value which should cause a
102     *                        {@link ConverterException}
103     * @param converterResult the String to be returned by
104     *                        {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
105     * @return TestItems reference to this object
106     */
107    public TestItems<T> addValidObjectWithStringResult(final T value, final String converterResult) {
108        return this.addValidObjectTestItem(value, converterResult, null, null);
109    }
110
111    /**
112     * Adds testData to be used for testing
113     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
114     * Test item must fail with {@link ConverterException}
115     *
116     * @param value invalid String-value which should cause a
117     *              {@link ConverterException}
118     * @return TestItems reference to this object
119     */
120    public TestItems<T> addInvalidString(final String value) {
121        return this.addStringTestItem(false, null, value, FacesMessage.SEVERITY_ERROR, null);
122    }
123
124    /**
125     * Adds testData to be used for testing
126     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
127     * Test item must fail with {@link ConverterException}
128     *
129     * @param value   invalid String-value which should cause a
130     *                {@link ConverterException}
131     * @param message which should be set within the {@link ConverterException}
132     * @return TestItems reference to this object
133     */
134    public TestItems<T> addInvalidStringWithMessage(final String value, final String message) {
135        return this.addStringTestItem(false, null, value, FacesMessage.SEVERITY_ERROR, message);
136    }
137
138    /**
139     * Adds testData to be used for testing
140     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
141     * Test item should pass without {@link ConverterException}
142     *
143     * @param value valid String-value which should pass without
144     *              {@link ConverterException}
145     * @return TestItems reference to this object
146     */
147    public TestItems<T> addValidString(final String value) {
148        return this.addStringTestItem(true, null, value, null, null);
149    }
150
151    /**
152     * Adds testData to be used for testing
153     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
154     * Test item should pass without {@link ConverterException} and the result
155     * should be the same as the given converterResult
156     *
157     * @param value           valid String-value which should pass without
158     *                        {@link ConverterException}
159     * @param converterResult the String to be returned by
160     *                        {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
161     * @return TestItems reference to this object
162     */
163    public TestItems<T> addValidStringWithObjectResult(final String value, final T converterResult) {
164        return this.addStringTestItem(true, converterResult, value, null, null);
165    }
166
167    /**
168     * Adds testData to be used for testing
169     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
170     * that defines a valid Object
171     *
172     * @param value           T value to be validated
173     * @param converterResult the String to be returned by
174     *                        {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
175     * @param level           {@link Severity} represent message severity, usually
176     *                        {@link FacesMessage#SEVERITY_ERROR}
177     * @param message         which should be set within the
178     *                        {@link ConverterException}
179     * @return TestItems reference to this object
180     */
181    private TestItems<T> addValidObjectTestItem(final T value, final String converterResult, final Severity level, final String message) {
182        final var item = new ConverterTestItem<T>();
183        item.setTestValue(value);
184        item.setStringValue(converterResult);
185        item.setValid(true);
186        item.setErrorMessage(message);
187        item.setSeverity(level);
188        validObjectTestItems.add(item);
189        return this;
190    }
191
192    /**
193     * Adds testData to be used for testing
194     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
195     * that defines an invalid Object
196     *
197     * @param value           T value to be validated
198     * @param converterResult the String to be returned by
199     *                        {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
200     * @param level           {@link Severity} represent message severity, usually
201     *                        {@link FacesMessage#SEVERITY_ERROR}
202     * @param message         which should be set within the
203     *                        {@link ConverterException}
204     * @return TestItems reference to this object
205     */
206    private TestItems<T> addinValidObjectTestItem(final T value, final String converterResult, final Severity level, final String message) {
207        final var item = new ConverterTestItem<T>();
208        item.setTestValue(value);
209        item.setStringValue(converterResult);
210        item.setValid(false);
211        item.setErrorMessage(message);
212        item.setSeverity(level);
213        invalidObjectTestItems.add(item);
214        return this;
215    }
216
217    /**
218     * Adds testData to be used for testing
219     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
220     *
221     * @param valid           indicating whether it is a valid or invalid item
222     * @param value           T value to be validated
223     * @param converterResult the String to be returned by
224     *                        {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
225     * @param level           {@link Severity} represent message severity, usually
226     *                        {@link FacesMessage#SEVERITY_ERROR}
227     * @param message         which should be set within the
228     *                        {@link ConverterException}
229     * @return TestItems reference to this object
230     */
231    private TestItems<T> addStringTestItem(final boolean valid, final T value, final String converterResult, final Severity level, final String message) {
232        final var item = new ConverterTestItem<T>();
233        item.setTestValue(value);
234        item.setStringValue(converterResult);
235        item.setValid(valid);
236        item.setErrorMessage(message);
237        item.setSeverity(level);
238        if (valid) {
239            validStringTestItems.add(item);
240        } else {
241            invalidStringTestItems.add(item);
242        }
243        return this;
244    }
245
246    /**
247     * Items that are used for testing
248     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
249     */
250    @java.lang.SuppressWarnings("all")
251    @lombok.Generated
252    List<ConverterTestItem<T>> getValidObjectTestItems() {
253        return this.validObjectTestItems;
254    }
255
256    /**
257     * Items that are used for testing
258     * {@link Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, Object)}
259     * but are invalid {@link Object}s
260     */
261    @java.lang.SuppressWarnings("all")
262    @lombok.Generated
263    List<ConverterTestItem<T>> getInvalidObjectTestItems() {
264        return this.invalidObjectTestItems;
265    }
266
267    /**
268     * Items that are used for testing
269     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
270     */
271    @java.lang.SuppressWarnings("all")
272    @lombok.Generated
273    List<ConverterTestItem<T>> getValidStringTestItems() {
274        return this.validStringTestItems;
275    }
276
277    /**
278     * Items that are used for testing
279     * {@link Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, String)}
280     */
281    @java.lang.SuppressWarnings("all")
282    @lombok.Generated
283    List<ConverterTestItem<T>> getInvalidStringTestItems() {
284        return this.invalidStringTestItems;
285    }
286
287    @java.lang.SuppressWarnings("all")
288    @lombok.Generated
289    Set<String> getRoundtripValues() {
290        return this.roundtripValues;
291    }
292}