Skip to content

Commit 65f039c

Browse files
authored
Merge pull request #468 from kit-data-manager/addMonitoring
Add a test for monitoring supported by new repo-core library.
2 parents ea3cf5c + 76409ed commit 65f039c

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2025 Karlsruhe Institute of Technology.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package edu.kit.datamanager.repo.test.integration;
17+
18+
import org.hamcrest.Matchers;
19+
import org.junit.Before;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.autoconfigure.domain.EntityScan;
25+
import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.context.annotation.ComponentScan;
29+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
30+
import org.springframework.restdocs.JUnitRestDocumentation;
31+
import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener;
32+
import org.springframework.test.annotation.DirtiesContext;
33+
import org.springframework.test.context.ActiveProfiles;
34+
import org.springframework.test.context.TestExecutionListeners;
35+
import org.springframework.test.context.TestPropertySource;
36+
import org.springframework.test.context.junit4.SpringRunner;
37+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
38+
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
39+
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
40+
import org.springframework.test.context.web.ServletTestExecutionListener;
41+
import org.springframework.test.web.servlet.MockMvc;
42+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
43+
import org.springframework.web.context.WebApplicationContext;
44+
45+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
46+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
47+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
48+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
49+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
50+
51+
/**
52+
* Test for the Prometheus actuator endpoint.
53+
* This test checks that the Prometheus endpoint is correctly exposed and that it contains the expected metrics.
54+
* Also checks that other actuator endpoints are not exposed.
55+
*/
56+
@RunWith(SpringRunner.class)
57+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
58+
@EntityScan("edu.kit.datamanager")
59+
@EnableJpaRepositories("edu.kit.datamanager")
60+
@ComponentScan({"edu.kit.datamanager"})
61+
@AutoConfigureMockMvc
62+
@TestExecutionListeners(listeners = {ServletTestExecutionListener.class,
63+
DependencyInjectionTestExecutionListener.class,
64+
DirtiesContextTestExecutionListener.class,
65+
TransactionalTestExecutionListener.class,
66+
WithSecurityContextTestExecutionListener.class})
67+
@ActiveProfiles("test")
68+
@TestPropertySource(properties = {"spring.datasource.url=jdbc:h2:mem:db_prometheus;DB_CLOSE_DELAY=-1;MODE=LEGACY;NON_KEYWORDS=VALUE"})
69+
@TestPropertySource(properties = {"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect"})
70+
@TestPropertySource(properties = {"spring.jpa.defer-datasource-initialization=true"})
71+
@TestPropertySource(properties = {"repo.monitoring.enabled=true"})
72+
@TestPropertySource(properties = {"repo.monitoring.serviceName=base_repo_test"})
73+
@TestPropertySource(properties = {"management.endpoints.web.exposure.include=prometheus"})
74+
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
75+
@AutoConfigureObservability
76+
public class ActuatorPrometheusTest {
77+
78+
@Rule
79+
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
80+
private MockMvc mockMvc;
81+
@Autowired
82+
private WebApplicationContext context;
83+
84+
@Before
85+
public void setUp() {
86+
87+
// setup mockMvc
88+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
89+
.apply(springSecurity())
90+
.build();
91+
}
92+
93+
@Test
94+
public void testForNotExposedActuators() throws Exception {
95+
// Check that all other endpoints are not exposed
96+
this.mockMvc.perform(get("/actuator/beans")).andDo(print()).andExpect(status().isNotFound());
97+
this.mockMvc.perform(get("/actuator/caches")).andDo(print()).andExpect(status().isNotFound());
98+
this.mockMvc.perform(get("/actuator/conditions")).andDo(print()).andExpect(status().isNotFound());
99+
this.mockMvc.perform(get("/actuator/configprops")).andDo(print()).andExpect(status().isNotFound());
100+
this.mockMvc.perform(get("/actuator/env")).andDo(print()).andExpect(status().isNotFound());
101+
this.mockMvc.perform(get("/actuator/loggers")).andDo(print()).andExpect(status().isNotFound());
102+
this.mockMvc.perform(get("/actuator/heapdump")).andDo(print()).andExpect(status().isNotFound());
103+
this.mockMvc.perform(get("/actuator/threaddump")).andDo(print()).andExpect(status().isNotFound());
104+
this.mockMvc.perform(get("/actuator/metrics")).andDo(print()).andExpect(status().isNotFound());
105+
this.mockMvc.perform(get("/actuator/scheduledtasks")).andDo(print()).andExpect(status().isNotFound());
106+
this.mockMvc.perform(get("/actuator/mappings")).andDo(print()).andExpect(status().isNotFound());
107+
}
108+
109+
@Test
110+
public void testActuator() throws Exception {
111+
// /actuator/prometheus
112+
this.mockMvc.perform(get("/actuator/prometheus")).andDo(print()).andExpect(status().isOk())
113+
.andExpect(content().string(Matchers.containsString("# TYPE base_repo_test_requests_served_total")))
114+
.andExpect(content().string(Matchers.containsString("# TYPE base_repo_test_unique_users")))
115+
.andExpect(content().string(Matchers.containsString("# TYPE base_repo_test_registered_users")))
116+
.andReturn();
117+
}
118+
}

0 commit comments

Comments
 (0)