3131import org .junit .jupiter .api .Test ;
3232
3333import static java .lang .String .format ;
34- import static org .junit .jupiter .api .Assertions .assertEquals ;
35- import static org .junit .jupiter .api .Assertions .assertFalse ;
36- import static org .junit .jupiter .api .Assertions .assertTrue ;
37-
38- public class ClaimValidatorTest {
39-
40- @ Test
41- public void initialStateTest () {
42- ClaimValidator validator = new ClaimValidator ();
43- assertFalse (validator .hasErrors ());
44- for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
45- int expected = counter .getDefaultMaxValue () < 0 ? Integer .MAX_VALUE : counter .getDefaultMaxValue ();
46- assertEquals (expected , validator .getMax (counter ), () -> format ("Max value '%s' is invalid" , counter ));
47- assertEquals (counter .getDefaultMinValue (), validator .getMin (counter ), () -> format ("Min value '%s' is invalid" , counter ));
48- assertTrue (validator .isValid (counter , expected ), () -> format ("max value (%s) should not be invalid" , expected ));
49- assertTrue (validator .isValid (counter , counter .getDefaultMinValue ()), () -> format ("min value (%s) should not be invalid" ,
50- counter .getDefaultMinValue ()));
51- }
34+ import static org .assertj .core .api .Assertions .assertThat ;
35+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
36+
37+ class ClaimValidatorTest {
38+
39+ @ Test
40+ void initialStateTest () {
41+ ClaimValidator validator = new ClaimValidator ();
42+ assertThat (validator .hasErrors ()).isFalse ();
43+ for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
44+ int expected = counter .getDefaultMaxValue () < 0 ? Integer .MAX_VALUE : counter .getDefaultMaxValue ();
45+ assertThat (validator .getMax (counter )).as (() -> format ("Max value '%s' is invalid" , counter )).isEqualTo (expected );
46+ assertThat (validator .getMin (counter )).as (() -> format ("Min value '%s' is invalid" , counter )).isEqualTo (counter .getDefaultMinValue ());
47+ assertThat (validator .isValid (counter , expected )).as (() -> format ("max value (%s) should not be invalid" , expected )).isTrue ();
48+ assertThat (validator .isValid (counter , counter .getDefaultMinValue ())).as (() -> format ("min value (%s) should not be invalid" ,
49+ counter .getDefaultMinValue ())).isTrue ();
50+ }
5251 }
5352
5453 @ Test
55- public void setMaxTest () {
54+ void setMaxTest () {
5655 ClaimValidator validator = new ClaimValidator ();
5756 int expected = 5 ;
5857 for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
5958 validator .setMax (counter , expected );
60- assertEquals ( expected , validator .getMax (counter ), ( ) -> format ("'%s' value is invalid" , counter ));
61- assertTrue (validator .isValid (counter , expected ));
59+ assertThat ( validator .getMax (counter )). as (( ) -> format ("'%s' value is invalid" , counter )). isEqualTo ( expected );
60+ assertThat (validator .isValid (counter , expected )). isTrue ( );
6261 }
6362 expected = Integer .MAX_VALUE ;
6463 for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
6564 validator .setMax (counter , -1 );
66- assertEquals ( expected , validator .getMax (counter ), ( ) -> format ("'%s' value is invalid" , counter ));
67- assertTrue (validator .isValid (counter , expected ));
65+ assertThat ( validator .getMax (counter )). as (( ) -> format ("'%s' value is invalid" , counter )). isEqualTo ( expected );
66+ assertThat (validator .isValid (counter , expected )). isTrue ( );
6867 }
6968 }
7069
7170 @ Test
72- public void setMinTest () {
71+ void setMinTest () {
7372 ClaimValidator validator = new ClaimValidator ();
7473 int expected = 5 ;
7574 for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
7675 validator .setMin (counter , expected );
77- assertEquals ( expected , validator .getMin (counter ), ( ) -> format ("'%s' value is invalid" , counter ));
78- assertTrue (validator .isValid (counter , expected ));
76+ assertThat ( validator .getMin (counter )). as (( ) -> format ("'%s' value is invalid" , counter )). isEqualTo ( expected );
77+ assertThat (validator .isValid (counter , expected )). isTrue ( );
7978 }
8079 expected = Integer .MAX_VALUE ;
8180 for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
8281 validator .setMax (counter , -1 );
83- assertEquals ( expected , validator .getMax (counter ), ( ) -> format ("'%s' value is invalid" , counter ));
84- assertTrue (validator .isValid (counter , expected ));
82+ assertThat ( validator .getMax (counter )). as (( ) -> format ("'%s' value is invalid" , counter )). isEqualTo ( expected );
83+ assertThat (validator .isValid (counter , expected )). isTrue ( );
8584 }
8685 }
8786
8887 @ Test
89- public void isValidTest () {
88+ void isValidTest () {
9089 int expected = 5 ;
9190 for (ClaimStatistic .Counter counter : ClaimStatistic .Counter .values ()) {
9291 ClaimValidator validator = new ClaimValidator ();
93- assertFalse (validator .hasErrors ());
92+ assertThat (validator .hasErrors ()). isFalse ( );
9493 validator .setMax (counter , expected );
95- assertTrue (validator .isValid (counter , expected ), ( ) -> format ("error with %s expected" , counter )) ;
96- assertFalse (validator .hasErrors (), ( ) -> format ("error with %s, counter" , counter )) ;
97- assertTrue (validator .isValid (counter , expected - 1 ), ( ) -> format ("error with %s -1" , counter )) ;
98- assertFalse (validator .hasErrors (), ( ) -> format ("error with %s, counter" , counter )) ;
99- assertFalse (validator .isValid (counter , expected + 1 ), ( ) -> format ("error with %s +1" , counter )) ;
100- assertTrue (validator .hasErrors (), ( ) -> format ("error with %s, counter" , counter )) ;
94+ assertThat (validator .isValid (counter , expected )). as (( ) -> format ("error with %s expected" , counter )). isTrue () ;
95+ assertThat (validator .hasErrors ()). as (( ) -> format ("error with %s, counter" , counter )). isFalse () ;
96+ assertThat (validator .isValid (counter , expected - 1 )). as (( ) -> format ("error with %s -1" , counter )). isTrue () ;
97+ assertThat (validator .hasErrors ()). as (( ) -> format ("error with %s, counter" , counter )). isFalse () ;
98+ assertThat (validator .isValid (counter , expected + 1 )). as (( ) -> format ("error with %s +1" , counter )). isFalse () ;
99+ assertThat (validator .hasErrors ()). as (( ) -> format ("error with %s, counter" , counter )). isTrue () ;
101100 }
102101 }
103102
@@ -108,7 +107,7 @@ private List<ClaimStatistic.Counter> getRequiredCounters() {
108107 }
109108
110109 @ Test
111- public void logIssuesTest () {
110+ void logIssuesTest () {
112111 ClaimStatistic statistic = new ClaimStatistic ();
113112 TestingLog log = new TestingLog ();
114113 try {
@@ -125,7 +124,7 @@ public void logIssuesTest() {
125124 validator .setMax (counter , expected );
126125 statistic .incCounter (counter , expected );
127126 validator .logIssues (statistic );
128- assertFalse (log .getCaptured (). isEmpty () );
127+ assertThat (log .getCaptured ()). isNotEmpty ( );
129128 required .entrySet ().stream ().filter (e -> e .getKey () != counter )
130129 .map (Map .Entry ::getValue ).forEach (log ::assertContains );
131130 if (required .entrySet ().contains (counter )) {
@@ -145,7 +144,36 @@ public void logIssuesTest() {
145144 }
146145
147146 @ Test
148- public void listIssuesTest () {
147+ void nullValuesTest () {
148+ ClaimValidator underTest = new ClaimValidator ();
149+ TestingLog log = new TestingLog ();
150+ try {
151+ DefaultLog .setInstance (log );
152+ underTest .setMin (null , 5 );
153+ assertThat (log .getCaptured ()).contains ("`null` passed as argument to setMin() -- ignoring" );
154+ underTest .setMax (null , 10 );
155+ assertThat (log .getCaptured ()).contains ("`null` passed as argument to setMax() -- ignoring" );
156+ assertThat (underTest .getMin (null )).isZero ();
157+ assertThat (log .getCaptured ()).contains ("`null` passed as argument to getMin() -- returning 0" );
158+ assertThat (underTest .getMax (null )).isZero ();
159+ assertThat (log .getCaptured ()).contains ("`null` passed as argument to getMax() -- returning 0" );
160+ } finally {
161+ DefaultLog .setInstance (null );
162+ }
163+
164+ assertThatThrownBy (() -> underTest .isValid (null , 5 )).isInstanceOf (NullPointerException .class )
165+ .hasMessageContaining ("counter must not be null." );
166+
167+ assertThatThrownBy (() -> underTest .logIssues (null )).isInstanceOf (NullPointerException .class )
168+ .hasMessageContaining ("statistic must not be null." );
169+
170+ assertThatThrownBy (() -> underTest .listIssues (null )).isInstanceOf (NullPointerException .class )
171+ .hasMessageContaining ("statistic must not be null." );
172+
173+ }
174+
175+ @ Test
176+ void listIssuesTest () {
149177 ClaimStatistic statistic = new ClaimStatistic ();
150178 List <String > required = getRequiredCounters ().stream ().map (ClaimStatistic .Counter ::name ).toList ();
151179
@@ -157,13 +185,51 @@ public void listIssuesTest() {
157185 validator .setMax (counter , value );
158186 statistic .incCounter (counter , value );
159187 Collection <String > actual = validator .listIssues (statistic );
160- assertEquals ( expected , actual );
188+ assertThat ( actual ). containsExactlyElementsOf ( expected );
161189 statistic .incCounter (counter , 1 );
162190 expected .add (counter .name ());
163191 expected .sort (String ::compareTo );
164192 actual = validator .listIssues (statistic );
165- assertEquals ( expected , actual );
193+ assertThat ( actual ). containsExactlyElementsOf ( expected );
166194 statistic .incCounter (counter , -1 - value );
167195 }
168196 }
197+
198+ @ Test
199+ void verifyMinimumIsSetToZeroByDefault () {
200+ ClaimValidator validator = new ClaimValidator ();
201+ validator .setMax (ClaimStatistic .Counter .IGNORED , 4711 );
202+ assertThat (validator .getMin (ClaimStatistic .Counter .IGNORED )).isZero ();
203+ }
204+
205+ @ Test
206+ void verifyHandlingIfMaximumIsLessThanZeroAndChangedTwice () {
207+ ClaimValidator validator = new ClaimValidator ();
208+ validator .setMax (ClaimStatistic .Counter .IGNORED , -4711 );
209+ assertThat (validator .getMax (ClaimStatistic .Counter .IGNORED )).isEqualTo (Integer .MAX_VALUE );
210+ validator .setMax (ClaimStatistic .Counter .IGNORED , 4711 );
211+ assertThat (validator .getMax (ClaimStatistic .Counter .IGNORED )).isEqualTo (4711 );
212+ }
213+
214+ @ Test
215+ void verifyHandlingIfMinAndMaxIsSetOnACounter () {
216+ ClaimValidator validator = new ClaimValidator ();
217+ validator .setMin (ClaimStatistic .Counter .IGNORED , 4711 );
218+ validator .setMax (ClaimStatistic .Counter .IGNORED , -4711 );
219+ assertThat (validator .getMax (ClaimStatistic .Counter .IGNORED )).isEqualTo (Integer .MAX_VALUE );
220+ assertThat (validator .getMin (ClaimStatistic .Counter .IGNORED )).isEqualTo (4711 );
221+
222+ validator .setMax (ClaimStatistic .Counter .IGNORED , 4710 );
223+ assertThat (validator .getMax (ClaimStatistic .Counter .IGNORED )).isEqualTo (4710 );
224+ assertThat (validator .getMin (ClaimStatistic .Counter .IGNORED )).isEqualTo (4710 );
225+ }
226+
227+ @ Test
228+ void verifyHandlingIfMinimumIsChangedTwice () {
229+ ClaimValidator validator = new ClaimValidator ();
230+ validator .setMin (ClaimStatistic .Counter .IGNORED , 4711 );
231+ assertThat (validator .getMin (ClaimStatistic .Counter .IGNORED )).isEqualTo (4711 );
232+ validator .setMin (ClaimStatistic .Counter .IGNORED , 1 );
233+ assertThat (validator .getMin (ClaimStatistic .Counter .IGNORED )).isEqualTo (1 );
234+ }
169235}
0 commit comments