diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-09-04 12:43:29 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-09-04 12:43:29 +0300 |
commit | 76beaca12884481b59ac3f110af85222514eae0f (patch) | |
tree | d9ade31ecce9ce62649bf9cb11d0cbc471ef3486 /app/src/main/java/it/niedermann/nextcloud/deck/util | |
parent | 72ed00737950e5ef6cf56f73d137ded29477aeb6 (diff) |
#671 Add unit tests for board and card extraction from URL
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/util')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/util/ProjectUtil.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/ProjectUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/ProjectUtil.java new file mode 100644 index 000000000..f8041c514 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/ProjectUtil.java @@ -0,0 +1,66 @@ +package it.niedermann.nextcloud.deck.util; + +import android.net.Uri; + +import androidx.annotation.NonNull; + +import java.net.URL; + +import it.niedermann.nextcloud.deck.model.Account; + +public class ProjectUtil { + + private ProjectUtil() { + } + + @NonNull + public static Uri getResourceUri(@NonNull Account account, @NonNull String link) throws IllegalArgumentException { + try { + // Assume link contains a fully qualified Uri including host + final URL u = new URL(link); + return Uri.parse(u.toString()); + } catch (Throwable linkIsNotQualified) { + try { + // Assume link is a absolute path that needs to be concatenated with account url for a complete Uri + final URL u = new URL(account.getUrl() + link); + return Uri.parse(u.toString()); + } catch (Throwable throwable) { + throw new IllegalArgumentException("Could not parse " + Uri.class.getSimpleName() + ": " + link, throwable); + } + } + } + + /** + * extracts the values of board- and card-ID from url. + * Depending on what kind of url it gets, it will return a long[] of lenght 1 or 2: + * If the url contains both values, you'll get 2, if it contains only the board, you'll get 1. + * <p> + * The order is fixed here: [boardId, cardId] + * + * @param url to extract from + * @return extracted and parsed values as long[] with length 1-2 + */ + public static long[] extractBoardIdAndCardIdFromUrl(@NonNull String url) { + // extract important part + String[] splitByPrefix = url.split(".*/index\\.php/apps/deck/#/board/"); + // split into board- and card part + String[] splitBySeparator = splitByPrefix[1].split("/card/"); + + // remove any unexpected stuff + if (splitBySeparator.length > 1 && splitBySeparator[1].contains("/")) { + splitBySeparator[1] = splitBySeparator[1].split("/")[0]; + } + if (splitBySeparator.length > 0 && splitBySeparator[0].contains("/")) { + splitBySeparator[0] = splitBySeparator[0].split("/")[0]; + } + + // return result + if (splitBySeparator.length == 1) { + return new long[]{Long.parseLong(splitBySeparator[0])}; + } else if (splitBySeparator.length == 2) { + return new long[]{Long.parseLong(splitBySeparator[0]), Long.parseLong(splitBySeparator[1])}; + } else { + throw new IllegalArgumentException("could not parse URL for board- and/or card-ID"); + } + } +} |