From 89222742e03238aab2a659bba2eee6bbc17ab24c Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Wed, 23 Nov 2022 16:15:07 +0100
Subject: [PATCH] Fix issues as noted by SonarLint

JUnit5 test classes and methods should have default package visibility
Similar tests should be grouped in a single Parameterized test
"Preconditions" and logging arguments should not require evaluation
Unused assignments should be removed
---
 .../openepics/names/NamingApplication.java    |  32 +--
 .../names/rest/filter/RestLogFilter.java      |   2 +-
 .../org/openepics/names/util/URLUtility.java  |   1 -
 .../names/util/EncodingUtilityTest.java       | 234 ++++--------------
 .../openepics/names/util/URLUtilityTest.java  | 185 +++-----------
 5 files changed, 93 insertions(+), 361 deletions(-)

diff --git a/src/main/java/org/openepics/names/NamingApplication.java b/src/main/java/org/openepics/names/NamingApplication.java
index e05d5d8d..487d1413 100644
--- a/src/main/java/org/openepics/names/NamingApplication.java
+++ b/src/main/java/org/openepics/names/NamingApplication.java
@@ -36,29 +36,29 @@ public class NamingApplication {
         SpringApplication.run(NamingApplication.class, args);
     }
 
-    @Value("${openapi.externaldocs.description}") String openapi_externaldocs_description;
-    @Value("${openapi.externaldocs.url}") String openapi_externaldocs_url;
-    @Value("${openapi.info.contact.email}") String openapi_info_contact_email;
-    @Value("${openapi.info.contact.name}") String openapi_info_contact_name;
-    @Value("${openapi.info.contact.url}") String openapi_info_contact_url;
-    @Value("${openapi.info.description}") String openapi_info_description;
-    @Value("${openapi.info.license.name}") String openapi_info_license_name;
-    @Value("${openapi.info.title}") String openapi_info_title;
+    @Value("${openapi.externaldocs.description}") String openapiExternaldocsDescription;
+    @Value("${openapi.externaldocs.url}") String openapiExternaldocsUrl;
+    @Value("${openapi.info.contact.email}") String openapiInfoContactEmail;
+    @Value("${openapi.info.contact.name}") String openapiInfoContactName;
+    @Value("${openapi.info.contact.url}") String openapiInfoContactUrl;
+    @Value("${openapi.info.description}") String openapiInfoDescription;
+    @Value("${openapi.info.license.name}") String openapiInfoLicenseName;
+    @Value("${openapi.info.title}") String openapiInfoTitle;
 
     @Bean
     public OpenAPI customOpenAPI(@Value("${app.version}") String appVersion) {
         return new OpenAPI()
                 .externalDocs(new ExternalDocumentation()
-                        .description(openapi_externaldocs_description)
-                        .url(openapi_externaldocs_url))
+                        .description(openapiExternaldocsDescription)
+                        .url(openapiExternaldocsUrl))
                 .info(new Info()
                         .contact(new Contact()
-                                .email(openapi_info_contact_email)
-                                .name(openapi_info_contact_name)
-                                .url(openapi_info_contact_url))
-                        .description(openapi_info_description)
-                        .license(new License().name(openapi_info_license_name))
-                        .title(openapi_info_title)
+                                .email(openapiInfoContactEmail)
+                                .name(openapiInfoContactName)
+                                .url(openapiInfoContactUrl))
+                        .description(openapiInfoDescription)
+                        .license(new License().name(openapiInfoLicenseName))
+                        .title(openapiInfoTitle)
                         .version(appVersion));
     }
 
