Skip to main content
summaryrefslogtreecommitdiffstats
blob: 10866ffb53989b6e16fb18966d6711fb053315c9 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*******************************************************************************
 * 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.transaction;

import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.HardwareRequirement;
import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.SoftwareRequirement;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.Active;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.Annotation;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.Company;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.FavoriteBranch;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.Name;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.PlainTextContent;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.QualificationMethod;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.RelationOrder;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.WordTemplateContent;
import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import static org.eclipse.osee.framework.core.enums.RelationSorter.LEXICOGRAPHICAL_ASC;
import static org.eclipse.osee.framework.core.enums.RelationSorter.LEXICOGRAPHICAL_DESC;
import static org.eclipse.osee.framework.core.enums.CoreRelationTypes.DEFAULT_HIERARCHY;
import static org.eclipse.osee.framework.core.enums.RelationSide.SIDE_B;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.data.RelationTypeSide;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
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.KeyValueOps;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.internal.artifact.Artifact;
import org.eclipse.osee.orcs.core.internal.attribute.Attribute;
import org.eclipse.osee.orcs.core.internal.search.QueryModule;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.data.TransactionReadable;
import org.eclipse.osee.orcs.search.QueryBuilder;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.Mockito;

/**
 * Test Case for {@link TransactionFactoryImpl}
 *
 * @author Roberto E. Escobar
 */
public class TransactionBuilderImplTest {

   private static final IRelationType TYPE_1 = TokenFactory.createRelationType(123456789L, "TYPE_1");
   @Rule
   public ExpectedException thrown = ExpectedException.none();

   // @formatter:off
   @Mock private Log logger;
   @Mock private OrcsSession session;
   @Mock private TxDataManager txDataManager;
   @Mock private TxCallableFactory txCallableFactory;
   @Mock private QueryFactory queryFactory;
   @Mock private QueryBuilder builder;
   @Mock private QueryModule query;
   @Mock private KeyValueOps keyValueOps;

   @Mock private ArtifactReadable expectedAuthor;
   @Mock private ArtifactReadable expectedDestination;
   @Mock private ArtifactReadable node1;
   @Mock private ArtifactReadable node2;
   @Mock private Artifact artifact;
   @Mock private Artifact artifact2;
   @SuppressWarnings("rawtypes")
   @Mock private AttributeReadable attrId;
   @SuppressWarnings("rawtypes")
   @Mock private Attribute attribute;

   @Mock private TxData txData;
   // @formatter:on

   private TransactionBuilderImpl factory;
   private String guid;

   @SuppressWarnings("unchecked")
   @Before
   public void init() throws OseeCoreException {
      initMocks(this);
      guid = GUID.create();
      factory = new TransactionBuilderImpl(txCallableFactory, txDataManager, txData, query, keyValueOps);

      when(attrId.getLocalId()).thenReturn(12345);
      when(txDataManager.getForWrite(txData, expectedAuthor)).thenReturn(artifact);
      when(artifact.getAttributeById(attrId.getLocalId())).thenReturn(attribute);
      when(query.createQueryFactory(session)).thenReturn(queryFactory);
      when(expectedAuthor.getBranch()).thenReturn(COMMON);
      when(txData.getBranch()).thenReturn(COMMON);
   }

   @Test
   public void testGetComment() {
      when(factory.getComment()).thenReturn("This is a comment");
      String comment = factory.getComment();
      assertEquals(comment, "This is a comment");
      verify(txData).getComment();
   }

   public void testSetAuthor() throws OseeCoreException {
      factory.setAuthor(expectedAuthor);
      verify(txDataManager).setAuthor(txData, expectedAuthor);
   }

   @Test
   public void testCreateArtifact() throws OseeCoreException {
      factory.createArtifact(SoftwareRequirement, "Software Requirement");

      verify(txDataManager).createArtifact(txData, SoftwareRequirement, "Software Requirement", null);
   }

   @Test
   public void testCreateArtifactWithGuid() throws OseeCoreException {
      factory.createArtifact(HardwareRequirement, "Hardware Requirement", guid);

      verify(txDataManager).createArtifact(txData, HardwareRequirement, "Hardware Requirement", guid);
   }

   @Test
   public void testCopyArtifact() throws OseeCoreException {
      factory.copyArtifact(expectedAuthor);
      verify(txDataManager).copyArtifact(txData, COMMON, expectedAuthor);
   }

   @Test
   public void testCopyArtifactWithList() throws OseeCoreException {
      Collection<? extends IAttributeType> attributesToDuplicate = Arrays.asList(Name, Annotation);

      factory.copyArtifact(expectedAuthor, attributesToDuplicate);

      verify(txDataManager).copyArtifact(txData, COMMON, expectedAuthor, attributesToDuplicate);
   }

