Skip to content
This repository was archived by the owner on Jun 29, 2018. It is now read-only.

Commit 8ef1319

Browse files
committed
Add tests and missing license-headers
1 parent 01fbf73 commit 8ef1319

9 files changed

Lines changed: 279 additions & 21 deletions

File tree

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/event/ClientApplicationEvent.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.io.Serializable;
1919

20+
import org.apache.commons.lang.builder.EqualsBuilder;
21+
import org.apache.commons.lang.builder.HashCodeBuilder;
22+
2023
import de.codecentric.boot.admin.model.Application;
2124

2225
/**
@@ -55,4 +58,27 @@ public Application getApplication() {
5558
*/
5659
public abstract String getType();
5760

61+
@Override
62+
public int hashCode() {
63+
return new HashCodeBuilder().append(application).append(timestamp).append(getType())
64+
.toHashCode();
65+
}
66+
67+
@Override
68+
public boolean equals(Object obj) {
69+
if (this == obj) {
70+
return true;
71+
}
72+
if (obj == null) {
73+
return false;
74+
}
75+
if (getClass() != obj.getClass()) {
76+
return false;
77+
}
78+
ClientApplicationEvent other = (ClientApplicationEvent) obj;
79+
return new EqualsBuilder().append(this.application, other.application)
80+
.append(this.timestamp, other.timestamp).append(this.getType(), other.getType())
81+
.isEquals();
82+
}
83+
5884
}

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/notify/MailNotifier.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.expression.spel.support.StandardEvaluationContext;
2525
import org.springframework.mail.MailSender;
2626
import org.springframework.mail.SimpleMailMessage;
27-
import org.springframework.mail.javamail.JavaMailSender;
2827

2928
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
3029

