Friday 7 June 2013

How To Remove Attachments Of Test Cases From HP QC 11 TestLab Folder Using Java




  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
package qc_field_clear;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import OTAClient.ClassFactory;
import OTAClient.IAttachmentFactory;
import OTAClient.IBaseFactory;
import OTAClient.IList;
import OTAClient.ITDConnection;
import OTAClient.ITSTest;
import OTAClient.ITestSet;
import OTAClient.ITestSetFolder;
import OTAClient.ITestSetTreeManager;

//import com.mercury.qualitycenter.otaclient.*;
import com4j.*;
//import com.tszadel.qctools4j.exception.QcException;

/******************************************************************************************************************************************
 *
 * @author PAVANGU
 *****************************************************************************************************************************************/
public class QCUsingOTA {
 ITDConnection itdc;
 
 /****************************************************************************************************************************************
  * This function will update <b>Ticket Type</b> field in QC with respective value of JIRA ticket status from given
  * defectMap
  * @param qcTestLabPath: Path of the folder in QC(TestLab Tab) like String path = "Root\\Project 2012\\07/10 | Wireless CTN Capture on Urock"
  * @param defectMap: JIRA ticket's & thier status From JIRA Database, same status is need to be updated in QC Ticket Type field
  *****************************************************************************************************************************************/
 public int updateTestCasesFieldsInQCTestLab(String qcTestLabPath)
    {
  int noOfTCUpdated = 0;
  ITestSet ts;
  ITSTest tc;  
  IList tsList;          /* To store list of TestSet's(including nested folders) of a Folder */
  IList tcList;     /* To store list of TestCases in a TestSet  */ 
  IBaseFactory tFactory;              /* Contains Test Cases in particular TestSet */
  ITestSetTreeManager tstm =  (itdc.testSetTreeManager()).queryInterface(ITestSetTreeManager.class); 
  ITestSetFolder tsFolder = ((tstm.nodeByPath(qcTestLabPath)).queryInterface(ITestSetFolder.class));
  tsList = (tsFolder.findTestSets("",false,"")).queryInterface(IList.class);
  //ITestSetFactory tsFactory = (tsFolder.testSetFactory()).queryInterface(ITestSetFactory.class);
  System.out.println("Updating Following TestSets in QC...");
  System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------");
  for(Com4jObject objTestSet:tsList)
  {
   
    ts = objTestSet.queryInterface(ITestSet.class);
    tFactory = (ts.tsTestFactory()).queryInterface(IBaseFactory.class);
////////////////////////////////////////////////////////////////////////////////////
//          TS_STATUS ,TS_EXEC_STATUS 
//   ITDFilter itf = (tFactory.filter()).queryInterface(ITDFilter.class);
//   itf.fields().add("TS_EXEC_STATUS");   
//   itf.text("Out of Scope");
//   itf.filter("TS_EXEC_STATUS");
//   itf.filter("TS_EXEC_STATUS","Out of Scope");
//   tcList = itf.newList();
/////////////////////////////////////////////////////////////////////////////////////   
    tcList = tFactory.newList("");   
    System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=        "+ ts.name() +"        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    System.out.println();
    for(Com4jObject objTest:tcList)
    {
     try
     {       
      tc = objTest.queryInterface(ITSTest.class); 
      
      if(tc.status().equals("No Run"))   /*  Clear the field only for No Run TC's */
      {
     //  tc.lockObject();    
       tc.field("TC_USER_01",null);        // Order ID
       tc.field("TC_USER_02",null);        // JIRA Ticket
       tc.field("TC_USER_03",null);        // Ticket Type
       tc.field("TC_USER_04",null);        // Browser / OS
       tc.field("TC_ACTUAL_TESTER",null);       // Tester    
       
       
       //tc.field("TC_USER_03", defectMap.get(tc.field("TC_USER_02")));                    //Putting New Value in TicketType field of QC taking Status from HashMap
         
       tc.post();
       noOfTCUpdated = noOfTCUpdated + 1;
       System.out.println(tc.status());  
       System.out.println(noOfTCUpdated +") "+ tc.field("TC_USER_03") +":  "+ tc.name());
      }
     }
     catch (Exception e) {
      // TODO Auto-generated catch block
      System.out.println("Exception occured: :(");
      e.printStackTrace();
      continue;
     }
    }
    System.out.println();  
  }
  System.out.println("****************************************************************************************************************************************");
  System.out.println("JIRA Ticket's status is updated in QC "); 
  return(noOfTCUpdated);
    }
  
