Skip to content

Commit 383bfbe

Browse files
refactor: Android implementation and unit tests of obfuscation
1 parent ee4e2d5 commit 383bfbe

File tree

3 files changed

+48
-61
lines changed

3 files changed

+48
-61
lines changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugNetworkLoggerModule.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33

44
import static com.instabug.apm.configuration.cp.APMFeature.APM_NETWORK_PLUGIN_INSTALLED;
55
import static com.instabug.apm.configuration.cp.APMFeature.CP_NATIVE_INTERCEPTION_ENABLED;
6-
import static com.instabug.apm.configuration.cp.APMFeature.NETWORK_INTERCEPTION_ENABLED;
76

87
import androidx.annotation.NonNull;
98

109
import com.facebook.react.bridge.Arguments;
1110
import com.facebook.react.bridge.Promise;
1211
import com.facebook.react.bridge.ReactApplicationContext;
1312
import com.facebook.react.bridge.ReactMethod;
14-
import com.facebook.react.bridge.ReadableMap;
1513
import com.facebook.react.bridge.WritableMap;
1614
import com.facebook.react.bridge.WritableNativeMap;
1715
import com.instabug.apm.InternalAPM;
18-
import com.instabug.apm.sanitization.AsyncSanitizer;
1916
import com.instabug.apm.sanitization.OnCompleteCallback;
2017
import com.instabug.library.logging.listeners.networklogs.NetworkLogSnapshot;
2118
import com.instabug.reactlibrary.utils.EventEmitterModule;
@@ -24,14 +21,13 @@
2421
import org.json.JSONException;
2522
import org.json.JSONObject;
2623

27-
import java.util.HashMap;
2824
import java.util.Map;
2925
import java.util.concurrent.ConcurrentHashMap;
3026

3127

3228
public class RNInstabugNetworkLoggerModule extends EventEmitterModule {
3329

34-
private final ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>> callbackMap = new ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>>();
30+
public final ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>> callbackMap = new ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>>();
3531

3632
public RNInstabugNetworkLoggerModule(ReactApplicationContext reactContext) {
3733
super(reactContext);
@@ -143,8 +139,8 @@ public void run() {
143139
@ReactMethod
144140
protected void updateNetworkLogSnapshot(String jsonString) {
145141

146-
JSONObject newJSONObject = null;
147-
String url = "";
142+
JSONObject newJSONObject;
143+
String url;
148144
NetworkLogSnapshot modifiedSnapshot = null;
149145
try {
150146
newJSONObject = new JSONObject(jsonString);
@@ -155,21 +151,13 @@ protected void updateNetworkLogSnapshot(String jsonString) {
155151
final String ID = newJSONObject.optString("id");
156152

157153
if (!url.isEmpty()) {
158-
modifiedSnapshot = new NetworkLogSnapshot(
159-
url,
160-
null,
161-
null,
162-
null,
163-
null,
164-
newJSONObject.optInt("responseCode")
165-
);
154+
modifiedSnapshot = new NetworkLogSnapshot(url, null, null, null, null, newJSONObject.optInt("responseCode"));
166155
}
167156

168157
final OnCompleteCallback<NetworkLogSnapshot> callback = callbackMap.get(ID);
169158
if (callback != null) {
170159
callback.onComplete(modifiedSnapshot);
160+
callbackMap.remove(ID);
171161
}
172-
callbackMap.remove(ID);
173-
174162
}
175163
}

android/src/test/java/com/instabug/reactlibrary/RNInstabugNetworkLoggerModuleTest.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,25 @@
1616
import org.mockito.ArgumentCaptor;
1717
import org.mockito.MockedStatic;
1818
import org.mockito.Mockito;
19-
import org.mockito.invocation.InvocationOnMock;
2019
import org.mockito.stubbing.Answer;
2120

2221
import com.instabug.apm.InternalAPM;
2322
import com.instabug.reactlibrary.utils.MainThreadHandler;
2423

25-
import java.util.concurrent.Executors;
26-
import java.util.concurrent.ScheduledExecutorService;
27-
2824
public 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
}

src/modules/Instabug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function handleAndroidNativeInterception() {
255255
}
256256

257257
/**
258-
* Control either to enable or disable the native interception logic for iOS.
258+
* Control either to enable or disable the native interception for iOS.
259259
*/
260260
function handleIOSNativeInterception(config: InstabugConfig) {
261261
if (Platform.OS === 'ios') {

0 commit comments

Comments
 (0)