Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e7018e76ecadc5aff4c06216b476934909d39e1 (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
/*******************************************************************************
 * 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.branch;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.ITransaction;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.TransactionCache;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.CreateBranchData;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**  */
public class BranchDataFactoryTest {

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

   // @formatter:off
   @Mock private BranchCache branchCache;
   @Mock private TransactionCache txCache;
   
   @Mock private IOseeBranch branch;
   @Mock private IOseeBranch parentToken;
   @Mock private Branch parent;
   @Mock private ArtifactReadable author;
   @Mock private ArtifactReadable associatedArtifact;
   @Mock private TransactionRecord txRecord;
   // @formatter:on

   private BranchDataFactory factory;

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

      factory = new BranchDataFactory(branchCache, txCache);
   }

   @Test
   public void testDataForTopLevelBranch() throws OseeCoreException {
      String branchName = "testDataForTopLevelBranch";
      String guid = GUID.create();
      when(branch.getName()).thenReturn(branchName);
      when(branch.getGuid()).thenReturn(guid);

      when(branchCache.getSystemRootBranch()).thenReturn(parent);
      when(txCache.getHeadTransaction(parent)).thenReturn(txRecord);

      CreateBranchData result = factory.createTopLevelBranchData(branch, author);

      verify(branchCache).getSystemRootBranch();
      verify(txCache).getHeadTransaction(parent);

      String comment = "Branch Creation for " + branchName;
      assertData(result, branchName, guid, BranchType.BASELINE, comment, txRecord, author, null, false);
   }

   @Test
   public void testDataForBaselineBranch() throws OseeCoreException {
      String branchName = "testDataForBaselineBranch";
      String guid = GUID.create();
      when(branch.getName()).thenReturn(branchName);
      when(branch.getGuid()).thenReturn(guid);

      when(branchCache.get(parentToken)).thenReturn(parent);
      when(txCache.getHeadTransaction(parent)).thenReturn(txRecord);

      CreateBranchData result = factory.createBaselineBranchData(branch, author, parentToken, associatedArtifact);

      verify(branchCache).get(parentToken);
      verify(txCache).getHeadTransaction(parent);

      String comment = "Branch Creation for " + branchName;
      assertData(result, branchName, guid, BranchType.BASELINE, comment, txRecord, author, associatedArtifact, false);
   }

   @Test
   public void testDataForWorkingBranch() throws OseeCoreException {
      String branchName = "testDataForWorkingBranch";
      String parentName = "testParentBranchName";
      String guid = GUID.create();
      when(branch.getName()).thenReturn(branchName);
      when(branch.getGuid()).thenReturn(guid);

      when(parent.getName()).thenReturn(parentName);

      when(branchCache.get(parentToken)).thenReturn(parent);
      when(txCache.getHeadTransaction(parent)).thenReturn(txRecord);

      CreateBranchData result = factory.createWorkingBranchData(branch, author, parentToken, associatedArtifact);

      verify(branchCache).get(parentToken);
      verify(txCache).getHeadTransaction(parent);

      String comment = String.format("New Branch from %s (%s)", parentName, txRecord.getId());
      assertData(result, branchName, guid, BranchType.WORKING, comment, txRecord, author, associatedArtifact, false);
   }

   @Test
   public void testDataForCopyTxBranch() throws OseeCoreException {
      String branchName = "testDataForCopyTxBranch";
      String parentName = "testParentBranchName";
      String guid = GUID.create();
      when(branch.getName()).thenReturn(branchName);
      when(branch.getGuid()).thenReturn(guid);

      when(parent.getName()).thenReturn(parentName);

      when(txCache.getOrLoad(99)).thenReturn(txRecord);
      when(txRecord.getBranch()).thenReturn(parent);

      ITransaction tx = TokenFactory.createTransaction(99);

      CreateBranchData result = factory.createCopyTxBranchData(branch, author, tx, null);

      verify(txCache).getOrLoad(99);
      verify(txRecord).getBranch();

      String comment = String.format("Transaction %d copied from %s to create Branch %s", 99, parentName, branchName);
      assertData(result, branchName, guid, BranchType.WORKING, comment, txRecord, author, null, true);
   }

   @Test
   public void testDataForPortBranch() throws OseeCoreException {
      String branchName = "testDataForPortBranch";
      String parentName = "testParentBranchName";
      String guid = GUID.create();
      when(branch.getName()).thenReturn(branchName);
      when(branch.getGuid()).thenReturn(guid);

      when(parent.getName()).thenReturn(parentName);

      when(txCache.getOrLoad(99)).thenReturn(txRecord);
      when(txRecord.getBranch()).thenReturn(parent);

      ITransaction tx = TokenFactory.createTransaction(99);

      CreateBranchData result = factory.createPortBranchData(branch, author, tx, null);

      verify(txCache).getOrLoad(99);
      verify(txRecord).getBranch();

      String comment = String.format("Transaction %d ported from %s to create Branch %s", 99, parentName, branchName);
      assertData(result, branchName, guid, BranchType.PORT, comment, txRecord, author, null, true);
   }

   private static void assertData(CreateBranchData actual, String branchName, String branchGuid, BranchType type, String comment, TransactionRecord fromTx, ArtifactReadable author, ArtifactReadable associatedArtifact, boolean isCopyFromTx) {
      assertEquals(branchName, actual.getName());
      assertEquals(branchGuid, actual.getGuid());

      assertEquals(type, actual.getBranchType());
      assertEquals(comment, actual.getCreationComment());
      assertEquals(fromTx, actual.getFromTransaction());

      assertEquals(-1, actual.getMergeAddressingQueryId());
      assertEquals(-1, actual.getMergeDestinationBranchId());

      assertEquals(author, actual.getUserArtifact());
      assertEquals(author.getLocalId(), actual.getUserArtifactId());

      assertEquals(associatedArtifact, actual.getAssociatedArtifact());

      int assocArtId = associatedArtifact == null ? -1 : associatedArtifact.getLocalId();
      assertEquals(assocArtId, actual.getAssociatedArtifactId());

      assertEquals(isCopyFromTx, actual.isTxCopyBranchType());
   }
}

Back to the top