ATLAS-2922: Multiplicity computation updated for SET/optional

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

Branch: refs/heads/branch-1.0
Commit: 6e0c97c07ebbec9bbbd43409a80df5aea54676bc
Parents: 7952682
Author: Ashutosh Mestry <ames...@hortonworks.com>
Authored: Tue Oct 16 09:24:26 2018 -0700
Committer: Ashutosh Mestry <ames...@hortonworks.com>
Committed: Thu Nov 1 15:42:58 2018 -0700

----------------------------------------------------------------------
 .../atlas/model/typedef/AtlasStructDef.java     |  1 +
 .../org/apache/atlas/type/AtlasTypeUtil.java    | 19 +++----
 .../atlas/v1/model/typedef/Multiplicity.java    | 18 ++++---
 .../org/apache/atlas/type/TestMultiplicity.java | 54 ++++++++++++++++++++
 .../store/graph/v2/AtlasStructDefStoreV2.java   | 25 +--------
 5 files changed, 76 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/atlas/blob/6e0c97c0/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
----------------------------------------------------------------------
diff --git 
a/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java 
b/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
index 013c753..921cf3f 100644
--- a/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
+++ b/intg/src/main/java/org/apache/atlas/model/typedef/AtlasStructDef.java
@@ -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) &&

http://git-wip-us.apache.org/repos/asf/atlas/blob/6e0c97c0/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 78df622..ff0c1fc 100644
--- a/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
+++ b/intg/src/main/java/org/apache/atlas/type/AtlasTypeUtil.java
@@ -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) {

http://git-wip-us.apache.org/repos/asf/atlas/blob/6e0c97c0/intg/src/main/java/org/apache/atlas/v1/model/typedef/Multiplicity.java
----------------------------------------------------------------------
diff --git 
a/intg/src/main/java/org/apache/atlas/v1/model/typedef/Multiplicity.java 
b/intg/src/main/java/org/apache/atlas/v1/model/typedef/Multiplicity.java
index 6da4ce5..45a8e15 100644
--- a/intg/src/main/java/org/apache/atlas/v1/model/typedef/Multiplicity.java
+++ b/intg/src/main/java/org/apache/atlas/v1/model/typedef/Multiplicity.java
@@ -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);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/atlas/blob/6e0c97c0/intg/src/test/java/org/apache/atlas/type/TestMultiplicity.java
----------------------------------------------------------------------
diff --git a/intg/src/test/java/org/apache/atlas/type/TestMultiplicity.java 
b/intg/src/test/java/org/apache/atlas/type/TestMultiplicity.java
new file mode 100644
index 0000000..d403af3
--- /dev/null
+++ b/intg/src/test/java/org/apache/atlas/type/TestMultiplicity.java
@@ -0,0 +1,54 @@
+/**
+ * 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));
+    }
+}

http://git-wip-us.apache.org/repos/asf/atlas/blob/6e0c97c0/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasStructDefStoreV2.java
----------------------------------------------------------------------
diff --git 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasStructDefStoreV2.java
 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasStructDefStoreV2.java
index 20c3b9b..1b67f11 100644
--- 
a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasStructDefStoreV2.java
+++ 
b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasStructDefStoreV2.java
@@ -589,29 +589,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();
 
@@ -619,7 +596,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());

Reply via email to