Commit 6a4fcb95 by ashutoshm Committed by Madhan Neethiraj

ATLAS-1646: added validation of classification attributes

parent dc89e7f4
...@@ -18,18 +18,18 @@ ...@@ -18,18 +18,18 @@
package org.apache.atlas.type; package org.apache.atlas.type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef; import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
/** /**
* Built-in types in Atlas. * Built-in types in Atlas.
...@@ -449,6 +449,10 @@ public class AtlasBuiltInTypes { ...@@ -449,6 +449,10 @@ public class AtlasBuiltInTypes {
return true; return true;
} }
if (obj instanceof String && StringUtils.isEmpty((String) obj)) {
return true;
}
return getNormalizedValue(obj) != null; return getNormalizedValue(obj) != null;
} }
......
...@@ -34,7 +34,7 @@ public class TestAtlasDateType { ...@@ -34,7 +34,7 @@ public class TestAtlasDateType {
private final AtlasDateType dateType = new AtlasDateType(); private final AtlasDateType dateType = new AtlasDateType();
private final Object[] validValues = { private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1), null, "", Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1", Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
}; };
...@@ -43,7 +43,7 @@ public class TestAtlasDateType { ...@@ -43,7 +43,7 @@ public class TestAtlasDateType {
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1", Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
}; };
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", }; private final Object[] invalidValues = { "12ab", "abcd", "-12ab", };
private final Date now = new Date(); private final Date now = new Date();
private final String strNow = AtlasBaseTypeDef.DATE_FORMATTER.format(now); private final String strNow = AtlasBaseTypeDef.DATE_FORMATTER.format(now);
...@@ -78,7 +78,7 @@ public class TestAtlasDateType { ...@@ -78,7 +78,7 @@ public class TestAtlasDateType {
assertNull(dateType.getNormalizedValue(null), "value=" + null); assertNull(dateType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) { for (Object value : validValues) {
if (value == null) { if (value == null || value == "") {
continue; continue;
} }
......
...@@ -36,6 +36,7 @@ import org.apache.atlas.repository.graphdb.AtlasVertex; ...@@ -36,6 +36,7 @@ import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.AtlasEntityStore; import org.apache.atlas.repository.store.graph.AtlasEntityStore;
import org.apache.atlas.repository.store.graph.EntityGraphDiscovery; import org.apache.atlas.repository.store.graph.EntityGraphDiscovery;
import org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext; import org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext;
import org.apache.atlas.type.AtlasClassificationType;
import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasStructType.AtlasAttribute; import org.apache.atlas.type.AtlasStructType.AtlasAttribute;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
...@@ -427,6 +428,10 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -427,6 +428,10 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
LOG.debug("Adding classifications={} to entity={}", classifications, guid); LOG.debug("Adding classifications={} to entity={}", classifications, guid);
} }
for (AtlasClassification classification : classifications) {
validateAndNormalize(classification);
}
EntityGraphMapper graphMapper = new EntityGraphMapper(deleteHandler, typeRegistry); EntityGraphMapper graphMapper = new EntityGraphMapper(deleteHandler, typeRegistry);
graphMapper.addClassifications(new EntityMutationContext(), guid, classifications); graphMapper.addClassifications(new EntityMutationContext(), guid, classifications);
...@@ -450,6 +455,8 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -450,6 +455,8 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
EntityGraphMapper graphMapper = new EntityGraphMapper(deleteHandler, typeRegistry); EntityGraphMapper graphMapper = new EntityGraphMapper(deleteHandler, typeRegistry);
validateAndNormalize(classification);
List<AtlasClassification> classifications = Collections.singletonList(classification); List<AtlasClassification> classifications = Collections.singletonList(classification);
for (String guid : guids) { for (String guid : guids) {
...@@ -573,4 +580,22 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -573,4 +580,22 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
return response; return response;
} }
private void validateAndNormalize(AtlasClassification classification) throws AtlasBaseException {
AtlasClassificationType type = typeRegistry.getClassificationTypeByName(classification.getTypeName());
if (type == null) {
throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_FOUND, classification.getTypeName());
}
List<String> messages = new ArrayList<>();
type.validateValue(classification, classification.getTypeName(), messages);
if (!messages.isEmpty()) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, messages);
}
type.getNormalizedValue(classification);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment