ApplePayConfigDataToValidateMerchantRequestDTOPopulator implements Populator<ApplePayConfigData, ValidateMerchantRequestDTO> { @Override public void populate(ApplePayConfigData applePayConfigData, ValidateMerchantRequestDTO validateMerchantRequestDTO) throws ConversionException { validateMerchantRequestDTO.setMerchantIdentifier(applePayConfigData.getMerchantId()); validateMerchantRequestDTO.setDisplayName(applePayConfigData.getMerchantName()); validateMerchantRequestDTO.setInitiative(WEB); final String currentWebSiteURL = worldpayUrlService.getWebsiteUrlForCurrentSite(); try { final URL url = new URL(currentWebSiteURL); validateMerchantRequestDTO.setInitiativeContext(url.getHost()); } catch (MalformedURLException e) { LOG.error("Malformed URL {}", currentWebSiteURL, e); } }  @Override void populate(ApplePayConfigData applePayConfigData, ValidateMerchantRequestDTO validateMerchantRequestDTO); @Required void setWorldpayUrlService(WorldpayUrlService worldpayUrlService);  }
@Test public void populateWithValidUrlPopulatesEverything() { when(worldpayUrlServiceMock.getWebsiteUrlForCurrentSite()).thenReturn(HTTPS_WWW_APPLE_COM_VALID_PATH); testObj.populate(applePayConfigData, validateMerchantRequestDTO); assertThat(validateMerchantRequestDTO.getInitiative()).isEqualTo(WEB); assertThat(validateMerchantRequestDTO.getDisplayName()).isEqualTo(MERCHANT_NAME); assertThat(validateMerchantRequestDTO.getMerchantIdentifier()).isEqualTo(MERCHANT_ID); assertThat(validateMerchantRequestDTO.getInitiativeContext()).isEqualTo(WWW_APPLE_COM); }
@Test public void populateInvalidUrlDoesNotPopulatesTheInitiativeContextButDoesPopulatesOtherValues() { when(worldpayUrlServiceMock.getWebsiteUrlForCurrentSite()).thenReturn(HTTPS_INVALID_URL_007); testObj.populate(applePayConfigData, validateMerchantRequestDTO); assertThat(validateMerchantRequestDTO.getInitiative()).isEqualTo(WEB); assertThat(validateMerchantRequestDTO.getDisplayName()).isEqualTo(MERCHANT_NAME); assertThat(validateMerchantRequestDTO.getMerchantIdentifier()).isEqualTo(MERCHANT_ID); assertThat(validateMerchantRequestDTO.getInitiativeContext()).isNullOrEmpty(); }