From 6c336d3adf229466fd24e3e160170affa6403ef5 Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Tue, 3 May 2022 23:38:45 +0200 Subject: [PATCH] Sonar fixes (#12) --- .../tasks/SimpleBNGGeometryTransformer.java | 22 ++++++++++++++----- ...ceTest.java => BingSearchServiceTest.java} | 4 ++-- .../search/tasks/PdokSearchServiceTest.java | 2 +- .../aerius/search/rest/SearchRestService.java | 4 +++- .../nl/aerius/search/tasks/CapabilityKey.java | 2 +- .../nl/aerius/search/tasks/TaskUtils.java | 10 ++++----- .../tasks/async/AsyncSearchTaskDelegator.java | 4 +--- search-sonar-report/pom.xml | 5 +++++ 8 files changed, 35 insertions(+), 18 deletions(-) rename search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/{PdokSearchServiceTest.java => BingSearchServiceTest.java} (94%) diff --git a/search-service-bing-geocoder/src/main/java/nl/aerius/search/tasks/SimpleBNGGeometryTransformer.java b/search-service-bing-geocoder/src/main/java/nl/aerius/search/tasks/SimpleBNGGeometryTransformer.java index eae3008..cee75c8 100644 --- a/search-service-bing-geocoder/src/main/java/nl/aerius/search/tasks/SimpleBNGGeometryTransformer.java +++ b/search-service-bing-geocoder/src/main/java/nl/aerius/search/tasks/SimpleBNGGeometryTransformer.java @@ -1,3 +1,19 @@ +/* + * Crown copyright + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ package nl.aerius.search.tasks; import org.geotools.geometry.jts.JTS; @@ -40,11 +56,7 @@ public String toBNGWKT(final String wkt) { final String unionWkt = writer.write(result); LOG.info("Input: {} Output: {}", wkt, unionWkt); return unionWkt; - } catch (final MismatchedDimensionException e) { - throw new InterpretationRuntimeException(e); - } catch (final TransformException e) { - throw new InterpretationRuntimeException(e); - } catch (final ParseException e) { + } catch (final MismatchedDimensionException | ParseException | TransformException e) { throw new InterpretationRuntimeException(e); } } diff --git a/search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java b/search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/BingSearchServiceTest.java similarity index 94% rename from search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java rename to search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/BingSearchServiceTest.java index 38159e1..20dd8eb 100644 --- a/search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java +++ b/search-service-bing-geocoder/src/test/java/nl/aerius/search/tasks/BingSearchServiceTest.java @@ -28,7 +28,7 @@ import nl.aerius.search.domain.SearchTaskResult; @SpringBootTest -class PdokSearchServiceTest { +class BingSearchServiceTest { @Autowired BingSearchService delegator; @Value("${nl.aerius.bing.apiKey:#{null}}") private String apiKey; @@ -44,6 +44,6 @@ void testWorksAtAll() { final SearchTaskResult suggestions = result.blockingGet(); - assertEquals(suggestions.getSuggestions().size(), 10, "Expected 10 results for 'utrecht'"); + assertEquals(10, suggestions.getSuggestions().size(), "Expected 10 results for 'utrecht'"); } } diff --git a/search-service-pdok-locatieservice/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java b/search-service-pdok-locatieservice/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java index 396da67..0ed3208 100644 --- a/search-service-pdok-locatieservice/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java +++ b/search-service-pdok-locatieservice/src/test/java/nl/aerius/search/tasks/PdokSearchServiceTest.java @@ -36,6 +36,6 @@ void testWorksAtAll() { final SearchTaskResult suggestions = result.blockingGet(); - assertEquals(suggestions.getSuggestions().size(), 10, "Expected 10 results for 'utrecht'"); + assertEquals(10, suggestions.getSuggestions().size(), "Expected 10 results for 'utrecht'"); } } diff --git a/search-service/src/main/java/nl/aerius/search/rest/SearchRestService.java b/search-service/src/main/java/nl/aerius/search/rest/SearchRestService.java index 911d19e..4564ddd 100644 --- a/search-service/src/main/java/nl/aerius/search/rest/SearchRestService.java +++ b/search-service/src/main/java/nl/aerius/search/rest/SearchRestService.java @@ -18,6 +18,7 @@ import java.util.List; import java.util.Optional; +import java.util.regex.Pattern; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; @@ -38,6 +39,7 @@ public class SearchRestService { private static final String DEFAULT_CAPABILITIES = "MOCK_0,MOCK_01,MOCK_05,RECEPTOR"; private static final String DEFAULT_REGION = "NL"; + private static final Pattern SCRUB_PATTERN = Pattern.compile("[\n\r\t]"); @Autowired SearchTaskDelegator taskDelegator; @@ -72,7 +74,7 @@ public SearchResult retrieveSearchResultsAsync(final String query, * Remove newlines, carriage returns, and tabs */ private static String scrub(final String query) { - return query.replaceAll("[\n\r\t]", ""); + return SCRUB_PATTERN.matcher(query).replaceAll(""); } @GetMapping(value = "/api/results/{uuid}") diff --git a/search-service/src/main/java/nl/aerius/search/tasks/CapabilityKey.java b/search-service/src/main/java/nl/aerius/search/tasks/CapabilityKey.java index eb627db..867a4a9 100644 --- a/search-service/src/main/java/nl/aerius/search/tasks/CapabilityKey.java +++ b/search-service/src/main/java/nl/aerius/search/tasks/CapabilityKey.java @@ -21,7 +21,7 @@ import nl.aerius.search.domain.SearchCapability; import nl.aerius.search.domain.SearchRegion; -public class CapabilityKey { +public final class CapabilityKey { private SearchCapability capability; private SearchRegion region; diff --git a/search-service/src/main/java/nl/aerius/search/tasks/TaskUtils.java b/search-service/src/main/java/nl/aerius/search/tasks/TaskUtils.java index 0a3b519..c112241 100644 --- a/search-service/src/main/java/nl/aerius/search/tasks/TaskUtils.java +++ b/search-service/src/main/java/nl/aerius/search/tasks/TaskUtils.java @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.slf4j.Logger; @@ -36,14 +37,13 @@ */ public final class TaskUtils { private static final Logger LOG = LoggerFactory.getLogger(TaskUtils.class); + private static final Pattern REGION_WARN_PATTERN = Pattern.compile("[\n\r\t]"); private static final Comparator COMPARATOR = Comparator .comparingDouble(SearchSuggestion::getScore) .reversed() .thenComparing(SearchSuggestion::getDescription); - // (o1, o2) -> Double.compare(o1.getScore(), o2.getScore()); - private TaskUtils() {} /** @@ -56,7 +56,7 @@ public static Map findTaskServices(final TaskF if (taskFactory.hasCapability(v)) { return true; } else { - log.error("No task for known capability: " + v); + log.error("No task for known capability: {}", v); return false; } }) @@ -74,8 +74,8 @@ public static Comparator getResultComparator() { public static Set parseCapabilities(final Collection capabilities, final String region) { final SearchRegion reg = SearchRegion.safeValueOf(region); - if (reg == null) { - LOG.warn("Requested region that does not exist: {}", region.replaceAll("[\n\r\t]", "_")); + if (reg == null && LOG.isWarnEnabled()) { + LOG.warn("Requested region that does not exist: {}", REGION_WARN_PATTERN.matcher(region).replaceAll("_")); return Set.of(); } diff --git a/search-service/src/main/java/nl/aerius/search/tasks/async/AsyncSearchTaskDelegator.java b/search-service/src/main/java/nl/aerius/search/tasks/async/AsyncSearchTaskDelegator.java index 78e4843..2bafe4d 100644 --- a/search-service/src/main/java/nl/aerius/search/tasks/async/AsyncSearchTaskDelegator.java +++ b/search-service/src/main/java/nl/aerius/search/tasks/async/AsyncSearchTaskDelegator.java @@ -85,9 +85,7 @@ public SearchResult retrieveSearchResultsAsync(final String query, final Set v.retrieveSearchResults(query)) - .doOnError(e -> { - LOG.error("Error while executing search task:", e); - }) + .doOnError(e -> LOG.error("Error while executing search task:", e)) .flatMap(Single::toFlowable) .doAfterNext(r -> task.complete(r)) .sequential() diff --git a/search-sonar-report/pom.xml b/search-sonar-report/pom.xml index 3d6e225..bf40db1 100644 --- a/search-sonar-report/pom.xml +++ b/search-sonar-report/pom.xml @@ -35,6 +35,11 @@ search-service ${project.version} + + nl.aerius + search-service-bing-geocoder + ${project.version} + nl.aerius search-service-extension