Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed92a9d90c0af9f38c8f3e3d383d6f09e4410224 (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
/*******************************************************************************
 * 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.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.data.TransactionId;
import org.eclipse.osee.framework.core.data.TransactionToken;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.data.CreateBranchData;
import org.eclipse.osee.orcs.search.BranchQuery;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.eclipse.osee.orcs.search.TransactionQuery;
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 BranchQuery branchQuery;
   @Mock private TransactionQuery txQuery;
   @Mock private QueryFactory queryFactory;
   @Mock private ResultSet<TransactionToken> results;
   @Mock private ResultSet<BranchReadable> branchResults;
   @Mock private BranchReadable parentBranch;
   // @formatter:on

   private final ArtifactId associatedArtifact = ArtifactId.valueOf(66);
   private final ArtifactId author = ArtifactId.valueOf(55);
   private final TransactionToken txRecord = TransactionToken.valueOf(99, parentBranch);
   private BranchDataFactory factory;

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

      when(queryFactory.transactionQuery()).thenReturn(txQuery);
      when(queryFactory.branchQuery()).thenReturn(branchQuery);
      factory = new BranchDataFactory(queryFactory);

      when(branchQuery.andIds(txRecord.getBranch())).thenReturn(branchQuery);
      when(branchQuery.getResults()).thenReturn(branchResults);
      when(branchResults.getExactlyOne()).thenReturn(parentBranch);

      when(txQuery.andIsHead(any(BranchId.class))).thenReturn(txQuery);
      when(txQuery.getTokens()).thenReturn(results);
      when(results.getExactlyOne()).thenReturn(txRecord);

      when(txQuery.andTxId(txRecord)).thenReturn(txQuery);

      when(parentBranch.getName()).thenReturn("testParentBranchName");
      when(parentBranch.getId()).thenReturn(44L);
   }

   @Test
   public void testDataForTopLevelBranch() throws OseeCoreException {
      IOseeBranch branch = TokenFactory.createBranch("testDataForTopLevelBranch");
      CreateBranchData result = factory.createTopLevelBranchData(branch, author);

      verify(txQuery).andIsHead(CoreBranches.SYSTEM_ROOT);

      String comment = String.format("New Branch from %s (%s)", CoreBranches.SYSTEM_ROOT.getName(), txRecord);
      assertData(result, branch.getName(), branch.getId(), BranchType.BASELINE, comment, txRecord, author,
         ArtifactId.SENTINEL, false);
   }

   @Test
   public void testDataForBaselineBranch() throws OseeCoreException {
      IOseeBranch branch = TokenFactory.createBranch("testDataForBaselineBranch");
      CreateBranchData result = factory.createBaselineBranchData(branch, author, parentBranch, associatedArtifact);

      verify(txQuery).andIsHead(parentBranch);

      String comment = String.format("New Branch from %s (%s)", parentBranch.getName(), txRecord.getId());
      assertData(result, branch.getName(), branch.getId(), BranchType.BASELINE, comment, txRecord, author,
         associatedArtifact, false);
   }

   @Test
   public void testDataForWorkingBranch() throws OseeCoreException {
      IOseeBranch branch = TokenFactory.createBranch("testDataForWorkingBranch");

      CreateBranchData result = factory.createWorkingBranchData(branch, author, parentBranch, associatedArtifact);
      verify(txQuery).andIsHead(parentBranch);

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

   @Test
   public void testDataForCopyTxBranch() throws OseeCoreException {
      IOseeBranch branch = TokenFactory.createBranch("testDataForCopyTxBranch");

      CreateBranchData result = factory.createCopyTxBranchData(branch, author, txRecord, ArtifactId.SENTINEL);

      verify(txQuery).andTxId(txRecord);
      verify(branchQuery).andIds(txRecord.getBranch());

      String comment = String.format("Transaction %d copied from %s to create Branch %s", txRecord.getId(),
         parentBranch.getName(), branch.getName());
      assertData(result, branch.getName(), branch.getId(), BranchType.WORKING, comment, txRecord, author,
         ArtifactId.SENTINEL, true);
   }

   @Test
   public void testDataForPortBranch() throws OseeCoreException {
      IOseeBranch branch = TokenFactory.createBranch("testDataForPortBranch");

      CreateBranchData result = factory.createPortBranchData(branch, author, txRecord, ArtifactId.SENTINEL);

      verify(txQuery).andTxId(txRecord);
      verify(branchQuery).andIds(txRecord.getBranch());

      String comment = String.format("Transaction %d ported from %s to create Branch %s", txRecord.getId(),
         parentBranch.getName(), branch.getName());
      assertData(result, branch.getName(), branch.getId(), BranchType.PORT, comment, txRecord, author,
         ArtifactId.SENTINEL, true);
   }

   private static void assertData(CreateBranchData actual, String branchName, Long branchUuid, BranchType type, String comment, TransactionId fromTx, ArtifactId author, ArtifactId associatedArtifact, boolean isCopyFromTx) {
      assertEquals(branchName, actual.getName());
      assertEquals(branchUuid, actual.getGuid());

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

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

      assertEquals(author, actual.getAuthor());
      assertEquals(associatedArtifact, actual.getAssociatedArtifact());

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

Back to the top