 /******************************************************************************************************************************************
  * This Function will return the Folder Id for QC(TestLab) Path
  * @param path
  * @return Folder ID of folder from QC which is same as DB folder ID
  *****************************************************************************************************************************************/
 public int getFolderID(String path)
 {  
//  int folderID = 0;  
  ITestSetTreeManager tstm =  (itdc.testSetTreeManager()).queryInterface(ITestSetTreeManager.class); 
  ITestSetFolder tsFolder = ((tstm.nodeByPath(path)).queryInterface(ITestSetFolder.class));
  return (tsFolder.nodeID());
 }
  
 /******************************************************************************************************************************************
  * To disconnect the project which was connected to QC Using OTA
  *****************************************************************************************************************************************/
 public void DisconnectQC()
 {
  itdc.disconnectProject();
  System.out.println("QC Using OTA Disconnected");
 }
 
 /******************************************************************************************************************************************
  *
  * @throws QcException
  *****************************************************************************************************************************************/
    public void ConnectQC()
    {
    // TODO Auto-generated method stub
        try {
           itdc = ClassFactory.createTDConnection();
           System.out.println("Connecting QC Using OTA...");
           //System.out.println("Connecton Status: "+itdc.connected());
           itdc.initConnectionEx("http://135.163.181.30/qcbin");
           //itdc.initConnectionEx("http://135.163.181.40/qcbin");
           //System.out.println("Connecton Status: "+itdc.connected());
           itdc.connectProjectEx("DEFAULT","eComQA","PGupta","PGupta");
           System.out.println("QC Using OTA Connected");
        }
        catch(Throwable ex)
        {              
           ex.printStackTrace();
        }      
    }
   
    /**
  *
  * @param qcTestLabPath
  */
 public int removeSSInQCTestLab(String qcTestLabPath)
 {
  int noOfTCUpdated = 0;
  int attachmentCount=0;
  boolean isRemoveAllAttachments = true;
  
  ITestSet ts;
  ITSTest tc;  
  IList tsList;           /* To store list of TestSet's(including nested folders) of a Folder */
  IList tcList;      /* To store list of TestCases in a TestSet  */
  IList tcAttachmentList; /* To store list of attachment of a single TC */
  
  IBaseFactory tFactory;              /* Contains Test Cases in particular TestSet */
  IAttachmentFactory tcAttachments;  
  
  ITestSetTreeManager tstm =  (itdc.testSetTreeManager()).queryInterface(ITestSetTreeManager.class); 
  ITestSetFolder tsFolder = ((tstm.nodeByPath(qcTestLabPath)).queryInterface(ITestSetFolder.class));
  tsList = (tsFolder.findTestSets("",false,"")).queryInterface(IList.class);
  //ITestSetFactory tsFactory = (tsFolder.testSetFactory()).queryInterface(ITestSetFactory.class);
  System.out.println("Updating Following TestSets in QC...");
  System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------");
  for(Com4jObject objTestSet:tsList)
  {   
   ts = objTestSet.queryInterface(ITestSet.class);
   tFactory = (ts.tsTestFactory()).queryInterface(IBaseFactory.class);   
   tcList = tFactory.newList("");   
   System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=        "+ ts.name() +"        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
   System.out.println();
   for(Com4jObject objTest:tcList)
   {
    try
    {       
     tc = objTest.queryInterface(ITSTest.class); 
     
     if(tc.status().equals("No Run"))   /*  Clear the field only for No Run TC's */
     {
      if(tc.hasAttachment())     /* Code to remove attachement from a TC */
      {
       tcAttachments = (tc.attachments()).queryInterface(IAttachmentFactory.class);
       tcAttachmentList = tcAttachments.newList("");
       attachmentCount = tcAttachmentList.count();
       
       if(isRemoveAllAttachments == true)               /* To remove all attachmets of a TC */
       {
        for(int i = 1; i <= attachmentCount; i++)    /* i = 1 because count starts from 1 */
        {
         tcAttachments.removeItem((tcAttachmentList.item(i)));
        }         
       }
       else           /* To remove only single attachment of TC */
       {
        tcAttachments.removeItem((tcAttachmentList.item(1))); 
       }        
      // tc.post();
       noOfTCUpdated = noOfTCUpdated + 1;
       System.out.println(noOfTCUpdated +") Has "+ attachmentCount +" attachments:  "+ tc.name());
      }
     }
    }
    catch (Exception e)
    {
     // TODO Auto-generated catch block
     System.out.println("Exception occured: :(");
     e.printStackTrace();
     continue;
    }
   }
   System.out.println();  
  }
  return(noOfTCUpdated);  
 }  
 
