-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathApmApiTest.java
376 lines (284 loc) · 11.7 KB
/
ApmApiTest.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.instabug.flutter;
import com.instabug.apm.APM;
import com.instabug.apm.InternalAPM;
import com.instabug.apm.configuration.cp.APMFeature;
import com.instabug.apm.configuration.cp.FeatureAvailabilityCallback;
import com.instabug.apm.model.ExecutionTrace;
import com.instabug.apm.networking.APMNetworkLogger;
import com.instabug.flutter.generated.ApmPigeon;
import com.instabug.flutter.modules.ApmApi;
import com.instabug.flutter.util.GlobalMocks;
import com.instabug.flutter.util.MockReflected;
import io.flutter.plugin.common.BinaryMessenger;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import java.util.HashMap;
import java.util.Map;
import static com.instabug.flutter.util.GlobalMocks.reflected;
import static com.instabug.flutter.util.MockResult.makeResult;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
public class ApmApiTest {
private final BinaryMessenger mMessenger = mock(BinaryMessenger.class);
private final ApmApi api = new ApmApi();
private MockedStatic<APM> mAPM;
private MockedStatic<InternalAPM> mInternalApmStatic;
private MockedStatic<ApmPigeon.ApmHostApi> mHostApi;
@Before
public void setUp() throws NoSuchMethodException {
mAPM = mockStatic(APM.class);
mInternalApmStatic = mockStatic(InternalAPM.class);
mHostApi = mockStatic(ApmPigeon.ApmHostApi.class);
GlobalMocks.setUp();
}
@After
public void cleanUp() {
mInternalApmStatic.close();
mAPM.close();
mHostApi.close();
GlobalMocks.close();
}
private ExecutionTrace mockTrace(String id) {
String name = "trace-name";
ExecutionTrace mTrace = mock(ExecutionTrace.class);
mAPM.when(() -> APM.startExecutionTrace(name)).thenReturn(mTrace);
api.startExecutionTrace(id, name, makeResult());
return mTrace;
}
@Test
public void testInit() {
BinaryMessenger messenger = mock(BinaryMessenger.class);
ApmApi.init(messenger);
mHostApi.verify(() -> ApmPigeon.ApmHostApi.setup(eq(messenger), any(ApmApi.class)));
}
@Test
public void testSetEnabled() {
boolean isEnabled = false;
api.setEnabled(isEnabled);
mAPM.verify(() -> APM.setEnabled(isEnabled));
}
@Test
public void testSetColdAppLaunchEnabled() {
boolean isEnabled = false;
api.setColdAppLaunchEnabled(isEnabled);
mAPM.verify(() -> APM.setColdAppLaunchEnabled(isEnabled));
}
@Test
public void testSetAutoUITraceEnabled() {
boolean isEnabled = false;
api.setAutoUITraceEnabled(isEnabled);
mAPM.verify(() -> APM.setAutoUITraceEnabled(isEnabled));
}
@Test
public void testStartExecutionTraceWhenTraceNotNull() {
String expectedId = "trace-id";
String name = "trace-name";
ApmPigeon.Result<String> result = makeResult((String actualId) -> assertEquals(expectedId, actualId));
mAPM.when(() -> APM.startExecutionTrace(name)).thenReturn(new ExecutionTrace(name));
api.startExecutionTrace(expectedId, name, result);
mAPM.verify(() -> APM.startExecutionTrace(name));
}
@Test
public void testStartExecutionTraceWhenTraceIsNull() {
String id = "trace-id";
String name = "trace-name";
ApmPigeon.Result<String> result = makeResult(Assert::assertNull);
mAPM.when(() -> APM.startExecutionTrace(name)).thenReturn(null);
api.startExecutionTrace(id, name, result);
mAPM.verify(() -> APM.startExecutionTrace(name));
}
@Test
public void testSetExecutionTraceAttribute() {
String id = "trace-id";
String key = "is_premium";
String value = "true";
ExecutionTrace mTrace = mockTrace(id);
api.setExecutionTraceAttribute(id, key, value);
verify(mTrace).setAttribute(key, value);
}
@Test
public void testEndExecutionTrace() {
String id = "trace-id";
ExecutionTrace mTrace = mockTrace(id);
api.endExecutionTrace(id);
verify(mTrace).end();
}
@Test
public void testStartFlow() {
String appFlowName = "appFlowName";
api.startFlow(appFlowName);
mAPM.verify(() -> APM.startFlow(appFlowName));
mAPM.verifyNoMoreInteractions();
}
@Test
public void testEndFlow() {
String appFlowName = "appFlowName";
api.startFlow(appFlowName);
mAPM.verify(() -> APM.startFlow(appFlowName));
mAPM.verifyNoMoreInteractions();
}
@Test
public void testSetFlowAttribute() {
String appFlowName = "appFlowName";
String flowAttributeKey = "attributeKey";
String flowAttributeValue = "attributeValue";
api.setFlowAttribute(appFlowName, flowAttributeKey, flowAttributeValue);
mAPM.verify(() -> APM.setFlowAttribute(appFlowName, flowAttributeKey, flowAttributeValue));
mAPM.verifyNoMoreInteractions();
}
@Test
public void testStartUITrace() {
String name = "login";
api.startUITrace(name);
mAPM.verify(() -> APM.startUITrace(name));
}
@Test
public void testEndUITrace() {
api.endUITrace();
mAPM.verify(APM::endUITrace);
}
@Test
public void testEndAppLaunch() {
api.endAppLaunch();
mAPM.verify(APM::endAppLaunch);
}
@Test
public void testNetworkLogAndroid() {
Map<String, Object> data = new HashMap<>();
String requestUrl = "https://example.com";
String requestBody = "hi";
String responseBody = "{\"hello\":\"world\"}";
String requestMethod = "POST";
String requestContentType = "text/plain";
String responseContentType = "application/json";
long requestBodySize = 20;
long responseBodySize = 50;
int responseCode = 401;
long requestDuration = 23000;
long requestStartTime = System.currentTimeMillis() / 1000;
HashMap<String, String> requestHeaders = new HashMap<>();
HashMap<String, String> responseHeaders = new HashMap<>();
String errorDomain = "ERROR_DOMAIN";
String serverErrorMessage = "SERVER_ERROR_MESSAGE";
data.put("url", requestUrl);
data.put("requestBody", requestBody);
data.put("responseBody", responseBody);
data.put("method", requestMethod);
data.put("requestContentType", requestContentType);
data.put("responseContentType", responseContentType);
data.put("requestBodySize", requestBodySize);
data.put("responseBodySize", responseBodySize);
data.put("errorDomain", errorDomain);
data.put("responseCode", responseCode);
data.put("requestDuration", requestDuration);
data.put("startTime", requestStartTime);
data.put("requestHeaders", requestHeaders);
data.put("responseHeaders", responseHeaders);
data.put("duration", requestDuration);
data.put("serverErrorMessage", serverErrorMessage);
MockedConstruction<APMNetworkLogger> mAPMNetworkLogger = mockConstruction(APMNetworkLogger.class);
MockedConstruction<JSONObject> mJSONObject = mockConstruction(JSONObject.class, (mock, context) -> when(mock.toString(anyInt())).thenReturn("{}"));
api.networkLogAndroid(data);
reflected.verify(() -> MockReflected.apmNetworkLog(
requestStartTime * 1000,
requestDuration / 1000,
"{}",
requestBody,
requestBodySize,
requestMethod,
requestUrl,
requestContentType,
"{}",
responseBody,
responseBodySize,
responseCode,
responseContentType,
errorDomain,
null,
serverErrorMessage,
null
));
mAPMNetworkLogger.close();
mJSONObject.close();
}
@Test
public void testStartUiTraceCP() {
String screenName = "screen-name";
long microTimeStamp = System.currentTimeMillis() / 1000;
long traceId = System.currentTimeMillis();
api.startCpUiTrace(screenName, microTimeStamp, traceId);
mInternalApmStatic.verify(() -> InternalAPM._startUiTraceCP(screenName, microTimeStamp, traceId));
mInternalApmStatic.verifyNoMoreInteractions();
}
@Test
public void testReportScreenLoadingCP() {
long startTimeStampMicro = System.currentTimeMillis() / 1000;
long durationMicro = System.currentTimeMillis() / 1000;
long uiTraceId = System.currentTimeMillis();
api.reportScreenLoadingCP(startTimeStampMicro, durationMicro, uiTraceId);
mInternalApmStatic.verify(() -> InternalAPM._reportScreenLoadingCP(startTimeStampMicro, durationMicro, uiTraceId));
mInternalApmStatic.verifyNoMoreInteractions();
}
@Test
public void testEndScreenLoading() {
long timeStampMicro = System.currentTimeMillis() / 1000;
long uiTraceId = System.currentTimeMillis();
api.endScreenLoadingCP(timeStampMicro, uiTraceId);
mInternalApmStatic.verify(() -> InternalAPM._endScreenLoadingCP(timeStampMicro, uiTraceId));
mInternalApmStatic.verifyNoMoreInteractions();
}
@Test
public void testIsEnabled() {
boolean expected = true;
ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual)));
mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(eq(APMFeature.SCREEN_LOADING), any(), any(FeatureAvailabilityCallback.class))).thenAnswer(invocation -> {
FeatureAvailabilityCallback callback = invocation.getArgument(1);
callback.invoke(expected);
return null;
});
api.isEnabled(result);
verify(result).success(expected);
}
@Test
public void testIsScreenLoadingEnabled() {
boolean expected = true;
ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual)));
mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())).thenAnswer(
invocation -> {
FeatureAvailabilityCallback callback = (FeatureAvailabilityCallback) invocation.getArguments()[2];
callback.invoke(expected);
return null;
});
api.isScreenLoadingEnabled(result);
mInternalApmStatic.verify(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any()));
mInternalApmStatic.verifyNoMoreInteractions();
verify(result).success(expected);
}
@Test
public void testIsEndScreenLoadingEnabled() {
boolean expected = true;
ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual)));
mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())).thenAnswer(
invocation -> {
FeatureAvailabilityCallback callback = (FeatureAvailabilityCallback) invocation.getArguments()[2];
callback.invoke(expected);
return null;
});
api.isEndScreenLoadingEnabled(result);
mInternalApmStatic.verify(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any()));
mInternalApmStatic.verifyNoMoreInteractions();
verify(result).success(expected);
}
@Test
public void testSetScreenLoadingMonitoringEnabled() {
boolean isEnabled = false;
api.setScreenLoadingEnabled(isEnabled);
mAPM.verify(() -> APM.setScreenLoadingEnabled(isEnabled));
}
}