AdminServerWebConfigurationTest.java 5.3 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
package de.codecentric.boot.admin.config;

18
import static org.hamcrest.CoreMatchers.hasItem;
19
import static org.hamcrest.CoreMatchers.instanceOf;
20 21 22
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertThat;
23 24
import static org.junit.Assert.assertTrue;

25 26 27
import java.util.ArrayList;
import java.util.List;

28 29
import org.junit.After;
import org.junit.Test;
30
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
31
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
32 33 34
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
35 36
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
37 38
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
39 40
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

41 42
import com.hazelcast.config.Config;

43
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
44 45 46
import de.codecentric.boot.admin.journal.store.HazelcastJournaledEventStore;
import de.codecentric.boot.admin.journal.store.JournaledEventStore;
import de.codecentric.boot.admin.journal.store.SimpleJournaledEventStore;
47
import de.codecentric.boot.admin.notify.MailNotifier;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;

public class AdminServerWebConfigurationTest {

	private AnnotationConfigWebApplicationContext context;

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

63 64 65 66
	@Test
	public void jacksonMapperPresentFromDefault() {
		AdminServerWebConfiguration config = new AdminServerWebConfiguration();

67
		List<HttpMessageConverter<?>> converters = new ArrayList<>();
68 69 70 71
		converters.add(new MappingJackson2HttpMessageConverter());

		config.extendMessageConverters(converters);

72
		assertThat(converters, hasItem(isA(MappingJackson2HttpMessageConverter.class)));
73 74 75 76 77 78
		assertThat(converters.size(), is(1));
	}

	@Test
	public void jacksonMapperPresentNeedExtend() {
		AdminServerWebConfiguration config = new AdminServerWebConfiguration();
79
		List<HttpMessageConverter<?>> converters = new ArrayList<>();
80 81 82 83 84 85 86

		config.extendMessageConverters(converters);

		assertThat(converters, hasItem(isA(MappingJackson2HttpMessageConverter.class)));
		assertThat(converters.size(), is(1));
	}

87 88
	@Test
	public void simpleConfig() {
89
		load();
90 91
		assertThat(context.getBean(ApplicationStore.class),
				is(instanceOf(SimpleApplicationStore.class)));
92
		assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
93
		assertTrue(context.getBeansOfType(MailNotifier.class).isEmpty());
94 95
		assertThat(context.getBean(JournaledEventStore.class),
				is(instanceOf(SimpleJournaledEventStore.class)));
96 97
	}

98 99
	@Test
	public void hazelcastConfig() {
100
		load(TestHazelcastConfig.class);
101 102 103 104
		assertThat(context.getBean(ApplicationStore.class),
				is(instanceOf(HazelcastApplicationStore.class)));
		assertThat(context.getBean(JournaledEventStore.class),
				is(instanceOf(HazelcastJournaledEventStore.class)));
105 106 107 108 109
		assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
	}

	@Test
	public void discoveryConfig() {
110
		load(NoopDiscoveryClientAutoConfiguration.class);
111 112
		assertThat(context.getBean(ApplicationStore.class),
				is(instanceOf(SimpleApplicationStore.class)));
113 114 115
		context.getBean(ApplicationDiscoveryListener.class);
	}

116 117 118
	@Configuration
	static class TestHazelcastConfig {
		@Bean
119
		public Config config() {
120 121 122 123
			return new Config();
		}
	}

124
	private void load(String... environment) {
125 126 127 128
		load(null, environment);
	}

	private void load(Class<?> config, String... environment) {
129
		AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
130 131 132
		if (config != null) {
			applicationContext.register(config);
		}
133
		applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
134
		applicationContext.register(ServerPropertiesAutoConfiguration.class);
135
		applicationContext.register(HazelcastAutoConfiguration.class);
136 137
		applicationContext.register(HazelcastStoreConfiguration.class);
		applicationContext.register(DiscoveryClientConfiguration.class);
138
		applicationContext.register(AdminServerWebConfiguration.class);
139

140 141 142 143 144
		EnvironmentTestUtils.addEnvironment(applicationContext, environment);
		applicationContext.refresh();
		this.context = applicationContext;
	}
}