Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4f233371819e1575a0f55f771ad85c540ca81313 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*******************************************************************************
 * Copyright (c) 2012 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.internal.artifact;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.orcs.core.ds.ArtifactData;
import org.eclipse.osee.orcs.core.ds.ArtifactDataFactory;
import org.eclipse.osee.orcs.core.ds.AttributeData;
import org.eclipse.osee.orcs.core.ds.OrcsData;
import org.eclipse.osee.orcs.core.ds.VersionData;
import org.eclipse.osee.orcs.core.internal.attribute.Attribute;
import org.eclipse.osee.orcs.core.internal.attribute.AttributeFactory;
import org.eclipse.osee.orcs.core.internal.attribute.AttributeManager;
import org.eclipse.osee.orcs.core.internal.relation.RelationFactory;
import org.eclipse.osee.orcs.core.internal.util.ValueProvider;
import org.eclipse.osee.orcs.core.internal.util.ValueProviderFactory;
import org.eclipse.osee.orcs.data.ArtifactTypes;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 * Test Case for {@link ArtifactFactory}
 * 
 * @author John Misinco
 */
public class ArtifactFactoryTest {

   @Rule
   public ExpectedException thrown = ExpectedException.none();

   // @formatter:off
   @Mock private Branch branch;
   @Mock private IArtifactType artifactType;
   @Mock private ArtifactData artifactData;
   @Mock private VersionData artifactVersion;
   
   @Mock private ArtifactDataFactory dataFactory;
   @Mock private AttributeFactory attributeFactory;
   @Mock private RelationFactory relationFactory;
   @Mock private ArtifactTypes artifactTypeCache;
   @Mock private ValueProviderFactory providerFactory;
   @Mock private ValueProvider<Branch, OrcsData> branchProvider;
   
   @Mock private Attribute<Object> attribute;
   @Mock private AttributeData attributeData;
   @Mock private Artifact source;
   
   @Mock private ArtifactData otherArtifactData;
   // @formatter:on

   private String guid;
   private ArtifactFactory artifactFactory;
   private List<IAttributeType> types;

   @Before
   public void init() throws OseeCoreException {
      MockitoAnnotations.initMocks(this);

      artifactFactory = new ArtifactFactory(dataFactory, attributeFactory, artifactTypeCache, providerFactory);

      guid = GUID.create();

      types = new ArrayList<IAttributeType>();
      types.add(CoreAttributeTypes.RelationOrder);
      types.add(CoreAttributeTypes.City);
      types.add(CoreAttributeTypes.Annotation);

      when(artifactData.getLocalId()).thenReturn(45);
      when(artifactData.getGuid()).thenReturn(guid);
      when(artifactData.getTypeUuid()).thenReturn(65L);
      when(artifactData.getVersion()).thenReturn(artifactVersion);
      when(artifactVersion.getBranchId()).thenReturn(23);

      when(
         attributeFactory.copyAttribute(any(AttributeData.class), any(IOseeBranch.class), any(AttributeManager.class))).thenReturn(
         attribute);

      when(otherArtifactData.getLocalId()).thenReturn(45);
      when(otherArtifactData.getGuid()).thenReturn(guid);
      when(otherArtifactData.getTypeUuid()).thenReturn(65L);
      when(otherArtifactData.getVersion()).thenReturn(artifactVersion);

      when(providerFactory.createBranchProvider(artifactData)).thenReturn(branchProvider);
      when(providerFactory.createBranchProvider(otherArtifactData)).thenReturn(branchProvider);

      when(branchProvider.get()).thenReturn(branch);

      when(artifactTypeCache.getByUuid(65L)).thenReturn(artifactType);
   }

   @Test
   public void testCreateArtifactFromBranchTypeAndGuid() throws OseeCoreException {
      when(dataFactory.create(branch, artifactType, guid)).thenReturn(artifactData);

      Artifact artifact = artifactFactory.createArtifact(branch, artifactType, guid);

      verify(dataFactory).create(branch, artifactType, guid);
      assertEquals(artifactType, artifact.getArtifactType());
      assertEquals(guid, artifact.getGuid());
   }

   @Test
   public void testCreateArtifactFromArtifactData() throws OseeCoreException {
      Artifact artifact = artifactFactory.createArtifact(artifactData);

      assertEquals(artifactType, artifact.getArtifactType());
      assertEquals(guid, artifact.getGuid());
   }