@@ -33,7 +32,7 @@ public class MailNotifier extends AbstractNotifier {
3332
private final static String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
3433

3534
private final SpelExpressionParser parser = new SpelExpressionParser();
36-
private MailSender sender;
35+
private final MailSender sender;
3736

3837
/**
3938
* recipients of the mail
@@ -80,10 +79,6 @@ protected void notify(ClientApplicationStatusChangedEvent event) {
8079
sender.send(message);
8180
}
8281

83-
public void setSender(JavaMailSender sender) {
84-
this.sender = sender;
85-
}
86-
8782
public void setTo(String[] to) {
8883
this.to = Arrays.copyOf(to, to.length);
8984
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/config/AdminServerWebConfigurationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.codecentric.boot.admin.config;
217

318
import static org.hamcrest.CoreMatchers.hasItem;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.codecentric.boot.admin.event;
17+
18+
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.not;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
26+
import java.io.Serializable;
27+
28+
import org.apache.commons.io.output.ByteArrayOutputStream;
29+
import org.junit.Test;
30+
31+
import de.codecentric.boot.admin.model.Application;
32+
33+
public class ClientApplicationEventTest {
34+
35+
@Test
36+
public void hashCode_equals() throws Exception {
37+
ClientApplicationEvent event1 = new ClientApplicationRegisteredEvent(
38+
Application.create("test").build());
39+
ClientApplicationEvent event2 = cloneBySerialization(event1);
40+
41+
assertThat(event1.hashCode(), is(event2.hashCode()));
42+
assertThat(event1, is(event2));
43+
}
44+
45+
@Test
46+
public void equals() throws Exception {
47+
ClientApplicationEvent event1 = new ClientApplicationRegisteredEvent(
48+
Application.create("test").build());
49+
ClientApplicationEvent event2 = new ClientApplicationDeregisteredEvent(
50+
Application.create("test").build());
51+
52+
assertThat(event1, not(is(event2)));
53+
}
54+
55+
@SuppressWarnings("unchecked")
56+
/**
57+
* yeah nasty but we need exact the same timestamp
58+
*/
59+
private <T extends Serializable> T cloneBySerialization(T obj)
60+
throws IOException, ClassNotFoundException {
61+
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
62+
try (ObjectOutputStream oos = new ObjectOutputStream(buf)) {
63+
oos.writeObject(obj);
64+
}
65+
66+
try (ObjectInputStream ois = new ObjectInputStream(
67+
new ByteArrayInputStream(buf.toByteArray()))) {
68+
return (T) ois.readObject();
69+
}
70+
}
71+
}
72+
73+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.codecentric.boot.admin.journal.store;
17+
18+
import static org.hamcrest.CoreMatchers.is;
19+
import static org.junit.Assert.assertThat;
20+
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import com.hazelcast.config.Config;
31+
import com.hazelcast.core.HazelcastInstance;
32+
import com.hazelcast.instance.HazelcastInstanceFactory;
33+
34+
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
35+
import de.codecentric.boot.admin.event.ClientApplicationEvent;
36+
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
37+
import de.codecentric.boot.admin.model.Application;
38+
39+
public class HazelcastJournaledEventStoreTest {
40+
41+
private HazelcastJournaledEventStore store;
42+
43+
@Before
44+
public void setup() {
45+
HazelcastInstance hazelcast = HazelcastInstanceFactory.newHazelcastInstance(new Config());
46+
store = new HazelcastJournaledEventStore(
47+
hazelcast.<ClientApplicationEvent> getList("testList"));
48+
}
49+
50+
@Test
51+
public void test_store() {
52+
Application application = Application.create("foo").withId("bar").build();
53+
List<ClientApplicationEvent> events = Arrays.asList(
54+
new ClientApplicationRegisteredEvent(application),
55+
new ClientApplicationDeregisteredEvent(application));
56+
57+
for (ClientApplicationEvent event : events) {
58+
store.store(event);
59+
}
60+
61+
// Items are stored in reverse order
62+
List<ClientApplicationEvent> reversed = new ArrayList<>(events);
63+
Collections.reverse(reversed);
64+
65+
assertThat(store.findAll(), is((Collection<ClientApplicationEvent>) reversed));
66+
}
67+
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/AbstractNotifierTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.codecentric.boot.admin.notify;
217

318
import static org.junit.Assert.assertFalse;
@@ -10,10 +25,10 @@
1025
import de.codecentric.boot.admin.model.StatusInfo;
1126

1227
public class AbstractNotifierTest {
13-
private TestableNotifier notifier = new TestableNotifier();
1428

1529
@Test
1630
public void test_onApplicationEvent() {
31+
TestableNotifier notifier = new TestableNotifier();
1732
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
1833
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
1934
StatusInfo.ofDown(), StatusInfo.ofUp()));
@@ -22,6 +37,7 @@ public void test_onApplicationEvent() {
2237

2338
@Test
2439
public void test_onApplicationEvent_disbaled() {
40+
TestableNotifier notifier = new TestableNotifier();
2541
notifier.setEnabled(false);
2642
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
2743
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
@@ -31,6 +47,7 @@ public void test_onApplicationEvent_disbaled() {
3147

3248
@Test
3349
public void test_onApplicationEvent_noSend() {
50+
TestableNotifier notifier = new TestableNotifier();
3451
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
3552
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
3653
StatusInfo.ofUnknown(), StatusInfo.ofUp()));
@@ -40,6 +57,7 @@ public void test_onApplicationEvent_noSend() {
4057

4158
@Test
4259
public void test_onApplicationEvent_noSend_wildcard() {
60+
TestableNotifier notifier = new TestableNotifier();
4361
notifier.setIgnoreChanges(new String[] { "*:UP" });
4462
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
4563
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
@@ -48,6 +66,19 @@ public void test_onApplicationEvent_noSend_wildcard() {
4866
assertFalse(notifier.hasNotified);
4967
}
5068

69+
@Test
70+
public void test_onApplicationEvent_throw_doesnt_propagate() {
71+
AbstractNotifier notifier = new AbstractNotifier() {
72+
@Override
73+
protected void notify(ClientApplicationStatusChangedEvent event) throws Exception {
74+
throw new RuntimeException();
75+
}
76+
};
77+
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
78+
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
79+
StatusInfo.ofOffline(), StatusInfo.ofUp()));
80+
}
81+
5182
private static class TestableNotifier extends AbstractNotifier {
5283
private boolean hasNotified;
5384

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/MailNotifierTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.codecentric.boot.admin.notify;
217

318
import static org.mockito.Matchers.eq;
@@ -26,6 +41,7 @@ public void setup() {
2641
notifier.setTo(new String[] { "foo@bar.com" });
2742
notifier.setCc(new String[] { "bar@foo.com" });
2843
notifier.setFrom("SBA <no-reply@example.com>");
44+
notifier.setSubject("#{application.id} is #{to.status}");
2945
}
3046

3147
@Test
@@ -39,7 +55,7 @@ public void test_onApplicationEvent() {
3955
expected.setCc(new String[] { "bar@foo.com" });
4056
expected.setFrom("SBA <no-reply@example.com>");
4157
expected.setText("App (-id-)\nstatus changed from DOWN to UP\n\nhttp://health");
42-
expected.setSubject("App (-id-) is UP");
58+
expected.setSubject("-id- is UP");
4359

4460
verify(sender).send(eq(expected));
4561
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/PagerdutyNotifierTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.codecentric.boot.admin.notify;
217

318
import static org.mockito.Matchers.eq;

0 commit comments

Comments
 (0)