Update spring-boot to 2.4
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / ConfigurationTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.rest;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
29
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;
45
46 import io.prometheus.client.exporter.common.TextFormat;
47
48 /**
49  * Test REST requests against configuration resource
50  */
51 @AutoConfigureMetrics
52 @TestPropertySource(locations = "classpath:application-test.properties")
53 @ContextConfiguration(
54     initializers = PropertyPasswordConfiguration.class,
55     classes = {SpringContextAware.class})
56 @Import(TraversalTestConfiguration.class)
57 @SpringBootTest(
58     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
59     classes = {SpringContextAware.class, TraversalApp.class})
60 public class ConfigurationTest extends AbstractSpringRestTest {
61     @Autowired
62     RestTemplate restTemplate;
63
64     @Value("${local.management.port}")
65     private int mgtPort;
66
67     private HttpEntity<String> httpEntity;
68     private String actuatorurl;
69     private HttpHeaders headers;
70
71     @Before
72     public void setup() throws UnsupportedEncodingException {
73
74         headers = new HttpHeaders();
75
76         headers.set("Accept", "text/plain");
77         headers.add("Real-Time", "true");
78         headers.add("X-FromAppId", "JUNIT");
79         headers.add("X-TransactionId", "JUNIT");
80
81         String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
82         headers.add("Authorization", "Basic " + authorization);
83
84         httpEntity = new HttpEntity<String>(headers);
85         baseUrl = "http://localhost:" + randomPort;
86         actuatorurl = "http://localhost:" + mgtPort;
87     }
88
89     @Test
90     public void TestManagementEndpointConfiguration() {
91         ResponseEntity<String> responseEntity = null;
92         String responseBody = null;
93
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"));
102
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"));
112
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"));
118     }
119 }