|
| 1 | +/* |
| 2 | + * Copyright 2019 namjug-kim |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.njkim.reactivecrypto.unicornx; |
| 18 | + |
| 19 | +import com.njkim.reactivecrypto.core.common.model.ExchangeVendor; |
| 20 | +import com.njkim.reactivecrypto.core.common.model.currency.CurrencyPair; |
| 21 | +import com.njkim.reactivecrypto.core.common.model.order.OrderBook; |
| 22 | +import com.njkim.reactivecrypto.core.common.model.order.TickData; |
| 23 | +import org.junit.Test; |
| 24 | +import reactor.core.publisher.Flux; |
| 25 | +import reactor.test.StepVerifier; |
| 26 | + |
| 27 | +import java.math.BigDecimal; |
| 28 | +import java.util.Collections; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +public class UnicornxWebsocketClientJavaTest { |
| 33 | + @Test |
| 34 | + public void unicornx_tick_data_subscribe() { |
| 35 | + // given |
| 36 | + CurrencyPair targetCurrencyPair = CurrencyPair.parse("BTC", "USDT"); |
| 37 | + Flux<TickData> tickDataFlux = new UnicornxWebsocketClient() |
| 38 | + .createTradeWebsocket(Collections.singletonList(targetCurrencyPair)); |
| 39 | + |
| 40 | + // when |
| 41 | + StepVerifier.create(tickDataFlux.limitRequest(2)) |
| 42 | + // then |
| 43 | + .assertNext(tickData -> { |
| 44 | + assertThat(tickData).isNotNull(); |
| 45 | + assertThat(tickData.getCurrencyPair()) |
| 46 | + .isEqualTo(targetCurrencyPair); |
| 47 | + assertThat(tickData.getExchangeVendor()) |
| 48 | + .isEqualTo(ExchangeVendor.UNICORNX); |
| 49 | + assertThat(tickData.getPrice()) |
| 50 | + .isGreaterThan(BigDecimal.ZERO); |
| 51 | + assertThat(tickData.getQuantity()) |
| 52 | + .isGreaterThan(BigDecimal.ZERO); |
| 53 | + }) |
| 54 | + .assertNext(tickData -> { |
| 55 | + assertThat(tickData).isNotNull(); |
| 56 | + assertThat(tickData.getCurrencyPair()) |
| 57 | + .isEqualTo(targetCurrencyPair); |
| 58 | + assertThat(tickData.getExchangeVendor()) |
| 59 | + .isEqualTo(ExchangeVendor.UNICORNX); |
| 60 | + assertThat(tickData.getPrice()) |
| 61 | + .isGreaterThan(BigDecimal.ZERO); |
| 62 | + assertThat(tickData.getQuantity()) |
| 63 | + .isGreaterThan(BigDecimal.ZERO); |
| 64 | + }) |
| 65 | + .verifyComplete(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void unicornx_orderBook_subscribe() { |
| 70 | + // given |
| 71 | + CurrencyPair targetCurrencyPair = CurrencyPair.parse("BTC", "USDT"); |
| 72 | + Flux<OrderBook> orderBookFlux = new UnicornxWebsocketClient() |
| 73 | + .createDepthSnapshot(Collections.singletonList(targetCurrencyPair)); |
| 74 | + |
| 75 | + // when |
| 76 | + StepVerifier.create(orderBookFlux.limitRequest(2)) |
| 77 | + // then |
| 78 | + .assertNext(orderBook -> { |
| 79 | + assertThat(orderBook).isNotNull(); |
| 80 | + assertThat(orderBook.getCurrencyPair()) |
| 81 | + .isEqualTo(targetCurrencyPair); |
| 82 | + assertThat(orderBook.getExchangeVendor()) |
| 83 | + .isEqualTo(ExchangeVendor.UNICORNX); |
| 84 | + assertThat(orderBook.getAsks()) |
| 85 | + .isNotEmpty(); |
| 86 | + assertThat(orderBook.getBids()) |
| 87 | + .isNotEmpty(); |
| 88 | + |
| 89 | + assertThat(orderBook.getAsks().get(0).getQuantity()) |
| 90 | + .isGreaterThan(BigDecimal.ZERO); |
| 91 | + assertThat(orderBook.getBids().get(0).getQuantity()) |
| 92 | + .isGreaterThan(BigDecimal.ZERO); |
| 93 | + |
| 94 | + assertThat(orderBook.getAsks().get(0).getPrice()) |
| 95 | + .withFailMessage("ask price must be bigger than bid price") |
| 96 | + .isGreaterThan(orderBook.getBids().get(0).getPrice()); |
| 97 | + |
| 98 | + assertThat(orderBook.getAsks().get(0).getPrice()) |
| 99 | + .withFailMessage("asks must be sorted by price asc") |
| 100 | + .isLessThan(orderBook.getAsks().get(1).getPrice()); |
| 101 | + assertThat(orderBook.getBids().get(0).getPrice()) |
| 102 | + .withFailMessage("bids must be sorted by price desc") |
| 103 | + .isGreaterThan(orderBook.getBids().get(1).getPrice()); |
| 104 | + }) |
| 105 | + .assertNext(orderBook -> { |
| 106 | + assertThat(orderBook).isNotNull(); |
| 107 | + assertThat(orderBook.getCurrencyPair()) |
| 108 | + .isEqualTo(targetCurrencyPair); |
| 109 | + assertThat(orderBook.getExchangeVendor()) |
| 110 | + .isEqualTo(ExchangeVendor.UNICORNX); |
| 111 | + assertThat(orderBook.getAsks()) |
| 112 | + .isNotEmpty(); |
| 113 | + assertThat(orderBook.getBids()) |
| 114 | + .isNotEmpty(); |
| 115 | + |
| 116 | + assertThat(orderBook.getAsks().get(0).getQuantity()) |
| 117 | + .isGreaterThan(BigDecimal.ZERO); |
| 118 | + assertThat(orderBook.getBids().get(0).getQuantity()) |
| 119 | + .isGreaterThan(BigDecimal.ZERO); |
| 120 | + |
| 121 | + assertThat(orderBook.getAsks().get(0).getPrice()) |
| 122 | + .withFailMessage("ask price must be bigger than bid price") |
| 123 | + .isGreaterThan(orderBook.getBids().get(0).getPrice()); |
| 124 | + |
| 125 | + assertThat(orderBook.getAsks().get(0).getPrice()) |
| 126 | + .withFailMessage("asks must be sorted by price asc") |
| 127 | + .isLessThan(orderBook.getAsks().get(1).getPrice()); |
| 128 | + assertThat(orderBook.getBids().get(0).getPrice()) |
| 129 | + .withFailMessage("bids must be sorted by price desc") |
| 130 | + .isGreaterThan(orderBook.getBids().get(1).getPrice()); |
| 131 | + }) |
| 132 | + .verifyComplete(); |
| 133 | + } |
| 134 | +} |
0 commit comments