Repository: atlas Updated Branches: refs/heads/branch-0.8 df4f87d09 -> 4dcb2c7bf
ATLAS-2842: fixed incorrect AtlasObjectId.equals() Signed-off-by: Madhan Neethiraj <mad...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/4dcb2c7b Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/4dcb2c7b Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/4dcb2c7b Branch: refs/heads/branch-0.8 Commit: 4dcb2c7bf00afd7cc1c82fec0ecc9456f200f1e1 Parents: df4f87d Author: Daniel Kelencz <dkele...@hortonworks.com> Authored: Mon Aug 27 07:32:12 2018 -0700 Committer: Madhan Neethiraj <mad...@apache.org> Committed: Mon Aug 27 07:39:45 2018 -0700 ---------------------------------------------------------------------- .../atlas/model/instance/AtlasObjectId.java | 12 +-- .../atlas/model/instance/TestAtlasObjectId.java | 107 +++++++++++++++++++ 2 files changed, 113 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/atlas/blob/4dcb2c7b/intg/src/main/java/org/apache/atlas/model/instance/AtlasObjectId.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/model/instance/AtlasObjectId.java b/intg/src/main/java/org/apache/atlas/model/instance/AtlasObjectId.java index 5f0e093..479bca5 100644 --- a/intg/src/main/java/org/apache/atlas/model/instance/AtlasObjectId.java +++ b/intg/src/main/java/org/apache/atlas/model/instance/AtlasObjectId.java @@ -31,6 +31,7 @@ import javax.xml.bind.annotation.XmlSeeAlso; import org.apache.atlas.model.PList; import org.apache.atlas.model.SearchFilter.SortType; import org.apache.atlas.model.typedef.AtlasBaseTypeDef; +import org.apache.commons.lang.StringUtils; import org.codehaus.jackson.annotate.JsonAutoDetect; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE; @@ -164,13 +165,12 @@ public class AtlasObjectId implements Serializable { AtlasObjectId that = (AtlasObjectId) o; - // if guid is null, equality should be based on typeName/uniqueAttributes - if (guid != null && Objects.equals(guid, that.guid)) { - return true; + // if guid is empty/null, equality should be based on typeName/uniqueAttributes + if (StringUtils.isEmpty(guid) && StringUtils.isEmpty(that.guid)) { + return Objects.equals(typeName, that.typeName) && Objects.equals(uniqueAttributes, that.uniqueAttributes); + } else { + return Objects.equals(guid, that.guid); } - - return Objects.equals(typeName, that.typeName) && - Objects.equals(uniqueAttributes, that.uniqueAttributes); } @Override http://git-wip-us.apache.org/repos/asf/atlas/blob/4dcb2c7b/intg/src/test/java/org/apache/atlas/model/instance/TestAtlasObjectId.java ---------------------------------------------------------------------- diff --git a/intg/src/test/java/org/apache/atlas/model/instance/TestAtlasObjectId.java b/intg/src/test/java/org/apache/atlas/model/instance/TestAtlasObjectId.java new file mode 100644 index 0000000..e333263 --- /dev/null +++ b/intg/src/test/java/org/apache/atlas/model/instance/TestAtlasObjectId.java @@ -0,0 +1,107 @@ +/** + * 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.model.instance; + +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; + +public class TestAtlasObjectId { + + @Test + public void testEqualsDifferentIdShouldNotEqual() + { + AtlasObjectId one = new AtlasObjectId("one"); + AtlasObjectId different = new AtlasObjectId("different"); + + assertNotEquals(one, different); + } + + @Test + public void testEqualsSameIdShouldEqual() + { + AtlasObjectId one = new AtlasObjectId("one"); + AtlasObjectId same = new AtlasObjectId("one"); + + assertEquals(one, same); + } + + @Test + public void testEqualsSameIdButDifferentTypeShouldEqual() + { + AtlasObjectId one = new AtlasObjectId("one", "onetype"); + AtlasObjectId same = new AtlasObjectId("one", "anothertype"); + + assertEquals(one, same); + } + + @Test + public void testEqualsDifferentIdButSameTypeShouldNotEqual() + { + AtlasObjectId one = new AtlasObjectId("one", "onetype"); + AtlasObjectId different = new AtlasObjectId("different", "onetype"); + + assertNotEquals(one, different); + } + + @Test + public void testEqualsNoGuidOnOneShouldNotEqual() + { + AtlasObjectId one = new AtlasObjectId("one", "onetype"); + AtlasObjectId different = new AtlasObjectId("onetype", new HashMap<>()); + + assertNotEquals(one, different); + } + + @Test + public void testEqualsNoGuidsButSameTypesShouldEqual() + { + AtlasObjectId one = new AtlasObjectId("onetype", new HashMap<>()); + AtlasObjectId same = new AtlasObjectId("onetype", new HashMap<>()); + + assertEquals(one, same); + } + + @Test + public void testEqualsNoGuidsAndDifferentUniqueAttributesShouldNotEqual() + { + AtlasObjectId one = new AtlasObjectId("onetype", new HashMap<>()); + + Map<String, Object> attrs = new HashMap<>(); + attrs.put("attr1", 1); + AtlasObjectId different = new AtlasObjectId("onetype", attrs); + + assertNotEquals(one, different); + } + + @Test + public void testEqualsDifferentUniqueAttrsAndSameGuidsShouldEqual() + { + AtlasObjectId one = new AtlasObjectId("one", "onetype", new HashMap<>()); + + Map<String, Object> attrs = new HashMap<>(); + attrs.put("attr1", 1); + AtlasObjectId same = new AtlasObjectId("one", "onetype", attrs); + + assertEquals(one, same); + } +}