PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/foobar-test/src/com/ripplesystem/foobar/test/FoobarServiceTest.java

https://github.com/yizumi/foobar-server
Java | 357 lines | 282 code | 45 blank | 30 comment | 0 complexity | c52257cd76463b79fddadc001bf245e8 MD5 | raw file
  1. package com.ripplesystem.foobar.test;
  2. import static org.junit.Assert.*;
  3. import java.util.Date;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
  8. import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
  9. import com.google.inject.Guice;
  10. import com.google.inject.Injector;
  11. import com.ripplesystem.foobar.command.*;
  12. import com.ripplesystem.foobar.model.ShopInfo;
  13. import com.ripplesystem.foobar.model.TransactionInfo;
  14. import com.ripplesystem.foobar.model.TransactionType;
  15. import com.ripplesystem.foobar.service.FoobarService;
  16. public class FoobarServiceTest
  17. {
  18. private Injector injector;
  19. private final LocalServiceTestHelper helper =
  20. new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  21. @Before
  22. public void setUp()
  23. {
  24. helper.setUp();
  25. injector = Guice.createInjector(new TestModule());
  26. }
  27. @After
  28. public void tearDown()
  29. {
  30. helper.tearDown();
  31. }
  32. @Test
  33. public void testFBServiceInSequence()
  34. {
  35. FoobarService fbs = injector.getInstance(FoobarService.class);
  36. // This is the beginning of everything
  37. String deviceId = "7968d30409c82898a293e4da37d689d5df3de471f55678f558a226374a3dab93";
  38. String deviceId2 = "7968d30409c82898a293e4da37d689d5df3de471f55678f558a226374a3dab94";
  39. String apnKey = "7968D304 09C82898 A293E4DA 37D689D5 DF3DE471 F55678F55 8A226374A 3DAB93";
  40. // Let's register the device
  41. FBGetTokenForDevice.Response resToken = testGetTokenForDevice(fbs, deviceId, apnKey);
  42. // Let's reigster another device
  43. FBGetTokenForDevice.Response resToken2 = testGetTokenForDevice(fbs, deviceId2, null);
  44. // Test if two devices tokens are different from each other
  45. assertFalse(resToken.getToken().equals(resToken2.getToken()));
  46. // Let's register the shop
  47. FBCreateShop.Response resShop = testCreateShop(fbs);
  48. // Update the shop
  49. FBUpdateShop.Response resUpdateShop = testUpdateShop(fbs, resShop.getShopKey());
  50. // Get the shop
  51. FBGetShop.Response getShopInfoRes = testGetShopInfo(fbs, resShop.getShopKey());
  52. // Login as the shop
  53. testLoginShop(fbs, getShopInfoRes.getShop());
  54. // Let's give points to this guy
  55. testAddPoints(fbs, resToken.getToken(), resShop.getShopKey());
  56. // Let's get list of shops
  57. testGetShopListForDevice(fbs, deviceId);
  58. // Let's get redeem token
  59. FBGetRedeemToken.Response resRedeemToken = testGetRedeemToken(fbs, deviceId, resUpdateShop.getShopKey());
  60. // Let's subtract points from this dude.
  61. testRedeemPoints(fbs, resUpdateShop.getShopKey(), resRedeemToken.getRedeemToken());
  62. // Let's remove the shop
  63. testRemoveShop(fbs, getShopInfoRes.getShop());
  64. // Let's get list of shops... this time it would be... empty!
  65. testGetShopListForDevice2(fbs, deviceId);
  66. // Okay, let's test the transaction history
  67. FBQueryTransactions.Response qRes = testQueryTransactionInfo(fbs, resShop.getShopKey(), resToken.getToken());
  68. // Delete
  69. testCancelTransaction(fbs, qRes.getTransactions().get(0), resToken.getToken());
  70. }
  71. /**
  72. * Hello world
  73. */
  74. private FBGetTokenForDevice.Response testGetTokenForDevice(FoobarService fbs, String deviceId, String deviceToken)
  75. {
  76. FBGetTokenForDevice cmd = new FBGetTokenForDevice();
  77. cmd.setDeviceId(deviceId);
  78. cmd.setDeviceToken(deviceToken);
  79. FBGetTokenForDevice.Response res1 = (FBGetTokenForDevice.Response)fbs.exec(cmd);
  80. assertNotNull(res1.getToken());
  81. // Run this for the second time to see if the device and token can be brought from persistence.
  82. cmd.setDeviceToken("41268108");
  83. FBGetTokenForDevice.Response res2 = (FBGetTokenForDevice.Response)fbs.exec(cmd);
  84. assertEquals(res1.getToken(), res2.getToken());
  85. return res2;
  86. }
  87. private FBCreateShop.Response testCreateShop(FoobarService fbs)
  88. {
  89. FBCreateShop cmd = new FBCreateShop();
  90. cmd.setName("まんじまけろ〜に");
  91. cmd.setAddress("東京都八王子西八王子1−2−3");
  92. cmd.setTel("03-1234-1234");
  93. cmd.setUrl("http://www.manjimakeroni.com");
  94. cmd.setEmail("izumi@apcandsons.com");
  95. cmd.setPassword("12345678");
  96. cmd.setPreferredLang("ja-JP");
  97. FBCreateShop.Response res = (FBCreateShop.Response)fbs.exec(cmd);
  98. assertEquals(true, res.isSuccess());
  99. assertNotNull(res.getShop());
  100. assertEquals(cmd.getName(), res.getShop().getName());
  101. assertEquals(cmd.getAddress(), res.getShop().getAddress());
  102. assertEquals(cmd.getTel(), res.getShop().getTel());
  103. assertEquals(cmd.getUrl(), res.getShop().getUrl());
  104. assertEquals(cmd.getEmail(), res.getShop().getEmail());
  105. assertEquals(cmd.getPassword(), res.getShop().getPassword());
  106. assertEquals(cmd.getPreferredLang(), res.getShop().getPreferredLang());
  107. return res;
  108. }
  109. private FBUpdateShop.Response testUpdateShop(FoobarService fbs, long shopKey)
  110. {
  111. FBUpdateShop cmd = new FBUpdateShop();
  112. cmd.setShopKey(shopKey);
  113. cmd.setName("まんじまけろーに");
  114. cmd.setAddress("東京都八王子西八王子2−3−4");
  115. cmd.setTel("080-1234-5678");
  116. cmd.setUrl("http://www.manjimakeroni.net");
  117. cmd.setEmail("izumi@apcandsons.net");
  118. cmd.setPassword("87654321");
  119. cmd.setPreferredLang("en-US");
  120. FBUpdateShop.Response res = (FBUpdateShop.Response)fbs.exec(cmd);
  121. assertTrue(res.isSuccess());
  122. assertNotNull(res.getShop());
  123. assertEquals("まんじまけろーに", res.getShop().getName());
  124. return res;
  125. }
  126. private FBGetShop.Response testGetShopInfo(FoobarService fbs, long shopKey)
  127. {
  128. FBGetShop cmd = new FBGetShop();
  129. cmd.setShopKey(shopKey);
  130. FBGetShop.Response res = (FBGetShop.Response)fbs.exec(cmd);
  131. ShopInfo shop = res.getShop();
  132. assertEquals("まんじまけろーに", shop.getName());
  133. assertEquals("東京都八王子西八王子2−3−4", shop.getAddress());
  134. assertEquals("080-1234-5678", shop.getTel());
  135. assertEquals("http://www.manjimakeroni.net", shop.getUrl());
  136. assertEquals(String.format("/foobar/GetShopImage?shopKey=%d", shop.getKey()), shop.getImageUrl());
  137. assertEquals("izumi@apcandsons.net", shop.getEmail());
  138. assertEquals("87654321", shop.getPassword());
  139. assertEquals("en-US", shop.getPreferredLang());
  140. return res;
  141. }
  142. private FBLoginShop.Response testLoginShop(FoobarService fbs, ShopInfo shop)
  143. {
  144. FBLoginShop cmd = new FBLoginShop();
  145. // Emulate wrong email address entry
  146. {
  147. cmd.setEmail(shop.getEmail() + "x");
  148. FBLoginShop.Response res = (FBLoginShop.Response)fbs.exec(cmd);
  149. assertFalse(res.isSuccess());
  150. assertEquals(FBLoginShop.Response.FAILCODE_SHOP_NOT_FOUND, res.getFailCode());
  151. }
  152. // Set the correct email this time
  153. cmd.setEmail(shop.getEmail());
  154. // Emulate a mistake by entering a wrong password
  155. {
  156. cmd.setPassword(shop.getPassword() + "x");
  157. FBLoginShop.Response res = (FBLoginShop.Response)fbs.exec(cmd);
  158. assertFalse(res.isSuccess());
  159. assertEquals(FBLoginShop.Response.FAILCODE_PASSWORD_MISMATCH, res.getFailCode());
  160. }
  161. // Get everything right this time.
  162. {
  163. cmd.setPassword(shop.getPassword());
  164. FBLoginShop.Response res = (FBLoginShop.Response)fbs.exec(cmd);
  165. assertTrue(res.isSuccess());
  166. assertNotNull(res.getShop());
  167. assertEquals(shop.getName(), res.getShop().getName());
  168. assertEquals(shop.getKey().longValue(), res.getShopKey());
  169. return res;
  170. }
  171. }
  172. private FBAddPoints.Response testAddPoints(FoobarService fbs, String userToken, long shopKey)
  173. {
  174. FBAddPoints cmd = new FBAddPoints();
  175. cmd.setUserToken(userToken);
  176. cmd.setShopKey(shopKey);
  177. cmd.setPoints(100);
  178. {
  179. FBAddPoints.Response res = (FBAddPoints.Response)fbs.exec(cmd);
  180. assertEquals(true, res.isSuccess());
  181. assertEquals(100, res.getCurrentPoints());
  182. }
  183. // Add points
  184. {
  185. cmd.setPoints(50);
  186. cmd.setUserToken("ZYXWV"); // Some random token
  187. FBAddPoints.Response res = (FBAddPoints.Response)fbs.exec(cmd);
  188. assertFalse(res.isSuccess());
  189. assertEquals(FBAddPoints.Response.FAILCODE_USER_NOT_FOUND, res.getFailCode());
  190. }
  191. // Add points one more time
  192. {
  193. cmd.setUserToken(userToken);
  194. cmd.setPoints(50);
  195. FBAddPoints.Response res = (FBAddPoints.Response)fbs.exec(cmd);
  196. assertEquals(true, res.isSuccess());
  197. assertEquals(150, res.getCurrentPoints());
  198. return res;
  199. }
  200. }
  201. private FBGetShopListForDevice.Response testGetShopListForDevice(FoobarService fbs, String deviceId)
  202. {
  203. FBGetShopListForDevice cmd = new FBGetShopListForDevice();
  204. cmd.setDeviceId(deviceId);
  205. FBGetShopListForDevice.Response res = (FBGetShopListForDevice.Response)fbs.exec(cmd);
  206. assertEquals(1, res.getShops().size());
  207. ShopInfo shop = (ShopInfo)res.getShops().get(0);
  208. assertEquals("まんじまけろーに", shop.getName());
  209. assertEquals((long)150, (long)res.getShops().get(0).getPoints());
  210. return res;
  211. }
  212. private FBGetRedeemToken.Response testGetRedeemToken(FoobarService fbs, String deviceId, long shopKey)
  213. {
  214. FBGetRedeemToken cmd = new FBGetRedeemToken();
  215. cmd.setDeviceId(deviceId);
  216. cmd.setShopKey(shopKey);
  217. FBGetRedeemToken.Response res = (FBGetRedeemToken.Response)fbs.exec(cmd);
  218. assertTrue(res.isSuccess());
  219. assertNotNull(res.getRedeemToken());
  220. assertNotNull(res.getExpiration());
  221. assertTrue(res.getExpiration().getTime() - new Date().getTime() >= 2.5 * 1000 * 60 * 60);
  222. assertTrue(res.getExpiration().getTime() - new Date().getTime() <= 3.5 * 1000 * 60 * 60);
  223. return res;
  224. }
  225. private FBRedeemPoints.Response testRedeemPoints(FoobarService fbs, long shopKey, String redeemToken)
  226. {
  227. FBRedeemPoints cmd = new FBRedeemPoints();
  228. cmd.setRedeemToken(redeemToken);
  229. cmd.setShopKey(shopKey);
  230. cmd.setPoints(200); // Make this fail!
  231. FBRedeemPoints.Response res = (FBRedeemPoints.Response)fbs.exec(cmd);
  232. assertFalse(res.isSuccess());
  233. assertEquals(150, res.getRemainingPoints());
  234. cmd.setPoints(100);
  235. FBRedeemPoints.Response res2 = (FBRedeemPoints.Response)fbs.exec(cmd);
  236. assertTrue(res2.isSuccess());
  237. assertEquals(50, res2.getRemainingPoints());
  238. return res2;
  239. }
  240. private FBDeleteShop.Response testRemoveShop(FoobarService fbs, ShopInfo shop)
  241. {
  242. FBDeleteShop cmd = new FBDeleteShop();
  243. cmd.setEmail(shop.getEmail());
  244. cmd.setPassword(shop.getPassword());
  245. FBDeleteShop.Response res = (FBDeleteShop.Response)fbs.exec(cmd);
  246. assertTrue(res.isSuccess());
  247. return res;
  248. }
  249. private FBGetShopListForDevice.Response testGetShopListForDevice2(FoobarService fbs, String deviceId)
  250. {
  251. FBGetShopListForDevice cmd = new FBGetShopListForDevice();
  252. cmd.setDeviceId(deviceId);
  253. FBGetShopListForDevice.Response res = (FBGetShopListForDevice.Response)fbs.exec(cmd);
  254. assertEquals(0, res.getShops().size());
  255. return res;
  256. }
  257. private FBQueryTransactions.Response testQueryTransactionInfo(FoobarService fbs, Long shopKey, String userToken)
  258. {
  259. FBQueryTransactions cmd = new FBQueryTransactions();
  260. cmd.setCount(10);
  261. cmd.setPage(0);
  262. cmd.setShopKey(shopKey);
  263. cmd.setUserToken(userToken);
  264. FBQueryTransactions.Response res = (FBQueryTransactions.Response)fbs.exec(cmd);
  265. // check that we are getting something.
  266. assertEquals(10, res.getCount());
  267. assertEquals(0, res.getPage());
  268. assertFalse(res.isHasMore());
  269. // Check the first item (Redeem points of 100)
  270. {
  271. TransactionInfo tx1 = res.getTransactions().get(0);
  272. assertNotNull(tx1);
  273. assertEquals(TransactionType.Redeem, tx1.getAddOrRedeem());
  274. assertEquals(100, tx1.getPoints());
  275. assertEquals("まんじまけろーに", tx1.getShopName());
  276. assertNotNull(tx1.getTime());
  277. assertNotNull(tx1.getUserName());
  278. }
  279. // Check the second item (Add points of 50)
  280. {
  281. TransactionInfo tx2 = res.getTransactions().get(1);
  282. assertNotNull(tx2);
  283. assertEquals(TransactionType.Add, tx2.getAddOrRedeem());
  284. assertEquals(50, tx2.getPoints());
  285. assertEquals("まんじまけろーに", tx2.getShopName());
  286. assertNotNull(tx2.getTime());
  287. assertNotNull(tx2.getUserName());
  288. }
  289. // Check the third item (Add points of 100)
  290. {
  291. TransactionInfo tx3 = res.getTransactions().get(2);
  292. assertNotNull(tx3);
  293. assertEquals(TransactionType.Add, tx3.getAddOrRedeem());
  294. assertEquals(100, tx3.getPoints());
  295. assertEquals("まんじまけろーに", tx3.getShopName());
  296. assertNotNull(tx3.getTime());
  297. assertNotNull(tx3.getUserName());
  298. return res;
  299. }
  300. }
  301. private void testCancelTransaction(FoobarService fbs, TransactionInfo tran, String userToken)
  302. {
  303. FBCancelTransaction cmd = new FBCancelTransaction();
  304. cmd.setTransactionKey(tran.getKey());
  305. FBCancelTransaction.Response res = (FBCancelTransaction.Response)fbs.exec(cmd);
  306. assertTrue(res.isSuccess());
  307. assertEquals(tran.getShopKey().longValue(), res.getShopKey());
  308. assertEquals(tran.getUserKey().longValue(), res.getUserKey());
  309. assertEquals(150, res.getRemainingPoints());
  310. assertEquals(userToken, res.getUserToken());
  311. }
  312. }