diff --git a/sdk-java/src/main/java/ly/count/sdk/java/Config.java b/sdk-java/src/main/java/ly/count/sdk/java/Config.java index 565790cd1..9fceca8b5 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/Config.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/Config.java @@ -14,6 +14,7 @@ import java.util.Set; import ly.count.sdk.java.internal.Byteable; import ly.count.sdk.java.internal.CoreFeature; +import ly.count.sdk.java.internal.ImmediateRequestGenerator; import ly.count.sdk.java.internal.Log; import ly.count.sdk.java.internal.LogCallback; import ly.count.sdk.java.internal.ModuleBase; @@ -466,6 +467,8 @@ public boolean restore(byte[] data, Log L) { */ File sdkStorageRootDirectory = null; + protected ImmediateRequestGenerator immediateRequestGenerator = null; + // /** // * Maximum size of all string keys // */ diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java index 86c5a5b32..85451fbf8 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java @@ -69,4 +69,9 @@ public void stop(CtxCore ctx) { shutdown = true; tasks.shutdown(); } + + @Override + public Transport getTransport() { + return transport; + } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java index 8fb6dd093..60b35fd54 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java @@ -23,8 +23,8 @@ protected interface InternalImmediateRequestCallback { void callback(JSONObject checkResponse); } - InternalImmediateRequestCallback callback; - Log L; + private InternalImmediateRequestCallback callback; + private Log L; /** * params fields: @@ -36,10 +36,9 @@ protected interface InternalImmediateRequestCallback { * 5 - callback * 6 - log module */ - protected JSONObject doInBackground(String requestData, String customEndpoint, Transport cp, boolean requestShouldBeDelayed, boolean networkingIsEnabled, InternalImmediateRequestCallback callback, Log log) { + private JSONObject doInBackground(String requestData, String customEndpoint, Transport cp, boolean requestShouldBeDelayed, boolean networkingIsEnabled, InternalImmediateRequestCallback callback, Log log) { this.callback = callback; L = log; - if (!networkingIsEnabled) { L.w("[ImmediateRequestMaker] ImmediateRequestMaker, Networking config is disabled, request cancelled. Endpoint[" + customEndpoint + "] request[" + requestData + "]"); @@ -63,7 +62,6 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T L.w("[ImmediateRequestMaker] While waiting for 0.5 seconds in ImmediateRequestMaker, sleep was interrupted"); } } - Request request = new Request(); request.params.add(requestData); request.endpoint(customEndpoint); @@ -72,15 +70,12 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T connection = cp.connection(request); } catch (IOException e) { L.e("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e.toString() + "]"); - return null; } - //connecting connection.connect(); int code = connection.getResponseCode(); - String receivedBuffer = cp.response(connection); if (receivedBuffer == null) { @@ -112,9 +107,8 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T return null; } - protected void onFinished(JSONObject result) { + private void onFinished(JSONObject result) { L.v("[ImmediateRequestMaker] onPostExecute"); - if (callback != null) { callback.callback(result); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Networking.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Networking.java index 4d659d1b9..965976a47 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Networking.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Networking.java @@ -8,4 +8,6 @@ public interface Networking { boolean check(CtxCore ctx); void stop(CtxCore ctx); + + Transport getTransport(); } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java new file mode 100644 index 000000000..7c798c6c2 --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java @@ -0,0 +1,39 @@ +package ly.count.sdk.java.internal; + +import java.util.concurrent.atomic.AtomicReference; +import ly.count.sdk.java.Countly; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static org.mockito.Mockito.mock; + +@RunWith(JUnit4.class) +public class ImmediateRequestTest { + Log L = mock(Log.class); + + /** + * Immediate request maker "doWork" function + * Immediate Request Generator is default and endpoint and data are not valid, and app key, server url is default + * should return null because response is not okay + * + * @throws InterruptedException + */ + @Test + public void doWork_null() throws InterruptedException { + Countly.instance().init(TestUtils.getBaseConfig()); + AtomicReference callbackResult = new AtomicReference<>(true); + + ImmediateRequestMaker immediateRequestMaker = new ImmediateRequestMaker(); + immediateRequestMaker.doWork("test_event", "/o?", SDKCore.instance.networking.getTransport(), false, true, + (result) -> { + if (result == null) { + callbackResult.set(false); + } + }, L); + + Thread.sleep(2000); // wait for background thread to finish + Assert.assertFalse(callbackResult.get()); // check if callback was called and response is null + } +} diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java index e393c1ce3..8f599d8e9 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.Scanner; import java.util.stream.Stream; +import ly.count.sdk.java.Config; import static ly.count.sdk.java.internal.SDKStorage.EVENT_QUEUE_FILE_NAME; import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_PREFIX; @@ -17,11 +18,43 @@ public class TestUtils { - private static String DELIMETER = ":::"; + static String DELIMETER = ":::"; + static String SERVER_URL = "https://try.count.ly"; + static String SERVER_APP_KEY = "COUNTLY_APP_KEY"; + static String DEVICE_ID = "some_random_test_device_id"; private TestUtils() { } + static Config getBaseConfig() { + File sdkStorageRootDirectory = getSdkStorageRootDirectory(); + checkSdkStorageRootDirectoryExist(sdkStorageRootDirectory); + Config config = new Config(SERVER_URL, SERVER_APP_KEY, sdkStorageRootDirectory); + config.setCustomDeviceId(DEVICE_ID); + + return config; + } + + static Config getVariantConfig(ImmediateRequestGenerator generator) { + Config config = getBaseConfig(); + InternalConfig internalConfig = new InternalConfig(config); + + internalConfig.immediateRequestGenerator = generator; + return config; + } + + static File getSdkStorageRootDirectory() { + // System specific folder structure + String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; + return new File(String.join(File.separator, sdkStorageRootPath)); + } + + static void checkSdkStorageRootDirectoryExist(File directory) { + if (directory.mkdirs()) { + throw new RuntimeException("Directory creation failed"); + } + } + /** * Get current request queue from target folder * @@ -80,7 +113,6 @@ protected static List getCurrentEventQueue(File targetFolder, Log log //do nothing } - //EventImplQueue.DELIMITER Arrays.stream(fileContent.split(DELIMETER)).forEach(s -> { final EventImpl event = EventImpl.fromJSON(s, (ev) -> { }, logger);