Allow semantic versioning in all templates in pap
[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.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;
27
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;
45
46 @SpringBootTest(classes = PolicyPapApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
47     properties = {"db.initialize=false"})
48 @ActiveProfiles("test")
49 @AutoConfigureMockMvc
50 @AutoConfigureObservability
51 @ContextConfiguration
52 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
53 public class TestActuatorEndpoints {
54
55     @Autowired
56     private WebApplicationContext context;
57
58     @Autowired
59     private MockMvc mock;
60
61     private final RequestPostProcessor mockSecurity = SecurityMockMvcRequestPostProcessors
62         .httpBasic("policyAdmin", "zb!XztG34");
63
64     @BeforeAll
65     public static void setupClass() {
66         Registry.newRegistry();
67     }
68
69     @BeforeEach
70     void setup() {
71         mock = MockMvcBuilders.webAppContextSetup(context).build();
72     }
73
74     @Test
75     void testMetricsEndpoint() throws Exception {
76         mock.perform(get("/plain-metrics").with(mockSecurity))
77             .andDo(print())
78             .andExpect(status().isOk())
79             .andExpect(jsonPath("$").isNotEmpty());
80     }
81
82     @Test
83     void testPrometheusEndpoint() throws Exception {
84         mock.perform(get("/metrics").with(mockSecurity))
85             .andDo(print())
86             .andExpect(status().isOk())
87             .andExpect(jsonPath("$").isNotEmpty());
88     }
89 }