From d680c5812dd29be2f5d92de29fc9ce1608694582 Mon Sep 17 00:00:00 2001 From: Jan-Peter Klein Date: Wed, 14 Jun 2023 16:10:11 +0200 Subject: [PATCH] cleaned up code and using parameterized tests --- .../org/cryptomator/ui/common/HttpHelper.java | 22 -- .../cryptomator/ui/error/ErrorController.java | 69 ++++- src/main/resources/fxml/error.fxml | 10 +- .../ui/error/ErrorControllerTest.java | 262 ++++++------------ 4 files changed, 144 insertions(+), 219 deletions(-) delete mode 100644 src/main/java/org/cryptomator/ui/common/HttpHelper.java diff --git a/src/main/java/org/cryptomator/ui/common/HttpHelper.java b/src/main/java/org/cryptomator/ui/common/HttpHelper.java deleted file mode 100644 index 32b44b64a..000000000 --- a/src/main/java/org/cryptomator/ui/common/HttpHelper.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.cryptomator.ui.common; - -import com.google.common.io.CharStreams; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; - -public class HttpHelper { - - private HttpHelper(){ - throw new IllegalStateException("Utility class"); - } - public static String readBody(HttpResponse response) throws IOException { - try (var in = response.body(); var reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { - return CharStreams.toString(reader); - } - } - -} diff --git a/src/main/java/org/cryptomator/ui/error/ErrorController.java b/src/main/java/org/cryptomator/ui/error/ErrorController.java index 5503cc595..fde9790d2 100644 --- a/src/main/java/org/cryptomator/ui/error/ErrorController.java +++ b/src/main/java/org/cryptomator/ui/error/ErrorController.java @@ -60,7 +60,7 @@ public class ErrorController implements FxController { private final Environment environment; private final BooleanProperty copiedDetails = new SimpleBooleanProperty(); - private final BooleanProperty lookUpSolutionVisibility = new SimpleBooleanProperty(); + private final BooleanProperty errorSolutionFound = new SimpleBooleanProperty(); private final BooleanProperty isLoadingHttpResponse = new SimpleBooleanProperty(); private ErrorDiscussion matchingErrorDiscussion; @@ -132,13 +132,13 @@ public class ErrorController implements FxController { private void loadHttpResponse(HttpResponse response){ if (response.statusCode() == 200) { - Map map = new Gson().fromJson( + Map errorDiscussionMap = new Gson().fromJson( new InputStreamReader(response.body(),StandardCharsets.UTF_8), new TypeToken>(){}.getType()); - if(map.values().stream().anyMatch(this::isPartialMatchFilter)) { + if(errorDiscussionMap.values().stream().anyMatch(this::isPartialMatchFilter)) { Comparator comp = this::compareExactMatch; - Optional value = map.values().stream().min(comp + Optional value = errorDiscussionMap.values().stream().min(comp .thenComparing(this::compareSecondLevelMatch) .thenComparing(this::compareThirdLevelMatch) .thenComparing(this::compareIsAnswered) @@ -146,7 +146,7 @@ public class ErrorController implements FxController { if(value.isPresent()){ matchingErrorDiscussion = value.get(); - lookUpSolutionVisibility.set(true); + errorSolutionFound.set(true); } } } @@ -156,10 +156,30 @@ public class ErrorController implements FxController { return errorDiscussion.title.contains(" " + errorCode.methodCode()); } + /** + * Compares two ErrorDiscussion objects based on their upvote counts and returns the result. + * + * @param ed1 The first ErrorDiscussion object. + * @param ed2 The second ErrorDiscussion object. + * @return An integer indicating which ErrorDiscussion object has a higher upvote count: + * - A positive value if ed2 has a higher upvote count than ed1, + * - A negative value if ed1 has a higher upvote count than ed2, + * - Or 0 if both upvote counts are equal. + */ public int compareUpvoteCount(ErrorDiscussion ed1, ErrorDiscussion ed2) { return Integer.compare(ed2.upvoteCount, ed1.upvoteCount); } + /** + * Compares two ErrorDiscussion objects based on their answered status and returns the result. + * + * @param ed1 The first ErrorDiscussion object. + * @param ed2 The second ErrorDiscussion object. + * @return An integer indicating the answered status of the ErrorDiscussion objects: + * - A negative value (-1) if ed1 is considered answered and ed2 is considered unanswered, + * - A positive value (1) if ed1 is considered unanswered and ed2 is considered answered, + * - Or 0 if both ErrorDiscussion objects are considered answered or unanswered. + */ public int compareIsAnswered(ErrorDiscussion ed1, ErrorDiscussion ed2) { if (ed1.answer!=null && ed2.answer==null) { return -1; @@ -169,6 +189,17 @@ public class ErrorController implements FxController { return 0; } } + + /** + * Compares two ErrorDiscussion objects based on the presence of an exact match with the error code in their titles and returns the result. + * + * @param ed1 The first ErrorDiscussion object. + * @param ed2 The second ErrorDiscussion object. + * @return An integer indicating the comparison result based on the presence of an exact match with the error code in the titles: + * - A negative value (-1) if ed1 has an exact match with the error code in the title and ed2 does not have a match, + * - A positive value (1) if ed1 does not have a match and ed2 has an exact match with the error code in the title, + * - Or 0 if both ErrorDiscussion objects either have an exact match or do not have a match with the error code in the titles. + */ public int compareExactMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) { if (ed1.title.contains(getErrorCode()) && !ed2.title.contains(getErrorCode())) { return -1; @@ -179,6 +210,16 @@ public class ErrorController implements FxController { } } + /** + * Compares two ErrorDiscussion objects based on the presence of a second-level match with the error code in their titles and returns the result. + * + * @param ed1 The first ErrorDiscussion object. + * @param ed2 The second ErrorDiscussion object. + * @return An integer indicating the comparison result based on the presence of a second-level match with the error code in the titles: + * - A negative value (-1) if ed1 has a second-level match with the error code in the title and ed2 does not have a match, + * - A positive value (1) if ed1 does not have a match and ed2 has a second-level match with the error code in the title, + * - Or 0 if both ErrorDiscussion objects either have a second-level match or do not have a match with the error code in the titles. + */ public int compareSecondLevelMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) { String value = " " + errorCode.methodCode() + ErrorCode.DELIM + errorCode.rootCauseCode(); if (ed1.title.contains(value) && !ed2.title.contains(value)) { @@ -190,6 +231,16 @@ public class ErrorController implements FxController { } } + /** + * Compares two ErrorDiscussion objects based on the presence of a third-level match with the error code in their titles and returns the result. + * + * @param ed1 The first ErrorDiscussion object. + * @param ed2 The second ErrorDiscussion object. + * @return An integer indicating the comparison result based on the presence of a third-level match with the error code in the titles: + * - A negative value (-1) if ed1 has a third-level match with the error code in the title and ed2 does not have a match, + * - A positive value (1) if ed1 does not have a match and ed2 has a third-level match with the error code in the title, + * - Or 0 if both ErrorDiscussion objects either have a third-level match or do not have a match with the error code in the titles. + */ public int compareThirdLevelMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) { String value = " " + errorCode.methodCode(); if (ed1.title.contains(value) && !ed2.title.contains(value)) { @@ -226,12 +277,12 @@ public class ErrorController implements FxController { return copiedDetails.get(); } - public BooleanProperty lookUpSolutionVisibilityProperty() { - return lookUpSolutionVisibility; + public BooleanProperty errorSolutionFoundProperty() { + return errorSolutionFound; } - public boolean getLookUpSolutionVisibility() { - return lookUpSolutionVisibility.get(); + public boolean getErrorSolutionFound() { + return errorSolutionFound.get(); } public BooleanProperty isLoadingHttpResponseProperty() { diff --git a/src/main/resources/fxml/error.fxml b/src/main/resources/fxml/error.fxml index ffac5e639..cc11db58a 100644 --- a/src/main/resources/fxml/error.fxml +++ b/src/main/resources/fxml/error.fxml @@ -34,19 +34,19 @@ -