/** * Validate that the key length does not exceed the configured limit. */ private void validateLength(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { // check for max char per value if (keyword.length() > maxCharacters) { throw new InvalidMetadataException( metadataEntity, "Metadata " + keyword + " exceeds maximum of " + maxCharacters + " characters."); } }
/** * Validate that the key is not reserved {@link MetadataConstants#TAGS_KEY}. */ private static void validateTagReservedKey(MetadataEntity metadataEntity, String key) throws InvalidMetadataException { if (MetadataConstants.TAGS_KEY.equals(key.toLowerCase())) { throw new InvalidMetadataException( metadataEntity, "Could not set metadata with reserved key " + MetadataConstants.TAGS_KEY); } }
/** * Validate that the key is not reserved {@link MetadataDataset#TAGS_KEY}. */ private void validateTagReservedKey(MetadataEntity metadataEntity, String key) throws InvalidMetadataException { if (MetadataDataset.TAGS_KEY.equals(key.toLowerCase())) { throw new InvalidMetadataException(metadataEntity, "Could not set metadata with reserved key " + MetadataDataset.TAGS_KEY); } }
/** * Validate the key matches the {@link #KEY_AND_TAG_MATCHER} character test. */ private static void validateKeyAndTagsFormat(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { if (!KEY_AND_TAG_MATCHER.matchesAllOf(keyword)) { throw new InvalidMetadataException(metadataEntity, String.format( "Illegal format for '%s'. Should only contain alphanumeric characters (a-z, A-Z, 0-9), " + "underscores and hyphens.", keyword)); } }
/** * Validate the value of a property matches the {@link #VALUE_MATCHER} character test. */ private static void validateValueFormat(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { if (!VALUE_MATCHER.matchesAllOf(keyword)) { throw new InvalidMetadataException(metadataEntity, String.format( "Illegal format for the value '%s'. Should only contain alphanumeric characters (a-z, A-Z, 0-9), " + "underscores, hyphens and whitespaces.", keyword)); } }
/** * Validate the value of a property matches the {@link #VALUE_MATCHER} character test. */ private void validateValueFormat(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { if (!VALUE_MATCHER.matchesAllOf(keyword)) { throw new InvalidMetadataException(metadataEntity, String.format("Illegal format for the value '%s'. Should " + "only contain alphanumeric characters (a-z, " + "A-Z, 0-9), underscores, hyphens and " + "whitespaces.", keyword)); } }
/** * Validate the key matches the {@link #KEY_AND_TAG_MATCHER} character test. */ private void validateKeyAndTagsFormat(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { if (!KEY_AND_TAG_MATCHER.matchesAllOf(keyword)) { throw new InvalidMetadataException(metadataEntity, String.format("Illegal format for '%s'. " + "Should only contain alphanumeric characters" + " (a-z, A-Z, 0-9), underscores and hyphens.", keyword)); } }
/** * Validate that the key length does not exceed the configured limit. */ private void validateLength(MetadataEntity metadataEntity, String keyword) throws InvalidMetadataException { // check for max char per value if (keyword.length() > cConf.getInt(Constants.Metadata.MAX_CHARS_ALLOWED)) { throw new InvalidMetadataException(metadataEntity, "Metadata " + keyword + " should not exceed maximum of " + cConf.get(Constants.Metadata.MAX_CHARS_ALLOWED) + " characters."); } } }
@Test public void testMesssages() { // test dataset InvalidMetadataException invalidMetadataException = new InvalidMetadataException(NamespaceId.DEFAULT.dataset("ds").toMetadataEntity(), "error"); String expectedMessage = "Unable to set metadata for dataset: ds " + "which exists in namespace: default. error"; Assert.assertEquals(expectedMessage, invalidMetadataException.getMessage()); // test program invalidMetadataException = new InvalidMetadataException(NamespaceId.DEFAULT.app("app").program(ProgramType.WORKER, "wk") .toMetadataEntity(), "error"); expectedMessage = "Unable to set metadata for worker: wk in application: app of version: -SNAPSHOT deployed in " + "namespace: default. error"; Assert.assertEquals(expectedMessage, invalidMetadataException.getMessage()); // test custom entity MetadataEntity customEntity = MetadataEntity.builder(NamespaceId.DEFAULT.dataset("ds").toMetadataEntity()) .appendAsType("field", "empName").build(); invalidMetadataException = new InvalidMetadataException(customEntity, "error"); expectedMessage = "Unable to set metadata for namespace=default,dataset=ds,field=empName of type 'field'. error"; Assert.assertEquals(expectedMessage, invalidMetadataException.getMessage()); } }