ResilientCartRepositoryImpl implements CartRepository { @Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; } ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager,
                                       final TaskExecutor taskExecutor);  ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager); @Override ShoppingCart createCart(); @Override ShoppingCart getShoppingCart(final String token); @Override String storeShoppingCart(final ShoppingCart shoppingCart); @Override void evictShoppingCart(final ShoppingCart shoppingCart);  }
@Test public void testGetShoppingCartNullOrEmptyToken() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertNull(repo.getShoppingCart(null)); assertNull(repo.getShoppingCart(" ")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedInActive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final ShoppingContext cachedCartCtx = context.mock(ShoppingContext.class, "cachedCartCtx"); final Shop shop = context.mock(Shop.class, "shop"); final long modified = System.currentTimeMillis(); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.LOGGED_IN)); allowing(cachedCart).getModifiedTimestamp(); will(returnValue(modified - 118000L)); allowing(cachedCart).getShoppingContext(); will(returnValue(cachedCartCtx)); allowing(cachedCartCtx).getShopId(); will(returnValue(111L)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue("120")); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedOutActive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.SESSION_EXPIRED)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedInActiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final ShoppingContext cachedCartCtx = context.mock(ShoppingContext.class, "cachedCartCtx"); final Shop shop = context.mock(Shop.class, "shop"); final long modified = System.currentTimeMillis(); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.LOGGED_IN)); allowing(cachedCart).getModifiedTimestamp(); will(returnValue(modified - 58000L)); allowing(cachedCart).getShoppingContext(); will(returnValue(cachedCartCtx)); allowing(cachedCartCtx).getShopId(); will(returnValue(111L)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue(null)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedOutActiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.SESSION_EXPIRED)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedInInactive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final ShoppingContext cachedCartCtx = context.mock(ShoppingContext.class, "cachedCartCtx"); final Shop shop = context.mock(Shop.class, "shop"); final long modified = System.currentTimeMillis(); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.LOGGED_IN)); allowing(cachedCart).getModifiedTimestamp(); will(returnValue(modified - 121000L)); allowing(cachedCart).getShoppingContext(); will(returnValue(cachedCartCtx)); allowing(cachedCartCtx).getShopId(); will(returnValue(111L)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue("120")); oneOf(cartCache).evict("IN-CACHE"); oneOf(shoppingCartStateService).findByGuid("IN-CACHE"); will(returnValue(null)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertNull(repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedOutInactive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.SESSION_EXPIRED)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertEquals(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedInInactiveMutable() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final long modified = System.currentTimeMillis(); final ShoppingCartImpl cachedCart = new ShoppingCartImpl() { @Override public long getModifiedTimestamp() { return modified - 121000L; } @Override public int getLogonState() { return ShoppingCart.LOGGED_IN; } }; cachedCart.getShoppingContext().setCustomerEmail("bob@doe.com"); cachedCart.getShoppingContext().setShopId(111L); final Shop shop = context.mock(Shop.class, "shop"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); oneOf(cartUpdateProcessor).invalidateShoppingCart(cachedCart); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue("120")); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cachedCart.getGuid(), shoppingCart.getGuid()); assertSame(cachedCart, shoppingCart); } }; assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedOutInactiveMutable() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final long modified = System.currentTimeMillis(); final byte[] cached = new byte[0]; final ShoppingCartImpl cachedCart = new ShoppingCartImpl() { @Override public long getModifiedTimestamp() { return modified - 121000L; } @Override public int getLogonState() { return ShoppingCart.SESSION_EXPIRED; } }; cachedCart.getShoppingContext().setCustomerEmail("bob@doe.com"); cachedCart.getShoppingContext().setShopId(111L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cachedCart.getGuid(), shoppingCart.getGuid()); assertNull(shoppingCart.getCustomerEmail()); } }; assertEquals(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedInInactiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final ShoppingContext cachedCartCtx = context.mock(ShoppingContext.class, "cachedCartCtx"); final Shop shop = context.mock(Shop.class, "shop"); final long modified = System.currentTimeMillis(); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.LOGGED_IN)); allowing(cachedCart).getModifiedTimestamp(); will(returnValue(modified - 61000L)); allowing(cachedCart).getShoppingContext(); will(returnValue(cachedCartCtx)); allowing(cachedCartCtx).getShopId(); will(returnValue(111L)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue(null)); oneOf(cartCache).evict("IN-CACHE"); oneOf(shoppingCartStateService).findByGuid("IN-CACHE"); will(returnValue(null)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertNull(repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromCacheLoggedOutInactiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.SESSION_EXPIRED)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertEquals(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartNotInCacheNotInDb() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("MISSING"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("MISSING"); will(returnValue(null)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertNull(repo.getShoppingCart("MISSING")); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedInActive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final Shop shop = context.mock(Shop.class, "shop"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail("bob@doe.com"); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.LOGGED_IN); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 118000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-ACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-ACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue("120")); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); final ShoppingCart newCart = repo.getShoppingCart("IN-DB-ACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertEquals(newCart.getCustomerEmail(), cart.getCustomerEmail()); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedOutActive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail(null); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); cart.getShoppingContext().setShopId(111L); assertEquals(cart.getLogonState(), ShoppingCart.SESSION_EXPIRED); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 118000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-ACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-ACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); final ShoppingCart newCart = repo.getShoppingCart("IN-DB-ACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertEquals(newCart.getCustomerEmail(), cart.getCustomerEmail()); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedInActiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final Shop shop = context.mock(Shop.class, "shop"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail("bob@doe.com"); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.LOGGED_IN); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 58000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-ACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-ACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue(null)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); final ShoppingCart newCart = repo.getShoppingCart("IN-DB-ACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertEquals(newCart.getCustomerEmail(), cart.getCustomerEmail()); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedOutActiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail(null); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.SESSION_EXPIRED); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 58000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-ACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-ACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); final ShoppingCart newCart = repo.getShoppingCart("IN-DB-ACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertEquals(newCart.getCustomerEmail(), cart.getCustomerEmail()); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedInInactive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final Shop shop = context.mock(Shop.class, "shop"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail("bob@doe.com"); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.LOGGED_IN); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 121000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-INACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-INACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartUpdateProcessor).invalidateShoppingCart(restored); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue("120")); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cart.getGuid(), shoppingCart.getGuid()); assertSame(restored, shoppingCart); } }; final ShoppingCart newCart = repo.getShoppingCart("IN-DB-INACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertSame(restored, newCart); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedOutInactive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail(null); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.SESSION_EXPIRED); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 121000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-INACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-INACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cart.getGuid(), shoppingCart.getGuid()); assertNull(shoppingCart.getCustomerEmail()); } }; final ShoppingCart newCart = repo.getShoppingCart("IN-DB-INACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertNull(newCart.getCustomerEmail()); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedInInactiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final Shop shop = context.mock(Shop.class, "shop"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail("bob@doe.com"); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.LOGGED_IN); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 61000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-INACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-INACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartUpdateProcessor).invalidateShoppingCart(restored); oneOf(shopService).getById(111L); will(returnValue(shop)); oneOf(shop).getAttributeValueByCode(AttributeNamesKeys.Shop.CART_SESSION_EXPIRY_SECONDS); will(returnValue(null)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cart.getGuid(), shoppingCart.getGuid()); assertSame(restored, shoppingCart); } }; final ShoppingCart newCart = repo.getShoppingCart("IN-DB-INACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertSame(restored, newCart); context.assertIsSatisfied(); }
@Test public void testGetShoppingCartFromDbLoggedOutInactiveNoSetting() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final ShoppingCartState cartState = context.mock(ShoppingCartState.class, "cartState"); final Shop shop = context.mock(Shop.class, "shop"); final MutableShoppingCart cart = new ShoppingCartImpl(); cart.getShoppingContext().setCustomerEmail(null); cart.getShoppingContext().setCustomerName("bob@doe.com"); cart.getShoppingContext().setCustomerShops(Collections.singletonList("SHOP10")); cart.getShoppingContext().setShopId(111L); cart.getShoppingContext().setShopCode("SHOP10"); assertEquals(cart.getLogonState(), ShoppingCart.SESSION_EXPIRED); final byte[] bytes = serializeCart(cart); final ShoppingCart restored = deserializeCart(bytes); final Instant stillActive = Instant.ofEpochMilli(System.currentTimeMillis() - 61000L); context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-DB-INACTIVE"); will(returnValue(null)); oneOf(shoppingCartStateService).findByGuid("IN-DB-INACTIVE"); will(returnValue(cartState)); allowing(cartState).getUpdatedTimestamp(); will(returnValue(stillActive)); oneOf(cartState).getState(); will(returnValue(bytes)); oneOf(cartUpdateProcessor).restoreState(bytes); will(returnValue(restored)); oneOf(cartCache).put(cart.getGuid(), bytes); oneOf(mockCartSerDes).saveState(restored); will(returnValue(bytes)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor) { @Override void storeAsynchronously(final ShoppingCart shoppingCart) { assertEquals(cart.getGuid(), shoppingCart.getGuid()); assertNull(shoppingCart.getCustomerEmail()); } }; final ShoppingCart newCart = repo.getShoppingCart("IN-DB-INACTIVE"); assertNotNull(newCart); assertEquals(newCart.getGuid(), cart.getGuid()); assertNull(newCart.getCustomerEmail()); context.assertIsSatisfied(); }