   @Test
   public void testCopyArtifact() throws OseeCoreException {
      when(source.getOrcsData()).thenReturn(artifactData);
      when(dataFactory.copy(branch, artifactData)).thenReturn(otherArtifactData);

      when(source.getAttributes(CoreAttributeTypes.Annotation)).thenAnswer(new ReturnAttribute(attribute));
      when(attribute.getOrcsData()).thenReturn(attributeData);
      when(artifactTypeCache.isValidAttributeType(artifactType, branch, CoreAttributeTypes.Annotation)).thenReturn(true);

      ArgumentCaptor<Artifact> implCapture = ArgumentCaptor.forClass(Artifact.class);

      Artifact actual = artifactFactory.copyArtifact(source, types, branch);

      verify(source, times(0)).getAttributes(CoreAttributeTypes.RelationOrder);
      verify(source, times(0)).getAttributes(CoreAttributeTypes.City);
      verify(source, times(1)).getAttributes(CoreAttributeTypes.Annotation);
      verify(attributeFactory).copyAttribute(eq(attributeData), eq(branch), implCapture.capture());

      Assert.assertTrue(implCapture.getValue().isLoaded());
      Assert.assertTrue(actual == implCapture.getValue());
   }

   @Test
   public void testIntroduceArtifactBranchException() throws OseeCoreException {
      when(source.getBranch()).thenReturn(branch);

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("Source artifact is on the same branch as [" + branch + "]");
      artifactFactory.introduceArtifact(source, branch);
   }

   @Test
   public void testIntroduceArtifact() throws OseeCoreException {
      Branch otherBranch = mock(Branch.class);

      when(source.getBranch()).thenReturn(otherBranch);
      when(source.getOrcsData()).thenReturn(artifactData);

      when(dataFactory.introduce(branch, artifactData)).thenReturn(otherArtifactData);

      when(source.getExistingAttributeTypes()).thenAnswer(new ReturnExistingTypes(types));
      when(source.getAttributes(CoreAttributeTypes.Annotation)).thenAnswer(new ReturnAttribute(attribute));
      when(attribute.getOrcsData()).thenReturn(attributeData);
      when(artifactTypeCache.isValidAttributeType(artifactType, branch, CoreAttributeTypes.Annotation)).thenReturn(true);

      ArgumentCaptor<Artifact> implCapture = ArgumentCaptor.forClass(Artifact.class);

      Artifact actual = artifactFactory.introduceArtifact(source, branch);

      verify(source, times(0)).getAttributes(CoreAttributeTypes.RelationOrder);
      verify(source, times(0)).getAttributes(CoreAttributeTypes.City);
      verify(source, times(1)).getAttributes(CoreAttributeTypes.Annotation);

      verify(attributeFactory).introduceAttribute(eq(attributeData), eq(branch), implCapture.capture());

      Assert.assertTrue(implCapture.getValue().isLoaded());
      Assert.assertTrue(actual == implCapture.getValue());
   }

   @Test
   public void testClone() throws OseeCoreException {
      when(source.getOrcsData()).thenReturn(artifactData);
      when(dataFactory.copy(branch, artifactData)).thenReturn(otherArtifactData);

      when(source.getExistingAttributeTypes()).thenAnswer(new ReturnExistingTypes(types));
      when(source.getAttributes(CoreAttributeTypes.Annotation)).thenAnswer(new ReturnAttribute(attribute));
      when(attribute.getOrcsData()).thenReturn(attributeData);
      when(artifactTypeCache.isValidAttributeType(artifactType, branch, CoreAttributeTypes.Annotation)).thenReturn(true);

      ArgumentCaptor<Artifact> implCapture = ArgumentCaptor.forClass(Artifact.class);

      Artifact actual = artifactFactory.copyArtifact(source, types, branch);

      verify(source, times(0)).getAttributes(CoreAttributeTypes.RelationOrder);
      verify(source, times(0)).getAttributes(CoreAttributeTypes.City);
      verify(source, times(1)).getAttributes(CoreAttributeTypes.Annotation);
      verify(attributeFactory).copyAttribute(eq(attributeData), eq(branch), implCapture.capture());
      Assert.assertTrue(implCapture.getValue().isLoaded());
      Assert.assertTrue(actual == implCapture.getValue());
   }

   private static final class ReturnAttribute implements Answer<List<Attribute<Object>>> {

      private final Attribute<Object> attribute;

      public ReturnAttribute(Attribute<Object> attribute) {
         this.attribute = attribute;
      }

      @Override
      public List<Attribute<Object>> answer(InvocationOnMock invocation) throws Throwable {
         return Collections.singletonList(attribute);
      }
   };

   private static final class ReturnExistingTypes implements Answer<List<IAttributeType>> {

      private final List<IAttributeType> types;

      public ReturnExistingTypes(List<IAttributeType> types) {
         this.types = types;
      }

      @Override
      public List<IAttributeType> answer(InvocationOnMock invocation) throws Throwable {
         return types;
      }
   };
}

Back to the top