Commit 00fb162d by Mandar Ambawane Committed by nixonrodrigues

ATLAS-3632: Max length validation for business-metadata string attributes and Test cases.

which cover 1.business-metadata typedef creation 2.entity-BM association 3.Max length check Signed-off-by: 's avatarnixonrodrigues <nixon@apache.org>
parent 76011077
......@@ -26,6 +26,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Array;
import java.util.*;
import static org.apache.atlas.model.typedef.AtlasBusinessMetadataDef.*;
......@@ -100,7 +101,7 @@ public class AtlasBusinessMetadataType extends AtlasStructType {
}
AtlasBusinessAttribute bmAttribute;
if (attribute.getAttributeType() instanceof AtlasBuiltInTypes.AtlasStringType) {
if (attrType instanceof AtlasBuiltInTypes.AtlasStringType) {
Integer maxStringLength = attribute.getOptionInt(ATTR_MAX_STRING_LENGTH);
if (maxStringLength == null) {
throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, attributeDef.getName(), "options." + ATTR_MAX_STRING_LENGTH);
......@@ -171,5 +172,46 @@ public class AtlasBusinessMetadataType extends AtlasStructType {
public int getMaxStringLength() {
return maxStringLength;
}
public boolean isValidLength(Object value) {
boolean ret = true;
if (value != null) {
AtlasType attrType = getAttributeType();
if (attrType instanceof AtlasBuiltInTypes.AtlasStringType) {
ret = isValidStringValue(value);
} else if (attrType instanceof AtlasArrayType) {
attrType = ((AtlasArrayType) attrType).getElementType();
if (attrType instanceof AtlasBuiltInTypes.AtlasStringType) {
ret = isValidArrayValue(value);
}
}
}
return ret;
}
private boolean isValidStringValue(Object obj) {
return obj == null || String.valueOf(obj).length() <= this.maxStringLength;
}
private boolean isValidArrayValue(Object obj) {
if (obj instanceof List || obj instanceof Set) {
Collection objList = (Collection) obj;
for (Object element : objList) {
if (!isValidStringValue(element)) {
return false;
}
}
} else if (obj.getClass().isArray()) {
int arrayLen = Array.getLength(obj);
for (int i = 0; i < arrayLen; i++) {
if (!isValidStringValue(Array.get(obj, i))) {
return false;
}
}
}
return true;
}
}
}
......@@ -178,6 +178,13 @@ public class AtlasTypeUtil {
Collections.<AtlasConstraintDef>emptyList());
}
public static AtlasAttributeDef createOptionalAttrDef(String name, String dataType, Map<String, String> options, String desc) {
return new AtlasAttributeDef(name, dataType, true,
Cardinality.SINGLE, 0, 1,
false, false, false, "",
Collections.<AtlasConstraintDef>emptyList(), options, desc, 0, null);
}
public static AtlasAttributeDef createRequiredAttrDef(String name, String dataType) {
return new AtlasAttributeDef(name, dataType, false,
Cardinality.SINGLE, 1, 1,
......@@ -285,6 +292,13 @@ public class AtlasTypeUtil {
return new AtlasEntityDef(name, description, version, Arrays.asList(attrDefs), superTypes, options);
}
public static AtlasBusinessMetadataDef createBusinessMetadataDef(String name, String description, String typeVersion, AtlasAttributeDef... attributeDefs) {
if (attributeDefs == null || attributeDefs.length == 0) {
return new AtlasBusinessMetadataDef(name, description, typeVersion);
}
return new AtlasBusinessMetadataDef(name, description, typeVersion, Arrays.asList(attributeDefs));
}
public static AtlasRelationshipDef createRelationshipTypeDef(String name,
String description,
String version,
......
......@@ -24,6 +24,7 @@ import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.instance.AtlasStruct;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasBusinessMetadataDef;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasEnumDef;
......@@ -58,6 +59,7 @@ import static org.apache.atlas.type.AtlasTypeUtil.createRequiredAttrDef;
import static org.apache.atlas.type.AtlasTypeUtil.createStructTypeDef;
import static org.apache.atlas.type.AtlasTypeUtil.createUniqueRequiredAttrDef;
import static org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId;
import static org.apache.atlas.type.AtlasTypeUtil.createBusinessMetadataDef;
/**
......@@ -661,6 +663,70 @@ public final class TestUtilsV2 {
return new AtlasEntityWithExtInfo(entity);
}
public static AtlasTypesDef defineEnumTypes() {
String _description = "_description";
AtlasEnumDef myEnum =
new AtlasEnumDef("ENUM_1", "ENUM_1" + _description, "1.0",
Arrays.asList(
new AtlasEnumElementDef("USER", "Element" + _description, 1),
new AtlasEnumElementDef("ROLE", "Element" + _description, 2),
new AtlasEnumElementDef("GROUP", "Element" + _description, 3)
));
AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(Collections.singletonList(myEnum),
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
populateSystemAttributes(ret);
return ret;
}
public static AtlasTypesDef defineBusinessMetadataTypes() {
String _description = "_description";
Map<String, String> options = new HashMap<>();
options.put("maxStrLength", "20");
AtlasBusinessMetadataDef bmNoApplicableTypes = createBusinessMetadataDef("bmNoApplicableTypes", _description, "1.0",
createOptionalAttrDef("attr0", "string", options, _description));
AtlasBusinessMetadataDef bmNoAttributes = createBusinessMetadataDef("bmNoAttributes", _description, "1.0", null);
options.put("applicableEntityTypes", "[\"" + DATABASE_TYPE + "\",\"" + TABLE_TYPE + "\"]");
AtlasBusinessMetadataDef bmWithAllTypes = createBusinessMetadataDef("bmWithAllTypes", _description, "1.0",
createOptionalAttrDef("attr1", AtlasBusinessMetadataDef.ATLAS_TYPE_BOOLEAN, options, _description),
createOptionalAttrDef("attr2", AtlasBusinessMetadataDef.ATLAS_TYPE_BYTE, options, _description),
createOptionalAttrDef("attr3", AtlasBusinessMetadataDef.ATLAS_TYPE_SHORT, options, _description),
createOptionalAttrDef("attr4", AtlasBusinessMetadataDef.ATLAS_TYPE_INT, options, _description),
createOptionalAttrDef("attr5", AtlasBusinessMetadataDef.ATLAS_TYPE_LONG, options, _description),
createOptionalAttrDef("attr6", AtlasBusinessMetadataDef.ATLAS_TYPE_FLOAT, options, _description),
createOptionalAttrDef("attr7", AtlasBusinessMetadataDef.ATLAS_TYPE_DOUBLE, options, _description),
createOptionalAttrDef("attr8", AtlasBusinessMetadataDef.ATLAS_TYPE_STRING, options, _description),
createOptionalAttrDef("attr9", AtlasBusinessMetadataDef.ATLAS_TYPE_DATE, options, _description),
createOptionalAttrDef("attr10", "ENUM_1", options, _description));
AtlasBusinessMetadataDef bmWithAllTypesMV = createBusinessMetadataDef("bmWithAllTypesMV", _description, "1.0",
createOptionalAttrDef("attr11", "array<boolean>", options, _description),
createOptionalAttrDef("attr12", "array<byte>", options, _description),
createOptionalAttrDef("attr13", "array<short>", options, _description),
createOptionalAttrDef("attr14", "array<int>", options, _description),
createOptionalAttrDef("attr15", "array<long>", options, _description),
createOptionalAttrDef("attr16", "array<float>", options, _description),
createOptionalAttrDef("attr17", "array<double>", options, _description),
createOptionalAttrDef("attr18", "array<string>", options, _description),
createOptionalAttrDef("attr19", "array<date>", options, _description),
createOptionalAttrDef("attr20", "array<ENUM_1>", options, _description));
AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(new ArrayList<>(),
new ArrayList<>(), new ArrayList<>(),
new ArrayList<>(), new ArrayList<>(),
Arrays.asList(bmNoApplicableTypes, bmNoAttributes, bmWithAllTypes, bmWithAllTypesMV));
populateSystemAttributes(ret);
return ret;
}
public static AtlasTypesDef defineHiveTypes() {
String _description = "_description";
AtlasEntityDef superTypeDefinition =
......@@ -1451,6 +1517,7 @@ public final class TestUtilsV2 {
populateSystemAttributes(typesDef.getClassificationDefs());
populateSystemAttributes(typesDef.getEntityDefs());
populateSystemAttributes(typesDef.getRelationshipDefs());
populateSystemAttributes(typesDef.getBusinessMetadataDefs());
}
public static void populateSystemAttributes(List<? extends AtlasBaseTypeDef> typeDefs) {
......
......@@ -1506,6 +1506,11 @@ public class AtlasEntityStoreV2 implements AtlasEntityStore {
if (attrValue != null) {
attrType.validateValue(attrValue, fieldName, messages);
boolean isValidLength = bmAttribute.isValidLength(attrValue);
if (!isValidLength) {
messages.add(fieldName + ": Business attribute-value exceeds maximum length limit");
}
} else if (!bmAttribute.getAttributeDef().getIsOptional()) {
final boolean isAttrValuePresent;
......
......@@ -21,11 +21,15 @@ import com.google.inject.Inject;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.TestModules;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasBusinessMetadataDef;
import org.apache.atlas.model.typedef.AtlasEnumDef;
import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasBusinessMetadataType.AtlasBusinessAttribute;
import org.apache.atlas.type.AtlasBusinessMetadataType;
import org.apache.atlas.type.AtlasStructType;
import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.testng.Assert;
......@@ -37,6 +41,7 @@ import org.testng.annotations.Test;
import java.io.IOException;
import java.util.*;
import static org.apache.atlas.model.typedef.AtlasBusinessMetadataDef.ATTR_MAX_STRING_LENGTH;
import static org.apache.atlas.model.typedef.AtlasBusinessMetadataDef.ATTR_OPTION_APPLICABLE_ENTITY_TYPES;
import static org.apache.atlas.utils.TestLoadModelUtils.loadBaseModel;
......@@ -82,7 +87,7 @@ public class AtlasBusinessMetadataDefStoreV2Test {
businessMetadataName = TEST_BUSINESS_METADATA + randomCount;
}
@Test
@Test(priority = -1)
public void createBusinessMetadataDef() throws AtlasBaseException {
createBusinessMetadataTypes(businessMetadataName);
Assert.assertEquals(typeRegistry.getAllBusinessMetadataDefs().size(), 1);
......@@ -92,6 +97,150 @@ public class AtlasBusinessMetadataDefStoreV2Test {
}
@Test
public void createBusinessMetadataDefWithoutAttributes() throws AtlasBaseException {
createBusinessMetadataTypesWithoutAttributes(businessMetadataName);
AtlasBusinessMetadataType businessMetadataType = typeRegistry.getBusinessMetadataTypeByName(businessMetadataName);
Assert.assertTrue(businessMetadataType.getAllAttributes() == null ? true : businessMetadataType.getAllAttributes().isEmpty());
}
private void createBusinessMetadataTypesWithoutAttributes(String businessMetadataName) throws AtlasBaseException {
List<AtlasBusinessMetadataDef> businessMetadataDefs = new ArrayList(typesDefs.getBusinessMetadataDefs());
businessMetadataDefs.add(createBusinessMetadataDefWithoutAttributes(businessMetadataName));
typesDefs.setBusinessMetadataDefs(businessMetadataDefs);
typeDefStore.createTypesDef(typesDefs);
}
private AtlasBusinessMetadataDef createBusinessMetadataDefWithoutAttributes(String businessMetadataName) {
AtlasBusinessMetadataDef businessMetadataDef = new AtlasBusinessMetadataDef(businessMetadataName, "test_description", null);
return businessMetadataDef;
}
@Test
public void createBusinessMetadataDefIsOptionalIsUnique() throws AtlasBaseException {
createBusinessMetadataTypesIsOptionalIsUnique(businessMetadataName);
AtlasBusinessMetadataType businessMetadataType = typeRegistry.getBusinessMetadataTypeByName(businessMetadataName);
AtlasStructType.AtlasAttribute atlasAttribute = businessMetadataType.getAttribute("test_business_attribute1");
AtlasStructDef.AtlasAttributeDef atlasAttributeDef = atlasAttribute.getAttributeDef();
Assert.assertFalse(atlasAttributeDef.getIsOptional());
Assert.assertTrue(atlasAttributeDef.getIsUnique());
}
private void createBusinessMetadataTypesIsOptionalIsUnique(String businessMetadataName) throws AtlasBaseException {
List<AtlasBusinessMetadataDef> businessMetadataDefs = new ArrayList(typesDefs.getBusinessMetadataDefs());
businessMetadataDefs.add(createBusinessMetadataDefIsOptionalIsUnique(businessMetadataName));
typesDefs.setBusinessMetadataDefs(businessMetadataDefs);
typeDefStore.createTypesDef(typesDefs);
}
private AtlasBusinessMetadataDef createBusinessMetadataDefIsOptionalIsUnique(String businessMetadataName) {
AtlasBusinessMetadataDef businessMetadataDef = new AtlasBusinessMetadataDef(businessMetadataName, "test_description", null);
addBusinessAttribute(businessMetadataDef, "test_business_attribute1", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "int",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, false, true);
return businessMetadataDef;
}
private void addBusinessAttribute(AtlasBusinessMetadataDef businessMetadataDef, String name, Set<String> applicableEntityTypes,
String typeName, AtlasStructDef.AtlasAttributeDef.Cardinality cardinality, boolean isOptional, boolean isUnique) {
AtlasStructDef.AtlasAttributeDef attributeDef = new AtlasStructDef.AtlasAttributeDef(name, typeName);
attributeDef.setCardinality(cardinality);
attributeDef.setOption(ATTR_OPTION_APPLICABLE_ENTITY_TYPES, AtlasType.toJson(applicableEntityTypes));
attributeDef.setIsOptional(isOptional);
attributeDef.setIsUnique(isUnique);
businessMetadataDef.addAttribute(attributeDef);
}
@Test
public void createBusinessMetadataDefParentApplicableType() throws AtlasBaseException {
createBusinessMetadataTypesParentApplicableType(businessMetadataName);
AtlasEntityType entityType = typeRegistry.getEntityTypeByName("hive_table");
AtlasBusinessAttribute businessAttribute = entityType.getBusinessAttribute(businessMetadataName, "test_business_attribute_asset_type");
AtlasStructDef.AtlasAttributeDef attributeDef = businessAttribute.getAttributeDef();
String applicableType = attributeDef.getOption(ATTR_OPTION_APPLICABLE_ENTITY_TYPES);
Assert.assertEquals(applicableType, "[\"Asset\"]");
}
private void createBusinessMetadataTypesParentApplicableType(String businessMetadataName) throws AtlasBaseException {
List<AtlasBusinessMetadataDef> businessMetadataDefs = new ArrayList(typesDefs.getBusinessMetadataDefs());
businessMetadataDefs.add(createBusinessMetadataDefParentApplicableType(businessMetadataName));
typesDefs.setBusinessMetadataDefs(businessMetadataDefs);
typeDefStore.createTypesDef(typesDefs);
}
private AtlasBusinessMetadataDef createBusinessMetadataDefParentApplicableType(String businessMetadataName) {
AtlasBusinessMetadataDef businessMetadataDef = new AtlasBusinessMetadataDef(businessMetadataName, "test_description", null);
addBusinessAttribute(businessMetadataDef, "test_business_attribute_asset_type", new HashSet<>(Arrays.asList("Asset")), "int",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
return businessMetadataDef;
}
private void createEnumTypes() throws AtlasBaseException {
List<AtlasEnumDef> atlasEnumDefs = new ArrayList(typesDefs.getEnumDefs());
String _description = "_description";
AtlasEnumDef myEnum =
new AtlasEnumDef("ENUM_1", "ENUM_1" + _description, "1.0",
Arrays.asList(
new AtlasEnumDef.AtlasEnumElementDef("USER", "Element" + _description, 1),
new AtlasEnumDef.AtlasEnumElementDef("ROLE", "Element" + _description, 2),
new AtlasEnumDef.AtlasEnumElementDef("GROUP", "Element" + _description, 3)
));
atlasEnumDefs.add(myEnum);
typesDefs.setEnumDefs(atlasEnumDefs);
}
@Test
public void createBusinessMetadataDefMultivaluedAttributes() throws AtlasBaseException {
createEnumTypes();
createBusinessMetadataTypesMultivaluedAttributes(businessMetadataName);
AtlasBusinessMetadataType businessMetadataType = typeRegistry.getBusinessMetadataTypeByName(businessMetadataName);
Assert.assertEquals(businessMetadataType.getAllAttributes().size(), 10);
Map<String, AtlasStructType.AtlasAttribute> attributeMap = businessMetadataType.getAllAttributes();
for (Map.Entry<String, AtlasStructType.AtlasAttribute> e : attributeMap.entrySet()) {
AtlasStructType.AtlasAttribute atlasAttribute = e.getValue();
AtlasStructDef.AtlasAttributeDef atlasAttributeDef = atlasAttribute.getAttributeDef();
Assert.assertTrue(atlasAttributeDef.getTypeName().startsWith("array<"));
}
}
private void createBusinessMetadataTypesMultivaluedAttributes(String businessMetadataName) throws AtlasBaseException {
List<AtlasBusinessMetadataDef> businessMetadataDefs = new ArrayList(typesDefs.getBusinessMetadataDefs());
businessMetadataDefs.add(createBusinessMetadataDefMultivaluedAttributes(businessMetadataName));
typesDefs.setBusinessMetadataDefs(businessMetadataDefs);
typeDefStore.createTypesDef(typesDefs);
}
private AtlasBusinessMetadataDef createBusinessMetadataDefMultivaluedAttributes(String businessMetadataName) {
AtlasBusinessMetadataDef businessMetadataDef = new AtlasBusinessMetadataDef(businessMetadataName, "test_description", null);
addBusinessAttribute(businessMetadataDef, "test_business_attribute1", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<boolean>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute2", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<byte>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute3", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<short>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute4", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<int>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute5", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<long>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute6", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<float>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute7", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<double>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute8", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<string>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute9", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<date>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
addBusinessAttribute(businessMetadataDef, "test_business_attribute10", new HashSet<>(Arrays.asList("hive_table", "fs_path")), "array<ENUM_1>",
AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE);
return businessMetadataDef;
}
@Test
public void deleteBusinessMetadataDefs() throws AtlasBaseException {
createBusinessMetadataTypes(businessMetadataName);
for (AtlasBusinessMetadataDef atlasBusinessMetaDataDef : typesDefs.getBusinessMetadataDefs()) {
......@@ -278,6 +427,9 @@ public class AtlasBusinessMetadataDefStoreV2Test {
attributeDef.setCardinality(cardinality);
attributeDef.setOption(ATTR_OPTION_APPLICABLE_ENTITY_TYPES, AtlasType.toJson(applicableEntityTypes));
if (typeName.contains(AtlasBaseTypeDef.ATLAS_TYPE_STRING)) {
attributeDef.setOption(ATTR_MAX_STRING_LENGTH, "20");
}
attributeDef.setIsOptional(true);
attributeDef.setIsUnique(false);
......
......@@ -92,6 +92,8 @@ public class AtlasEntityStoreV2Test extends AtlasEntityTestBase {
AtlasTypesDef[] testTypesDefs = new AtlasTypesDef[] { TestUtilsV2.defineDeptEmployeeTypes(),
TestUtilsV2.defineHiveTypes(),
TestUtilsV2.defineTypeWithNestedCollectionAttributes(),
TestUtilsV2.defineEnumTypes(),
TestUtilsV2.defineBusinessMetadataTypes()
};
createTypesDef(testTypesDefs);
......@@ -145,7 +147,7 @@ public class AtlasEntityStoreV2Test extends AtlasEntityTestBase {
assertEquals(cost,30);
}
@Test
@Test(priority = -1)
public void testCreate() throws Exception {
init();
......@@ -1229,4 +1231,94 @@ public class AtlasEntityStoreV2Test extends AtlasEntityTestBase {
tblEntity = getEntityFromStore(tblEntityGuid);
Assert.assertTrue(tblEntity.getLabels().isEmpty());
}
@Test
public void testAddBusinessAttributesStringMaxLengthCheck() throws Exception {
Map<String, Map<String, Object>> bmMapReq = new HashMap<>();
Map<String, Object> bmAttrMapReq = new HashMap<>();
bmAttrMapReq.put("attr8", "01234567890123456789");
bmMapReq.put("bmWithAllTypes", bmAttrMapReq);
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmMapReq, false);
AtlasEntityWithExtInfo entity = entityStore.getById(dbEntity.getEntity().getGuid());
Map<String, Map<String, Object>> bmMapRes = entity.getEntity().getBusinessAttributes();
Assert.assertEquals(bmMapReq, bmMapRes);
}
@Test
public void testAddBusinessAttributesStringMaxLengthCheck_2() throws Exception {
Map<String, Map<String, Object>> bmMapReq = new HashMap<>();
Map<String, Object> bmAttrMapReq = new HashMap<>();
bmAttrMapReq.put("attr8", "012345678901234567890");
bmMapReq.put("bmWithAllTypes", bmAttrMapReq);
try {
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmMapReq, false);
} catch (AtlasBaseException e) {
Assert.assertEquals(AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS, e.getAtlasErrorCode());
return;
}
Assert.fail();
}
@Test(dependsOnMethods = "testAddBusinessAttributesStringMaxLengthCheck")
public void testUpdateBusinessAttributesStringMaxLengthCheck() throws Exception {
Map<String, Map<String, Object>> bmMapReq = new HashMap<>();
Map<String, Object> bmAttrMapReq = new HashMap<>();
bmAttrMapReq.put("attr8", "0123456789");
bmMapReq.put("bmWithAllTypes", bmAttrMapReq);
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmMapReq, true);
AtlasEntityWithExtInfo entity = entityStore.getById(dbEntity.getEntity().getGuid());
Map<String, Map<String, Object>> bmMapRes = entity.getEntity().getBusinessAttributes();
Assert.assertEquals(bmMapReq, bmMapRes);
}
@Test(dependsOnMethods = "testAddBusinessAttributesStringMaxLengthCheck")
public void testUpdateBusinessAttributesStringMaxLengthCheck_2() throws Exception {
Map<String, Map<String, Object>> bmMapReq = new HashMap<>();
Map<String, Object> bmAttrMapReq = new HashMap<>();
bmAttrMapReq.put("attr8", "012345678901234567890");
bmMapReq.put("bmWithAllTypes", bmAttrMapReq);
try {
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmMapReq, true);
} catch (AtlasBaseException e) {
Assert.assertEquals(AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS, e.getAtlasErrorCode());
return;
}
Assert.fail();
}
@Test(dependsOnMethods = "testAddBusinessAttributesStringMaxLengthCheck")
public void testUpdateBusinessAttributesStringMaxLengthCheck_3() throws Exception {
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
List<String> stringList = new ArrayList<>();
stringList.add("0123456789");
stringList.add("0123456789");
attrValueMapReq.put("attr18", stringList);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmAttrMapReq, true);
AtlasEntityWithExtInfo entity = entityStore.getById(dbEntity.getEntity().getGuid());
Map<String, Map<String, Object>> bmAttrMapRes = entity.getEntity().getBusinessAttributes();
Assert.assertEquals(bmAttrMapReq, bmAttrMapRes);
}
@Test(dependsOnMethods = "testAddBusinessAttributesStringMaxLengthCheck")
public void testUpdateBusinessAttributesStringMaxLengthCheck_4() throws Exception {
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
List<String> stringList = new ArrayList<>();
stringList.add("0123456789");
stringList.add("012345678901234567890");
attrValueMapReq.put("attr18", stringList);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
try {
entityStore.addOrUpdateBusinessAttributes(dbEntity.getEntity().getGuid(), bmAttrMapReq, true);
} catch (AtlasBaseException e) {
Assert.assertEquals(AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS, e.getAtlasErrorCode());
return;
}
Assert.fail();
}
}
\ No newline at end of file
......@@ -36,6 +36,7 @@ import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2;
import org.apache.atlas.store.AtlasTypeDefStore;
import org.apache.atlas.type.AtlasTypeUtil;
import org.apache.atlas.web.rest.EntityREST;
import org.apache.commons.lang.time.DateUtils;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
......@@ -46,12 +47,7 @@ import org.testng.annotations.Test;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Guice(modules = {TestModules.TestOnlyModule.class})
public class TestEntityREST {
......@@ -72,6 +68,10 @@ public class TestEntityREST {
public void setUp() throws Exception {
AtlasTypesDef typesDef = TestUtilsV2.defineHiveTypes();
typeStore.createTypesDef(typesDef);
AtlasTypesDef enumDef = TestUtilsV2.defineEnumTypes();
typeStore.createTypesDef(enumDef);
AtlasTypesDef metadataDef = TestUtilsV2.defineBusinessMetadataTypes();
typeStore.createTypesDef(metadataDef);
}
@AfterClass
......@@ -468,4 +468,256 @@ public class TestEntityREST {
private void deleteClassification(String guid, String classificationName) throws AtlasBaseException {
entityREST.deleteClassification(guid, classificationName, null);
}
private Map<String, Map<String, Object>> populateBusinessMetadataAttributeMap(Map<String, Map<String, Object>> bmAttrMapReq) {
if (bmAttrMapReq == null) {
bmAttrMapReq = new HashMap<>();
}
Map<String, Object> attrValueMapReq = new HashMap<>();
attrValueMapReq.put("attr1", true);
attrValueMapReq.put("attr2", Byte.MAX_VALUE);
attrValueMapReq.put("attr3", Short.MAX_VALUE);
attrValueMapReq.put("attr4", Integer.MAX_VALUE);
attrValueMapReq.put("attr5", Long.MAX_VALUE);
attrValueMapReq.put("attr6", Float.MAX_VALUE);
attrValueMapReq.put("attr7", Double.MAX_VALUE);
attrValueMapReq.put("attr8", "value8");
attrValueMapReq.put("attr9", new Date());
attrValueMapReq.put("attr10", "USER");
bmAttrMapReq.put("bmWithAllTypes", attrValueMapReq);
return bmAttrMapReq;
}
private Map<String, Map<String, Object>> populateMultivaluedBusinessMetadataAttributeMap(Map<String, Map<String, Object>> bmAttrMapReq) {
if (bmAttrMapReq == null) {
bmAttrMapReq = new HashMap<>();
}
Map<String, Object> attrValueMapReq = new HashMap<>();
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(false);
booleanList.add(true);
attrValueMapReq.put("attr11", booleanList);
List<Byte> byteList = new ArrayList<>();
byteList.add(Byte.MIN_VALUE);
byteList.add(Byte.MAX_VALUE);
attrValueMapReq.put("attr12", byteList);
List<Short> shortList = new ArrayList<>();
shortList.add(Short.MIN_VALUE);
shortList.add(Short.MAX_VALUE);
attrValueMapReq.put("attr13", shortList);
List<Integer> integerList = new ArrayList<>();
integerList.add(Integer.MIN_VALUE);
integerList.add(Integer.MAX_VALUE);
attrValueMapReq.put("attr14", integerList);
List<Long> longList = new ArrayList<>();
longList.add(Long.MIN_VALUE);
longList.add(Long.MAX_VALUE);
attrValueMapReq.put("attr15", longList);
List<Float> floatList = new ArrayList<>();
floatList.add(Float.MIN_VALUE);
floatList.add(Float.MAX_VALUE);
attrValueMapReq.put("attr16", floatList);
List<Double> doubleList = new ArrayList<>();
doubleList.add(Double.MIN_VALUE);
doubleList.add(Double.MAX_VALUE);
attrValueMapReq.put("attr17", doubleList);
List<String> stringList = new ArrayList<>();
stringList.add("value-1");
stringList.add("value-2");
attrValueMapReq.put("attr18", stringList);
List<Date> dateList = new ArrayList<>();
Date date = new Date();
dateList.add(date);
dateList.add(DateUtils.addDays(date, 2));
attrValueMapReq.put("attr19", dateList);
List<String> enumList = new ArrayList<>();
enumList.add("ROLE");
enumList.add("GROUP");
attrValueMapReq.put("attr20", enumList);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
return bmAttrMapReq;
}
@Test
public void testAddOrUpdateBusinessMetadataAttributes_1() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = populateBusinessMetadataAttributeMap(null);
bmAttrMapReq = populateMultivaluedBusinessMetadataAttributeMap(bmAttrMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
AtlasEntityWithExtInfo entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
AtlasEntity atlasEntity = entityWithExtInfo.getEntity();
Map<String, Map<String, Object>> bmAttrMapRes = atlasEntity.getBusinessAttributes();
Assert.assertEquals(bmAttrMapReq, bmAttrMapRes);
}
@Test
public void testAddOrUpdateBusinessMetadataAttributes_2() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = populateBusinessMetadataAttributeMap(null);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
bmAttrMapReq = populateMultivaluedBusinessMetadataAttributeMap(null);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), true, bmAttrMapReq);
AtlasEntityWithExtInfo entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
AtlasEntity atlasEntity = entityWithExtInfo.getEntity();
Map<String, Map<String, Object>> bmAttrMapRes = atlasEntity.getBusinessAttributes();
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes"));
Assert.assertNotNull(bmAttrMapRes.get("bmWithAllTypesMV"));
}
@Test
public void testAddOrUpdateBusinessMetadataAttributes_3() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = populateBusinessMetadataAttributeMap(null);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
AtlasEntityWithExtInfo entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
AtlasEntity atlasEntity = entityWithExtInfo.getEntity();
Map<String, Map<String, Object>> bmAttrMapRes = atlasEntity.getBusinessAttributes();
Assert.assertEquals(bmAttrMapReq, bmAttrMapRes);
Map<String, Object> attrValueMapReq = new HashMap<>();
bmAttrMapReq = new HashMap<>();
attrValueMapReq.put("attr8", "value8-updated");
bmAttrMapReq.put("bmWithAllTypes", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), true, bmAttrMapReq);
entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
atlasEntity = entityWithExtInfo.getEntity();
bmAttrMapRes = atlasEntity.getBusinessAttributes();
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr1"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr2"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr3"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr4"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr5"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr6"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr7"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr9"));
Assert.assertNull(bmAttrMapRes.get("bmWithAllTypes").get("attr10"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr8"), "value8-updated");
}
@Test
public void testAddOrUpdateBusinessAttributes_4() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = populateBusinessMetadataAttributeMap(null);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
AtlasEntityWithExtInfo entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
AtlasEntity atlasEntity = entityWithExtInfo.getEntity();
Map<String, Map<String, Object>> bmAttrMapRes = atlasEntity.getBusinessAttributes();
Assert.assertEquals(bmAttrMapReq, bmAttrMapRes);
Map<String, Object> attrValueMapReq_2 = new HashMap<>();
Map<String, Map<String, Object>> bmAttrMapReq_2 = new HashMap<>();
attrValueMapReq_2.put("attr8", "value8-updated");
bmAttrMapReq_2.put("bmWithAllTypes", attrValueMapReq_2);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq_2);
entityWithExtInfo = entityREST.getById(dbEntity.getGuid(), false, false);
atlasEntity = entityWithExtInfo.getEntity();
Map<String, Map<String, Object>> bmAttrMapRes_2 = atlasEntity.getBusinessAttributes();
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr1"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr1"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr2"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr2"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr3"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr3"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr4"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr4"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr5"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr5"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr6"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr6"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr7"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr7"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr9"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr9"));
Assert.assertEquals(bmAttrMapRes.get("bmWithAllTypes").get("attr10"), bmAttrMapRes_2.get("bmWithAllTypes").get("attr10"));
Assert.assertEquals(bmAttrMapRes_2.get("bmWithAllTypes").get("attr8"), "value8-updated");
}
@Test(expectedExceptions = AtlasBaseException.class)
public void testAddOrUpdateBusinessAttributes_5() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
attrValueMapReq.put("attr2", "value2");
bmAttrMapReq.put("bmWithAllTypes", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
}
@Test(expectedExceptions = AtlasBaseException.class)
public void testAddOrUpdateBusinessAttributes_6() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
attrValueMapReq.put("attr14", 14);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
}
@Test(expectedExceptions = AtlasBaseException.class)
public void testAddOrUpdateBusinessAttributes_7() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
List<String> stringList = new ArrayList<>();
stringList.add("value-1");
stringList.add("value-2");
attrValueMapReq.put("attr16", stringList);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
}
@Test(expectedExceptions = AtlasBaseException.class)
public void testAddOrUpdateBusinessAttributes_8() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
attrValueMapReq.put("attr10", "USER123");
bmAttrMapReq.put("bmWithAllTypes", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
}
@Test(expectedExceptions = AtlasBaseException.class)
public void testAddOrUpdateBusinessAttributes_9() throws Exception {
createTestEntity();
Map<String, Map<String, Object>> bmAttrMapReq = new HashMap<>();
Map<String, Object> attrValueMapReq = new HashMap<>();
List<String> stringList = new ArrayList<>();
stringList.add("USER123");
stringList.add("ROLE123");
attrValueMapReq.put("attr20", stringList);
bmAttrMapReq.put("bmWithAllTypesMV", attrValueMapReq);
entityREST.addOrUpdateBusinessAttributes(dbEntity.getGuid(), false, bmAttrMapReq);
}
}
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