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

20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23 24 25 26

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
27
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
28 29 30
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
31
import org.springframework.context.annotation.Configuration;
32 33
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
34
import org.springframework.test.context.junit4.SpringRunner;
35

36 37 38
import de.codecentric.boot.admin.AdminApplicationTest.TestAdminApplication;
import de.codecentric.boot.admin.config.EnableAdminServer;

39 40
/**
 * Integration test to verify the correct functionality of the REST API.
41
 *
42 43
 * @author Dennis Schulte
 */
44
@RunWith(SpringRunner.class)
45 46
@SpringBootTest(classes = TestAdminApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
		"spring.cloud.config.enabled=false" })
47 48 49 50
public class AdminApplicationTest {

	@Value("${local.server.port}")
	private int port = 0;
51

52
	@Test
53
	public void testGetApplications() {
54
		@SuppressWarnings("rawtypes")
55 56
		ResponseEntity<List> entity = new TestRestTemplate()
				.getForEntity("http://localhost:" + port + "/api/applications", List.class);
57 58
		assertEquals(HttpStatus.OK, entity.getStatusCode());
	}
59

60 61 62 63
	@Test
	public void testReverseProxy() {
		String apiBaseUrl = "http://localhost:" + port + "/api/applications";

64 65 66 67 68 69 70 71 72
		Map<String, String> application = new HashMap<>();
		application.put("name", "TestApp");
		application.put("managementUrl", "http://localhost:" + port);
		application.put("serviceUrl", "http://localhost:" + port);
		application.put("healthUrl", "http://localhost:" + port + "/health");

		@SuppressWarnings("unchecked")
		ResponseEntity<Map<String, String>> entity = new TestRestTemplate().postForEntity(
				apiBaseUrl, application, (Class<Map<String, String>>) (Class<?>) Map.class);
73 74

		@SuppressWarnings("rawtypes")
75
		ResponseEntity<Map> app = new TestRestTemplate()
76
				.getForEntity(apiBaseUrl + "/" + entity.getBody().get("id"), Map.class);
77 78 79 80 81
		assertEquals(HttpStatus.OK, app.getStatusCode());
		assertEquals("TestApp", app.getBody().get("name"));

		@SuppressWarnings("rawtypes")
		ResponseEntity<Map> info = new TestRestTemplate()
82
				.getForEntity(apiBaseUrl + "/" + entity.getBody().get("id") + "/info", Map.class);
83 84 85
		assertEquals(HttpStatus.OK, info.getStatusCode());

		@SuppressWarnings("rawtypes")
86
		ResponseEntity<Map> health = new TestRestTemplate()
87
				.getForEntity(apiBaseUrl + "/" + entity.getBody().get("id") + "/health", Map.class);
88 89
		assertEquals(HttpStatus.OK, health.getStatusCode());

90
	}
91

92 93 94 95 96
	@Configuration
	@EnableAutoConfiguration
	@EnableAdminServer
	public static class TestAdminApplication {
	}
97
}