Commit 20aa6cf2 by Madhan Neethiraj

ATLAS-1335: multi-value attribute handling in AtlasStructType to be consistent…

ATLAS-1335: multi-value attribute handling in AtlasStructType to be consistent with TypeSystem for backward compatibility
parent 5d8c80a7
......@@ -56,6 +56,7 @@ public enum AtlasErrorCode {
TYPE_HAS_REFERENCES(409, "ATLAS4092E", "Given type {0} has references"),
TYPE_MATCH_FAILED(409, "ATLAS4093E", "Given type {0} doesn't match {1}"),
INVALID_TYPE_DEFINITION(409, "ATLAS4094E", "Invalid type definition {0}"),
INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY(409, "ATLAS4095E", "Cardinality of attribute {0}.{1} requires a list or set type"),
INTERNAL_ERROR(500, "ATLAS5001E", "Internal server error {0}"),
INDEX_CREATION_FAILED(500, "ATLAS5002E", "Index creation failed for {0}"),
......
......@@ -280,7 +280,7 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
public AtlasAttributeDef() { this(null, null); }
public AtlasAttributeDef(String name, String typeName) {
this(name, typeName, false, Cardinality.SINGLE, 1, 1, false, false, null);
this(name, typeName, false, Cardinality.SINGLE, COUNT_NOT_SET, COUNT_NOT_SET, false, false, null);
}
public AtlasAttributeDef(String name, String typeName, boolean isOptional, Cardinality cardinality,
......
......@@ -39,8 +39,8 @@ public class AtlasArrayType extends AtlasType {
private static final Logger LOG = LoggerFactory.getLogger(AtlasArrayType.class);
private final String elementTypeName;
private final int minCount;
private final int maxCount;
private int minCount;
private int maxCount;
private AtlasType elementType;
......@@ -89,10 +89,14 @@ public class AtlasArrayType extends AtlasType {
return elementTypeName;
}
public void setMinCount(int minCount) { this.minCount = minCount; }
public int getMinCount() {
return minCount;
}
public void setMaxCount(int maxCount) { this.maxCount = maxCount; }
public int getMaxCount() {
return maxCount;
}
......
......@@ -111,9 +111,15 @@ public class AtlasStructType extends AtlasType {
Cardinality cardinality = attributeDef.getCardinality();
if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
attrType = new AtlasArrayType(attrType,
attributeDef.getValuesMinCount(),
attributeDef.getValuesMaxCount());
if (!(attrType instanceof AtlasArrayType)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY,
getTypeName(), attributeDef.getName());
}
AtlasArrayType arrayType = (AtlasArrayType)attrType;
arrayType.setMinCount(attributeDef.getValuesMinCount());
arrayType.setMaxCount(attributeDef.getValuesMaxCount());
}
a.put(attributeDef.getName(), attrType);
......
......@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.instance.AtlasStruct;
......@@ -29,6 +30,7 @@ import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_INT;
import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_DATE;
import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.ATLAS_TYPE_BIGINTEGER;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality;
......@@ -49,18 +51,24 @@ public class TestAtlasStructType {
private final List<Object> invalidValues;
{
AtlasAttributeDef multiValuedAttribMinMax = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MIN_MAX, ATLAS_TYPE_INT);
AtlasAttributeDef multiValuedAttribMin = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MIN, ATLAS_TYPE_INT);
AtlasAttributeDef multiValuedAttribMax = new AtlasAttributeDef(MULTI_VAL_ATTR_NAME_MAX, ATLAS_TYPE_INT);
AtlasAttributeDef multiValuedAttribMinMax = new AtlasAttributeDef();
AtlasAttributeDef multiValuedAttribMin = new AtlasAttributeDef();
AtlasAttributeDef multiValuedAttribMax = new AtlasAttributeDef();
multiValuedAttribMinMax.setName(MULTI_VAL_ATTR_NAME_MIN_MAX);
multiValuedAttribMinMax.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
multiValuedAttribMinMax.setCardinality(Cardinality.LIST);
multiValuedAttribMinMax.setValuesMinCount(MULTI_VAL_ATTR_MIN_COUNT);
multiValuedAttribMinMax.setValuesMaxCount(MULTI_VAL_ATTR_MAX_COUNT);
multiValuedAttribMinMax.setCardinality(Cardinality.SET);
multiValuedAttribMin.setName(MULTI_VAL_ATTR_NAME_MIN);
multiValuedAttribMin.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
multiValuedAttribMin.setCardinality(Cardinality.SET);
multiValuedAttribMin.setValuesMinCount(MULTI_VAL_ATTR_MIN_COUNT);
multiValuedAttribMinMax.setCardinality(Cardinality.LIST);
multiValuedAttribMax.setName(MULTI_VAL_ATTR_NAME_MAX);
multiValuedAttribMax.setTypeName(AtlasBaseTypeDef.getArrayTypeName(ATLAS_TYPE_INT));
multiValuedAttribMax.setCardinality(Cardinality.LIST);
multiValuedAttribMax.setValuesMaxCount(MULTI_VAL_ATTR_MAX_COUNT);
AtlasStructDef structDef = ModelTestUtil.newStructDef();
......@@ -173,6 +181,23 @@ public class TestAtlasStructType {
}
}
@Test
public void testInvalidStructDef_MultiValuedAttributeNotArray() {
AtlasAttributeDef invalidMultiValuedAttrib = new AtlasAttributeDef("invalidAttributeDef", ATLAS_TYPE_INT);
invalidMultiValuedAttrib.setCardinality(Cardinality.LIST);
AtlasStructDef invalidStructDef = ModelTestUtil.newStructDef();
invalidStructDef.addAttribute(invalidMultiValuedAttrib);
try {
AtlasStructType invalidStructType = new AtlasStructType(invalidStructDef, ModelTestUtil.getTypesRegistry());
fail("invalidStructDef not detected: structDef=" + invalidStructDef + "; structType=" + invalidStructType);
} catch (AtlasBaseException excp) {
assertTrue(excp.getAtlasErrorCode() == AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY);
}
}
private static AtlasStructType getStructType(AtlasStructDef structDef) {
try {
return new AtlasStructType(structDef, ModelTestUtil.getTypesRegistry());
......
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