Commit 2fbea9f2 by Ashutosh Mestry

ATLAS-2922: Multiplicity computation updated for SET/optional

parent c6aaef08
......@@ -441,6 +441,7 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
}
}
@JsonIgnore
public boolean isSoftReferenced() {
return this.options != null &&
getOptions().containsKey(AtlasAttributeDef.ATTRDEF_OPTION_SOFT_REFERENCE) &&
......
......@@ -482,11 +482,15 @@ public class AtlasTypeUtil {
ret.setDefaultValue(attributeDef.getDefaultValue());
ret.setDescription(attributeDef.getDescription());
ret.setOptions(attributeDef.getOptions());
ret.setMultiplicity(getMultiplicity(attributeDef));
final int lower;
final int upper;
return ret;
}
if (attributeDef.getCardinality() == AtlasAttributeDef.Cardinality.SINGLE) {
public static Multiplicity getMultiplicity(AtlasAttributeDef attributeDef) {
int lower;
int upper;
if (attributeDef.getCardinality() == Cardinality.SINGLE) {
lower = attributeDef.getIsOptional() ? 0 : 1;
upper = 1;
} else {
......@@ -499,14 +503,7 @@ public class AtlasTypeUtil {
upper = attributeDef.getValuesMaxCount() < 2 ? Integer.MAX_VALUE : attributeDef.getValuesMaxCount();
}
Multiplicity multiplicity = new Multiplicity();
multiplicity.setLower(lower);
multiplicity.setUpper(upper);
multiplicity.setIsUnique(AtlasAttributeDef.Cardinality.SET.equals(attributeDef.getCardinality()));
ret.setMultiplicity(multiplicity);
return ret;
return new Multiplicity(lower, upper, Cardinality.SET.equals(attributeDef.getCardinality()));
}
public static Map<String, Object> toMap(AtlasEntity entity) {
......
......@@ -125,15 +125,21 @@ public class Multiplicity implements Serializable {
@Override
public void serialize(Multiplicity value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value != null) {
if (value.equals(Multiplicity.REQUIRED)) {
jgen.writeString("required");
} else if (value.equals(Multiplicity.OPTIONAL)) {
jgen.writeString("optional");
final String serializedValue;
if (value.getLower() < 1) {
serializedValue = "optional";
} else if (value.equals(Multiplicity.COLLECTION)) {
jgen.writeString("collection");
serializedValue = "collection";
} else if (value.equals(Multiplicity.SET)) {
jgen.writeString("set");
serializedValue = "set";
} else if (value.equals(Multiplicity.REQUIRED)) {
serializedValue = "required";
} else { // default value
serializedValue = "required";
}
jgen.writeString(serializedValue);
}
}
}
......
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.type;
import org.apache.atlas.model.typedef.AtlasStructDef;
import org.apache.atlas.v1.model.typedef.AttributeDefinition;
import org.apache.atlas.v1.model.typedef.Multiplicity;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
public class TestMultiplicity {
@Test
public void verify() {
assertMultiplicity(AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, false, 1, 1);
assertMultiplicity(AtlasStructDef.AtlasAttributeDef.Cardinality.LIST, false, 1, Integer.MAX_VALUE);
assertMultiplicity(AtlasStructDef.AtlasAttributeDef.Cardinality.SET, true, 0, Integer.MAX_VALUE);
}
private void assertMultiplicity(AtlasStructDef.AtlasAttributeDef.Cardinality cardinality,
boolean optionality, int expectedLower, int expectedUpper) {
AtlasStructDef.AtlasAttributeDef attributeDef = new AtlasStructDef.AtlasAttributeDef();
attributeDef.setCardinality(cardinality);
attributeDef.setIsOptional(optionality);
Multiplicity multiplicity = AtlasTypeUtil.getMultiplicity(attributeDef);
assertNotNull(multiplicity);
assertEquals(multiplicity.getLower(), expectedLower);
assertEquals(multiplicity.getUpper(), expectedUpper);
AttributeDefinition attributeDefinition = new AttributeDefinition();
attributeDefinition.setMultiplicity(multiplicity);
assertNotNull(AtlasType.toJson(attributeDefinition));
}
}
......@@ -585,29 +585,6 @@ public class AtlasStructDefStoreV2 extends AtlasAbstractDefStoreV2<AtlasStructDe
return ret;
}
public static Multiplicity getMultiplicity(AtlasAttributeDef attributeDef) {
final int lower;
final int upper;
final boolean isUnique = AtlasAttributeDef.Cardinality.SET.equals(attributeDef.getCardinality());
if (attributeDef.getCardinality() == AtlasAttributeDef.Cardinality.SINGLE) {
lower = attributeDef.getIsOptional() ? 0 : 1;
upper = 1;
} else {
if(attributeDef.getIsOptional()) {
lower = 0;
} else {
lower = attributeDef.getValuesMinCount() < 1 ? 1 : attributeDef.getValuesMinCount();
}
upper = attributeDef.getValuesMaxCount() < 2 ? Integer.MAX_VALUE : attributeDef.getValuesMaxCount();
}
Multiplicity ret = new Multiplicity(lower, upper, isUnique);
return ret;
}
public static AttributeDefinition toAttributeDefinition(AtlasAttribute attribute) {
final AtlasAttributeDef attrDef = attribute.getAttributeDef();
......@@ -615,7 +592,7 @@ public class AtlasStructDefStoreV2 extends AtlasAbstractDefStoreV2<AtlasStructDe
ret.setName(attrDef.getName());
ret.setDataTypeName(attrDef.getTypeName());
ret.setMultiplicity(getMultiplicity(attrDef));
ret.setMultiplicity(AtlasTypeUtil.getMultiplicity(attrDef));
ret.setIsComposite(attribute.isOwnedRef());
ret.setIsUnique(attrDef.getIsUnique());
ret.setIsIndexable(attrDef.getIsIndexable());
......
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