This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
/
ServiceTestConfiguration.java
311 lines (278 loc) · 12.7 KB
/
ServiceTestConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.nanotrader.web.configuration;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anySetOf;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.nanotrader.data.domain.Account;
import org.springframework.nanotrader.data.domain.Accountprofile;
import org.springframework.nanotrader.data.domain.Holding;
import org.springframework.nanotrader.data.domain.HoldingAggregate;
import org.springframework.nanotrader.data.domain.HoldingSummary;
import org.springframework.nanotrader.data.domain.MarketSummary;
import org.springframework.nanotrader.data.domain.Order;
import org.springframework.nanotrader.data.domain.PortfolioSummary;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.service.TradingService;
import org.springframework.nanotrader.data.service.TradingServiceImpl;
import org.springframework.nanotrader.data.util.FinancialUtils;
import org.springframework.nanotrader.service.cache.DataCreationProgressCache;
import org.springframework.nanotrader.service.support.AdminServiceFacade;
import org.springframework.nanotrader.service.support.AdminServiceFacadeImpl;
import org.springframework.nanotrader.service.support.TradingServiceFacade;
import org.springframework.nanotrader.service.support.TradingServiceFacadeImpl;
/**
* ServiceTestConfiguration provides test objects and mock service layer for unit tests.
*
* @author Brian Dussault
* @author
*/
@Configuration
@Profile("test")
public class ServiceTestConfiguration {
//Holding constants
public static Integer HOLDING_ID = 100;
public static Integer ACCOUNT_ID = 500;
public static BigDecimal PURCHASE_PRICE = BigDecimal.valueOf(50000);
public static String SYMBOL = "VMW";
public static BigDecimal QUANTITY = BigDecimal.valueOf(200);
//Account profile constants
public static Integer PROFILE_ID = 400;
public static String USER_ID = "johndoe";
public static String EMAIL = "anon@springsource.com";
public static String FULL_NAME = "John Doe";
public static String CC_NUMBER = "999999999";
public static String ADDRESS = "45 Test Dr.";
public static Integer NOT_A_VALID_PROFILE = 900;
public static String PASSWORD = "password";
public static String BAD_USER_ID = "NA";
public static String BAD_PASSWORD = "NA";
//Order constants
public static Integer ORDER_ID = 555;
public static BigDecimal ORDER_PRICE = BigDecimal.valueOf(100);
public static BigDecimal ORDER_QUANTITY = BigDecimal.valueOf(200);
public static String ORDER_TYPE_BUY = "buy";
public static String ORDER_STATUS_CLOSED = "closed";
//Quote constants
public static Integer QUOTE_ID = 1;
public static String COMPANY_NAME = "VMware";
public static BigDecimal HIGH = BigDecimal.valueOf(50.02);
public static BigDecimal OPEN = BigDecimal.valueOf(40.11);
public static BigDecimal VOLUME = BigDecimal.valueOf(3000);
public static BigDecimal CURRENT_PRICE = BigDecimal.valueOf(48.44);
public static Integer RANDOM_QUOTES_COUNT = 5;
//Account constants
public static BigDecimal ACCOUNT_OPEN_BALANCE = BigDecimal.valueOf(55.02);
public static BigDecimal ACCOUNT_BALANCE = BigDecimal.valueOf(40.11);
public static Integer LOGOUT_COUNT = new Integer(5);
public static Integer LOGIN_COUNT = new Integer(4);
public static String AUTH_TOKEN = "faef8649-280d-4ba4-bdf6-574e758a04a7";
//Portfolio Summary
public static Integer HOLDING_COUNT = 1;
public static BigDecimal BASIS = BigDecimal.valueOf(150.25);
public static BigDecimal MARKET_VALUE = BigDecimal.valueOf(300.10);
//Market Summary
public static BigDecimal MARKET_INDEX = BigDecimal.valueOf(100.25);
public static BigDecimal MARKET_OPENING = BigDecimal.valueOf(35.25);
public static BigDecimal MARKET_VOLUME = BigDecimal.valueOf(40.45);
//Holding Summary
public static BigDecimal HOLDING_SUMMARY_GAINS = BigDecimal.valueOf(1000.54);
public static BigDecimal GAIN1 = BigDecimal.valueOf(600.54);
public static BigDecimal GAIN2 = BigDecimal.valueOf(400.00);
public static String SYMBOL2 = "OTHER";
public static String PAGE_LABEL = "page";
public static String PAGE_SIZE = "pageSize";
public static String TOTAL_RECORDS = "totalRecords";
public static Long RESULT_COUNT = new Long(1);
public static String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date(1329759342904l));
@Bean
public TradingService tradingService() {
TradingService tradingService = Mockito.mock(TradingService.class);
when(tradingService.findHolding(eq(100), eq(ACCOUNT_ID))).thenReturn(holding());
when(tradingService.findHoldingsByAccountId(eq(ACCOUNT_ID), any(Integer.class), any(Integer.class))).thenReturn(holdings());
when(tradingService.findCountOfHoldingsByAccountId(eq(ACCOUNT_ID))).thenReturn(RESULT_COUNT);
when(tradingService.findCountOfOrders(eq(ACCOUNT_ID) , any(String.class))).thenReturn(RESULT_COUNT);
when(tradingService.updateHolding(any(Holding.class))).thenReturn(holding());
when(tradingService.findAccountProfile(400)).thenReturn(accountProfile());
when(tradingService.findAccountProfile(NOT_A_VALID_PROFILE)).thenReturn(null);
when(tradingService.updateAccountProfile(any(Accountprofile.class), any(String.class))).thenReturn(accountProfile());
when(tradingService.findOrder(eq(999), eq(ACCOUNT_ID))).thenReturn(order());
when(tradingService.saveOrder(any(Order.class))).thenReturn(null);
when(tradingService.saveAccountProfile(any(Accountprofile.class))).thenReturn(accountProfile());
when(tradingService.updateOrder(any(Order.class))).thenReturn(null);
when(tradingService.findOrdersByStatus(eq(ACCOUNT_ID), any(String.class), any(Integer.class), any(Integer.class))).thenReturn(orders());
when(tradingService.findOrders(eq(ACCOUNT_ID), any(Integer.class), any(Integer.class))).thenReturn(orders());
when(tradingService.findQuoteBySymbol(eq(SYMBOL))).thenReturn(quote());
when(tradingService.findRandomQuotes(RANDOM_QUOTES_COUNT)).thenReturn(quotes());
when(tradingService.findQuotesBySymbols(anySetOf(String.class))).thenReturn(quotes());
when(tradingService.findAccount(eq(ACCOUNT_ID))).thenReturn(account());
when(tradingService.findAccountByProfile(any(Accountprofile.class))).thenReturn(account());
when(tradingService.findPortfolioSummary(eq(ACCOUNT_ID))).thenReturn(portfolioSummary());
when(tradingService.findMarketSummary()).thenReturn(marketSummary());
when(tradingService.login(eq(USER_ID), eq(PASSWORD))).thenReturn(accountProfile());
when(tradingService.login(eq(BAD_USER_ID), eq(BAD_PASSWORD))).thenReturn(null);
when(tradingService.findHoldingSummary(eq(ACCOUNT_ID))).thenReturn(holdingSummary());
doNothing().when(tradingService).logout(any(String.class));
return tradingService;
}
@Bean
public TradingServiceFacade tradingServiceFacade() {
return new TradingServiceFacadeImpl();
}
@Bean
public AdminServiceFacade adminServiceFacade() {
return new AdminServiceFacadeImpl();
}
@Bean
public DataCreationProgressCache getProgressCache() {
return new DataCreationProgressCache();
}
@Bean
public Holding holding() {
Holding holding = new Holding();
holding.setHoldingid(HOLDING_ID);
holding.setAccountAccountid(ACCOUNT_ID);
holding.setPurchasedate(new Date(1329759342904l));
holding.setQuoteSymbol(SYMBOL);
holding.setPurchaseprice(PURCHASE_PRICE);
holding.setQuantity(QUANTITY);
return holding;
}
@Bean
public Account account() {
Account account = new Account();
account.setAccountid(ACCOUNT_ID);
account.setBalance(ACCOUNT_BALANCE);
account.setOpenbalance(ACCOUNT_OPEN_BALANCE);
account.setLogincount(LOGIN_COUNT);
account.setLogoutcount(LOGOUT_COUNT);
account.setCreationdate(new Date(1329759342904l));
account.setLastlogin(new Date(1329759342904l));
return account;
}
@Bean
public Accountprofile accountProfile() {
Accountprofile accountProfile = new Accountprofile();
accountProfile.setProfileid(PROFILE_ID);
accountProfile.setUserid(USER_ID);
accountProfile.setPasswd(PASSWORD);
accountProfile.setAddress(ADDRESS);
accountProfile.setEmail(EMAIL);
accountProfile.setFullname(FULL_NAME);
accountProfile.setCreditcard(CC_NUMBER);
accountProfile.setAuthtoken(AUTH_TOKEN);
Set<Account> accounts = new HashSet<Account>();
accounts.add(account());
accountProfile.setAccounts(accounts);
return accountProfile;
}
@Bean
public Order order() {
Order order = new Order();
Account account = new Account();
account.setAccountid(ACCOUNT_ID);
order.setAccountAccountid(account);
order.setOrderid(ORDER_ID);
order.setPrice(ORDER_PRICE);
order.setOrderstatus(ORDER_STATUS_CLOSED);
order.setOrdertype(ORDER_TYPE_BUY);
order.setOpendate(new Date(1329759342904l));
order.setCompletiondate(new Date(1329759342904l));
order.setHoldingHoldingid(holding());
order.setQuantity(ORDER_QUANTITY);
order.setOrderfee(TradingServiceImpl.DEFAULT_ORDER_FEE);
order.setQuote(quote());
return order;
}
public Quote quote() {
Quote quote = new Quote();
quote.setQuoteid(QUOTE_ID);
quote.setSymbol(SYMBOL);
quote.setCompanyname(COMPANY_NAME);
quote.setHigh(HIGH);
quote.setOpen1(OPEN);
quote.setVolume(VOLUME);
quote.setPrice(CURRENT_PRICE);
return quote;
}
public List<Quote> quotes() {
List<Quote> quotes = new ArrayList<Quote>();
quotes.add(quote());
return quotes;
}
public List<Order> orders() {
List<Order> orders = new ArrayList<Order>();
orders.add(order());
return orders;
}
public List<Holding> holdings() {
List<Holding> holdings = new ArrayList<Holding>();
holdings.add(holding());
return holdings;
}
public PortfolioSummary portfolioSummary() {
PortfolioSummary portfolioSummary = new PortfolioSummary();
portfolioSummary.setNumberOfHoldings(HOLDING_COUNT);
portfolioSummary.setTotalBasis(BASIS);
portfolioSummary.setTotalMarketValue(MARKET_VALUE);
return portfolioSummary;
}
public MarketSummary marketSummary() {
MarketSummary marketSummary = new MarketSummary();
marketSummary.setSummaryDate(new Date(1329759342904l));
marketSummary.setTradeStockIndexAverage(MARKET_INDEX);
marketSummary.setTradeStockIndexOpenAverage(MARKET_OPENING);
marketSummary.setTradeStockIndexVolume(MARKET_VOLUME);
List<Quote> loserQuotes = new ArrayList<Quote>();
loserQuotes.add(quote());
marketSummary.setTopLosers(loserQuotes);
List<Quote> gainingQuotes = new ArrayList<Quote>();
gainingQuotes.add(quote());
marketSummary.setTopGainers(gainingQuotes);
return marketSummary;
}
public HoldingSummary holdingSummary() {
HoldingSummary holdingSummary = new HoldingSummary();
List<HoldingAggregate> holdings = new ArrayList<HoldingAggregate>();
holdingSummary.setHoldingsTotalGains(HOLDING_SUMMARY_GAINS.setScale(2, RoundingMode.HALF_UP));
HoldingAggregate holding1 = new HoldingAggregate();
holding1.setSymbol(SYMBOL);
holding1.setGain(GAIN1.setScale(2, RoundingMode.HALF_UP));
holding1.setPercent(FinancialUtils.calculateGainPercentage(holding1.getGain(), holdingSummary.getHoldingsTotalGains()).setScale(2, RoundingMode.HALF_UP));
holdings.add(holding1);
HoldingAggregate holding2 = new HoldingAggregate();
holding2.setSymbol(SYMBOL2);
holding2.setGain(GAIN2.setScale(2, RoundingMode.HALF_UP));
holding2.setPercent(FinancialUtils.calculateGainPercentage(holding2.getGain(), holdingSummary.getHoldingsTotalGains()).setScale(2, RoundingMode.HALF_UP));
holdings.add(holding2);
holdingSummary.setHoldingRollups(holdings);
return holdingSummary;
}
}