From 50b85306bf3219306a090172a0581fa5f81a04d1 Mon Sep 17 00:00:00 2001 From: crazzyghost Date: Fri, 1 May 2020 23:48:23 +0000 Subject: [PATCH] indicator tests --- build.gradle | 3 +- .../com/crazzyghost/alphavantage/Config.java | 22 ++ .../alphavantage/indicator/Indicator.java | 25 +- src/test/java/indicator/IndicatorTest.java | 263 ++++++++++++++++++ src/test/java/indicator/data/dema.json | 19 ++ src/test/java/indicator/data/ema.json | 19 ++ src/test/java/indicator/data/kama.json | 19 ++ src/test/java/indicator/data/tema.json | 19 ++ src/test/java/indicator/data/trima.json | 19 ++ src/test/java/indicator/data/wma.json | 19 ++ 10 files changed, 405 insertions(+), 22 deletions(-) create mode 100644 src/test/java/indicator/IndicatorTest.java create mode 100644 src/test/java/indicator/data/dema.json create mode 100644 src/test/java/indicator/data/ema.json create mode 100644 src/test/java/indicator/data/kama.json create mode 100644 src/test/java/indicator/data/tema.json create mode 100644 src/test/java/indicator/data/trima.json create mode 100644 src/test/java/indicator/data/wma.json diff --git a/build.gradle b/build.gradle index 0be1e3c..201b0dd 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,7 @@ sourceCompatibility = 1.8 repositories { mavenCentral() + jcenter() } jacoco { @@ -27,8 +28,8 @@ test.finalizedBy jacocoTestReport dependencies { testImplementation group: 'junit', name: 'junit', version: '4.12' + testImplementation 'com.github.gmazzo:okhttp-mock:1.3.2' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.squareup.okhttp3:logging-interceptor:4.5.0' implementation 'com.squareup.moshi:moshi:1.8.0' - } diff --git a/src/main/java/com/crazzyghost/alphavantage/Config.java b/src/main/java/com/crazzyghost/alphavantage/Config.java index 324b5b0..43dcd09 100644 --- a/src/main/java/com/crazzyghost/alphavantage/Config.java +++ b/src/main/java/com/crazzyghost/alphavantage/Config.java @@ -1,14 +1,20 @@ package com.crazzyghost.alphavantage; +import java.util.concurrent.TimeUnit; + +import okhttp3.OkHttpClient; + public class Config { private String key; private int timeOut; + private OkHttpClient httpClient; public static String BASE_URL = "https://www.alphavantage.co/query?"; public Config(Builder builder) { this.key = builder.key; this.timeOut = builder.timeOut; + this.httpClient = builder.httpClient == null ? defaultClient(builder.timeOut): builder.httpClient; } public int getTimeOut() { @@ -19,13 +25,24 @@ public String getKey() { return key; } + public OkHttpClient getOkHttpClient(){ + return this.httpClient; + } + public static Builder builder(){ return new Builder(); } + private OkHttpClient defaultClient(int timeOut){ + return new OkHttpClient.Builder() + .connectTimeout(timeOut, TimeUnit.SECONDS) + .build(); + } + public static class Builder{ private String key; private int timeOut; + private OkHttpClient httpClient; public Builder key(String key){ this.key = key; @@ -37,6 +54,11 @@ public Builder timeOut(int timeOut){ return this; } + public Builder httpClient(OkHttpClient httpClient){ + this.httpClient = httpClient; + return this; + } + public Config build(){ return new Config(this); } diff --git a/src/main/java/com/crazzyghost/alphavantage/indicator/Indicator.java b/src/main/java/com/crazzyghost/alphavantage/indicator/Indicator.java index c197b74..4b45c38 100644 --- a/src/main/java/com/crazzyghost/alphavantage/indicator/Indicator.java +++ b/src/main/java/com/crazzyghost/alphavantage/indicator/Indicator.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.lang.reflect.Type; import java.util.Map; -import java.util.concurrent.TimeUnit; import com.crazzyghost.alphavantage.AlphaVantageException; import com.crazzyghost.alphavantage.Config; @@ -32,30 +31,21 @@ import com.squareup.moshi.Types; import okhttp3.Call; -import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import okhttp3.logging.HttpLoggingInterceptor; -import okhttp3.logging.HttpLoggingInterceptor.Level; -public class Indicator{ + +public class Indicator { private IndicatorRequest.Builder builder; private Fetcher.SuccessCallback successCallback; private Fetcher.FailureCallback failureCallback; private final Config config; - private final OkHttpClient client; private IndicatorRequest request; public Indicator(final Config config) { this.config = config; - request = null; - final HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); - logging.setLevel(Level.BASIC); - client = new OkHttpClient.Builder() - .connectTimeout(config.getTimeOut(), TimeUnit.SECONDS) - .addInterceptor(logging) - .build(); + this.request = null; } @@ -71,7 +61,7 @@ public void fetch(){ .url(Config.BASE_URL + UrlExtractor.extract(this.request) + config.getKey()) .build(); - client.newCall(request).enqueue(new okhttp3.Callback() { + config.getOkHttpClient().newCall(request).enqueue(new okhttp3.Callback() { @Override public void onFailure(final Call call, final IOException e) { if(failureCallback != null){ @@ -547,7 +537,6 @@ public class MAMARequestProxy extends SimpleIndicatorRequestProxy ref = new AtomicReference<>(); + AlphaVantage.api() + .indicator() + .sma() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60) + .onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testEMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .ema() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60).onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testWMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .wma() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60).onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testDEMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .dema() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60).onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testTEMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .tema() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60).onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testTRIMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .trima() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60).onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testKAMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .kama() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .timePeriod(60) + .onFailure((e) -> lock.countDown()) + .onSuccess((PeriodicSeriesResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } + + @Test + public void testMAMA() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(1); + AtomicReference ref = new AtomicReference<>(); + + AlphaVantage + .api() + .indicator() + .mama() + .forSymbol("IBM") + .interval(Interval.WEEKLY) + .seriesType(SeriesType.OPEN) + .fastLimit(0.1) + .slowLimit(0.5) + .seriesType(SeriesType.OPEN) + .onFailure((e) -> lock.countDown()) + .onSuccess((MAMAResponse e) -> { + lock.countDown(); + ref.set(e); + }) + .dataType(DataType.JSON) + .fetch(); + + lock.await(); + assertEquals(ref.get().getIndicatorUnits().size(), 2); + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/dema.json b/src/test/java/indicator/data/dema.json new file mode 100644 index 0000000..8310cc1 --- /dev/null +++ b/src/test/java/indicator/data/dema.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "DEMA", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: DEMA": { + "2020-04-29": { + "DEMA": "0.9120" + }, + "2020-04-24": { + "DEMA": "0.9118" + } + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/ema.json b/src/test/java/indicator/data/ema.json new file mode 100644 index 0000000..524fe76 --- /dev/null +++ b/src/test/java/indicator/data/ema.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "Exponential Moving Average (EMA)", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: EMA": { + "2020-04-29": { + "EMA": "0.9120" + }, + "2020-04-24": { + "EMA": "0.9118" + } + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/kama.json b/src/test/java/indicator/data/kama.json new file mode 100644 index 0000000..1971ecf --- /dev/null +++ b/src/test/java/indicator/data/kama.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "KAMA", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: KAMA": { + "2020-04-29": { + "KAMA": "0.9120" + }, + "2020-04-24": { + "KAMA": "0.9118" + } + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/tema.json b/src/test/java/indicator/data/tema.json new file mode 100644 index 0000000..4b7c8de --- /dev/null +++ b/src/test/java/indicator/data/tema.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "TEMA", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: TEMA": { + "2020-04-29": { + "TEMA": "0.9120" + }, + "2020-04-24": { + "TEMA": "0.9118" + } + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/trima.json b/src/test/java/indicator/data/trima.json new file mode 100644 index 0000000..ed5fcb9 --- /dev/null +++ b/src/test/java/indicator/data/trima.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "TRIMA", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: TRIMA": { + "2020-04-29": { + "TRIMA": "0.9120" + }, + "2020-04-24": { + "TRIMA": "0.9118" + } + } +} \ No newline at end of file diff --git a/src/test/java/indicator/data/wma.json b/src/test/java/indicator/data/wma.json new file mode 100644 index 0000000..67a3c13 --- /dev/null +++ b/src/test/java/indicator/data/wma.json @@ -0,0 +1,19 @@ +{ + "Meta Data": { + "1: Symbol": "USDEUR", + "2: Indicator": "Weighted Moving Average (WMA)", + "3: Last Refreshed": "2020-04-29", + "4: Interval": "weekly", + "5: Time Period": 10, + "6: Series Type": "open", + "7: Time Zone": "US/Eastern" + }, + "Technical Analysis: WMA": { + "2020-04-29": { + "WMA": "0.9120" + }, + "2020-04-24": { + "WMA": "0.9118" + } + } +} \ No newline at end of file