AdminApplicationHazelcastTest.java 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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;

import static org.junit.Assert.assertEquals;
19
import static org.junit.Assert.assertFalse;
20
import static org.junit.Assert.assertNotNull;
21 22 23 24 25
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
26 27 28 29 30 31

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
32
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
33
import org.springframework.boot.test.TestRestTemplate;
34
import org.springframework.context.annotation.Bean;
35 36 37 38 39
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

40 41 42 43 44
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ListConfig;
import com.hazelcast.config.MapConfig;

45 46 47 48 49
import de.codecentric.boot.admin.config.EnableAdminServer;
import de.codecentric.boot.admin.model.Application;

/**
 * Integration test to verify the correct functionality of the REST API with Hazelcast
50
 *
51 52 53 54
 * @author Dennis Schulte
 */
public class AdminApplicationHazelcastTest {

55 56 57 58
	@Configuration
	@EnableAutoConfiguration
	@EnableAdminServer
	public static class TestAdminApplication {
59 60 61 62 63 64 65 66
		@Bean
		public Config hazelcastConfig() {
			return new Config()
					.addMapConfig(new MapConfig("spring-boot-admin-application-store")
							.setBackupCount(1).setEvictionPolicy(EvictionPolicy.NONE))
					.addListConfig(new ListConfig("spring-boot-admin-application-store")
							.setBackupCount(1).setMaxSize(1000));
		}
67 68
	}

69
	private RestTemplate template = new TestRestTemplate();
70 71
	private EmbeddedWebApplicationContext instance1;
	private EmbeddedWebApplicationContext instance2;
72

73
	@Before
74 75
	public void setup() throws InterruptedException {
		System.setProperty("hazelcast.wait.seconds.before.join", "0");
76 77
		instance1 = (EmbeddedWebApplicationContext) SpringApplication.run(
				TestAdminApplication.class,
78
				new String[] { "--server.port=0", "--spring.jmx.enabled=false" });
79 80
		instance2 = (EmbeddedWebApplicationContext) SpringApplication.run(
				TestAdminApplication.class,
81
				new String[] { "--server.port=0", "--spring.jmx.enabled=false" });
82 83 84 85
	}

	@After
	public void shutdown() {
86 87
		instance1.close();
		instance2.close();
88 89 90 91
	}

	@Test
	public void test() {
92 93 94 95 96 97
		Application app = Application.create("Hazelcast Test")
				.withHealthUrl("http://127.0.0.1/health").build();
		Application app2 = Application.create("Hazelcast Test")
				.withHealthUrl("http://127.0.0.1:2/health").build();
		Application app3 = Application.create("Do not find")
				.withHealthUrl("http://127.0.0.1:3/health").build();
98

99 100 101
		// publish app on instance1
		ResponseEntity<Application> postResponse = registerApp(app, instance1);
		app = postResponse.getBody();
102
		assertEquals(HttpStatus.CREATED, postResponse.getStatusCode());
103
		assertNotNull(app.getId());
104

105 106 107 108 109
		// publish app2 on instance2
		ResponseEntity<Application> postResponse2 = registerApp(app2, instance2);
		app2 = postResponse2.getBody();
		assertEquals(HttpStatus.CREATED, postResponse.getStatusCode());
		assertNotNull(app2.getId());
110

111 112
		// retrieve app from instance2
		ResponseEntity<Application> getResponse = getApp(app.getId(), instance2);
113
		assertEquals(HttpStatus.OK, getResponse.getStatusCode());
114 115 116 117 118 119 120 121 122
		assertEquals(app, getResponse.getBody());

		// retrieve app and app2 from instance1 (but not app3)
		app3 = registerApp(app3, instance1).getBody();
		Collection<Application> apps = getAppByName("Hazelcast Test", instance1).getBody();
		assertEquals(2, apps.size());
		assertTrue(apps.contains(app));
		assertTrue(apps.contains(app2));
		assertFalse(apps.contains(app3));
123 124
	}

125 126
	private ResponseEntity<Application> getApp(String id, EmbeddedWebApplicationContext context) {
		int port = context.getEmbeddedServletContainer().getPort();
127 128
		ResponseEntity<Application> getResponse = template.getForEntity(
				"http://localhost:" + port + "/api/applications/" + id, Application.class);
129 130 131
		return getResponse;
	}

132 133
	private ResponseEntity<Application> registerApp(Application app,
			EmbeddedWebApplicationContext context) {
134
		int port = context.getEmbeddedServletContainer().getPort();
135 136
		return template.postForEntity("http://localhost:" + port + "/api/applications", app,
				Application.class);
137
	}
138 139

	@SuppressWarnings("unchecked")
140 141
	private ResponseEntity<Collection<Application>> getAppByName(String name,
			EmbeddedWebApplicationContext context) {
142
		int port = context.getEmbeddedServletContainer().getPort();
143 144
		ResponseEntity<?> getResponse = template.getForEntity(
				"http://localhost:" + port + "/api/applications?name={name}", ApplicationList.class,
145
				Collections.singletonMap("name", name));
146 147 148 149 150 151 152 153
		return (ResponseEntity<Collection<Application>>) getResponse;
	}

	public static class ApplicationList extends ArrayList<Application> {
		private static final long serialVersionUID = 1L;
		// needed for JSON deserialization
	}

154
}