ContentOverrideValidator { @SuppressWarnings("checkstyle:JavadocMethod") public void validate(Collection<? extends ContentOverrideDTO> overrides) { if (overrides != null) { Set<String> invalidLabels = new HashSet<>(); Set<String> invalidProps = new HashSet<>(); Set<String> invalidValues = new HashSet<>(); for (ContentOverrideDTO override : overrides) { if (override != null) { String label = override.getContentLabel(); String name = override.getName(); String value = override.getValue(); if (label == null || label.length() == 0 || label.length() > MAX_VALUE_LENGTH) { invalidLabels.add(label != null ? label : "null"); } if (name == null || this.blacklist.contains(name.toLowerCase()) || name.length() > MAX_VALUE_LENGTH) { invalidProps.add(name != null ? name : "null"); } if (value == null || value.length() == 0 || value.length() > MAX_VALUE_LENGTH) { invalidValues.add(value != null ? value : "null"); } } } if (!invalidLabels.isEmpty() || !invalidProps.isEmpty() || !invalidValues.isEmpty()) { StringBuilder builder = new StringBuilder(); if (!invalidLabels.isEmpty()) { builder.append(i18n.tr("The following content labels are invalid: {0}", String.join(", ", invalidLabels))); } if (!invalidProps.isEmpty()) { if (builder.length() > 0) { builder.append('\n'); } builder.append(i18n.tr("The following content properties cannot be overridden: {0}", String.join(", ", invalidProps))); } if (!invalidValues.isEmpty()) { if (builder.length() > 0) { builder.append('\n'); } builder.append(i18n.tr("The following override values are invalid: {0}", String.join(", ", invalidValues))); } throw new BadRequestException(builder.toString()); } } } @Inject  ContentOverrideValidator(Configuration config, I18n i18n); @SuppressWarnings("checkstyle:JavadocMethod") /** * Validates the given ContentOverrideDTO instances, checking that the overridden properties * aren't protected and both the property name and value are short enough to fit in the * database. If any of the overrides are invalid, this method throws an exception. * * @param overrides * A collection of ContentOverrideDTO instances to validate * * @throws BadRequestException * if the collection of overrides contains an invalid override */ void validate(Collection<? extends ContentOverrideDTO> overrides); static final int MAX_VALUE_LENGTH; static final Set<String> DEFAULT_BLACKLIST; static final Set<String> HOSTED_BLACKLIST; }
@Test public void testValidateWithValidCollection() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); }
@Test public void testValidateWithValidEmptyCollection() { this.validator.validate(new LinkedList<>()); }
@Test public void testValidateWithValidNullCollection() { this.validator.validate(null); }
@Test public void testValidateWithNullContentLabel() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel(null) .setName("test_name-x") .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithEmptyContentLabel() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("") .setName("test_name-x") .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithLongContentLabel() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); StringBuilder builder = new StringBuilder(); while (builder.length() < ContentOverrideValidator.MAX_VALUE_LENGTH) { builder.append("longstring"); } ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel(builder.toString()) .setName("test_name-x") .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithNullPropertyName() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName(null) .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithEmptyPropertyName() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName("") .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithLongPropertyName() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); StringBuilder builder = new StringBuilder(); while (builder.length() < ContentOverrideValidator.MAX_VALUE_LENGTH) { builder.append("longstring"); } ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName(builder.toString()) .setValue("test_value-x"); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithNullOverrideValue() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName("test_name-x") .setValue(null); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithEmptyOverrideValue() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName("test_name-x") .setValue(""); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }
@Test public void testValidateWithLongOverrideValue() { List<ContentOverrideDTO> overrides = this.buildOverridesList(3); this.validator.validate(overrides); StringBuilder builder = new StringBuilder(); while (builder.length() < ContentOverrideValidator.MAX_VALUE_LENGTH) { builder.append("longstring"); } ContentOverrideDTO invalid = new ContentOverrideDTO() .setContentLabel("test_label-x") .setName("test_name-x") .setValue(builder.toString()); overrides.add(invalid); assertThrows(BadRequestException.class, () -> this.validator.validate(overrides)); }