From c3ddf74c0676113b1bd522f029c9ed4342fd0fb1 Mon Sep 17 00:00:00 2001 From: crazzyghost Date: Thu, 21 May 2020 03:54:44 +0000 Subject: [PATCH] add multiple asyn request tests for forex --- .../java/forex/MutipleAsyncRequestTest.java | 136 ++++++++++++++++++ src/test/java/forex/data/weekly.json | 2 +- ...est.java => MultipleAsyncRequestTest.java} | 2 +- 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/test/java/forex/MutipleAsyncRequestTest.java rename src/test/java/indicator/{MultipleRequestTest.java => MultipleAsyncRequestTest.java} (99%) diff --git a/src/test/java/forex/MutipleAsyncRequestTest.java b/src/test/java/forex/MutipleAsyncRequestTest.java new file mode 100644 index 0000000..caa4c0c --- /dev/null +++ b/src/test/java/forex/MutipleAsyncRequestTest.java @@ -0,0 +1,136 @@ +package forex; + +import static org.junit.Assert.assertTrue; +import static util.TestUtils.stream; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import com.crazzyghost.alphavantage.AlphaVantage; +import com.crazzyghost.alphavantage.Config; +import com.crazzyghost.alphavantage.forex.response.ForexResponse; +import com.crazzyghost.alphavantage.parameters.DataType; +import com.crazzyghost.alphavantage.parameters.Interval; +import com.crazzyghost.alphavantage.parameters.OutputSize; + +import org.junit.Before; +import org.junit.Test; + +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.logging.HttpLoggingInterceptor.Level; +import okhttp3.mock.Behavior; +import okhttp3.mock.MockInterceptor; +import util.TestUtils; + +public class MutipleAsyncRequestTest { + + MockInterceptor mockInterceptor = new MockInterceptor(Behavior.RELAYED); + HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); + Config config; + + @Before + public void setUp() throws IOException { + TestUtils.forDirectory("forex"); + + loggingInterceptor.level(Level.BASIC); + OkHttpClient client = new OkHttpClient.Builder().connectTimeout(4, TimeUnit.SECONDS) + .callTimeout(4, TimeUnit.SECONDS).addInterceptor(loggingInterceptor).addInterceptor(mockInterceptor) + .build(); + + config = Config.builder().key("demo").httpClient(client).build(); + + AlphaVantage.api().init(config); + + mockInterceptor.addRule() + .get("https://www.alphavantage.co/query?function=FX_MONTHLY&from_symbol=GHS&to_symbol=USD&datatype=json&apikey=demo") + .respond(stream("monthly")); + + mockInterceptor.addRule() + .get("https://www.alphavantage.co/query?function=FX_WEEKLY&from_symbol=GHS&to_symbol=USD&datatype=json&apikey=demo") + .respond(stream("weekly")); + + mockInterceptor.addRule() + .get("https://www.alphavantage.co/query?function=FX_DAILY&outputsize=full&from_symbol=EUR&to_symbol=USD&datatype=json&apikey=demo") + .respond(stream("daily")); + + mockInterceptor.addRule() + .get("https://www.alphavantage.co/query?interval=5min&function=FX_INTRADAY&outputsize=full&from_symbol=EUR&to_symbol=USD&datatype=json&apikey=demo") + .respond(stream("intraday")); + } + + @Test + public void testMultipleCalls() throws InterruptedException { + CountDownLatch lock = new CountDownLatch(4); + AtomicReference monthlyRef = new AtomicReference<>(); + AtomicReference weeklyRef = new AtomicReference<>(); + AtomicReference dailyRef = new AtomicReference<>(); + AtomicReference intradayRef = new AtomicReference<>(); + + AlphaVantage.api() + .forex() + .monthly() + .fromSymbol("GHS") + .toSymbol("USD") + .dataType(DataType.JSON) + .onSuccess(e -> { + monthlyRef.set(e); + lock.countDown(); + }) + .fetch(); + + AlphaVantage.api() + .forex() + .weekly() + .fromSymbol("GHS") + .toSymbol("USD") + .dataType(DataType.JSON) + .onSuccess(e -> { + weeklyRef.set(e); + lock.countDown(); + }) + .fetch(); + + AlphaVantage.api() + .forex() + .daily() + .fromSymbol("EUR") + .toSymbol("USD") + .outputSize(OutputSize.FULL) + .dataType(DataType.JSON) + .onSuccess(e -> { + dailyRef.set(e); + lock.countDown(); + }) + .fetch(); + + AlphaVantage.api() + .forex() + .intraday() + .toSymbol("USD") + .fromSymbol("EUR") + .dataType(DataType.JSON) + .interval(Interval.FIVE_MIN) + .outputSize(OutputSize.FULL) + .onSuccess(e -> { + intradayRef.set(e); + lock.countDown(); + }) + .fetch(); + + lock.await(); + + System.out.println(monthlyRef.get().getMetaData().getInformation()); + System.out.println(weeklyRef.get().getMetaData().getInformation()); + System.out.println(dailyRef.get().getMetaData().getInformation()); + assertTrue(monthlyRef.get().getMetaData().getInformation().matches("Forex Monthly(.*)")); + assertTrue(weeklyRef.get().getMetaData().getInformation().matches("Forex Weekly(.*)")); + assertTrue(dailyRef.get().getMetaData().getInformation().matches("Forex Daily(.*)")); + assertTrue(intradayRef.get().getMetaData().getInformation().matches("FX Intraday.*")); + + } + + +} \ No newline at end of file diff --git a/src/test/java/forex/data/weekly.json b/src/test/java/forex/data/weekly.json index a7575dd..fc72d5b 100644 --- a/src/test/java/forex/data/weekly.json +++ b/src/test/java/forex/data/weekly.json @@ -1,6 +1,6 @@ { "Meta Data": { - "1. Information": "Forex Daily Prices (open, high, low, close)", + "1. Information": "Forex Weekly Prices (open, high, low, close)", "2. From Symbol": "EUR", "3. To Symbol": "USD", "4. Output Size": "Full size", diff --git a/src/test/java/indicator/MultipleRequestTest.java b/src/test/java/indicator/MultipleAsyncRequestTest.java similarity index 99% rename from src/test/java/indicator/MultipleRequestTest.java rename to src/test/java/indicator/MultipleAsyncRequestTest.java index 1708449..ae55a2c 100644 --- a/src/test/java/indicator/MultipleRequestTest.java +++ b/src/test/java/indicator/MultipleAsyncRequestTest.java @@ -30,7 +30,7 @@ import okhttp3.mock.Behavior; import okhttp3.mock.MockInterceptor; -public class MultipleRequestTest { +public class MultipleAsyncRequestTest { MockInterceptor mockInterceptor = new MockInterceptor(Behavior.UNORDERED); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();