From 6dd7523428d6587844bc5a612bdbbf6133c53f3e Mon Sep 17 00:00:00 2001 From: Ryan D. Brooks Date: Sat, 1 Sep 2018 09:59:09 -0700 Subject: refactor: Make RelationManager tests less fragile Change-Id: I840f8fc1d0f7e6238392c7dda8bc2ec00f731821 --- .../eclipse/osee/orcs/core/OrcsMockUtility.java | 70 ++++++++++++++++++++++ .../orcs/core/internal/attribute/ArtifactTest.java | 6 -- .../internal/relation/RelationManagerTest.java | 59 +++++------------- .../relation/impl/RelationManagerImplTest.java | 40 +++++-------- .../orcs/core/internal/artifact/ArtifactImpl.java | 18 ++---- 5 files changed, 104 insertions(+), 89 deletions(-) create mode 100644 plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/OrcsMockUtility.java diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/OrcsMockUtility.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/OrcsMockUtility.java new file mode 100644 index 00000000000..08c40578a02 --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/OrcsMockUtility.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2018 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.core; + +import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.Name; +import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.RelationOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.eclipse.osee.framework.core.data.AttributeTypeId; +import org.eclipse.osee.framework.core.data.AttributeTypeToken; +import org.eclipse.osee.framework.core.data.BranchId; +import org.eclipse.osee.framework.core.data.IArtifactType; +import org.eclipse.osee.framework.core.enums.ModificationType; +import org.eclipse.osee.orcs.core.ds.ArtifactData; +import org.eclipse.osee.orcs.core.ds.Attribute; +import org.eclipse.osee.orcs.core.ds.AttributeData; +import org.eclipse.osee.orcs.core.ds.VersionData; +import org.eclipse.osee.orcs.core.internal.artifact.Artifact; +import org.eclipse.osee.orcs.core.internal.artifact.ArtifactImpl; +import org.eclipse.osee.orcs.core.internal.attribute.AttributeFactory; +import org.eclipse.osee.orcs.core.internal.graph.GraphData; +import org.eclipse.osee.orcs.data.ArtifactTypes; +import org.mockito.Matchers; + +/** + * @author Ryan D. Brooks + */ +public class OrcsMockUtility { + + public static Artifact createTestArtifact(GraphData graph, BranchId branch, IArtifactType artifactType, Long artifactId, String name) { + AttributeFactory attributeFactory = mock(AttributeFactory.class); + ArtifactTypes artifactTypeCache = mock(ArtifactTypes.class); + + VersionData version = mock(VersionData.class); + when(version.getBranch()).thenReturn(branch); + + ArtifactData artifactData = mock(ArtifactData.class); + when(artifactData.getVersion()).thenReturn(version); + when(artifactData.getTypeUuid()).thenReturn(artifactType.getId()); + when(artifactData.getId()).thenReturn(artifactId); + when(artifactData.getModType()).thenReturn(ModificationType.NEW); + + when(artifactTypeCache.get(artifactType.getId())).thenReturn(artifactType); + Artifact artifact = new ArtifactImpl(artifactTypeCache, artifactData, attributeFactory); + artifact.setGraph(graph); + + when(attributeFactory.getMaxOccurrenceLimit(Matchers.any(AttributeTypeId.class))).thenReturn(1); + + addAttribute(artifact, Name, name); + addAttribute(artifact, RelationOrder, ""); + artifact.getBranch(); + return artifact; + } + + public static void addAttribute(Artifact artifact, AttributeTypeToken attributeType, String value) { + Attribute attribute = mock(Attribute.class); + AttributeData attributeData = mock(AttributeData.class); + when(attribute.getOrcsData()).thenReturn(attributeData); + when(attribute.getValue()).thenReturn(value); + artifact.add(attributeType, attribute); + } +} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/ArtifactTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/ArtifactTest.java index 9be89e8aa4d..c5c3b2b4cdb 100644 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/ArtifactTest.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/ArtifactTest.java @@ -157,12 +157,6 @@ public class ArtifactTest { verify(attributeFactory).createAttributeWithDefaults(artifact, artifactData, CoreAttributeTypes.City); } - @Test - public void testGetLocalId() { - artifact.getLocalId(); - verify(artifactData).getLocalId(); - } - @Test public void testGetGuid() { artifact.getGuid(); diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/RelationManagerTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/RelationManagerTest.java index 575396ddffc..261d62a3418 100644 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/RelationManagerTest.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/RelationManagerTest.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.osee.orcs.core.internal.relation; +import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.SoftwareRequirement; import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON; import static org.eclipse.osee.framework.core.enums.CoreRelationTypes.DEFAULT_HIERARCHY; import static org.eclipse.osee.framework.core.enums.CoreRelationTypes.IS_CHILD; @@ -43,6 +44,7 @@ import org.eclipse.osee.framework.jdk.core.type.ResultSets; import org.eclipse.osee.framework.jdk.core.util.GUID; import org.eclipse.osee.logger.Log; import org.eclipse.osee.orcs.OrcsSession; +import org.eclipse.osee.orcs.core.OrcsMockUtility; import org.eclipse.osee.orcs.core.ds.RelationData; import org.eclipse.osee.orcs.core.internal.artifact.Artifact; import org.eclipse.osee.orcs.core.internal.graph.GraphData; @@ -85,13 +87,6 @@ public class RelationManagerTest { @Mock private OrcsSession session; @Mock private GraphData graph; - @Mock private Artifact node1; - @Mock private Artifact node2; - @Mock private Artifact node3; - @Mock private Artifact node4; - @Mock private Artifact node5; - @Mock private Artifact node6; - @Mock private RelationNodeAdjacencies adjancies1; @Mock private RelationNodeAdjacencies adjancies2; @@ -110,6 +105,12 @@ public class RelationManagerTest { private RelationManager manager; private Map mockDb; + private Artifact node1; + private Artifact node2; + private Artifact node3; + private Artifact node4; + private Artifact node5; + private Artifact node6; @SuppressWarnings({"unchecked", "rawtypes"}) @Before @@ -119,49 +120,20 @@ public class RelationManagerTest { String sessionId = GUID.create(); when(session.getGuid()).thenReturn(sessionId); + node1 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 11L, "z"); + node2 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 22L, "y"); + node3 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 33L, "x"); + node4 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 44L, "w"); + node5 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 55L, "v"); + node6 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 66L, "u"); + manager = RelationManagerFactory.createRelationManager(logger, types, relationFactory, loader, provider); when(loader.loadNodes(eq(session), eq(graph), anyCollectionOf(Integer.class), eq(LoadLevel.ALL))).thenAnswer( new LoaderAnswer()); - when(node1.getLocalId()).thenReturn(11); - when(node2.getLocalId()).thenReturn(22); - when(node3.getLocalId()).thenReturn(33); - when(node4.getLocalId()).thenReturn(44); - when(node5.getLocalId()).thenReturn(55); - when(node6.getLocalId()).thenReturn(66); - - when(node1.getBranch()).thenReturn(COMMON); - when(node2.getBranch()).thenReturn(COMMON); - when(node3.getBranch()).thenReturn(COMMON); - when(node4.getBranch()).thenReturn(COMMON); - when(node5.getBranch()).thenReturn(COMMON); - when(node6.getBranch()).thenReturn(COMMON); - - when(node1.getName()).thenReturn("z"); - when(node2.getName()).thenReturn("y"); - when(node3.getName()).thenReturn("x"); - when(node4.getName()).thenReturn("w"); - when(node5.getName()).thenReturn("v"); - when(node6.getName()).thenReturn("u"); - - when(node1.getGraph()).thenReturn(graph); - when(node2.getGraph()).thenReturn(graph); - when(node3.getGraph()).thenReturn(graph); - when(node4.getGraph()).thenReturn(graph); - when(node5.getGraph()).thenReturn(graph); - when(node6.getGraph()).thenReturn(graph); - when(graph.getTransaction()).thenReturn(TransactionId.SENTINEL); - when(node1.getArtifactTypeId()).thenReturn(CoreArtifactTypes.SoftwareRequirement); - when(node2.getArtifactTypeId()).thenReturn(CoreArtifactTypes.SoftwareRequirement); - when(node3.getArtifactTypeId()).thenReturn(CoreArtifactTypes.SoftwareRequirement); - - when(node1.getOrderData()).thenReturn(""); - when(node2.getOrderData()).thenReturn(""); - when(node3.getOrderData()).thenReturn(""); - mockDb = new HashMap<>(); mockDb.put(11, node1); mockDb.put(22, node2); @@ -217,7 +189,6 @@ public class RelationManagerTest { private void setupAdjacencies(Artifact node, Relation... relations) { RelationNodeAdjacencies adjacents = new RelationNodeAdjacencies(); - graph.addAdjacencies(node, adjacents); when(graph.getAdjacencies(node)).thenReturn(adjacents); for (Relation relation : relations) { adjacents.add(relation.getRelationType(), relation); diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/impl/RelationManagerImplTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/impl/RelationManagerImplTest.java index bf59c1427df..a5e842cb568 100644 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/impl/RelationManagerImplTest.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/relation/impl/RelationManagerImplTest.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.osee.orcs.core.internal.relation.impl; +import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.SoftwareRequirement; import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON; import static org.eclipse.osee.framework.core.enums.CoreBranches.SYSTEM_ROOT; import static org.eclipse.osee.framework.core.enums.CoreRelationTypes.DEFAULT_HIERARCHY; @@ -55,6 +56,7 @@ import org.eclipse.osee.framework.jdk.core.type.ResultSet; import org.eclipse.osee.framework.jdk.core.util.GUID; import org.eclipse.osee.logger.Log; import org.eclipse.osee.orcs.OrcsSession; +import org.eclipse.osee.orcs.core.OrcsMockUtility; import org.eclipse.osee.orcs.core.internal.artifact.Artifact; import org.eclipse.osee.orcs.core.internal.graph.GraphData; import org.eclipse.osee.orcs.core.internal.relation.Relation; @@ -105,13 +107,6 @@ public class RelationManagerImplTest { @Mock private GraphData graph; - @Mock private Artifact node1; - @Mock private Artifact node2; - @Mock private Artifact node3; - @Mock private Artifact node4; - @Mock private Artifact node5; - @Mock private Artifact node6; - @Mock private RelationNodeAdjacencies container1; @Mock private RelationNodeAdjacencies container2; @@ -137,6 +132,12 @@ public class RelationManagerImplTest { private final RelationTypes relationTypes = new RelationTypesImpl(null); private RelationManager manager; + private Artifact node1; + private Artifact node2; + private Artifact node3; + private Artifact node4; + private Artifact node5; + private Artifact node6; @Before public void setUp() { @@ -147,23 +148,12 @@ public class RelationManagerImplTest { String sessionId = GUID.create(); when(session.getGuid()).thenReturn(sessionId); - when(node1.getLocalId()).thenReturn(11); - when(node2.getLocalId()).thenReturn(22); - when(node3.getLocalId()).thenReturn(33); - when(node4.getLocalId()).thenReturn(44); - when(node5.getLocalId()).thenReturn(55); - when(node6.getLocalId()).thenReturn(66); - - when(node1.getGraph()).thenReturn(graph); - when(node2.getGraph()).thenReturn(graph); - when(node3.getGraph()).thenReturn(graph); - when(node4.getGraph()).thenReturn(graph); - when(node5.getGraph()).thenReturn(graph); - when(node6.getGraph()).thenReturn(graph); - - when(node1.getBranch()).thenReturn(COMMON); - when(node2.getBranch()).thenReturn(COMMON); - when(node3.getBranch()).thenReturn(SYSTEM_ROOT); + node1 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 11L, "z"); + node2 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 22L, "y"); + node3 = OrcsMockUtility.createTestArtifact(graph, SYSTEM_ROOT, SoftwareRequirement, 33L, "x"); + node4 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 44L, "w"); + node5 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 55L, "v"); + node6 = OrcsMockUtility.createTestArtifact(graph, COMMON, SoftwareRequirement, 66L, "u"); when(graph.getTransaction()).thenReturn(TransactionId.SENTINEL); @@ -726,7 +716,6 @@ public class RelationManagerImplTest { List children = Arrays.asList(node2); when(relation1.getRelationType()).thenReturn(DEFAULT_HIERARCHY); - when(node1.isDeleteAllowed()).thenReturn(true); when(container1.getList(EXCLUDE_DELETED)).thenReturn(allRelations); when(container1.getList(DEFAULT_HIERARCHY, EXCLUDE_DELETED, node1, IS_PARENT)).thenReturn(allRelations); when(container1.getList(DEFAULT_HIERARCHY, EXCLUDE_DELETED, node1, IS_PARENT)).thenReturn(asAParent); @@ -744,7 +733,6 @@ public class RelationManagerImplTest { verify(container1).getList(EXCLUDE_DELETED); verify(resolver).resolve(session, graph, allRelations, SIDE_A, SIDE_B); verify(container1).getList(DEFAULT_HIERARCHY, EXCLUDE_DELETED, node1, IS_PARENT); - verify(node1).delete(); verify(relation1).getIdForSide(SIDE_A); verify(relation1).delete(); diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/ArtifactImpl.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/ArtifactImpl.java index eb2e221bbf8..a91f706260a 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/ArtifactImpl.java +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/artifact/ArtifactImpl.java @@ -77,7 +77,7 @@ public class ArtifactImpl extends AttributeManagerImpl implements Artifact { @Override public Integer getLocalId() { - return getOrcsData().getLocalId(); + return getId().intValue(); } @Override @@ -102,13 +102,9 @@ public class ArtifactImpl extends AttributeManagerImpl implements Artifact { return artifactData.getVersion().getBranch(); } - private IArtifactType getArtifactType() { - return artifactTypeCache.get(getOrcsData().getTypeUuid()); - } - @Override public IArtifactType getArtifactTypeId() { - return getArtifactType(); + return artifactTypeCache.get(getOrcsData().getTypeUuid()); } @Override @@ -165,7 +161,7 @@ public class ArtifactImpl extends AttributeManagerImpl implements Artifact { @Override public String getExceptionString() { try { - return String.format("artifact type[%s] id[%s] on branch[%s]", getArtifactType(), getId(), getBranch()); + return String.format("artifact type[%s] id[%s] on branch[%s]", getArtifactTypeId(), getId(), getBranch()); } catch (OseeCoreException ex) { return Lib.exceptionToString(ex); } @@ -218,17 +214,13 @@ public class ArtifactImpl extends AttributeManagerImpl implements Artifact { @Override public String toString() { try { - return String.format("artifact [type=[%s] guid=[%s] branch=[%s]]", getArtifactType(), getGuid(), getBranch()); + return String.format("artifact [type=[%s] guid=[%s] branch=[%s]]", getArtifactTypeId(), getGuid(), + getBranch()); } catch (OseeCoreException ex) { return Lib.exceptionToString(ex); } } - @Override - public Long getId() { - return Long.valueOf(getLocalId()); - } - @Override public Iterable>> getAttributeIterable() { return null; -- cgit v1.2.3