PaymentModulesManagerImpl implements PaymentModulesManager { @Override public void allowPaymentGatewayForShop(final String label, final String shopCode) { final Shop shop = shopService.getShopByCode(shopCode); if (shop != null) { final AttrValueShop av = shop.getAttributeByCode(AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL); if (av != null) { final String[] allowed = StringUtils.split(av.getVal(), ','); if (allowed == null || allowed.length == 0) { shopService.updateAttributeValue( shop.getShopId(), AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL, label); } else if (!Arrays.asList(allowed).contains(label)) { final List<String> updated = new ArrayList<>(Arrays.asList(allowed)); updated.add(label); shopService.updateAttributeValue( shop.getShopId(), AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL, StringUtils.join(updated, ',')); } } else { shopService.updateAttributeValue( shop.getShopId(), AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL, label); } } } PaymentModulesManagerImpl(final ServiceLocator serviceLocator,
                                     final SystemService systemService,
                                     final ShopService shopService); @Override Collection<PaymentModule> getPaymentModules(); @Override Collection<PaymentGatewayDescriptor> getPaymentGatewaysDescriptors(final String paymentModuleLabel); @Override void allowPaymentGateway(final String label); @Override void allowPaymentGatewayForShop(final String label, final String shopCode); @Override void disallowPaymentGateway(final String label); @Override void disallowPaymentGatewayForShop(final String label, final String shopCode); @Override List<PaymentGatewayDescriptor> getPaymentGatewaysDescriptors(final boolean includeDisabled, final String shopCode); @Override PaymentGateway getPaymentGateway(final String paymentGatewayLabel, final String shopCode); @Override void register(final PaymentModule paymentModule);  }
@Test public void testAllowPaymentGatewayForShop() { final ShopService shopService = mockery.mock(ShopService.class); final Shop shop = mockery.mock(Shop.class); final AttrValueShop attrValueShop = mockery.mock(AttrValueShop.class); mockery.checking(new Expectations() {{ allowing(shopService).getShopByCode("SHOP10"); will(returnValue(shop)); allowing(shop).getShopId(); will(returnValue(10L)); allowing(shop).getAttributeByCode(AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL); will(returnValue(attrValueShop)); }}); mockery.checking(new Expectations() {{ oneOf(attrValueShop).getVal(); will(returnValue("aaa,bbb,ccc,ddd")); oneOf(shopService).updateAttributeValue(10L, AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL, "aaa,bbb,ccc,ddd,eee"); }}); mockery.checking(new Expectations() {{ oneOf(attrValueShop).getVal(); will(returnValue("aaa,bbb")); }}); mockery.checking(new Expectations() {{ oneOf(attrValueShop).getVal(); will(returnValue("xx,yy,")); oneOf(shopService).updateAttributeValue(10L, AttributeNamesKeys.Shop.SHOP_ACTIVE_PAYMENT_GATEWAYS_LABEL, "xx,yy,zz"); }}); PaymentModulesManager pmm = new PaymentModulesManagerImpl( null, null, shopService); pmm.allowPaymentGatewayForShop("eee", "SHOP10"); pmm = new PaymentModulesManagerImpl( null, null, shopService); pmm.allowPaymentGatewayForShop("aaa", "SHOP10"); pmm = new PaymentModulesManagerImpl( null, null, shopService); pmm.allowPaymentGatewayForShop("zz", "SHOP10"); }