diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.mvn/jvm.config diff --git a/.mvn/maven.config b/.mvn/maven.config new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.mvn/maven.config diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..97a6e07 --- /dev/null +++ b/pom.xml @@ -0,0 +1,124 @@ + + + 4.0.0 + + io.ljunggren + exchange-rate-api + 0.0.1-SNAPSHOT + + exchange-rate-api + + + UTF-8 + 17 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + org.apache.httpcomponents.client5 + httpclient5 + 5.5 + + + com.fasterxml.jackson.core + jackson-annotations + 2.20 + + + org.apache.commons + commons-lang3 + 3.18.0 + + + commons-io + commons-io + 2.20.0 + + + io.ljunggren + json-utils + 0.0.1-SNAPSHOT + + + org.projectlombok + lombok + 1.18.38 + provided + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + + maven-clean-plugin + 3.4.0 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + diff --git a/src/main/java/io/ljunggren/exchange/rate/api/ExchangeRateApi.java b/src/main/java/io/ljunggren/exchange/rate/api/ExchangeRateApi.java new file mode 100644 index 0000000..d897adb --- /dev/null +++ b/src/main/java/io/ljunggren/exchange/rate/api/ExchangeRateApi.java @@ -0,0 +1,27 @@ +package io.ljunggren.exchange.rate.api; + +import org.apache.commons.lang3.StringUtils; + +import io.ljunggren.exchange.rate.api.exception.ExchangeRateException; +import io.ljunggren.exchange.rate.api.model.ExchangeRate; +import io.ljunggren.exchange.rate.api.service.ExchangeRateService; + +public class ExchangeRateApi { + + private ExchangeRateService exchangeRateService; + + public ExchangeRateApi() { + this.exchangeRateService = new ExchangeRateService(); + } + + public ExchangeRate getByCurrency(String currency) throws ExchangeRateException { + if (StringUtils.isBlank(currency)) { + throw new ExchangeRateException("Currency is required"); + } + if (!currency.matches("^[a-zA-Z]{3}$")) { + throw new ExchangeRateException("Currency must be 3 letters"); + } + return exchangeRateService.getByCurrency(currency); + } + +} diff --git a/src/main/java/io/ljunggren/exchange/rate/api/exception/ExchangeRateException.java b/src/main/java/io/ljunggren/exchange/rate/api/exception/ExchangeRateException.java new file mode 100644 index 0000000..607d26f --- /dev/null +++ b/src/main/java/io/ljunggren/exchange/rate/api/exception/ExchangeRateException.java @@ -0,0 +1,15 @@ +package io.ljunggren.exchange.rate.api.exception; + +public class ExchangeRateException extends Exception { + + private static final long serialVersionUID = 2833089672708594219L; + + public ExchangeRateException(String message) { + super(message); + } + + public ExchangeRateException(Throwable e) { + super(e); + } + +} diff --git a/src/main/java/io/ljunggren/exchange/rate/api/model/ExchangeRate.java b/src/main/java/io/ljunggren/exchange/rate/api/model/ExchangeRate.java new file mode 100644 index 0000000..d61d910 --- /dev/null +++ b/src/main/java/io/ljunggren/exchange/rate/api/model/ExchangeRate.java @@ -0,0 +1,47 @@ +package io.ljunggren.exchange.rate.api.model; + +import java.time.Instant; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class ExchangeRate { + + private String result; + private String provider; + private String documentation; + @JsonProperty("terms_of_use") + private String termsOfUse; + @JsonProperty("time_last_update_unix") + private long touchedUnix; + @JsonProperty("time_last_update_utc") + private String touchedUtc; + @JsonProperty("time_next_update_unix") + private long nextTouchedUnix; + @JsonProperty("time_next_update_utc") + private String nextTouchedUtc; + @JsonProperty("time_eol_unix") + private boolean timeEolUnix; + @JsonProperty("base_code") + private String baseCurrency; + private Map rates; + + @JsonIgnore + public boolean shouldUpdate() { + return Instant.now().getEpochSecond() > nextTouchedUnix; + } + +} diff --git a/src/main/java/io/ljunggren/exchange/rate/api/service/ExchangeRateService.java b/src/main/java/io/ljunggren/exchange/rate/api/service/ExchangeRateService.java new file mode 100644 index 0000000..277628f --- /dev/null +++ b/src/main/java/io/ljunggren/exchange/rate/api/service/ExchangeRateService.java @@ -0,0 +1,39 @@ +package io.ljunggren.exchange.rate.api.service; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import io.ljunggren.exchange.rate.api.exception.ExchangeRateException; +import io.ljunggren.exchange.rate.api.model.ExchangeRate; +import io.ljunggren.json.utils.JsonUtils; + +public class ExchangeRateService { + + private final String URI = "https://open.er-api.com/v6/latest/"; + + public ExchangeRate getByCurrency(String currency) throws ExchangeRateException { + HttpGet get = new HttpGet(URI + currency.toUpperCase()); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpClientResponseHandler responseHandler = response -> { + return EntityUtils.toString(response.getEntity(), "UTF-8"); + }; + String json = httpClient.execute(get, responseHandler); + return parse(json, ExchangeRate.class); + } catch (Exception e) { + throw new ExchangeRateException(e); + } + } + + private T parse(String json, Class clazz) throws Exception { + try { + return JsonUtils.jsonToObject(json, clazz); + } catch (JsonProcessingException e) { + throw new Exception("Unable to parse response"); + } + } +} diff --git a/src/test/java/io/ljunggren/exchange/rate/api/AppTest.java b/src/test/java/io/ljunggren/exchange/rate/api/AppTest.java new file mode 100644 index 0000000..48435ee --- /dev/null +++ b/src/test/java/io/ljunggren/exchange/rate/api/AppTest.java @@ -0,0 +1,19 @@ +package io.ljunggren.exchange.rate.api; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +public class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } +} diff --git a/src/test/java/io/ljunggren/exchange/rate/api/model/ExchangeRateTest.java b/src/test/java/io/ljunggren/exchange/rate/api/model/ExchangeRateTest.java new file mode 100644 index 0000000..b07694e --- /dev/null +++ b/src/test/java/io/ljunggren/exchange/rate/api/model/ExchangeRateTest.java @@ -0,0 +1,25 @@ +package io.ljunggren.exchange.rate.api.model; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; + +import io.ljunggren.json.utils.JsonUtils; + +class ExchangeRateTest { + + private String readFromResources(String file) throws IOException { + return IOUtils.toString(this.getClass().getResourceAsStream(file), "UTF-8"); + } + + @Test + void deserializeTest() throws IOException { + String json = readFromResources("/exchangeRate.json"); + ExchangeRate exchangeRateConversion = JsonUtils.jsonToObject(json, ExchangeRate.class); + assertNotNull(exchangeRateConversion); + } + +} diff --git a/src/test/resources/exchangeRate.json b/src/test/resources/exchangeRate.json new file mode 100644 index 0000000..79fb367 --- /dev/null +++ b/src/test/resources/exchangeRate.json @@ -0,0 +1,177 @@ +{ + "result" : "success", + "provider" : "https://www.exchangerate-api.com", + "documentation" : "https://www.exchangerate-api.com/docs/free", + "terms_of_use" : "https://www.exchangerate-api.com/terms", + "time_last_update_unix" : 1757894551, + "time_last_update_utc" : "Mon, 15 Sep 2025 00:02:31 +0000", + "time_next_update_unix" : 1757982381, + "time_next_update_utc" : "Tue, 16 Sep 2025 00:26:21 +0000", + "time_eol_unix" : 0, + "base_code" : "USD", + "rates" : { + "USD" : 1, + "AED" : 3.6725, + "AFN" : 67.979409, + "ALL" : 82.69123, + "AMD" : 383.323833, + "ANG" : 1.79, + "AOA" : 918.435844, + "ARS" : 1447.17, + "AUD" : 1.504807, + "AWG" : 1.79, + "AZN" : 1.700328, + "BAM" : 1.667861, + "BBD" : 2, + "BDT" : 121.685845, + "BGN" : 1.667878, + "BHD" : 0.376, + "BIF" : 2983.55425, + "BMD" : 1, + "BND" : 1.28347, + "BOB" : 6.906989, + "BRL" : 5.392517, + "BSD" : 1, + "BTN" : 88.340813, + "BWP" : 13.416016, + "BYN" : 3.360522, + "BZD" : 2, + "CAD" : 1.384296, + "CDF" : 2874.180411, + "CHF" : 0.796799, + "CLP" : 951.767788, + "CNY" : 7.12638, + "COP" : 3900.735844, + "CRC" : 503.665679, + "CUP" : 24, + "CVE" : 94.030018, + "CZK" : 20.748657, + "DJF" : 177.721, + "DKK" : 6.362873, + "DOP" : 63.30698, + "DZD" : 129.797051, + "EGP" : 48.199569, + "ERN" : 15, + "ETB" : 142.617785, + "EUR" : 0.852764, + "FJD" : 2.23254, + "FKP" : 0.73784, + "FOK" : 6.362775, + "GBP" : 0.73784, + "GEL" : 2.700238, + "GGP" : 0.73784, + "GHS" : 12.309839, + "GIP" : 0.73784, + "GMD" : 73.266267, + "GNF" : 8701.870296, + "GTQ" : 7.659586, + "GYD" : 209.224173, + "HKD" : 7.781084, + "HNL" : 26.167933, + "HRK" : 6.42515, + "HTG" : 130.904449, + "HUF" : 333.468411, + "IDR" : 16406.508443, + "ILS" : 3.335032, + "IMP" : 0.73784, + "INR" : 88.340871, + "IQD" : 1308.456721, + "IRR" : 43062.77144, + "ISK" : 122.222755, + "JEP" : 0.73784, + "JMD" : 160.099792, + "JOD" : 0.709, + "JPY" : 147.723052, + "KES" : 129.23056, + "KGS" : 87.448725, + "KHR" : 4010.938008, + "KID" : 1.504805, + "KMF" : 419.532367, + "KRW" : 1391.703023, + "KWD" : 0.305325, + "KYD" : 0.833333, + "KZT" : 540.77636, + "LAK" : 21667.052128, + "LBP" : 89500, + "LKR" : 301.562669, + "LRD" : 185.825803, + "LSL" : 17.37698, + "LYD" : 5.409047, + "MAD" : 9.012638, + "MDL" : 16.557248, + "MGA" : 4472.548435, + "MKD" : 52.476425, + "MMK" : 2100.772909, + "MNT" : 3576.469294, + "MOP" : 8.014516, + "MRU" : 39.997233, + "MUR" : 45.498715, + "MVR" : 15.434868, + "MWK" : 1734.887068, + "MXN" : 18.455145, + "MYR" : 4.204881, + "MZN" : 63.59925, + "NAD" : 17.37698, + "NGN" : 1502.976421, + "NIO" : 36.776231, + "NOK" : 9.869465, + "NPR" : 141.345301, + "NZD" : 1.67992, + "OMR" : 0.384497, + "PAB" : 1, + "PEN" : 3.483822, + "PGK" : 4.223703, + "PHP" : 57.16823, + "PKR" : 283.806523, + "PLN" : 3.629384, + "PYG" : 7131.392431, + "QAR" : 3.64, + "RON" : 4.326722, + "RSD" : 99.932885, + "RUB" : 83.973278, + "RWF" : 1451.829385, + "SAR" : 3.75, + "SBD" : 8.215668, + "SCR" : 15.068005, + "SDG" : 458.527226, + "SEK" : 9.328197, + "SGD" : 1.283094, + "SHP" : 0.73784, + "SLE" : 23.326149, + "SLL" : 23326.149019, + "SOS" : 571.902236, + "SRD" : 39.240336, + "SSP" : 4649.28976, + "STN" : 20.892717, + "SYP" : 13091.420228, + "SZL" : 17.37698, + "THB" : 31.744569, + "TJS" : 9.477585, + "TMT" : 3.498658, + "TND" : 2.906176, + "TOP" : 2.382416, + "TRY" : 41.352061, + "TTD" : 6.785167, + "TVD" : 1.504805, + "TWD" : 30.264759, + "TZS" : 2453.322421, + "UAH" : 41.256881, + "UGX" : 3500.173157, + "UYU" : 39.951659, + "UZS" : 12259.091697, + "VES" : 160.4479, + "VND" : 26260.849652, + "VUV" : 118.803871, + "WST" : 2.671177, + "XAF" : 559.376489, + "XCD" : 2.7, + "XCG" : 1.79, + "XDR" : 0.728892, + "XOF" : 559.376489, + "XPF" : 101.762029, + "YER" : 239.612216, + "ZAR" : 17.376993, + "ZMW" : 23.721686, + "ZWL" : 26.7283 + } +} \ No newline at end of file