1+ package org .logstash .health ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import org .junit .Test ;
5+ import org .junit .experimental .runners .Enclosed ;
6+ import org .junit .runner .RunWith ;
7+ import org .junit .runners .Parameterized ;
8+ import org .junit .runners .Parameterized .Parameter ;
9+ import org .junit .runners .Parameterized .Parameters ;
10+
11+ import java .util .ArrayList ;
12+ import java .util .Collection ;
13+ import java .util .Collections ;
14+ import java .util .EnumSet ;
15+ import java .util .List ;
16+ import java .util .stream .Collectors ;
17+
18+ import static com .google .common .collect .Collections2 .orderedPermutations ;
19+ import static org .hamcrest .CoreMatchers .is ;
20+ import static org .hamcrest .CoreMatchers .equalTo ;
21+ import static org .hamcrest .MatcherAssert .assertThat ;
22+ import static org .logstash .health .Status .*;
23+
24+ @ RunWith (Enclosed .class )
25+ public class StatusTest {
26+
27+ public static class Tests {
28+ @ Test
29+ public void testReduceUnknown () {
30+ assertThat (UNKNOWN .reduce (UNKNOWN ), is (UNKNOWN ));
31+ assertThat (UNKNOWN .reduce (GREEN ), is (GREEN ));
32+ assertThat (UNKNOWN .reduce (YELLOW ), is (YELLOW ));
33+ assertThat (UNKNOWN .reduce (RED ), is (RED ));
34+ }
35+
36+ @ Test
37+ public void testReduceGreen () {
38+ assertThat (GREEN .reduce (UNKNOWN ), is (GREEN ));
39+ assertThat (GREEN .reduce (GREEN ), is (GREEN ));
40+ assertThat (GREEN .reduce (YELLOW ), is (YELLOW ));
41+ assertThat (GREEN .reduce (RED ), is (RED ));
42+ }
43+
44+ @ Test
45+ public void testReduceYellow () {
46+ assertThat (YELLOW .reduce (UNKNOWN ), is (YELLOW ));
47+ assertThat (YELLOW .reduce (GREEN ), is (YELLOW ));
48+ assertThat (YELLOW .reduce (YELLOW ), is (YELLOW ));
49+ assertThat (YELLOW .reduce (RED ), is (RED ));
50+ }
51+
52+ @ Test
53+ public void testReduceRed () {
54+ assertThat (RED .reduce (UNKNOWN ), is (RED ));
55+ assertThat (RED .reduce (GREEN ), is (RED ));
56+ assertThat (RED .reduce (YELLOW ), is (RED ));
57+ assertThat (RED .reduce (RED ), is (RED ));
58+ }
59+ }
60+
61+ @ RunWith (Parameterized .class )
62+ public static class JacksonSerialization {
63+ @ Parameters (name = "{0}" )
64+ public static Iterable <?> data () {
65+ return EnumSet .allOf (Status .class );
66+ }
67+
68+ @ Parameter
69+ public Status status ;
70+
71+ private final ObjectMapper mapper = new ObjectMapper ();
72+
73+ @ Test
74+ public void testSerialization () throws Exception {
75+ assertThat (mapper .writeValueAsString (status ), is (equalTo ('"' + status .name ().toLowerCase () + '"' )));
76+ }
77+ }
78+
79+ @ RunWith (Parameterized .class )
80+ public static class ReduceCommutativeSpecification {
81+ @ Parameters (name = "{0}<=>{1}" )
82+ public static Collection <Object []> data () {
83+ return getCombinations (EnumSet .allOf (Status .class ), 2 );
84+ }
85+
86+ @ Parameter (0 )
87+ public Status statusA ;
88+ @ Parameter (1 )
89+ public Status statusB ;
90+
91+ @ Test
92+ public void testReduceCommutative () {
93+ assertThat (statusA .reduce (statusB ), is (statusB .reduce (statusA )));
94+ }
95+
96+ private static <T extends Comparable <T >> List <Object []> getCombinations (Collection <T > source , int count ) {
97+ return orderedPermutations (source ).stream ()
98+ .map ((l ) -> l .subList (0 , count ))
99+ .map (ArrayList ::new ).peek (Collections ::sort )
100+ .collect (Collectors .toSet ())
101+ .stream ()
102+ .map (List ::toArray )
103+ .collect (Collectors .toList ());
104+ }
105+ }
106+ }
0 commit comments