Commit 8ef1319c by Johannes Edmeier

Add tests and missing license-headers

parent 01fbf73d
......@@ -17,6 +17,9 @@ package de.codecentric.boot.admin.event;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import de.codecentric.boot.admin.model.Application;
/**
......@@ -55,4 +58,27 @@ public abstract class ClientApplicationEvent implements Serializable {
*/
public abstract String getType();
@Override
public int hashCode() {
return new HashCodeBuilder().append(application).append(timestamp).append(getType())
.toHashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ClientApplicationEvent other = (ClientApplicationEvent) obj;
return new EqualsBuilder().append(this.application, other.application)
.append(this.timestamp, other.timestamp).append(this.getType(), other.getType())
.isEquals();
}
}
......@@ -24,7 +24,6 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
......@@ -33,7 +32,7 @@ public class MailNotifier extends AbstractNotifier {
private final static String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
private final SpelExpressionParser parser = new SpelExpressionParser();
private MailSender sender;
private final MailSender sender;
/**
* recipients of the mail
......@@ -80,10 +79,6 @@ public class MailNotifier extends AbstractNotifier {
sender.send(message);
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
public void setTo(String[] to) {
this.to = Arrays.copyOf(to, to.length);
}
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.hasItem;
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.event;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Test;
import de.codecentric.boot.admin.model.Application;
public class ClientApplicationEventTest {
@Test
public void hashCode_equals() throws Exception {
ClientApplicationEvent event1 = new ClientApplicationRegisteredEvent(
Application.create("test").build());
ClientApplicationEvent event2 = cloneBySerialization(event1);
assertThat(event1.hashCode(), is(event2.hashCode()));
assertThat(event1, is(event2));
}
@Test
public void equals() throws Exception {
ClientApplicationEvent event1 = new ClientApplicationRegisteredEvent(
Application.create("test").build());
ClientApplicationEvent event2 = new ClientApplicationDeregisteredEvent(
Application.create("test").build());
assertThat(event1, not(is(event2)));
}
@SuppressWarnings("unchecked")
/**
* yeah nasty but we need exact the same timestamp
*/
private <T extends Serializable> T cloneBySerialization(T obj)
throws IOException, ClassNotFoundException {
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
try (ObjectOutputStream oos = new ObjectOutputStream(buf)) {
oos.writeObject(obj);
}
try (ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(buf.toByteArray()))) {
return (T) ois.readObject();
}
}
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.journal.store;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceFactory;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.model.Application;
public class HazelcastJournaledEventStoreTest {
private HazelcastJournaledEventStore store;
@Before
public void setup() {
HazelcastInstance hazelcast = HazelcastInstanceFactory.newHazelcastInstance(new Config());
store = new HazelcastJournaledEventStore(
hazelcast.<ClientApplicationEvent> getList("testList"));
}
@Test
public void test_store() {
Application application = Application.create("foo").withId("bar").build();
List<ClientApplicationEvent> events = Arrays.asList(
new ClientApplicationRegisteredEvent(application),
new ClientApplicationDeregisteredEvent(application));
for (ClientApplicationEvent event : events) {
store.store(event);
}
// Items are stored in reverse order
List<ClientApplicationEvent> reversed = new ArrayList<>(events);
Collections.reverse(reversed);
assertThat(store.findAll(), is((Collection<ClientApplicationEvent>) reversed));
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.notify;
import static org.junit.Assert.assertFalse;
......@@ -10,10 +25,10 @@ import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
public class AbstractNotifierTest {
private TestableNotifier notifier = new TestableNotifier();
@Test
public void test_onApplicationEvent() {
TestableNotifier notifier = new TestableNotifier();
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofDown(), StatusInfo.ofUp()));
......@@ -22,6 +37,7 @@ public class AbstractNotifierTest {
@Test
public void test_onApplicationEvent_disbaled() {
TestableNotifier notifier = new TestableNotifier();
notifier.setEnabled(false);
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
......@@ -31,6 +47,7 @@ public class AbstractNotifierTest {
@Test
public void test_onApplicationEvent_noSend() {
TestableNotifier notifier = new TestableNotifier();
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofUnknown(), StatusInfo.ofUp()));
......@@ -40,6 +57,7 @@ public class AbstractNotifierTest {
@Test
public void test_onApplicationEvent_noSend_wildcard() {
TestableNotifier notifier = new TestableNotifier();
notifier.setIgnoreChanges(new String[] { "*:UP" });
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
......@@ -48,6 +66,19 @@ public class AbstractNotifierTest {
assertFalse(notifier.hasNotified);
}
@Test
public void test_onApplicationEvent_throw_doesnt_propagate() {
AbstractNotifier notifier = new AbstractNotifier() {
@Override
protected void notify(ClientApplicationStatusChangedEvent event) throws Exception {
throw new RuntimeException();
}
};
notifier.onClientApplicationStatusChanged(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
StatusInfo.ofOffline(), StatusInfo.ofUp()));
}
private static class TestableNotifier extends AbstractNotifier {
private boolean hasNotified;
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.eq;
......@@ -26,6 +41,7 @@ public class MailNotifierTest {
notifier.setTo(new String[] { "foo@bar.com" });
notifier.setCc(new String[] { "bar@foo.com" });
notifier.setFrom("SBA <no-reply@example.com>");
notifier.setSubject("#{application.id} is #{to.status}");
}
@Test
......@@ -39,7 +55,7 @@ public class MailNotifierTest {
expected.setCc(new String[] { "bar@foo.com" });
expected.setFrom("SBA <no-reply@example.com>");
expected.setText("App (-id-)\nstatus changed from DOWN to UP\n\nhttp://health");
expected.setSubject("App (-id-) is UP");
expected.setSubject("-id- is UP");
verify(sender).send(eq(expected));
}
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.eq;
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.registry;
import static org.hamcrest.CoreMatchers.is;
......@@ -45,7 +60,8 @@ public class StatusUpdaterTest {
when(template.getForEntity("health", Map.class)).thenReturn(
ResponseEntity.ok().body((Map) Collections.singletonMap("status", "UP")));
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
Application app = store.find("id");
......@@ -55,13 +71,14 @@ public class StatusUpdaterTest {
@Test
public void test_update_statusUnchanged() {
when(template.getForEntity("health", Map.class)).thenReturn(
ResponseEntity.ok((Map) Collections.singletonMap("status", "UNKNOWN")));
when(template.getForEntity("health", Map.class))
.thenReturn(ResponseEntity.ok((Map) Collections.singletonMap("status", "UNKNOWN")));
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
verify(publisher, never()).publishEvent(
argThat(isA(ClientApplicationStatusChangedEvent.class)));
verify(publisher, never())
.publishEvent(argThat(isA(ClientApplicationStatusChangedEvent.class)));
}
@Test
......@@ -69,25 +86,28 @@ public class StatusUpdaterTest {
// HTTP 200 - UP
when(template.getForEntity("health", Map.class)).thenReturn(ResponseEntity.ok((Map) null));
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
assertThat(store.find("id").getStatusInfo().getStatus(), is("UP"));
// HTTP != 200 - DOWN
when(template.getForEntity("health", Map.class)).thenReturn(
ResponseEntity.status(503).body((Map) null));
when(template.getForEntity("health", Map.class))
.thenReturn(ResponseEntity.status(503).body((Map) null));
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
assertThat(store.find("id").getStatusInfo().getStatus(), is("DOWN"));
}
@Test
public void test_update_offline() {
when(template.getForEntity("health", Map.class)).thenThrow(
new ResourceAccessException("error"));
when(template.getForEntity("health", Map.class))
.thenThrow(new ResourceAccessException("error"));
updater.updateStatus(Application.create("foo").withId("id").withHealthUrl("health").build());
updater.updateStatus(
Application.create("foo").withId("id").withHealthUrl("health").build());
assertThat(store.find("id").getStatusInfo().getStatus(), is("OFFLINE"));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment