1616import org .mockito .ArgumentCaptor ;
1717import org .mockito .MockedStatic ;
1818import org .mockito .Mockito ;
19- import org .mockito .invocation .InvocationOnMock ;
2019import org .mockito .stubbing .Answer ;
2120
2221import com .instabug .apm .InternalAPM ;
2322import com .instabug .reactlibrary .utils .MainThreadHandler ;
2423
25- import java .util .concurrent .Executors ;
26- import java .util .concurrent .ScheduledExecutorService ;
27-
2824public class RNInstabugNetworkLoggerModuleTest {
2925
30- // Mock MainThread
31- private final static ScheduledExecutorService mainThread = Executors .newSingleThreadScheduledExecutor ();
32-
3326 // Mock Objects
3427 private MockedStatic <Looper > mockLooper ;
3528 private MockedStatic <MainThreadHandler > mockMainThreadHandler ;
36- private RNInstabugNetworkLoggerModule networkLoggerModule ;
29+ private RNInstabugNetworkLoggerModule rnInstabugNetworkLoggerModule ;
3730 private Promise mockPromise ;
3831
3932 @ Before
40- public void mockMainThreadHandler () throws Exception {
33+ public void mockMainThreadHandler () {
4134 // Mock Object
4235 ReactApplicationContext mockReactApplicationContext = mock (ReactApplicationContext .class );
4336 mockPromise = mock (Promise .class );
44- networkLoggerModule = new RNInstabugNetworkLoggerModule (mockReactApplicationContext );
37+ rnInstabugNetworkLoggerModule = new RNInstabugNetworkLoggerModule (mockReactApplicationContext );
4538
4639 // Mock static functions
4740 mockLooper = mockStatic (Looper .class );
@@ -51,12 +44,9 @@ public void mockMainThreadHandler() throws Exception {
5144 when (Looper .getMainLooper ()).thenReturn (mockMainThreadLooper );
5245
5346 // 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 ;
6050 };
6151 Mockito .doAnswer (handlerPostAnswer ).when (MainThreadHandler .class );
6252 MainThreadHandler .runOnMainThread (any (Runnable .class ));
@@ -73,55 +63,52 @@ public void tearDown() {
7363 @ Test
7464 public void testGetName () {
7565 // Test the getName method
76- String name = networkLoggerModule .getName ();
66+ String name = rnInstabugNetworkLoggerModule .getName ();
7767 assertEquals ("IBGNetworkLogger" , name );
7868 }
7969
8070 @ Test
8171 public void testAddListener () {
8272 // Test addListener method
83- networkLoggerModule .addListener ("event_name" );
73+ rnInstabugNetworkLoggerModule .addListener ("event_name" );
8474 // Nothing to assert, but check no exceptions are thrown
8575 }
8676
8777 @ Test
8878 public void testRemoveListeners () {
8979 // Test removeListeners method
90- networkLoggerModule .removeListeners (1 );
80+ rnInstabugNetworkLoggerModule .removeListeners (1 );
9181 // Nothing to assert, but check no exceptions are thrown
9282 }
9383
9484 @ Test
9585 public void testIsNativeInterceptionEnabled_True () {
9686
97-
9887 // Mock InternalAPM behavior within the scope of this test
9988 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 );
10290
10391 // Execute the method
104- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
92+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
10593
10694 // Capture the Promise.resolve() call
10795 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
10896 verify (mockPromise ).resolve (captor .capture ());
10997
11098 // Assert that true was passed to resolve
99+ internalAPMMock .verify (() -> InternalAPM ._isFeatureEnabledCP (CP_NATIVE_INTERCEPTION_ENABLED , "" ));
111100 assertTrue (captor .getValue ());
112101 }
113102 }
114103
115104 @ Test
116105 public void testIsNativeInterceptionEnabled_False () {
117106
118-
119107 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 );
122109
123110 // Execute the method
124- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
111+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
125112
126113 // Capture the Promise.resolve() call
127114 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -135,14 +122,12 @@ public void testIsNativeInterceptionEnabled_False() {
135122 @ Test
136123 public void testIsNativeInterceptionEnabled_Exception () {
137124
138-
139125 // Simulate an exception in InternalAPM
140126 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" ));
143128
144129 // Execute the method
145- networkLoggerModule .isNativeInterceptionEnabled (mockPromise );
130+ rnInstabugNetworkLoggerModule .isNativeInterceptionEnabled (mockPromise );
146131
147132 // Capture the Promise.resolve() call in case of an exception
148133 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -156,33 +141,30 @@ public void testIsNativeInterceptionEnabled_Exception() {
156141 @ Test
157142 public void testHasAPMNetworkPlugin_True () {
158143
159-
160144 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 );
163146
164147 // Execute the method
165- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
148+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
166149
167150 // Capture the Promise.resolve() call
168151 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
169152 verify (mockPromise ).resolve (captor .capture ());
170153
171154 // Assert that true was passed to resolve
155+ internalAPMMock .verify (() -> InternalAPM ._isFeatureEnabledCP (APM_NETWORK_PLUGIN_INSTALLED , "" ));
172156 assertTrue (captor .getValue ());
173157 }
174158 }
175159
176160 @ Test
177161 public void testHasAPMNetworkPlugin_False () {
178162
179-
180163 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 );
183165
184166 // Execute the method
185- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
167+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
186168
187169 // Capture the Promise.resolve() call
188170 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -196,14 +178,12 @@ public void testHasAPMNetworkPlugin_False() {
196178 @ Test
197179 public void testHasAPMNetworkPlugin_Exception () {
198180
199-
200181 // Simulate an exception in InternalAPM
201182 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" ));
204184
205185 // Execute the method
206- networkLoggerModule .hasAPMNetworkPlugin (mockPromise );
186+ rnInstabugNetworkLoggerModule .hasAPMNetworkPlugin (mockPromise );
207187
208188 // Capture the Promise.resolve() call in case of an exception
209189 ArgumentCaptor <Boolean > captor = ArgumentCaptor .forClass (Boolean .class );
@@ -213,4 +193,23 @@ public void testHasAPMNetworkPlugin_Exception() {
213193 assertFalse (captor .getValue ());
214194 }
215195 }
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+ }
216215}
0 commit comments