SLAs for async methods
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestActuatorEndpoints.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
24 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
25 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.runner.RunWith;
33 import org.onap.policy.common.utils.services.Registry;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.context.TestConfiguration;
38 import org.springframework.core.annotation.Order;
39 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
40 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
41 import org.springframework.test.annotation.DirtiesContext;
42 import org.springframework.test.context.ActiveProfiles;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
47 import org.springframework.web.context.WebApplicationContext;
48
49 @RunWith(SpringRunner.class)
50 @SpringBootTest(webEnvironment = RANDOM_PORT)
51 @ActiveProfiles("test")
52 @AutoConfigureMockMvc
53 @ContextConfiguration
54 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
55 public class TestActuatorEndpoints {
56
57     @Autowired
58     private WebApplicationContext context;
59
60     @Autowired
61     private TestSecurityConfig securityConfig;
62
63     @Autowired
64     private MockMvc mock;
65
66     @BeforeClass
67     public static void setupClass() {
68         Registry.newRegistry();
69     }
70
71     @BeforeEach
72     void setup() {
73         mock = MockMvcBuilders.webAppContextSetup(context).build();
74     }
75
76     @Test
77     public void testMetricsEndpoint() throws Exception {
78         mock.perform(get("/plain-metrics")).andDo(print())
79             .andExpect(status().isOk())
80             .andExpect(jsonPath("$").isNotEmpty());
81     }
82
83     @Test
84     public void testPrometheusEndpoint() throws Exception {
85         mock.perform(get("/metrics")).andDo(print())
86             .andExpect(status().isOk())
87             .andExpect(jsonPath("$").isNotEmpty());
88     }
89
90     @TestConfiguration
91     @Order(1)
92     public static class TestSecurityConfig extends WebSecurityConfigurerAdapter {
93         @Override
94         protected void configure(HttpSecurity httpSecurity) throws Exception {
95             httpSecurity.csrf().disable()
96                 .authorizeRequests().anyRequest().permitAll();
97         }
98     }
99 }