-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUserMetricsInterceptorTest.java
205 lines (171 loc) · 7.61 KB
/
UserMetricsInterceptorTest.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
package bio.terra.app.usermetrics;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import bio.terra.app.configuration.ApplicationConfiguration;
import bio.terra.app.configuration.UserMetricsConfiguration;
import bio.terra.common.category.Unit;
import bio.terra.common.exception.UnauthorizedException;
import bio.terra.common.fixtures.AuthenticationFixtures;
import bio.terra.common.iam.AuthenticatedUserRequest;
import bio.terra.common.iam.AuthenticatedUserRequestFactory;
import bio.terra.model.CloudPlatform;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
@ActiveProfiles({"google", "unittest"})
@ContextConfiguration(classes = UserLoggingMetrics.class)
@SpringBootTest
@Tag(Unit.TAG)
class UserMetricsInterceptorTest {
@Autowired private UserLoggingMetrics eventProperties;
@MockBean private BardClient bardClient;
@MockBean private AuthenticatedUserRequestFactory authenticatedUserRequestFactory;
@MockBean private ApplicationConfiguration applicationConfiguration;
@MockBean private UserMetricsConfiguration metricsConfig;
private ExecutorService metricsPerformanceThreadpool;
private UserMetricsInterceptor userMetricsInterceptor;
@Captor private ArgumentCaptor<AuthenticatedUserRequest> authCaptor;
@Mock HttpServletRequest request;
@Mock HttpServletResponse response;
private static final String APP_ID = "testapp";
private static final String BARD_BASE_PATH = "https://bard.com";
private static final String TOKEN = "footoken";
private static final String DNS_NAME = "some.dnsname.org";
private static final String METHOD = "post";
private static final String REQUEST_URI = "/foo/bar";
private static final Integer NUM_EXECUTOR_THREADS = 3;
private static final AuthenticatedUserRequest TEST_USER =
AuthenticationFixtures.userRequest(TOKEN);
@BeforeEach
void setUp() {
eventProperties.get().clear();
when(metricsConfig.ignorePaths()).thenReturn(List.of());
when(metricsConfig.appId()).thenReturn(APP_ID);
when(metricsConfig.bardBasePath()).thenReturn(BARD_BASE_PATH);
when(applicationConfiguration.getDnsName()).thenReturn(DNS_NAME);
when(request.getMethod()).thenReturn(METHOD.toLowerCase());
when(request.getRequestURI()).thenReturn(REQUEST_URI);
metricsPerformanceThreadpool = Executors.newFixedThreadPool(NUM_EXECUTOR_THREADS);
userMetricsInterceptor =
new UserMetricsInterceptor(
bardClient,
authenticatedUserRequestFactory,
applicationConfiguration,
metricsConfig,
eventProperties,
metricsPerformanceThreadpool);
}
@AfterEach
void tearDown() {
if (metricsPerformanceThreadpool != null) {
metricsPerformanceThreadpool.shutdownNow();
}
}
private void mockRequestAuth(HttpServletRequest request) {
when(authenticatedUserRequestFactory.from(request)).thenReturn(TEST_USER);
}
@Test
void testSendEvent() throws Exception {
mockRequestAuth(request);
runAndWait();
verify(bardClient)
.logEvent(
authCaptor.capture(),
eq(
new BardEvent(
UserMetricsInterceptor.API_EVENT_NAME,
Map.of(
BardEventProperties.METHOD_FIELD_NAME,
METHOD.toUpperCase(),
BardEventProperties.PATH_FIELD_NAME,
REQUEST_URI),
APP_ID,
DNS_NAME)));
assertThat("token is correct", authCaptor.getValue().getToken(), equalTo(TOKEN));
}
@Test
void testSendEventWithAdditionalProperties() throws Exception {
UUID datasetId = UUID.randomUUID();
String datasetName = "datasetName";
UUID snapshotId = UUID.randomUUID();
String snapshotName = "snapshotName";
String billingProfileId = UUID.randomUUID().toString();
CloudPlatform cloudPlatform = CloudPlatform.GCP;
eventProperties.set(BardEventProperties.DATASET_ID_FIELD_NAME, datasetId);
eventProperties.set(BardEventProperties.DATASET_NAME_FIELD_NAME, datasetName);
eventProperties.set(BardEventProperties.SNAPSHOT_ID_FIELD_NAME, snapshotId);
eventProperties.set(BardEventProperties.SNAPSHOT_NAME_FIELD_NAME, snapshotName);
eventProperties.set(BardEventProperties.BILLING_PROFILE_ID_FIELD_NAME, billingProfileId);
eventProperties.set(BardEventProperties.CLOUD_PLATFORM_FIELD_NAME, cloudPlatform);
mockRequestAuth(request);
runAndWait();
verify(bardClient)
.logEvent(
authCaptor.capture(),
eq(
new BardEvent(
UserMetricsInterceptor.API_EVENT_NAME,
Map.of(
BardEventProperties.METHOD_FIELD_NAME, METHOD.toUpperCase(),
BardEventProperties.PATH_FIELD_NAME, REQUEST_URI,
BardEventProperties.DATASET_ID_FIELD_NAME, datasetId,
BardEventProperties.DATASET_NAME_FIELD_NAME, datasetName,
BardEventProperties.SNAPSHOT_ID_FIELD_NAME, snapshotId,
BardEventProperties.SNAPSHOT_NAME_FIELD_NAME, snapshotName,
BardEventProperties.BILLING_PROFILE_ID_FIELD_NAME, billingProfileId,
BardEventProperties.CLOUD_PLATFORM_FIELD_NAME, cloudPlatform),
APP_ID,
DNS_NAME)));
assertThat("token is correct", authCaptor.getValue().getToken(), equalTo(TOKEN));
}
@Test
void testSendEventNotFiredWithNoBardBasePath() throws Exception {
when(metricsConfig.bardBasePath()).thenReturn(null);
runAndWait();
verify(bardClient, never()).logEvent(any(), any());
}
@Test
void testSendEventNotFiredWithNoToken() throws Exception {
when(authenticatedUserRequestFactory.from(any()))
.thenThrow(new UnauthorizedException("Building AuthenticatedUserRequest failed"));
runAndWait();
verify(bardClient, never()).logEvent(any(), any());
}
@ParameterizedTest
@ValueSource(strings = {REQUEST_URI, "/foo/*"})
void testSendEventNotFiredWithIgnoredUrl(String ignorePath) throws Exception {
when(metricsConfig.ignorePaths()).thenReturn(List.of(ignorePath));
mockRequestAuth(request);
runAndWait();
verify(bardClient, never()).logEvent(any(), any());
}
private void runAndWait() throws Exception {
userMetricsInterceptor.afterCompletion(request, response, new Object(), null);
// This waits for the threadpool to wrap up so that we can examine the results
metricsPerformanceThreadpool.awaitTermination(100, TimeUnit.MILLISECONDS);
}
}