 public int attachSSInQCTestLab(String qcTestLabPath)
 {
  int noOfTCUpdated = 0;
  int attachmentCount=0;
  boolean isRemoveAllAttachments = true;
  
  ITestSet ts;
  ITSTest tc;  
  IList tsList;           /* To store list of TestSet's(including nested folders) of a Folder */
  IList tcList;      /* To store list of TestCases in a TestSet  */
  IList tcAttachmentList; /* To store list of attachment of a single TC */
  
  IBaseFactory tFactory;              /* Contains Test Cases in particular TestSet */
  IAttachmentFactory tcAttachments;  
  
  ITestSetTreeManager tstm =  (itdc.testSetTreeManager()).queryInterface(ITestSetTreeManager.class); 
  ITestSetFolder tsFolder = ((tstm.nodeByPath(qcTestLabPath)).queryInterface(ITestSetFolder.class));
  tsList = (tsFolder.findTestSets("",false,"")).queryInterface(IList.class);
  //ITestSetFactory tsFactory = (tsFolder.testSetFactory()).queryInterface(ITestSetFactory.class);
  System.out.println("Updating Following TestSets in QC...");
  System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------");
  for(Com4jObject objTestSet:tsList)
  {   
   ts = objTestSet.queryInterface(ITestSet.class);
   tFactory = (ts.tsTestFactory()).queryInterface(IBaseFactory.class);   
   tcList = tFactory.newList("");   
   System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=        "+ ts.name() +"        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
   System.out.println();
   for(Com4jObject objTest:tcList)
   {
    try
    {       
     tc = objTest.queryInterface(ITSTest.class); 
     if(tc.hasAttachment())     /* Code to remove attachement from a TC */
     {
      tcAttachments = (tc.attachments()).queryInterface(IAttachmentFactory.class);
      tcAttachmentList = tcAttachments.newList("");
      attachmentCount = tcAttachmentList.count();
      
      if(isRemoveAllAttachments == true)               /* To remove all attachmets of a TC */
      {
       for(int i = 1; i <= attachmentCount; i++)    /* i = 1 because count starts from 1 */
       {
        tcAttachments.removeItem((tcAttachmentList.item(i)));
       }         
      }
      else           /* To remove only single attachment of TC */
      {
       tcAttachments.removeItem((tcAttachmentList.item(1))); 
      }        
     // tc.post();
      noOfTCUpdated = noOfTCUpdated + 1;
      System.out.println(noOfTCUpdated +") Has "+ attachmentCount +" attachments:  "+ tc.name());
     }
    }
    catch (Exception e)
    {
     // TODO Auto-generated catch block
     System.out.println("Exception occured: :(");
     e.printStackTrace();
     continue;
    }
   }
   System.out.println();  
  }
  return(noOfTCUpdated);  
 }  
}



Working Code







No comments:

Post a Comment