-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
EndpointMBeanTests.java
212 lines (180 loc) · 8.37 KB
/
EndpointMBeanTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.endpoint.jmx;
import java.net.URL;
import java.net.URLClassLoader;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.ReflectionException;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.FatalBeanException;
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.InvocationContext;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
/**
* Tests for {@link EndpointMBean}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
class EndpointMBeanTests {
private static final Object[] NO_PARAMS = {};
private static final String[] NO_SIGNATURE = {};
private TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(new TestJmxOperation());
private TestJmxOperationResponseMapper responseMapper = new TestJmxOperationResponseMapper();
@Test
void createWhenResponseMapperIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new EndpointMBean(null, null, mock(ExposableJmxEndpoint.class)))
.withMessageContaining("ResponseMapper must not be null");
}
@Test
void createWhenEndpointIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new EndpointMBean(mock(JmxOperationResponseMapper.class), null, null))
.withMessageContaining("Endpoint must not be null");
}
@Test
void getMBeanInfoShouldReturnMBeanInfo() {
EndpointMBean bean = createEndpointMBean();
MBeanInfo info = bean.getMBeanInfo();
assertThat(info.getDescription()).isEqualTo("MBean operations for endpoint test");
}
@Test
void invokeShouldInvokeJmxOperation() throws MBeanException, ReflectionException {
EndpointMBean bean = createEndpointMBean();
Object result = bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
assertThat(result).isEqualTo("result");
}
@Test
void invokeWhenOperationFailedShouldTranslateException() {
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(new TestJmxOperation((arguments) -> {
throw new FatalBeanException("test failure");
}));
EndpointMBean bean = new EndpointMBean(this.responseMapper, null, endpoint);
assertThatExceptionOfType(MBeanException.class)
.isThrownBy(() -> bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE))
.withCauseInstanceOf(IllegalStateException.class).withMessageContaining("test failure");
}
@Test
void invokeWhenOperationFailedWithJdkExceptionShouldReuseException() {
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(new TestJmxOperation((arguments) -> {
throw new UnsupportedOperationException("test failure");
}));
EndpointMBean bean = new EndpointMBean(this.responseMapper, null, endpoint);
assertThatExceptionOfType(MBeanException.class)
.isThrownBy(() -> bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE))
.withCauseInstanceOf(UnsupportedOperationException.class).withMessageContaining("test failure");
}
@Test
void invokeWhenActionNameIsNotAnOperationShouldThrowException() {
EndpointMBean bean = createEndpointMBean();
assertThatExceptionOfType(ReflectionException.class)
.isThrownBy(() -> bean.invoke("missingOperation", NO_PARAMS, NO_SIGNATURE))
.withCauseInstanceOf(IllegalArgumentException.class)
.withMessageContaining("no operation named missingOperation");
}
@Test
void invokeShouldInvokeJmxOperationWithBeanClassLoader() throws ReflectionException, MBeanException {
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(
new TestJmxOperation((arguments) -> ClassUtils.getDefaultClassLoader()));
URLClassLoader beanClassLoader = new URLClassLoader(new URL[0], getClass().getClassLoader());
EndpointMBean bean = new EndpointMBean(this.responseMapper, beanClassLoader, endpoint);
Object result = bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
assertThat(result).isEqualTo(beanClassLoader);
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(originalClassLoader);
}
@Test
void invokeWhenOperationIsInvalidShouldThrowException() {
TestJmxOperation operation = new TestJmxOperation() {
@Override
public Object invoke(InvocationContext context) {
throw new InvalidEndpointRequestException("test failure", "test");
}
};
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(operation);
EndpointMBean bean = new EndpointMBean(this.responseMapper, null, endpoint);
assertThatExceptionOfType(ReflectionException.class)
.isThrownBy(() -> bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE))
.withRootCauseInstanceOf(IllegalArgumentException.class).withMessageContaining("test failure");
}
@Test
void invokeWhenMonoResultShouldBlockOnMono() throws MBeanException, ReflectionException {
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(
new TestJmxOperation((arguments) -> Mono.just("monoResult")));
EndpointMBean bean = new EndpointMBean(this.responseMapper, null, endpoint);
Object result = bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
assertThat(result).isEqualTo("monoResult");
}
@Test
void invokeWhenFluxResultShouldCollectToMonoListAndBlockOnMono() throws MBeanException, ReflectionException {
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(
new TestJmxOperation((arguments) -> Flux.just("flux", "result")));
EndpointMBean bean = new EndpointMBean(this.responseMapper, null, endpoint);
Object result = bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
assertThat(result).asList().containsExactly("flux", "result");
}
@Test
void invokeShouldCallResponseMapper() throws MBeanException, ReflectionException {
TestJmxOperationResponseMapper responseMapper = spy(this.responseMapper);
EndpointMBean bean = new EndpointMBean(responseMapper, null, this.endpoint);
bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
then(responseMapper).should().mapResponseType(String.class);
then(responseMapper).should().mapResponse("result");
}
@Test
void getAttributeShouldThrowException() {
EndpointMBean bean = createEndpointMBean();
assertThatExceptionOfType(AttributeNotFoundException.class).isThrownBy(() -> bean.getAttribute("test"))
.withMessageContaining("EndpointMBeans do not support attributes");
}
@Test
void setAttributeShouldThrowException() {
EndpointMBean bean = createEndpointMBean();
assertThatExceptionOfType(AttributeNotFoundException.class)
.isThrownBy(() -> bean.setAttribute(new Attribute("test", "test")))
.withMessageContaining("EndpointMBeans do not support attributes");
}
@Test
void getAttributesShouldReturnEmptyAttributeList() {
EndpointMBean bean = createEndpointMBean();
AttributeList attributes = bean.getAttributes(new String[] { "test" });
assertThat(attributes).isEmpty();
}
@Test
void setAttributesShouldReturnEmptyAttributeList() {
EndpointMBean bean = createEndpointMBean();
AttributeList sourceAttributes = new AttributeList();
sourceAttributes.add(new Attribute("test", "test"));
AttributeList attributes = bean.setAttributes(sourceAttributes);
assertThat(attributes).isEmpty();
}
private EndpointMBean createEndpointMBean() {
return new EndpointMBean(this.responseMapper, null, this.endpoint);
}
}