diff --git a/README.md b/README.md index f3dc87659..dc6d067ef 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Countly Java SDK -This repository contains the Countly Java SDK, which can be integrated into Java applications. The Countly Java SDK is intended to be used with [Countly Community Edition](https://github.com/Countly/countly-server) or [Countly Enterprise Edition](https://count.ly/product). +This repository contains the Countly Java SDK, which can be integrated into Java applications. The Countly Java SDK is intended to be used with [Countly Lite](https://countly.com/lite), [Countly Flex](https://countly.com/flex), [Countly Enterprise](https://countly.com/enterprise). ## What is Countly? @@ -12,16 +12,16 @@ and [desktop](https://count.ly/desktop-analytics) applications. [Ensuring privac Track, measure, and take action - all without leaving Countly. * **Questions or feature requests?** [Join the Countly Community on Discord](https://discord.gg/countly) -* **Looking for the Countly Server?** [Countly Community Edition repository](https://github.com/Countly/countly-server) +* **Looking for the Countly Server?** [Countly Server repository](https://github.com/Countly/countly-server) * **Looking for other Countly SDKs?** [An overview of all Countly SDKs for mobile, web and desktop](https://support.count.ly/hc/en-us/articles/360037236571-Downloading-and-Installing-SDKs#officially-supported-sdks) ## Integrating Countly SDK in your projects For a detailed description on how to use this SDK [check out our documentation](https://support.count.ly/hc/en-us/articles/360037813891-Java). -For information about how to add the SDK to your project, please check [this section of the documentation](https://support.count.ly/hc/en-us/articles/360037813891-Java#adding-sdk-to-the-project). +For information about how to add the SDK to your project, please check [this section of the documentation](https://support.count.ly/hc/en-us/articles/360037813891-Java#h_01HABV0K6BZ251ANK02RZK3Z5H). -You can find minimal SDK integration information for your project in [this section of the documentation](https://support.count.ly/hc/en-us/articles/360037813891-Java#minimal-setup). +You can find minimal SDK integration information for your project in [this section of the documentation](https://support.count.ly/hc/en-us/articles/360037813891-Java#h_01HABV0K6C4H1G71VV85BDXV91). For an example integration of this SDK, you can have a look [here](https://github.com/Countly/countly-sdk-java/tree/master/app-java/src/main/java/ly/count/java/demo). 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 38d02a0ab..8d2edcd65 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 @@ -69,7 +69,7 @@ private JSONObject doInBackground(String requestData, String customEndpoint, Tra try { connection = cp.connection(request, null); } catch (IOException e) { - L.e("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e.toString() + "]"); + L.e("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e + "]"); return null; } //connecting diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java index 289771702..becdeadd5 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java @@ -958,4 +958,4 @@ private int getHourFromTimeStamp(long timeStamp) { return calendar.get(Calendar.HOUR_OF_DAY); } -} +} \ No newline at end of file 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..c479c25a7 --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java @@ -0,0 +1,84 @@ +package ly.count.sdk.java.internal; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import ly.count.sdk.java.Countly; +import org.json.JSONObject; +import org.junit.After; +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); + + @After + public void stop() { + Countly.stop(true); + } + + /** + * 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 if thread is interrupted + */ + @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 + } + + /** + * Immediate request maker "doWork" function + * Immediate Request Generator is override and returns desired value + * should return desired value + * + * @throws InterruptedException if thread is interrupted + */ + @Test + public void doWork() throws InterruptedException { + Countly.instance().init(TestUtils.getBaseConfig()); + + JSONObject requestResult = new JSONObject(); + requestResult.put("result", "Success"); + requestResult.put("count", 6); + + ImmediateRequestI requestMaker = (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> { + Assert.assertEquals("test_event", requestData); + Assert.assertEquals("/o?", customEndpoint); + Assert.assertTrue(networkingIsEnabled); + Assert.assertFalse(requestShouldBeDelayed); + callback.callback(requestResult); + }; + + SDKCore.instance.config.immediateRequestGenerator = () -> requestMaker; + ImmediateRequestI requestMakerGenerated = SDKCore.instance.config.immediateRequestGenerator.createImmediateRequestMaker(); + + AtomicInteger reqValidator = new AtomicInteger(0); + requestMakerGenerated.doWork("test_event", "/o?", SDKCore.instance.networking.getTransport(), false, true, + (result) -> { + Assert.assertEquals(requestResult, result); + reqValidator.set(requestResult.getInt("count")); + }, L); + + Thread.sleep(2000); // wait for background thread to finish + Assert.assertEquals(6, reqValidator.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 6139a00ad..be48b342e 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 @@ -19,7 +19,7 @@ public class TestUtils { static String DELIMETER = ":::"; - static String SERVER_URL = "https://try.count.ly"; + static String SERVER_URL = "https://test.count.ly"; static String SERVER_APP_KEY = "COUNTLY_APP_KEY"; static String DEVICE_ID = "some_random_test_device_id"; @@ -205,4 +205,18 @@ public static Map parseQueryParams(String data) { } return paramMap; } + + 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.exists() && directory.isDirectory())) { + if (!directory.mkdirs()) { + throw new RuntimeException("Directory creation failed"); + } + } + } } \ No newline at end of file