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.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;
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;
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(webEnvironment = RANDOM_PORT)
48 @ActiveProfiles("test")
51 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
52 public class TestActuatorEndpoints {
55 private WebApplicationContext context;
61 public static void setupClass() {
62 Registry.newRegistry();
67 mock = MockMvcBuilders.webAppContextSetup(context).build();
71 public void testMetricsEndpoint() throws Exception {
72 mock.perform(get("/plain-metrics").with(SecurityMockMvcRequestPostProcessors.httpBasic(
73 "policyAdmin", "zb!XztG34")))
75 .andExpect(status().isOk())
76 .andExpect(jsonPath("$").isNotEmpty());
80 public void testPrometheusEndpoint() throws Exception {
81 mock.perform(get("/metrics").with(SecurityMockMvcRequestPostProcessors.httpBasic("policyAdmin", "zb!XztG34")))
83 .andExpect(status().isOk())
84 .andExpect(jsonPath("$").isNotEmpty());