StatusUpdaterTest.java 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package de.codecentric.boot.admin.registry;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;

public class StatusUpdaterTest {

	private StatusUpdater updater;
	private SimpleApplicationStore store;
	private RestTemplate template;
	private ApplicationEventPublisher publisher;

	@Before
	public void setup() {
		store = new SimpleApplicationStore();
		template = mock(RestTemplate.class);
		updater = new StatusUpdater(template, store);
		publisher = mock(ApplicationEventPublisher.class);
		updater.setApplicationEventPublisher(publisher);
	}

	@Test
59
	@SuppressWarnings("rawtypes")
60 61
	public void test_update_statusChanged() {
		when(template.getForEntity("health", Map.class)).thenReturn(
62
				ResponseEntity.ok().body((Map) Collections.singletonMap("status", "UP")));
63

64 65
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
66 67 68 69

		Application app = store.find("id");

		assertThat(app.getStatusInfo().getStatus(), is("UP"));
70
		verify(publisher).publishEvent(argThat(isA(ClientApplicationStatusChangedEvent.class)));
71 72 73
	}

	@Test
74
	@SuppressWarnings("rawtypes")
75
	public void test_update_statusUnchanged() {
76 77
		when(template.getForEntity("health", Map.class))
				.thenReturn(ResponseEntity.ok((Map) Collections.singletonMap("status", "UNKNOWN")));
78

79 80
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
81

82 83
		verify(publisher, never())
				.publishEvent(argThat(isA(ClientApplicationStatusChangedEvent.class)));
84 85 86
	}

	@Test
87
	@SuppressWarnings("rawtypes")
88 89
	public void test_update_noBody() {
		// HTTP 200 - UP
90
		when(template.getForEntity("health", Map.class)).thenReturn(ResponseEntity.ok((Map) null));
91

92 93
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
94 95 96 97

		assertThat(store.find("id").getStatusInfo().getStatus(), is("UP"));

		// HTTP != 200 - DOWN
98 99
		when(template.getForEntity("health", Map.class))
				.thenReturn(ResponseEntity.status(503).body((Map) null));
100

101 102
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
103 104 105 106 107 108

		assertThat(store.find("id").getStatusInfo().getStatus(), is("DOWN"));
	}

	@Test
	public void test_update_offline() {
109 110
		when(template.getForEntity("health", Map.class))
				.thenThrow(new ResourceAccessException("error"));
111

112 113
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
114 115 116 117 118

		assertThat(store.find("id").getStatusInfo().getStatus(), is("OFFLINE"));
	}

	@Test
119
	@SuppressWarnings("rawtypes")
120
	public void test_updateStatusForApplications() {
121 122
		Application app1 = Application.create("foo").withId("id-1").withHealthUrl("health-1")
				.build();
123 124
		store.save(app1);

125
		Application app2 = Application.create("foo").withId("id-2").withHealthUrl("health-2")
126 127 128
				.withStatusInfo(StatusInfo.valueOf("UP", 0L)).build();
		store.save(app2);

129 130
		when(template.getForEntity("health-2", Map.class))
				.thenReturn(ResponseEntity.ok((Map) null));
131 132 133 134 135 136 137

		updater.updateStatusForAllApplications();

		verify(template, never()).getForEntity("health-1", Map.class);
	}

}