16
16
import org .mockito .ArgumentCaptor ;
17
17
import org .mockito .MockedStatic ;
18
18
import org .mockito .Mockito ;
19
- import org .mockito .invocation .InvocationOnMock ;
20
19
import org .mockito .stubbing .Answer ;
21
20
22
21
import com .instabug .apm .InternalAPM ;
23
22
import com .instabug .reactlibrary .utils .MainThreadHandler ;
24
23
25
- import java .util .concurrent .Executors ;
26
- import java .util .concurrent .ScheduledExecutorService ;
27
-
28
24
public class RNInstabugNetworkLoggerModuleTest {
29
25
30
- // Mock MainThread
31
- private final static ScheduledExecutorService mainThread = Executors .newSingleThreadScheduledExecutor ();
32
-
33
26
// Mock Objects
34
27
private MockedStatic <Looper > mockLooper ;
35
28
private MockedStatic <MainThreadHandler > mockMainThreadHandler ;
36
- private RNInstabugNetworkLoggerModule networkLoggerModule ;
29
+ private RNInstabugNetworkLoggerModule rnInstabugNetworkLoggerModule ;
37
30
private Promise mockPromise ;
38
31
39
32
@ Before
40
- public void mockMainThreadHandler () throws Exception {
33
+ public void mockMainThreadHandler () {
41
34
// Mock Object
42
35
ReactApplicationContext mockReactApplicationContext = mock (ReactApplicationContext .class );
43
36
mockPromise = mock (Promise .class );
44
- networkLoggerModule = new RNInstabugNetworkLoggerModule (mockReactApplicationContext );
37
+ rnInstabugNetworkLoggerModule = new RNInstabugNetworkLoggerModule (mockReactApplicationContext );
45
38
46
39
// Mock static functions
47
40
mockLooper = mockStatic (Looper .class );
@@ -51,12 +44,9 @@ public void mockMainThreadHandler() throws Exception {
51
44
when (Looper .getMainLooper ()).thenReturn (mockMainThreadLooper );
52
45
53
46
// Override runOnMainThread
54
- Answer <Boolean > handlerPostAnswer = new Answer <Boolean >() {
55
- @ Override
56
- public Boolean answer (InvocationOnMock invocation ) throws Throwable {
57
- invocation .getArgument (0 , Runnable .class ).run ();
58
- return true ;
59
- }
47
+ Answer <Boolean > handlerPostAnswer = invocation -> {
48
+ invocation .getArgument (0 , Runnable .class ).run ();
49
+ return true ;
60
50
};
61
51
Mockito .doAnswer (handlerPostAnswer ).when (MainThreadHandler .class );
62
52
MainThreadHandler .runOnMainThread (any (Runnable .class ));
@@ -73,55 +63,52 @@ public void tearDown() {
73
63
@ Test
74
64
public void testGetName () {
75
65
// Test the getName method
76
- String name = networkLoggerModule .getName ();
66
+ String name = rnInstabugNetworkLoggerModule .getName ();
77
67
assertEquals ("IBGNetworkLogger" , name );
78
68
}
79
69
80
70
@ Test
81
71
public void testAddListener () {
82
72
// Test addListener method
83
- networkLoggerModule .addListener ("event_name" );
73
+ rnInstabugNetworkLoggerModule .addListener ("event_name" );
84
74
// Nothing to assert, but check no exceptions are thrown
85
75
}
86
76
87
77
@ Test
88
78
public void testRemoveListeners () {
89
79
// Test removeListeners method
90
- networkLoggerModule .removeListeners (1 );
80
+ rnInstabugNetworkLoggerModule .removeListeners (1 );
91
81
// Nothing to assert, but check no exceptions are thrown
92
82
}
93
83
94
84
@ Test
95
85
public void testIsNativeInterceptionEnabled_True () {
96
86
97
-
98
87
// Mock InternalAPM behavior within the scope of this test
99
88
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
100
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" ))
101
- .thenReturn (true );
89
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" )).thenReturn (true );
102
90
103
91
// Execute the method
104
- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
92
+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
105
93
106
94
// Capture the Promise.resolve() call
107
95
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
108
96
verify (mockPromise ).resolve (captor .capture ());
109
97
110
98
// Assert that true was passed to resolve
99
+ internalAPMMock .verify (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" ));
111
100
assertTrue (captor .getValue ());
112
101
}
113
102
}
114
103
115
104
@ Test
116
105
public void testIsNativeInterceptionEnabled_False () {
117
106
118
-
119
107
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
120
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" ))
121
- .thenReturn (false );
108
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" )).thenReturn (false );
122
109
123
110
// Execute the method
124
- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
111
+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
125
112
126
113
// Capture the Promise.resolve() call
127
114
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -135,14 +122,12 @@ public void testIsNativeInterceptionEnabled_False() {
135
122
@ Test
136
123
public void testIsNativeInterceptionEnabled_Exception () {
137
124
138
-
139
125
// Simulate an exception in InternalAPM
140
126
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
141
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (anyString (), anyString ()))
142
- .thenThrow (new RuntimeException ("Error" ));
127
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (anyString (), anyString ())).thenThrow (new RuntimeException ("Error" ));
143
128
144
129
// Execute the method
145
- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
130
+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
146
131
147
132
// Capture the Promise.resolve() call in case of an exception
148
133
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -156,33 +141,30 @@ public void testIsNativeInterceptionEnabled_Exception() {
156
141
@ Test
157
142
public void testHasAPMNetworkPlugin_True () {
158
143
159
-
160
144
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
161
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" ))
162
- .thenReturn (true );
145
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" )).thenReturn (true );
163
146
164
147
// Execute the method
165
- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
148
+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
166
149
167
150
// Capture the Promise.resolve() call
168
151
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
169
152
verify (mockPromise ).resolve (captor .capture ());
170
153
171
154
// Assert that true was passed to resolve
155
+ internalAPMMock .verify (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" ));
172
156
assertTrue (captor .getValue ());
173
157
}
174
158
}
175
159
176
160
@ Test
177
161
public void testHasAPMNetworkPlugin_False () {
178
162
179
-
180
163
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
181
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" ))
182
- .thenReturn (false );
164
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" )).thenReturn (false );
183
165
184
166
// Execute the method
185
- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
167
+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
186
168
187
169
// Capture the Promise.resolve() call
188
170
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -196,14 +178,12 @@ public void testHasAPMNetworkPlugin_False() {
196
178
@ Test
197
179
public void testHasAPMNetworkPlugin_Exception () {
198
180
199
-
200
181
// Simulate an exception in InternalAPM
201
182
try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
202
- internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (anyString (), anyString ()))
203
- .thenThrow (new RuntimeException ("Error" ));
183
+ internalAPMMock .when (() -> InternalAPM ._isFeatureEnabledCP (anyString (), anyString ())).thenThrow (new RuntimeException ("Error" ));
204
184
205
185
// Execute the method
206
- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
186
+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
207
187
208
188
// Capture the Promise.resolve() call in case of an exception
209
189
ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -213,4 +193,23 @@ public void testHasAPMNetworkPlugin_Exception() {
213
193
assertFalse (captor .getValue ());
214
194
}
215
195
}
196
+
197
+ @ Test
198
+ public void testRegisterNetworkLogsListenerCalled () {
199
+ try (MockedStatic <InternalAPM > internalAPMMock = mockStatic (InternalAPM .class )) {
200
+ // Run the method
201
+ rnInstabugNetworkLoggerModule .registerNetworkLogsListener ();
202
+
203
+ // Verify the sanitizer was registered
204
+ internalAPMMock .verify (() -> InternalAPM ._registerNetworkLogSanitizer (any ()));
205
+ }
206
+ }
207
+
208
+
209
+ @ Test
210
+ public void testUpdateNetworkLogSnapshotInvalidJson () {
211
+ String invalidJsonString = "{\" id\" :\" testId\" " ;
212
+
213
+ assertThrows (RuntimeException .class , () -> rnInstabugNetworkLoggerModule .updateNetworkLogSnapshot (invalidJsonString ));
214
+ }
216
215
}
0 commit comments