Skip to main content
summaryrefslogtreecommitdiffstats
blob: 96df447014b35564d4e462276da8269afc533c9c (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
/*******************************************************************************
 * 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.client.integration.tests.integration.define;

import static org.eclipse.osee.client.demo.DemoBranches.SAW_Bld_1;
import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.client.test.framework.OseeLogMonitorRule;
import org.eclipse.osee.define.blam.operation.FixAttributeOperation;
import org.eclipse.osee.define.blam.operation.FixAttributeOperation.Display;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.conflict.ConflictManagerExternal;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.httpRequests.PurgeBranchHttpRequestOperation;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
 * @author Angel Avila
 */

public class FixAttributeOperationTest {

   @Rule
   public OseeClientIntegrationRule integration = new OseeClientIntegrationRule(OSEE_CLIENT_DEMO);

   @Rule
   public OseeLogMonitorRule monitorRule = new OseeLogMonitorRule();

   private static final String WORKING_BRANCH_NAME = "BranchWorking";

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

   // @formatter:off
   @Mock private OperationLogger logger;
   @Mock private Display display;
 
   @Captor private ArgumentCaptor<List<String[]>> captor; 
   @Captor private ArgumentCaptor<List<String[]>> captor2; 
  
   // @formatter:on

   private Branch branchWorking;
   private String itemId;

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

      branchWorking = BranchManager.createWorkingBranch(SAW_Bld_1, WORKING_BRANCH_NAME);
      Branch branch1 = editBranch(branchWorking, "branch1");
      Branch branch2 = editBranch(branchWorking, "branch2");

      commit(branch1, branchWorking);
      commit(branch2, branchWorking);
   }

   @After
   public void tearDown() throws OseeCoreException {
      boolean isPending = OseeEventManager.getPreferences().isPendRunning();
      try {
         OseeEventManager.getPreferences().setPendRunning(true);
         Operations.executeWorkAndCheckStatus(new PurgeBranchHttpRequestOperation(branchWorking, true));
      } finally {
         OseeEventManager.getPreferences().setPendRunning(isPending);
      }
   }

   @Test
   public void testNullBranchCheck() throws OseeCoreException {
      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("branch cannot be null");

      executeOp(null, false);
   }

   @Test
   public void testNonWorkingBranchCheck() throws OseeCoreException {
      Branch mockBranch = Mockito.mock(Branch.class);
      when(mockBranch.toString()).thenReturn("mock branch");
      when(mockBranch.getBranchType()).thenReturn(BranchType.BASELINE);

      thrown.expect(OseeArgumentException.class);
      thrown.expectMessage("Invalid branch selected [mock branch]. Only working branches are allowed.");

      executeOp(mockBranch, false);
   }

   @Test
   public void testDetectDuplicatesButDontFix() throws OseeCoreException {
      // test multiple runs without committing fixes
      for (int i = 0; i < 2; i++) {
         reset(display);

         executeOp(branchWorking, false);

         verify(display).displayReport(eq("Fix Duplicate Report"), captor.capture());

         List<String[]> data = captor.getValue();
         assertRow(data, 0, branchWorking.getName(), itemId, "Robot API", CoreAttributeTypes.Partition.getName(),
            "Unspecified, Navigation, Navigation", "Unspecified, Navigation");

         Artifact testRobotAPI =
            ArtifactQuery.getArtifactFromTypeAndName(CoreArtifactTypes.SoftwareRequirement, "Robot API", branchWorking);

         List<String> values = testRobotAPI.getAttributesToStringList(CoreAttributeTypes.Partition);
         Collections.sort(values);
         assertEquals("Navigation", values.get(0));
         assertEquals("Navigation", values.get(1));
         assertEquals("Unspecified", values.get(2));
      }
   }

   @Test
   public void testTestFix() throws OseeCoreException {
      executeOp(branchWorking, true);

      verify(display).displayReport(eq("Fix Duplicate Report"), captor.capture());

      //@formatter:off
      assertRow(captor.getValue(), 0, branchWorking.getName(), itemId, "Robot API", CoreAttributeTypes.Partition.getName(), "Unspecified, Navigation, Navigation", "Unspecified, Navigation");
      //@formatter: on
      
      Artifact testRobotAPI =
         ArtifactQuery.getArtifactFromTypeAndName(CoreArtifactTypes.SoftwareRequirement, "Robot API", branchWorking);

      List<String> values = testRobotAPI.getAttributesToStringList(CoreAttributeTypes.Partition);
      Collections.sort(values);
      assertEquals("Navigation", values.get(0)); 
      assertEquals("Unspecified", values.get(1)); 
      
      // Run Again Empty Report should result
      reset(display);
      executeOp(branchWorking, true);

      verify(display).displayReport(eq("Fix Duplicate Report"), captor2.capture());
      
      String expectedString = "-- no duplicates found --";
      assertRow(captor2.getValue(), 0, expectedString, expectedString, expectedString, expectedString, expectedString, expectedString);      
   }

   private static void assertRow(List<String[]> data, int index, String... expecteds) {
      assertNotNull(data);
      assertTrue(data.size() > index);
      String[] actuals = data.get(index);
      assertEquals(expecteds.length, actuals.length);

      for (int i = 0; i < expecteds.length; i++) {
         assertEquals(expecteds[i], actuals[i]);
      }
   }

   private void executeOp(Branch branch, boolean commitChangesBool) throws OseeCoreException {
      IOperation operation = new FixAttributeOperation(logger, display, branch, commitChangesBool);
      Operations.executeWorkAndCheckStatus(operation);
   }
   
   private Branch editBranch(Branch parentBranch, String workingBranchName) throws OseeCoreException {
      String branchName = String.format("%s_%s", FixAttributeOperationTest.class.getSimpleName(), workingBranchName);
      Branch branch = BranchManager.createWorkingBranch(parentBranch, branchName);

      Artifact robotAPI =
         ArtifactQuery.getArtifactFromTypeAndName(CoreArtifactTypes.SoftwareRequirement, "Robot API", branch);
      robotAPI.addAttribute(CoreAttributeTypes.Partition, "Navigation");

      itemId = robotAPI.getGuid();

      SkynetTransaction transaction = TransactionManager.createTransaction(branch, "Adding Attribute");
      transaction.addArtifact(robotAPI);
      transaction.execute();
      return branch;
   }

   private void commit(Branch source, Branch destination) throws OseeCoreException {
      boolean archiveSourceBranch = false;
      boolean overwriteUnresolvedConflicts = true;
      ConflictManagerExternal conflictManager = new ConflictManagerExternal(destination, source);
      BranchManager.commitBranch(new NullProgressMonitor(), conflictManager, archiveSourceBranch,
         overwriteUnresolvedConflicts);
   }
}

Back to the top