View Javadoc
1   package com.opencsv.validators;
2   
3   import com.opencsv.exceptions.CsvValidationException;
4   import org.junit.jupiter.api.Assertions;
5   import org.junit.jupiter.api.BeforeEach;
6   import org.junit.jupiter.api.DisplayName;
7   import org.junit.jupiter.api.Test;
8   import org.junit.jupiter.params.ParameterizedTest;
9   import org.junit.jupiter.params.provider.CsvSource;
10  import org.junit.jupiter.params.provider.ValueSource;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import static org.junit.jupiter.api.Assertions.*;
16  import static org.mockito.Mockito.*;
17  
18  public class LineValidatorAggregatorTest {
19      private static final String BAD = "bad";
20      private static final String AWFUL = "awful";
21      private LineValidatorAggregator aggregator;
22      private LineDoesNotHaveForbiddenString lineDoesNotHaveBadString;
23      private LineDoesNotHaveForbiddenString lineDoesNotHaveAwfulString;
24  
25      @BeforeEach
26      public void setup() {
27          aggregator = new LineValidatorAggregator();
28          lineDoesNotHaveBadString = new LineDoesNotHaveForbiddenString(BAD);
29          lineDoesNotHaveAwfulString = new LineDoesNotHaveForbiddenString(AWFUL);
30  
31      }
32  
33  
34      @DisplayName("Validate that")
35      @ParameterizedTest(name = "Line \"{0}\" should be {1} and if invalid it {2} have first forbidden string error and {3} have second forbidden string error")
36      @CsvSource({"a;b;c, valid, will not, will not",
37              "empty, valid, will not, will not",
38              "null, valid, will not, will not",
39              "awful;b;c, invalid, will, will not",
40              "a;bad;c, invalid, will not, will",
41              "awful;bad;c, invalid, will, will"})
42      public void validateLine(String line, String valid, String willHaveAwful, String willHaveBadString) {
43          aggregator.addValidator(null);
44          aggregator.addValidator(lineDoesNotHaveAwfulString);
45          aggregator.addValidator(lineDoesNotHaveBadString);
46  
47          Assertions.assertAll("Parameter data must be valid.",
48                  () -> assertNotNull(line),
49                  () -> assertFalse(line.isEmpty()),
50                  () -> assertTrue("valid".equals(valid) || "invalid".equals(valid)),
51                  () -> assertTrue("will".equals(willHaveAwful) || "will not".equals(willHaveAwful)),
52                  () -> assertTrue("will".equals(willHaveBadString) || "will not".equals(willHaveBadString))
53          );
54  
55  
56          assertEquals(aggregator.isValid(line), "valid".equals(valid));
57  
58          String testLine = preprocessLine(line);
59  
60          try {
61              aggregator.validate(testLine);
62              if ("invalid".equals(valid)) {
63                  fail("was supposed to be invalid!");
64              }
65          } catch (CsvValidationException ex) {
66              if ("valid".equals(valid)) {
67                  fail("was supposed to be valid!");
68              }
69              String exceptionMessage = ex.getMessage();
70              Assertions.assertAll("Exception message is incorrect",
71                      () -> assertEquals("will".equals(willHaveAwful), exceptionMessage.contains(lineDoesNotHaveAwfulString.getMessage()), "Supposed to have Awful message."),
72                      () -> assertEquals("will".equals(willHaveBadString), exceptionMessage.contains(lineDoesNotHaveBadString.getMessage()), "Supposed to have Bad message."));
73          }
74      }
75  
76      private String preprocessLine(String line) {
77          switch (line) {
78              case "null":
79                  return null;
80              case "empty":
81                  return "";
82              default:
83                  return line;
84          }
85      }
86  
87      @DisplayName("Everything is valid with no validators.")
88      @ParameterizedTest(name = "Line \"{0}\" is valid")
89      @ValueSource(strings = {"a;b;c", "awful;b;c", "a;bad;c", "awful;bad;c", "empty", "null"})
90      public void noValidators(String line) {
91  
92          Assertions.assertAll("Parameter data must be valid.",
93                  () -> assertNotNull(line),
94                  () -> assertFalse(line.isEmpty())
95          );
96  
97          String testLine = preprocessLine(line);
98  
99          try {
100             aggregator.validate(testLine);
101         } catch (CsvValidationException ex) {
102             fail("was supposed to be valid!");
103         }
104     }
105 
106     @DisplayName("Everything is valid with null validators.")
107     @ParameterizedTest(name = "Line \"{0}\" is valid")
108     @ValueSource(strings = {"a;b;c", "awful;b;c", "a;bad;c", "awful;bad;c", "empty", "null"})
109     public void nullValidators(String line) {
110 
111         aggregator.addValidator(null);
112 
113         Assertions.assertAll("Parameter data must be valid.",
114                 () -> assertNotNull(line),
115                 () -> assertFalse(line.isEmpty())
116         );
117 
118         String testLine = preprocessLine(line);
119 
120         try {
121             aggregator.validate(testLine);
122         } catch (CsvValidationException ex) {
123             fail("was supposed to be valid!");
124         }
125     }
126 
127     @DisplayName("Short circuit if there are no validators present.")
128     @Test
129     public void shortCircuitIfNoValidators() throws CsvValidationException {
130         List<LineValidator> spyList = spy(new ArrayList<>());
131 
132         aggregator.setValidators(spyList);
133 
134         aggregator.validate("a,b,c");
135 
136         verify(spyList).isEmpty();
137         verifyNoMoreInteractions(spyList);
138     }
139 }