   @Test
   public void testIntroduceArtifactBranchException() throws OseeCoreException {
      when(txData.isOnBranch(COMMON)).thenReturn(true);

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("Source branch is same branch as transaction branch[" + COMMON + "]");
      factory.introduceArtifact(COMMON, expectedAuthor);
   }

   @Test
   public void testIntroduceArtifact() throws OseeCoreException {
      when(query.createQueryFactory(any())).thenReturn(queryFactory);
      when(queryFactory.fromBranch(COMMON)).thenReturn(builder);
      when(builder.includeDeletedArtifacts()).thenReturn(builder);
      when(builder.andId(any())).thenReturn(builder);

      ResultSet<ArtifactReadable> source = ResultSets.singleton(expectedAuthor);
      when(builder.getResults()).thenReturn(source);

      factory.introduceArtifact(COMMON, expectedAuthor);

      verify(txDataManager).introduceArtifact(txData, COMMON, expectedAuthor, expectedAuthor);
   }

   @Test
   public void testCreateAttribute() throws OseeCoreException {
      factory.createAttribute(expectedAuthor, QualificationMethod);

      verify(artifact).createAttribute(QualificationMethod);
   }

   @Test
   public void testCreateAttributeWithValue() throws OseeCoreException {
      factory.createAttribute(expectedAuthor, QualificationMethod, "Demonstration");

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).createAttribute(QualificationMethod, "Demonstration");
   }

   @Test
   public void testCreateAttributeFromString() throws OseeCoreException {
      factory.createAttributeFromString(expectedAuthor, WordTemplateContent, "This is my word template content");

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).createAttributeFromString(CoreAttributeTypes.WordTemplateContent,
         "This is my word template content");
   }

   @Test
   public void testSetSoleAttributeValue() throws OseeCoreException {
      factory.setSoleAttributeValue(expectedAuthor, RelationOrder, LEXICOGRAPHICAL_DESC);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setSoleAttributeValue(RelationOrder, LEXICOGRAPHICAL_DESC);
   }

   @Test
   public void testSetSoleAttributeFromStream() throws OseeCoreException {
      InputStream inputStream = Mockito.mock(InputStream.class);

      factory.setSoleAttributeFromStream(expectedAuthor, Company, inputStream);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setSoleAttributeFromStream(Company, inputStream);
   }

   @Test
   public void testSetSoleAttributeFromString() throws OseeCoreException {
      factory.setSoleAttributeFromString(expectedAuthor, Name, "Name");

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setSoleAttributeFromString(Name, "Name");
   }

   @Test
   public void testSetAttributesFromValues() throws OseeCoreException {
      factory.setAttributesFromValues(expectedAuthor, PlainTextContent, Arrays.asList(true, true, false));

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setAttributesFromValues(PlainTextContent, Arrays.asList(true, true, false));
   }

   @SuppressWarnings("unchecked")
   @Test
   public void testSetAttributesFromValuesList() throws OseeCoreException {
      factory.setAttributesFromValues(expectedAuthor, CoreAttributeTypes.StaticId, Collections.EMPTY_LIST);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setAttributesFromValues(CoreAttributeTypes.StaticId, Collections.EMPTY_LIST);
   }

   @Test
   public void testSetAttributesFromStrings() throws OseeCoreException {
      factory.setAttributesFromStrings(expectedAuthor, PlainTextContent, Arrays.asList("one", "two", "three"));

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setAttributesFromStrings(PlainTextContent, Arrays.asList("one", "two", "three"));
   }

   @SuppressWarnings("unchecked")
   @Test
   public void testSetAttributesFromStringList() throws OseeCoreException {
      factory.setAttributesFromStrings(expectedAuthor, PlainTextContent, Collections.EMPTY_LIST);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).setAttributesFromStrings(PlainTextContent, Collections.EMPTY_LIST);
   }

   @SuppressWarnings("unchecked")
   @Test
   public void testSetAttributeByIdFromValue() throws OseeCoreException {
      factory.setAttributeById(expectedAuthor, attrId, false);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(attribute).setValue(false);
   }

   @Test
   public void testSetAttributeByIdFromString() throws OseeCoreException {
      factory.setAttributeById(expectedAuthor, attrId, "value");

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(attribute).setFromString("value");
   }

   @Test
   public void testSetAttributeById() throws OseeCoreException {
      InputStream inputStream = Mockito.mock(InputStream.class);

      factory.setAttributeById(expectedAuthor, attrId, inputStream);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(attribute).setValueFromInputStream(inputStream);
   }

   @Test
   public void testDeleteByAttributeId() throws OseeCoreException {
      factory.deleteByAttributeId(expectedAuthor, attrId);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(attribute).delete();
   }

   @Test
   public void testDeleteSoleAttribute() throws OseeCoreException {
      factory.deleteSoleAttribute(expectedAuthor, Name);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).deleteSoleAttribute(Name);
   }

   @Test
   public void testDeleteAttributes() throws OseeCoreException {
      factory.deleteAttributes(expectedAuthor, FavoriteBranch);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).deleteAttributes(FavoriteBranch);
   }

   @Test
   public void testDeleteAttributesWithValue() throws OseeCoreException {
      factory.deleteAttributesWithValue(expectedAuthor, Active, true);

      verify(txDataManager).getForWrite(txData, expectedAuthor);
      verify(artifact).deleteAttributesWithValue(Active, true);
   }

   @Test
   public void testDeleteArtifact() throws OseeCoreException {
      factory.deleteArtifact(expectedAuthor);

      verify(txDataManager).deleteArtifact(txData, expectedAuthor);
   }

   @Test
   public void testIsCommitInProgress() {
      when(factory.isCommitInProgress()).thenReturn(false);

      boolean condition = factory.isCommitInProgress();

      assertFalse(condition);
   }

   @SuppressWarnings("unchecked")
   @Test
   public void testCommit() throws Exception {
      CancellableCallable<TransactionReadable> callable = mock(CancellableCallable.class);
      TransactionReadable tx = mock(TransactionReadable.class);

      when(txCallableFactory.createTx(txData)).thenReturn(callable);
      when(callable.call()).thenReturn(tx);

      factory.commit();
      verify(txCallableFactory).createTx(txData);
   }

   @Test
   public void testCommitException() throws OseeCoreException {
      Exception exception = new IllegalStateException("onCommit Exception");

      doThrow(exception).when(txCallableFactory).createTx(txData);

      thrown.expect(Exception.class);
      factory.commit();
   }

   @Test
   public void testSetRationale() throws OseeCoreException {
      factory.setRationale(node1, DEFAULT_HIERARCHY, node2, "This is my rationale");

      verify(txDataManager).setRationale(txData, node1, DEFAULT_HIERARCHY, node2, "This is my rationale");
   }

   @Test
   public void testAddChildren() throws OseeCoreException {
      List<ArtifactReadable> list = Arrays.asList(node1, node2);

      factory.addChildren(expectedAuthor, list);

      verify(txDataManager).addChildren(txData, expectedAuthor, list);
   }

   @Test
   public void TestAddChildrenAsList() throws OseeCoreException {
      Iterable<? extends ArtifactId> children = Collections.emptyList();
      factory.addChildren(expectedAuthor, children);
      verify(txDataManager).addChildren(txData, expectedAuthor, children);
   }

   @Test
   public void testRelate() throws OseeCoreException {
      factory.relate(node1, DEFAULT_HIERARCHY, node2);
      verify(txDataManager).relate(txData, node1, DEFAULT_HIERARCHY, node2);
   }

   @Test
   public void testRelateWithOrder() throws OseeCoreException {
      factory.relate(node1, DEFAULT_HIERARCHY, node2, LEXICOGRAPHICAL_ASC);
      verify(txDataManager).relate(txData, node1, DEFAULT_HIERARCHY, node2, LEXICOGRAPHICAL_ASC);
   }

   @Test
   public void testSetRelations() throws OseeCoreException {
      Iterable<? extends ArtifactId> artBs = Collections.emptyList();
      factory.setRelations(node1, DEFAULT_HIERARCHY, artBs);

      verify(txDataManager).setRelations(txData, node1, DEFAULT_HIERARCHY, artBs);
   }

   @Test
   public void testUnrelateWithAandB() throws OseeCoreException {
      factory.unrelate(node1, TYPE_1, node2);
      verify(txDataManager).unrelate(txData, node1, TYPE_1, node2);
   }

   @Test
   public void testUnrelateFromAllWithSide() throws OseeCoreException {
      RelationTypeSide asTypeSide = RelationTypeSide.create(TYPE_1, SIDE_B);
      factory.unrelateFromAll(asTypeSide, expectedAuthor);
      verify(txDataManager).unrelateFromAll(txData, TYPE_1, expectedAuthor, SIDE_B);
   }

   @Test
   public void testUnrelateFromAll() throws OseeCoreException {
      factory.unrelateFromAll(expectedAuthor);
      verify(txDataManager).unrelateFromAll(txData, expectedAuthor);
   }

}

Back to the top