From e2916700bc88f51ecddfb1dbe01f70822a36eb68 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Mon, 17 Jul 2023 17:00:59 -0700 Subject: [PATCH] Updated generateShortLinkSync and test --- .../io/branch/referral/BranchApiTests.java | 2 +- .../main/java/io/branch/referral/Branch.java | 40 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Branch-SDK/src/androidTest/java/io/branch/referral/BranchApiTests.java b/Branch-SDK/src/androidTest/java/io/branch/referral/BranchApiTests.java index 3508b36b7..517debc20 100644 --- a/Branch-SDK/src/androidTest/java/io/branch/referral/BranchApiTests.java +++ b/Branch-SDK/src/androidTest/java/io/branch/referral/BranchApiTests.java @@ -73,7 +73,7 @@ public void run() { @Test public void test00GetShortUrlSyncFailure() { String url = new BranchShortLinkBuilder(getTestContext()).getShortUrl(); - Assert.assertNull(url); + Assert.assertNotNull(url); } @Test diff --git a/Branch-SDK/src/main/java/io/branch/referral/Branch.java b/Branch-SDK/src/main/java/io/branch/referral/Branch.java index 2c7a36c3d..d422dd21d 100644 --- a/Branch-SDK/src/main/java/io/branch/referral/Branch.java +++ b/Branch-SDK/src/main/java/io/branch/referral/Branch.java @@ -1662,35 +1662,27 @@ public void cancelShareLinkDialog(boolean animateClose) { // PRIVATE FUNCTIONS private String generateShortLinkSync(ServerRequestCreateUrl req) { - if (trackingController.isTrackingDisabled()) { - return req.getLongUrl(); + ServerResponse response = null; + try { + int timeOut = prefHelper_.getTimeout() + 2000; // Time out is set to slightly more than link creation time to prevent any edge case + response = new GetShortLinkTask().execute(req).get(timeOut, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException ignore) { + } + String url = null; + if (req.isDefaultToLongUrl()) { + url = req.getLongUrl(); } - if (initState_ == SESSION_STATE.INITIALISED) { - ServerResponse response = null; + if (response != null && response.getStatusCode() == HttpURLConnection.HTTP_OK) { try { - int timeOut = prefHelper_.getTimeout() + 2000; // Time out is set to slightly more than link creation time to prevent any edge case - response = new GetShortLinkTask().execute(req).get(timeOut, TimeUnit.MILLISECONDS); - } catch (InterruptedException | ExecutionException | TimeoutException ignore) { - } - String url = null; - if (req.isDefaultToLongUrl()) { - url = req.getLongUrl(); - } - if (response != null && response.getStatusCode() == HttpURLConnection.HTTP_OK) { - try { - url = response.getObject().getString("url"); - if (req.getLinkPost() != null) { - linkCache_.put(req.getLinkPost(), url); - } - } catch (JSONException e) { - e.printStackTrace(); + url = response.getObject().getString("url"); + if (req.getLinkPost() != null) { + linkCache_.put(req.getLinkPost(), url); } + } catch (JSONException e) { + e.printStackTrace(); } - return url; - } else { - PrefHelper.Debug("Warning: User session has not been initialized"); } - return null; + return url; } private JSONObject convertParamsStringToDictionary(String paramString) {