Skip to main content
summaryrefslogtreecommitdiffstats
blob: ccd39fe8ad3355d1b39dad3a8ad361e64adcf477 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.define.traceability.blam;

import static org.eclipse.osee.framework.core.enums.DeletionFlag.EXCLUDE_DELETED;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.define.traceability.data.RequirementData;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.util.XResultData;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.plugin.core.util.AIFile;
import org.eclipse.osee.framework.plugin.core.util.OseeData;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.ui.skynet.blam.AbstractBlam;
import org.eclipse.osee.framework.ui.skynet.blam.VariableMap;
import org.eclipse.swt.program.Program;

/**
 * @author Roberto E. Escobar
 */
public class UiToTestBlam extends AbstractBlam {
   private static final String EMPTY_STRING = "";

   @Override
   public String getName() {
      return "UI To Test Report";
   }

   @Override
   public Collection<String> getCategories() {
      return Arrays.asList("Define.Trace");
   }

   @Override
   public String getDescriptionUsage() {
      return "Usage Info here";
   }

   @Override
   public String getXWidgetsXml() {
      StringBuilder builder = new StringBuilder();
      builder.append("<xWidgets>");
      builder.append("<XWidget xwidgetType=\"XFileSelectionDialog\" displayName=\"Select UI List File\" />");
      builder.append("<XWidget xwidgetType=\"XBranchSelectWidget\" displayName=\"Requirements Branch\" />");
      builder.append("</xWidgets>");
      return builder.toString();
   }

   @Override
   public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception {
      String fileName = variableMap.getString("Select UI List File");
      Branch branch = variableMap.getBranch("Requirements Branch");

      if (branch == null) {
         throw new OseeArgumentException("Branch cannot be null");
      }

      XResultData resultData = new XResultData();
      resultData.log("UI To Test Traceability");

      Set<Artifact> toTrace = new HashSet<Artifact>();
      String input = null;

      monitor.beginTask("UI To Test Trace Report", IProgressMonitor.UNKNOWN);
      try {
         monitor.subTask("Gathering Requirements");
         if (Strings.isValid(fileName)) {
            input = getUIsFromFile(monitor, fileName);
         }

         if (Strings.isValid(input)) {
            for (String ui : input.split("\n")) {
               String toGet = ui.trim();
               toTrace.addAll(ArtifactQuery.getArtifactListFromName(toGet, branch, EXCLUDE_DELETED));
            }
         } else {
            RequirementData requirements = new RequirementData(branch);
            requirements.initialize(monitor);
            toTrace.addAll(requirements.getAllSwRequirements());
         }

         StringWriter appendable = new StringWriter();

         monitor.subTask("Gathering Test Unit Trace");

         if (toTrace.isEmpty()) {
            addRow(appendable, "No Requirements found");
         } else {
            addRow(appendable, String.format("[%s] Requirements Found", toTrace.size()));

            List<String> headers = new ArrayList<String>();
            for (Column column : Column.values()) {
               headers.add(column.name());
            }
            addRow(appendable, headers.toArray(new String[headers.size()]));
            for (Artifact requirement : toTrace) {
               processTrace(appendable, requirement, "Verified By", CoreRelationTypes.Verification__Verifier);
               processTrace(appendable, requirement, "Used By", CoreRelationTypes.Uses__TestUnit);
               processTrace(appendable, requirement, "Validated By", CoreRelationTypes.Validation__Validator);
            }
         }

         if (appendable.getBuffer().length() > 0) {
            String outFileName = "UI_To_TestUnit." + Lib.getDateTimeString() + ".csv";
            IFile iFile = OseeData.getIFile(outFileName);
            AIFile.writeToFile(iFile, appendable.toString());
            Program.launch(iFile.getLocation().toOSString());
         }
      } finally {
         monitor.subTask("Done");
         System.gc();
      }
   }

   private void addRow(Appendable appendable, String... data) throws OseeCoreException {
      if (data != null && data.length > 0) {
         try {
            for (int index = 0; index < data.length; index++) {
               appendable.append("\"");
               appendable.append(data[index]);
               appendable.append("\"");
               if (index + 1 < data.length) {
                  appendable.append(",");
               }
            }
            appendable.append("\n");
         } catch (Exception ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
   }

   private String getUIsFromFile(IProgressMonitor monitor, String filePath) throws Exception {
      String input;
      File file = new File(filePath);
      if (file == null || !file.exists()) {
         throw new OseeArgumentException("UI list file not accessible");
      }
      IFileStore fileStore = EFS.getStore(file.toURI());
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, monitor));
         input = Lib.inputStreamToString(inputStream);
      } finally {
         if (inputStream != null) {
            inputStream.close();
         }
      }
      return input;
   }

   private String[] asArray(String... data) {
      String[] toReturn = new String[Column.values().length];
      if (data != null && data.length > 0) {
         for (int index = 0; index < toReturn.length; index++) {
            if (data.length > index) {
               toReturn[index] = data[index];
            }
         }
      }
      return toReturn;
   }

   private void processTrace(Appendable appendable, Artifact requirement, String testType, IRelationTypeSide relationType) throws OseeCoreException {
      String uiTitle = requirement.getName();
      String uiType = requirement.getArtifactTypeName();

      List<Artifact> testUnits = requirement.getRelatedArtifacts(relationType);
      if (testUnits.isEmpty()) {
         addRow(appendable,
            asArray(uiTitle, uiType, "NONE", "NONE", EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING));
      } else {
         for (Artifact testUnit : testUnits) {
            String testUnitName = testUnit.getName();
            String testUnitType = testUnit.getArtifactTypeName();
            List<String> verified = getTrace(testUnit, CoreRelationTypes.Verification__Requirement);
            List<String> used = getTrace(testUnit, CoreRelationTypes.Uses__Requirement);
            List<String> validates = getTrace(testUnit, CoreRelationTypes.Validation__Requirement);
            String verifyStr = org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", verified);
            String usesStr = org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", used);
            String validatesStr = org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", validates);

            addRow(appendable,
               asArray(uiTitle, uiType, testType, testUnitName, testUnitType, verifyStr, validatesStr, usesStr));
         }
      }
   }

   private List<String> getTrace(Artifact testUnit, IRelationTypeSide relation) throws OseeCoreException {
      List<String> toReturn = new ArrayList<String>();
      for (Artifact item : testUnit.getRelatedArtifacts(relation)) {
         toReturn.add(item.getName());
      }
      if (toReturn.isEmpty()) {
         toReturn.add("NONE");
      }
      return toReturn;
   }

   private static enum Column {
      UI_Title,
      Requirement_Type,
      Relates_To_Test_Unit,
      Test_Unit_Name,
      Test_Unit_Type,
      Verified_By,
      Validated_By,
      Used_By;
   }

}

Back to the top