2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.rest;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.aai.TraversalApp;
33 import org.onap.aai.TraversalTestConfiguration;
34 import org.onap.aai.config.SpringContextAware;
35 import org.onap.aai.restclient.PropertyPasswordConfiguration;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.context.annotation.Import;
41 import org.springframework.http.*;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.TestPropertySource;
44 import org.springframework.web.client.RestTemplate;
46 import io.prometheus.client.exporter.common.TextFormat;
49 * Test REST requests against configuration resource
52 @TestPropertySource(locations = "classpath:application-test.properties")
53 @ContextConfiguration(
54 initializers = PropertyPasswordConfiguration.class,
55 classes = {SpringContextAware.class})
56 @Import(TraversalTestConfiguration.class)
58 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
59 classes = {SpringContextAware.class, TraversalApp.class})
60 public class ConfigurationTest extends AbstractSpringRestTest {
62 RestTemplate restTemplate;
64 @Value("${local.management.port}")
67 private HttpEntity<String> httpEntity;
68 private String actuatorurl;
69 private HttpHeaders headers;
72 public void setup() throws UnsupportedEncodingException {
74 headers = new HttpHeaders();
76 headers.set("Accept", "text/plain");
77 headers.add("Real-Time", "true");
78 headers.add("X-FromAppId", "JUNIT");
79 headers.add("X-TransactionId", "JUNIT");
81 String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
82 headers.add("Authorization", "Basic " + authorization);
84 httpEntity = new HttpEntity<String>(headers);
85 baseUrl = "http://localhost:" + randomPort;
86 actuatorurl = "http://localhost:" + mgtPort;
90 public void TestManagementEndpointConfiguration() {
91 ResponseEntity<String> responseEntity = null;
92 String responseBody = null;
94 // set Accept as text/plain in order to get access of endpoint "/actuator/prometheus"
95 responseEntity = restTemplate.exchange(actuatorurl + "/actuator/prometheus", HttpMethod.GET,
96 httpEntity, String.class);
97 responseBody = responseEntity.getBody();
98 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
99 System.out.println("responseBody---------" + responseBody);
100 assertFalse(responseBody.contains("aai_uri"));
101 assertTrue(responseBody.contains("group_id"));
103 // Set Accept as MediaType.APPLICATION_JSON in order to get access of endpoint
104 // "/actuator/info" and "/actuator/health"
105 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
106 httpEntity = new HttpEntity<String>(headers);
107 responseEntity = restTemplate.exchange(actuatorurl + "/actuator/info", HttpMethod.GET,
108 httpEntity, String.class);
109 responseBody = (String) responseEntity.getBody();
110 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
111 assertTrue(responseBody.contains("aai-traversal"));
113 responseEntity = restTemplate.exchange(actuatorurl + "/actuator/health", HttpMethod.GET,
114 httpEntity, String.class);
115 responseBody = (String) responseEntity.getBody();
116 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
117 assertTrue(responseBody.contains("UP"));