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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
25 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.onap.policy.pap.main.PolicyPapApplication;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
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.web.servlet.MockMvc;
42 import org.springframework.test.web.servlet.request.RequestPostProcessor;
43 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
44 import org.springframework.web.context.WebApplicationContext;
46 @SpringBootTest(classes = PolicyPapApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
47 properties = {"db.initialize=false"})
48 @ActiveProfiles("test")
50 @AutoConfigureObservability
52 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
53 public class TestActuatorEndpoints {
56 private WebApplicationContext context;
61 private final RequestPostProcessor mockSecurity = SecurityMockMvcRequestPostProcessors
62 .httpBasic("policyAdmin", "zb!XztG34");
65 public static void setupClass() {
66 Registry.newRegistry();
71 mock = MockMvcBuilders.webAppContextSetup(context).build();
75 void testMetricsEndpoint() throws Exception {
76 mock.perform(get("/plain-metrics").with(mockSecurity))
78 .andExpect(status().isOk())
79 .andExpect(jsonPath("$").isNotEmpty());
83 void testPrometheusEndpoint() throws Exception {
84 mock.perform(get("/metrics").with(mockSecurity))
86 .andExpect(status().isOk())
87 .andExpect(jsonPath("$").isNotEmpty());