atlas-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mad...@apache.org
Subject incubator-atlas git commit: ATLAS-1434: Updated typename validation to allow "." for classifications
Date Wed, 11 Jan 2017 08:22:40 GMT
Repository: incubator-atlas
Updated Branches:
  refs/heads/master d8db43100 -> e92502ddf


ATLAS-1434: Updated typename validation to allow "." for classifications

Signed-off-by: Madhan Neethiraj <madhan@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-atlas/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-atlas/commit/e92502dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-atlas/tree/e92502dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-atlas/diff/e92502dd

Branch: refs/heads/master
Commit: e92502ddffe6632af515468b60c6b52add8d50e1
Parents: d8db431
Author: ashutoshm <amestry@hortonworks.com>
Authored: Mon Jan 9 11:40:08 2017 -0800
Committer: Madhan Neethiraj <madhan@apache.org>
Committed: Wed Jan 11 00:05:03 2017 -0800

----------------------------------------------------------------------
 .../org/apache/atlas/type/AtlasTypeUtil.java    | 20 ++++++++++++-
 .../atlas/services/DefaultMetadataService.java  | 30 +++++++++++++-------
 .../service/DefaultMetadataServiceTest.java     |  5 ++--
 3 files changed, 42 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/e92502dd/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java b/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
index 19553bf..9f3b90e 100644
--- a/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
+++ b/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
@@ -56,9 +56,13 @@ import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_MAP_SUF
  */
 public class AtlasTypeUtil {
     private static final Set<String> ATLAS_BUILTIN_TYPENAMES = new HashSet<>();
-    private static final String  NAME_REGEX   = "[a-zA-z][a-zA-Z0-9_ ]*";
+    private static final String  NAME_REGEX   = "[a-zA-Z][a-zA-Z0-9_ ]*";
+    private static final String  TRAIT_NAME_REGEX   = "[a-zA-Z][a-zA-Z0-9_ .]*";
     private static final Pattern NAME_PATTERN = Pattern.compile(NAME_REGEX);
+    private static final Pattern TRAIT_NAME_PATTERN = Pattern.compile(TRAIT_NAME_REGEX);
 
+    private static final String InvalidTypeNameErrorMessage = "Only characters, numbers and
'_' are allowed in names.";
+    private static final String InvalidTraitTypeNameErrorMessage = "Only characters, numbers,
'.' and '_' are allowed in names.";
 
     static {
         Collections.addAll(ATLAS_BUILTIN_TYPENAMES, AtlasBaseTypeDef.ATLAS_BUILTIN_TYPES);
@@ -93,6 +97,20 @@ public class AtlasTypeUtil {
         return m.matches();
     }
 
+    public static String getInvalidTypeNameErrorMessage() {
+        return InvalidTypeNameErrorMessage;
+    }
+
+    public static boolean isValidTraitTypeName(String typeName) {
+        Matcher m = TRAIT_NAME_PATTERN.matcher(typeName);
+
+        return m.matches();
+    }
+
+    public static String getInvalidTraitTypeNameErrorMessage() {
+        return InvalidTraitTypeNameErrorMessage;
+    }
+
     public static void validateType(AtlasBaseTypeDef typeDef) throws AtlasBaseException {
         String typeName = typeDef.getName();
 

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/e92502dd/repository/src/main/java/org/apache/atlas/services/DefaultMetadataService.java
----------------------------------------------------------------------
diff --git a/repository/src/main/java/org/apache/atlas/services/DefaultMetadataService.java
b/repository/src/main/java/org/apache/atlas/services/DefaultMetadataService.java
index edd12ab..8307835 100755
--- a/repository/src/main/java/org/apache/atlas/services/DefaultMetadataService.java
+++ b/repository/src/main/java/org/apache/atlas/services/DefaultMetadataService.java
@@ -71,6 +71,7 @@ import org.codehaus.jettison.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashSet;
@@ -88,6 +89,9 @@ import javax.inject.Singleton;
  */
 @Singleton
 public class DefaultMetadataService implements MetadataService, ActiveStateChangeHandler,
TypeDefChangeListener {
+    private enum OperationType {
+        CREATE, UPDATE, DELETE
+    };
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultMetadataService.class);
     private final short maxAuditResults;
@@ -177,12 +181,12 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
      */
     @Override
     public JSONObject createType(String typeDefinition) throws AtlasException {
-        return createOrUpdateTypes(typeDefinition, false);
+        return createOrUpdateTypes(OperationType.CREATE, typeDefinition, false);
     }
 
-    private JSONObject createOrUpdateTypes(String typeDefinition, boolean isUpdate) throws
AtlasException {
+    private JSONObject createOrUpdateTypes(OperationType opType, String typeDefinition, boolean
isUpdate) throws AtlasException {
         typeDefinition = ParamChecker.notEmpty(typeDefinition, "type definition");
-        TypesDef typesDef = validateTypeDefinition(typeDefinition);
+        TypesDef typesDef = validateTypeDefinition(opType, typeDefinition);
 
 
         try {
@@ -214,10 +218,12 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
 
     @Override
     public JSONObject updateType(String typeDefinition) throws AtlasException {
-        return createOrUpdateTypes(typeDefinition, true);
+        return createOrUpdateTypes(OperationType.UPDATE, typeDefinition, true);
     }
 
-    private TypesDef validateTypeDefinition(String typeDefinition) throws AtlasException
{
+    private TypesDef validateTypeDefinition(OperationType opType, String typeDefinition)
throws AtlasException {
+        final String exceptionErrorMessageFormat = "%s for '%s' failed: %s";
+
         try {
             TypesDef typesDef = TypesSerialization.fromJson(typeDefinition);
             if (typesDef.isEmpty()) {
@@ -226,22 +232,26 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
 
             for (HierarchicalTypeDefinition<ClassType> t : typesDef.classTypesAsJavaList())
{
                 if (!AtlasTypeUtil.isValidTypeName(t.typeName))
-                    throw new AtlasException("Only characters, numbers and '_' are allowed
in class names. " + t.toString());
+                    throw new AtlasException(
+                            String.format(exceptionErrorMessageFormat, opType.toString(),
t.typeName, AtlasTypeUtil.getInvalidTypeNameErrorMessage()));
             }
 
             for (StructTypeDefinition t : typesDef.structTypesAsJavaList()) {
                 if (!AtlasTypeUtil.isValidTypeName(t.typeName))
-                    throw new AtlasException("Only characters, numbers and '_' are allowed
in struct names. " + t.toString());
+                    throw new AtlasException(
+                            String.format(exceptionErrorMessageFormat, opType.toString(),
t.typeName, AtlasTypeUtil.getInvalidTypeNameErrorMessage()));
             }
 
             for (EnumTypeDefinition t : typesDef.enumTypesAsJavaList()) {
                 if (!AtlasTypeUtil.isValidTypeName(t.name))
-                    throw new AtlasException("Only characters, numbers and '_' are allowed
in enum names. " + t.toString());
+                    throw new AtlasException(
+                            String.format(exceptionErrorMessageFormat, opType.toString(),
t.name, AtlasTypeUtil.getInvalidTypeNameErrorMessage()));
             }
 
             for (HierarchicalTypeDefinition<TraitType> t : typesDef.traitTypesAsJavaList())
{
-                if (!AtlasTypeUtil.isValidTypeName(t.typeName))
-                    throw new AtlasException("Only characters, numbers and '_' are allowed
in trait names. " + t.toString());
+                if (!AtlasTypeUtil.isValidTraitTypeName(t.typeName))
+                    throw new AtlasException(
+                            String.format(exceptionErrorMessageFormat, opType.toString(),
t.typeName, AtlasTypeUtil.getInvalidTraitTypeNameErrorMessage()));
             }
 
             return typesDef;

http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/e92502dd/repository/src/test/java/org/apache/atlas/service/DefaultMetadataServiceTest.java
----------------------------------------------------------------------
diff --git a/repository/src/test/java/org/apache/atlas/service/DefaultMetadataServiceTest.java
b/repository/src/test/java/org/apache/atlas/service/DefaultMetadataServiceTest.java
index 4b500ff..aa1cdaa 100644
--- a/repository/src/test/java/org/apache/atlas/service/DefaultMetadataServiceTest.java
+++ b/repository/src/test/java/org/apache/atlas/service/DefaultMetadataServiceTest.java
@@ -39,6 +39,7 @@ import org.apache.atlas.repository.audit.HBaseTestUtils;
 import org.apache.atlas.repository.graph.AtlasGraphProvider;
 import org.apache.atlas.services.DefaultMetadataService;
 import org.apache.atlas.services.MetadataService;
+import org.apache.atlas.type.AtlasTypeUtil;
 import org.apache.atlas.typesystem.IReferenceableInstance;
 import org.apache.atlas.typesystem.IStruct;
 import org.apache.atlas.typesystem.ITypedReferenceableInstance;
@@ -1146,7 +1147,7 @@ public class DefaultMetadataServiceTest {
 
     @Test
     public void testTypeWithDotsCreationShouldNotBeCreated() throws AtlasException, JSONException
{
-        String typeName = "test_.v1_type_"+ RandomStringUtils.randomAlphanumeric(10);
+        String typeName = "test_.v1_type_XXXX";
         HierarchicalTypeDefinition<ClassType> typeDef = TypesUtil.createClassTypeDef(
                 typeName, ImmutableSet.<String>of(),
                 TypesUtil.createUniqueRequiredAttrDef("test_type_attribute", DataTypes.STRING_TYPE));
@@ -1156,7 +1157,7 @@ public class DefaultMetadataServiceTest {
             metadataService.createType(TypesSerialization.toJson(typesDef));
             fail("Expected IllegalArgumentException");
         } catch (IllegalArgumentException e) {
-            //expected
+            assertTrue (e.getCause().getMessage().contains(AtlasTypeUtil.getInvalidTypeNameErrorMessage()),
e.getCause().getMessage());
         }
     }
 


Mime
View raw message