1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.startup.healthcheck;
21
22 import static org.hamcrest.Matchers.containsString;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Properties;
29
30 import org.junit.Test;
31 import org.neo4j.server.logging.InMemoryAppender;
32
33
34 public class StartupHealthCheckTest {
35
36 @Test
37 public void shouldPassWithNoRules() {
38 StartupHealthCheck check = new StartupHealthCheck();
39 assertTrue(check.run());
40 }
41
42 @Test
43 public void shouldRunAllHealthChecksToCompletionIfNonFail() {
44 StartupHealthCheck check = new StartupHealthCheck(getPassingRules());
45 assertTrue(check.run());
46 }
47
48 @Test
49 public void shouldFailIfOneOrMoreHealthChecksFail() {
50 StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
51 assertFalse(check.run());
52 }
53
54 @Test
55 public void shouldLogFailedRule() {
56 StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
57 InMemoryAppender appender = new InMemoryAppender(StartupHealthCheck.log);
58 check.run();
59
60 assertThat(appender.toString(), containsString("ERROR - blah blah"));
61 }
62
63 @Test
64 public void shouldAdvertiseFailedRule() {
65 StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
66 check.run();
67 assertNotNull(check.failedRule());
68 }
69
70 private StartupHealthCheckRule[] getWithOneFailingRule() {
71 StartupHealthCheckRule[] rules = new StartupHealthCheckRule[5];
72
73 for(int i = 0; i < rules.length; i++) {
74 rules[i] = new StartupHealthCheckRule() {
75 public boolean execute(Properties properties) {
76 return true;
77 }
78
79 public String getFailureMessage() {
80 return "blah blah";
81 }};
82 }
83
84 rules[rules.length / 2] = new StartupHealthCheckRule() {
85 public boolean execute(Properties properties) {
86 return false;
87 }
88
89 public String getFailureMessage() {
90 return "blah blah";
91 }};
92
93 return rules;
94 }
95
96 private StartupHealthCheckRule[] getPassingRules() {
97 StartupHealthCheckRule[] rules = new StartupHealthCheckRule[5];
98
99 for(int i = 0; i < rules.length; i++) {
100 rules[i] = new StartupHealthCheckRule() {
101 public boolean execute(Properties properties) {
102 return true;
103 }
104
105 public String getFailureMessage() {
106 return "blah blah";
107 }};
108 }
109
110 return rules;
111 }
112 }