3fdb1f42875df207e64a20447f4dd735bab7af4c
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestActuatorEndpoints.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
38 import org.springframework.test.annotation.DirtiesContext;
39 import org.springframework.test.context.ActiveProfiles;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringRunner;
42 import org.springframework.test.web.servlet.MockMvc;
43 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
44 import org.springframework.web.context.WebApplicationContext;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(webEnvironment = RANDOM_PORT)
48 @ActiveProfiles("test")
49 @AutoConfigureMockMvc
50 @ContextConfiguration
51 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
52 public class TestActuatorEndpoints {
53
54     @Autowired
55     private WebApplicationContext context;
56
57     @Autowired
58     private MockMvc mock;
59
60     @BeforeClass
61     public static void setupClass() {
62         Registry.newRegistry();
63     }
64
65     @BeforeEach
66     void setup() {
67         mock = MockMvcBuilders.webAppContextSetup(context).build();
68     }
69
70     @Test
71     public void testMetricsEndpoint() throws Exception {
72         mock.perform(get("/plain-metrics").with(SecurityMockMvcRequestPostProcessors.httpBasic(
73             "policyAdmin", "zb!XztG34")))
74             .andDo(print())
75             .andExpect(status().isOk())
76             .andExpect(jsonPath("$").isNotEmpty());
77     }
78
79     @Test
80     public void testPrometheusEndpoint() throws Exception {
81         mock.perform(get("/metrics").with(SecurityMockMvcRequestPostProcessors.httpBasic("policyAdmin", "zb!XztG34")))
82             .andDo(print())
83             .andExpect(status().isOk())
84             .andExpect(jsonPath("$").isNotEmpty());
85     }
86 }