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