StatusUpdaterTest.java 4.36 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 59 60
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
	public void test_update_statusChanged() {
		when(template.getForEntity("health", Map.class)).thenReturn(
61
				ResponseEntity.ok().body((Map) Collections.singletonMap("status", "UP")));
62

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

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

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

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

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

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

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

89 90
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
91 92 93 94

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

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

98 99
		updater.updateStatus(
				Application.create("foo").withId("id").withHealthUrl("health").build());
100 101 102 103 104 105

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

	@Test
	public void test_update_offline() {
106 107
		when(template.getForEntity("health", Map.class))
				.thenThrow(new ResourceAccessException("error"));
108

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

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

	@Test
	public void test_updateStatusForApplications() {
117 118
		Application app1 = Application.create("foo").withId("id-1").withHealthUrl("health-1")
				.build();
119 120
		store.save(app1);

121
		Application app2 = Application.create("foo").withId("id-2").withHealthUrl("health-2")
122 123 124
				.withStatusInfo(StatusInfo.valueOf("UP", 0L)).build();
		store.save(app2);

125 126
		when(template.getForEntity("health-2", Map.class))
				.thenReturn(ResponseEntity.ok((Map) null));
127 128 129 130 131 132 133

		updater.updateStatusForAllApplications();

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

}