From 2a1533f441d37a7e181bc11a7cc0af106aef2f62 Mon Sep 17 00:00:00 2001 From: FarukBraimo Date: Thu, 20 Jun 2024 23:00:56 +0200 Subject: [PATCH] - Fix: adding more validations --- .../com/vodacom/falcon/model/request/AuthLoginRequest.java | 4 ++-- .../falcon/model/request/UserRegistrationRequest.java | 3 ++- .../java/com/vodacom/falcon/service/InsightService.java | 2 +- .../com/vodacom/falcon/service/WeatherForecastService.java | 7 +++---- .../java/com/vodacom/falcon/util/ValidationFactory.java | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/vodacom/falcon/model/request/AuthLoginRequest.java b/src/main/java/com/vodacom/falcon/model/request/AuthLoginRequest.java index 43c226d..adbdb8a 100644 --- a/src/main/java/com/vodacom/falcon/model/request/AuthLoginRequest.java +++ b/src/main/java/com/vodacom/falcon/model/request/AuthLoginRequest.java @@ -5,8 +5,8 @@ import jakarta.validation.constraints.Size; public record AuthLoginRequest( - @NotBlank(message = "Username should not be null or empty") @Pattern(regexp = "[a-zA-Z0-9 ]") + @NotBlank(message = "Username should not be null or empty" ) String username, - @NotBlank(message = "Username should not be null or empty") @Size(min = 6, max = 64) + @NotBlank(message = "Username should not be null or empty") String password) { } diff --git a/src/main/java/com/vodacom/falcon/model/request/UserRegistrationRequest.java b/src/main/java/com/vodacom/falcon/model/request/UserRegistrationRequest.java index 52fa031..0f0aee0 100644 --- a/src/main/java/com/vodacom/falcon/model/request/UserRegistrationRequest.java +++ b/src/main/java/com/vodacom/falcon/model/request/UserRegistrationRequest.java @@ -1,7 +1,8 @@ package com.vodacom.falcon.model.request; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; public record UserRegistrationRequest(@NotBlank(message = "Username should not be null or empty") String username, - @NotBlank(message = "Username should not be null or empty") String password) { + @NotBlank(message = "Username should not be null or empty") @Size(min = 6, max = 64) String password) { } diff --git a/src/main/java/com/vodacom/falcon/service/InsightService.java b/src/main/java/com/vodacom/falcon/service/InsightService.java index 0db03a9..2a015dc 100644 --- a/src/main/java/com/vodacom/falcon/service/InsightService.java +++ b/src/main/java/com/vodacom/falcon/service/InsightService.java @@ -36,7 +36,7 @@ public class InsightService { public InsightResponse getInsight(String city) throws ResourceNotFoundException { log.info("Getting insights for {}", city); - ValidationFactory.validateSearch("Country", city); + ValidationFactory.validateSearch("City", city); MetadataResponse metadata = new MetadataResponse(); diff --git a/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java b/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java index 54e356d..5d75973 100644 --- a/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java +++ b/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java @@ -26,9 +26,8 @@ public class WeatherForecastService { @Value("${open-weather-map.apiKeyV3}") private String openWeatherApiKeyV3; - private final String OPEN_WEATHER_API_VERSION = "2.5"; // TODO: Add feature flag Or row controller, to determine the version to use. (2.5 0r 3.0) + private final String OPEN_WEATHER_API_VERSION = "3.0"; // TODO: Add feature flag Or row controller, to determine the version to use. (2.5 0r 3.0) - // TODO: CACHE THE RESPONSE public WeatherForecastResponse getWeatherForecast(String city) throws ResourceNotFoundException { return WeatherForecastResponse .builder() @@ -41,10 +40,10 @@ private OpenWeatherForecastResponse buildWeatherForecast(String city) throws Res if (location == null) { log.info("Couldn't find location for {}", city); - throw new ResourceNotFoundException(String.format("Location: %s Not found", city)); + throw new ResourceNotFoundException(String.format("City: %s Not found", city)); } - String url = String.format("%s/data/%s/onecall?units=metric&cnt=4&exclude=hourly,minutely,alerts&lat=%s&lon=%s&appid=%s", FalconDefaults.OPEN_WEATHER_API_BASE_URL, OPEN_WEATHER_API_VERSION, location.getLat(), location.getLon(), openWeatherApiKeyV2); + String url = String.format("%s/data/%s/onecall?units=metric&cnt=4&exclude=hourly,minutely,alerts&lat=%s&lon=%s&appid=%s", FalconDefaults.OPEN_WEATHER_API_BASE_URL, OPEN_WEATHER_API_VERSION, location.getLat(), location.getLon(), openWeatherApiKeyV3); HttpResponse response = APICaller.getData(url); if (response != null) { OpenWeatherForecastResponse openWeatherForecast = deserialize(response.body(), OpenWeatherForecastResponse.class); diff --git a/src/main/java/com/vodacom/falcon/util/ValidationFactory.java b/src/main/java/com/vodacom/falcon/util/ValidationFactory.java index e3a08af..8562acd 100644 --- a/src/main/java/com/vodacom/falcon/util/ValidationFactory.java +++ b/src/main/java/com/vodacom/falcon/util/ValidationFactory.java @@ -4,7 +4,7 @@ public class ValidationFactory { public static void validateSearch(String term, String value) throws ResourceNotFoundException { - boolean isValid = value != null && value.length() > 3 && value.matches("^[a-zA-Z]*$"); + boolean isValid = value != null && value.length() > 3; if (!isValid) { throw new ResourceNotFoundException(String.format("Please provide a valid: %s", term)); }