BannerValidator { public static void validate(Banner banner) throws InvalidIdentityZoneConfigurationException { if (banner != null){ if (StringUtils.hasText(banner.getLink())) { if (!UaaUrlUtils.isUrl(banner.getLink())) { throw new InvalidIdentityZoneConfigurationException("Invalid banner link: " + banner.getLink() + ". Must be a properly formatted URI beginning with http: } } if (StringUtils.hasText(banner.getBackgroundColor())) { if(!hexColorPattern.matcher(banner.getBackgroundColor()).matches()) { throw new InvalidIdentityZoneConfigurationException("Invalid banner background color: " + banner.getBackgroundColor() + ". Must be a properly formatted hexadecimal color code.", null); } } if (StringUtils.hasText(banner.getTextColor())) { if(!hexColorPattern.matcher(banner.getTextColor()).matches()) { throw new InvalidIdentityZoneConfigurationException("Invalid banner text color: " + banner.getTextColor() + ". Must be a properly formatted hexadecimal color code.", null); } } if (StringUtils.hasText(banner.getLogo())) { if(!Base64.isBase64(banner.getLogo())) { throw new InvalidIdentityZoneConfigurationException("Invalid banner logo. Must be in BASE64 format.", null); } } } }  static void validate(Banner banner);  }
@Test public void validatesUrls() throws InvalidIdentityZoneConfigurationException { String[] validUrls = { "https: "https: "http: "ftp: "https: "https: "https: "https: "https: "https: "https: "https: "https: "https: "https: "https: "http: "http: }; String[] invalidUrls = { "example", "example.com", "example.com:666", " " " " "rdar: "h: ": "ftps: }; for(String url : validUrls) { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setLink(url); BannerValidator.validate(testBanner); } for(String url : invalidUrls) { try { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setLink(url); BannerValidator.validate(testBanner); fail("Didn't throw error on " + url); } catch(InvalidIdentityZoneConfigurationException e) { assertThat(e.getMessage(), Matchers.containsString("Invalid banner link: " + url + ". Must be a properly formatted URI beginning with http: } } }
@Test public void testValidateColor() throws InvalidIdentityZoneConfigurationException { String[] validColors = { "#123456", "#000", "#DEDBEF", "#b0dc8a" }; String[] invalidColors = { "#00", "#12345", "F123", "red", "cyan", }; for(String color : validColors) { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setTextColor(color); testBanner.setBackgroundColor(color); BannerValidator.validate(testBanner); } for(String color : invalidColors) { try { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setTextColor(color); BannerValidator.validate(testBanner); fail("Didn't throw error on " + color); } catch(InvalidIdentityZoneConfigurationException e) { assertThat(e.getMessage(), Matchers.containsString("Invalid banner text color: " + color + ". Must be a properly formatted hexadecimal color code.")); } } for(String color : invalidColors) { try { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setBackgroundColor(color); BannerValidator.validate(testBanner); fail("Didn't throw error on " + color); } catch(InvalidIdentityZoneConfigurationException e) { assertThat(e.getMessage(), Matchers.containsString("Invalid banner background color: " + color + ". Must be a properly formatted hexadecimal color code.")); } } }
@Test public void testBase64Logo() throws InvalidIdentityZoneConfigurationException { String[] validBase64 = { "BIPUQGEWGPIUB64", "" }; String[] invalidBase64 = { "%%%%%", "~45234" }; for(String base64 : validBase64) { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setLogo(base64); BannerValidator.validate(testBanner); } for(String base64 : invalidBase64) { try { BrandingInformation.Banner testBanner = new BrandingInformation.Banner(); testBanner.setLogo(base64); BannerValidator.validate(testBanner); fail("Didn't throw error on " + base64); } catch(InvalidIdentityZoneConfigurationException e) { assertThat(e.getMessage(), Matchers.containsString("Invalid banner logo. Must be in BASE64 format.")); } } }