@Test public void testValidateWhenValueContainsMicrosoftShouldThrowBadRequest() { assertThrows(BadRequestException.class, () -> underTest.validate(accountTags("apple", "microsoftapple"))); }
public void validate(List<AccountTag> accountTags) { for (AccountTagResponse defaultTag : getDefaults().getResponses()) { Optional<AccountTag> requestContainsUnmodifiableTag = accountTags .stream() .filter(e -> e.getTagKey().equals(defaultTag.getKey())) .findFirst(); if (requestContainsUnmodifiableTag.isPresent()) { throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey())); } } for (AccountTag accountTag : accountTags) { Pattern pattern = Pattern.compile(ACCOUNT_TAG_PATTERN); Matcher keyMatcher = pattern.matcher(accountTag.getTagKey()); Matcher valueMatcher = pattern.matcher(accountTag.getTagValue()); if (!keyMatcher.matches()) { throw new BadRequestException( String.format("The key '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagKey())); } if (!valueMatcher.matches()) { throw new BadRequestException( String.format("The value '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagValue())); } if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) { throw new BadRequestException( String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey())); } if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) { throw new BadRequestException( String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue())); } } }
DefaultInternalAccountTagService { public void validate(List<AccountTag> accountTags) { for (AccountTagResponse defaultTag : getDefaults().getResponses()) { Optional<AccountTag> requestContainsUnmodifiableTag = accountTags .stream() .filter(e -> e.getTagKey().equals(defaultTag.getKey())) .findFirst(); if (requestContainsUnmodifiableTag.isPresent()) { throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey())); } } for (AccountTag accountTag : accountTags) { Pattern pattern = Pattern.compile(ACCOUNT_TAG_PATTERN); Matcher keyMatcher = pattern.matcher(accountTag.getTagKey()); Matcher valueMatcher = pattern.matcher(accountTag.getTagValue()); if (!keyMatcher.matches()) { throw new BadRequestException( String.format("The key '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagKey())); } if (!valueMatcher.matches()) { throw new BadRequestException( String.format("The value '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagValue())); } if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) { throw new BadRequestException( String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey())); } if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) { throw new BadRequestException( String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue())); } } } }
DefaultInternalAccountTagService { public void validate(List<AccountTag> accountTags) { for (AccountTagResponse defaultTag : getDefaults().getResponses()) { Optional<AccountTag> requestContainsUnmodifiableTag = accountTags .stream() .filter(e -> e.getTagKey().equals(defaultTag.getKey())) .findFirst(); if (requestContainsUnmodifiableTag.isPresent()) { throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey())); } } for (AccountTag accountTag : accountTags) { Pattern pattern = Pattern.compile(ACCOUNT_TAG_PATTERN); Matcher keyMatcher = pattern.matcher(accountTag.getTagKey()); Matcher valueMatcher = pattern.matcher(accountTag.getTagValue()); if (!keyMatcher.matches()) { throw new BadRequestException( String.format("The key '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagKey())); } if (!valueMatcher.matches()) { throw new BadRequestException( String.format("The value '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagValue())); } if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) { throw new BadRequestException( String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey())); } if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) { throw new BadRequestException( String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue())); } } } DefaultInternalAccountTagService(CloudbreakResourceReaderService cloudbreakResourceReaderService); }
DefaultInternalAccountTagService { public void validate(List<AccountTag> accountTags) { for (AccountTagResponse defaultTag : getDefaults().getResponses()) { Optional<AccountTag> requestContainsUnmodifiableTag = accountTags .stream() .filter(e -> e.getTagKey().equals(defaultTag.getKey())) .findFirst(); if (requestContainsUnmodifiableTag.isPresent()) { throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey())); } } for (AccountTag accountTag : accountTags) { Pattern pattern = Pattern.compile(ACCOUNT_TAG_PATTERN); Matcher keyMatcher = pattern.matcher(accountTag.getTagKey()); Matcher valueMatcher = pattern.matcher(accountTag.getTagValue()); if (!keyMatcher.matches()) { throw new BadRequestException( String.format("The key '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagKey())); } if (!valueMatcher.matches()) { throw new BadRequestException( String.format("The value '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagValue())); } if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) { throw new BadRequestException( String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey())); } if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) { throw new BadRequestException( String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue())); } } } DefaultInternalAccountTagService(CloudbreakResourceReaderService cloudbreakResourceReaderService); @PostConstruct void init(); void merge(List<AccountTagResponse> accountTagResponses); void validate(List<AccountTag> accountTags); }
DefaultInternalAccountTagService { public void validate(List<AccountTag> accountTags) { for (AccountTagResponse defaultTag : getDefaults().getResponses()) { Optional<AccountTag> requestContainsUnmodifiableTag = accountTags .stream() .filter(e -> e.getTagKey().equals(defaultTag.getKey())) .findFirst(); if (requestContainsUnmodifiableTag.isPresent()) { throw new BadRequestException(String.format("Tag with %s key exist as an unmodifiable tag.", defaultTag.getKey())); } } for (AccountTag accountTag : accountTags) { Pattern pattern = Pattern.compile(ACCOUNT_TAG_PATTERN); Matcher keyMatcher = pattern.matcher(accountTag.getTagKey()); Matcher valueMatcher = pattern.matcher(accountTag.getTagValue()); if (!keyMatcher.matches()) { throw new BadRequestException( String.format("The key '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagKey())); } if (!valueMatcher.matches()) { throw new BadRequestException( String.format("The value '%s' can only can not start with microsoft or azure or aws or windows or space", accountTag.getTagValue())); } if (isAccountTagContainsTemplate(accountTag.getTagKey()) && isAccountTagInvalid(accountTag.getTagKey())) { throw new BadRequestException( String.format("The key '%s' of the tag contains invalid templates", accountTag.getTagKey())); } if (isAccountTagContainsTemplate(accountTag.getTagValue()) && isAccountTagInvalid(accountTag.getTagValue())) { throw new BadRequestException( String.format("The value '%s' of the tag contains invalid templates", accountTag.getTagValue())); } } } DefaultInternalAccountTagService(CloudbreakResourceReaderService cloudbreakResourceReaderService); @PostConstruct void init(); void merge(List<AccountTagResponse> accountTagResponses); void validate(List<AccountTag> accountTags);  }