diff --git a/src/main/java/org/openepics/names/rest/filter/RestLogFilter.java b/src/main/java/org/openepics/names/rest/filter/RestLogFilter.java
index f1755abf..b17011dd 100644
--- a/src/main/java/org/openepics/names/rest/filter/RestLogFilter.java
+++ b/src/main/java/org/openepics/names/rest/filter/RestLogFilter.java
@@ -198,7 +198,7 @@ public class RestLogFilter implements Filter {
                 logLine.setStatusCode(resp.getStatus());
             }
         } finally {
-            if (restRequest) {
+            if (restRequest && LOGGER.isLoggable(Level.INFO)) {
                 LOGGER.log(Level.INFO, mapper.writeValueAsString(logLine));
             }
         }
diff --git a/src/main/java/org/openepics/names/util/URLUtility.java b/src/main/java/org/openepics/names/util/URLUtility.java
index d0a115a7..54a9461e 100644
--- a/src/main/java/org/openepics/names/util/URLUtility.java
+++ b/src/main/java/org/openepics/names/util/URLUtility.java
@@ -147,7 +147,6 @@ public class URLUtility {
         String reverseUrl = StringUtils.reverse(url);
         int index = reverseUrl.indexOf(DELIMITER_PATH);
         if (index == -1) {
-            path0 = null;
             path1 = url;
         } else {
             path0 = StringUtils.reverse(reverseUrl.substring(index + 1));
diff --git a/src/test/java/org/openepics/names/util/EncodingUtilityTest.java b/src/test/java/org/openepics/names/util/EncodingUtilityTest.java
index 247c5a2a..99018c9f 100644
--- a/src/test/java/org/openepics/names/util/EncodingUtilityTest.java
+++ b/src/test/java/org/openepics/names/util/EncodingUtilityTest.java
@@ -22,225 +22,79 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 /**
  * Purpose to test EncodingUtility class.
  *
  * @author Lars Johansson
  */
-public class EncodingUtilityTest {
+class EncodingUtilityTest {
 
     /**
      * Test for encoding a string.
      */
     @Test
-    void testEncodeStringNull() {
+    void encodeNull() {
         String s = null;
         assertThrows(NullPointerException.class, () -> { EncodingUtility.encode(s); });
     }
 
     /**
      * Test for encoding a string.
+     *
+     * @param input value to be encoded
+     * @param expected expected encoded value
      */
-    @Test
-    void testEncodeStringEmpty() {
-        String s = "";
-        assertEquals(s, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringPercent() {
-        String s = "%";
-        String expected = "%25";
-        assertEquals(expected, EncodingUtility.encode(s));
+    @ParameterizedTest
+    @CsvSource(value = {
+            "'',''",
+            "%,%25",
+            "+,%2B",
+            "_,_",
+            "' ',+",
+            "12345678,12345678",
+            "abcdefgh,abcdefgh",
+            "abc+efgh,abc%2Befgh",
+            "abcd fgh,abcd+fgh",
+            "ab de+gh,ab+de%2Bgh"
+    })
+    void encode(String input, String expected) {
+        assertEquals(expected, EncodingUtility.encode(input));
     }
 
     /**
-     * Test for encoding a string.
+     * Test for decoding a string.
      */
     @Test
-    void testEncodeStringPlus() {
-        String s = "+";
-        String expected = "%2B";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringUnderscore() {
-        String s = "_";
-        String expected ="_";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringWhitespace() {
-        String s = " ";
-        String expected ="+";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringNumbers() {
-        String s = "12345678";
-        assertEquals(s, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringLetters() {
-        String s = "abcdefgh";
-        assertEquals(s, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringLettersPlus() {
-        String s = "abc+efgh";
-        String expected = "abc%2Befgh";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringLettersWhitespace() {
-        String s = "abcd fgh";
-        String expected = "abcd+fgh";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testEncodeStringLettersPlusWhitespace() {
-        String s = "ab de+gh";
-        String expected = "ab+de%2Bgh";
-        assertEquals(expected, EncodingUtility.encode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringNull() {
+    void decodeNull() {
         String s = null;
         assertThrows(NullPointerException.class, () -> { EncodingUtility.decode(s); });
 
     }
 
     /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringEmpty() {
-        String s = "";
-        assertEquals(s, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringPercent() {
-        String s = "%25";
-        String expected = "%";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringPlus() {
-        String s = "%2B";
-        String expected = "+";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringUnderscore() {
-        String s = "_";
-        String expected = "_";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringWhitespace() {
-        String s = "+";
-        String expected = " ";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringNumbers() {
-        String s = "12345678";
-        assertEquals(s, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringLetters() {
-        String s = "abcdefgh";
-        assertEquals(s, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringLettersPlus() {
-        String s = "abc%2Befgh";
-        String expected = "abc+efgh";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringLettersWhitespace() {
-        String s = "abcd+fgh";
-        String expected = "abcd fgh";
-        assertEquals(expected, EncodingUtility.decode(s));
-    }
-
-    /**
-     * Test for encoding a string.
-     */
-    @Test
-    void testDecodeStringLettersPlusWhitespace() {
-        String s = "ab+de%2Bgh";
-        String expected = "ab de+gh";
-        assertEquals(expected, EncodingUtility.decode(s));
+     * Test for decoding a string.
+     *
+     * @param input value to be decoded
+     * @param expected expected decoded value
+     */
+    @ParameterizedTest
+    @CsvSource(value = {
+            "'',''",
+            "%25,%",
+            "%2B,+",
+            "_,_",
+            "+,' '",
+            "12345678,12345678",
+            "abcdefgh,abcdefgh",
+            "abc%2Befgh,abc+efgh",
+            "abcd+fgh,abcd fgh",
+            "ab+de%2Bgh,ab de+gh"
+    })
+    void decode(String input, String expected) {
+        assertEquals(expected, EncodingUtility.decode(input));
     }
 
 }
diff --git a/src/test/java/org/openepics/names/util/URLUtilityTest.java b/src/test/java/org/openepics/names/util/URLUtilityTest.java
index c835be49..7273cdc6 100644
--- a/src/test/java/org/openepics/names/util/URLUtilityTest.java
+++ b/src/test/java/org/openepics/names/util/URLUtilityTest.java
@@ -20,165 +20,44 @@ package org.openepics.names.util;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 /**
  * Purpose to test URLUtility class.
  *
  * @author Lars Johansson
  */
-public class URLUtilityTest {
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLNull() {
-        String url = null;
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLEmpty() {
-        String url = "";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLWhitespace() {
-        String url = " ";
-        String expected = "+";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLString() {
-        String url = "abcd1234";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPath() {
-        String url = "abcd.xhtml";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQuery() {
-        String url = "abcd.xhtml?efgh";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryPercent() {
-        String url = "/abcd.xhtml?uvwx-0%";
-        String expected = "/abcd.xhtml?uvwx-0%25";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryPlus() {
-        String url = "abcd.xhtml?efg+h";
-        String expected = "abcd.xhtml?efg%2Bh";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryWhitespace() {
-        String url = "abcd.xhtml?ef gh";
-        String expected = "abcd.xhtml?ef+gh";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue1Pair() {
-        String url = "abcd.xhtml?efgh=ijkl";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue1PairEmpty() {
-        String url = "abcd.xhtml?efgh=";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue1PairComplex() {
-        String url = "abcd.xhtml?efgh=ijkl mnop+qrst+ uvwx";
-        String expected = "abcd.xhtml?efgh=ijkl+mnop%2Bqrst%2B+uvwx";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue2Pairs() {
-        String url = "abcd.xhtml?efgh=ijkl&mnop=qrst";
-        assertEquals(url, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue2PairsComplex() {
-        String url = "abcd.xhtml?efgh=i+kl&mnop=qr t";
-        String expected = "abcd.xhtml?efgh=i%2Bkl&mnop=qr+t";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryAttributeValue3PairsComplex() {
-        String url = "abcd.xhtml?efgh&uvwx=yzabc  def++ghijklm+ nop&abcdefgh=";
-        String expected = "abcd.xhtml?efgh&uvwx=yzabc++def%2B%2Bghijklm%2B+nop&abcdefgh=";
-        assertEquals(expected, URLUtility.encodeURL(url));
-    }
-
-    /**
-     * Test for encoding a URL.
-     */
-    @Test
-    void testEncodeURLPathQueryIpPortPercent() {
-        String url = "http://127.0.0.1:8080/a/b/c/d/e/f/RFQ-010:EMR-FS-0%";
-        String expected = "http://127.0.0.1:8080/a/b/c/d/e/f/RFQ-010%3AEMR-FS-0%25";
-        assertEquals(expected, URLUtility.encodeURL(url));
+class URLUtilityTest {
+
+    /**
+     * Test for encoding a URL.
+     *
+     * @param input value to be encoded
+     * @param expected expected encoded value
+     */
+    @ParameterizedTest
+    @CsvSource(value = {
+            ",",
+            "'',''",
+            "' ',+",
+            "abcd1234,abcd1234",
+            "abcd.xhtml,abcd.xhtml",
+            "abcd.xhtml?efgh,abcd.xhtml?efgh",
+            "/abcd.xhtml?uvwx-0%,/abcd.xhtml?uvwx-0%25",
+            "abcd.xhtml?efg+h,abcd.xhtml?efg%2Bh",
+            "abcd.xhtml?ef gh,abcd.xhtml?ef+gh",
+            "abcd.xhtml?efgh=ijkl,abcd.xhtml?efgh=ijkl",
+            "abcd.xhtml?efgh=,abcd.xhtml?efgh=",
+            "abcd.xhtml?efgh=ijkl mnop+qrst+ uvwx,abcd.xhtml?efgh=ijkl+mnop%2Bqrst%2B+uvwx",
+            "abcd.xhtml?efgh=ijkl&mnop=qrst,abcd.xhtml?efgh=ijkl&mnop=qrst",
+            "abcd.xhtml?efgh=i+kl&mnop=qr t,abcd.xhtml?efgh=i%2Bkl&mnop=qr+t",
+            "abcd.xhtml?efgh&uvwx=yzabc  def++ghijklm+ nop&abcdefgh=,abcd.xhtml?efgh&uvwx=yzabc++def%2B%2Bghijklm%2B+nop&abcdefgh=",
+            "http://127.0.0.1:8080/a/b/c/d/e/f/RFQ-010:EMR-FS-0%,http://127.0.0.1:8080/a/b/c/d/e/f/RFQ-010%3AEMR-FS-0%25"
+
+    })
+    void encodeURL(String input, String expected) {
+        assertEquals(expected, URLUtility.encodeURL(input));
     }
 
 }
-- 
GitLab