2013-01-08 142 views
1

使用Java Rally REST API可以将Excel文件附加到测试用例吗?使用Java Rally REST API附加文件

+0

我不确定我们是否有这样做的例子或没有...下面是如何在.NET中做到这一点(我会认为他们是相当相似的):http://stackoverflow.com/questions/11388652/how-to-add-an-attachment-to-a-user-story-using-rally-rest-net/11398741#11398741 –

回答

1

下面是如何在Java REST中执行此操作的示例。这是一个图像文件,但你明白了:)

import com.google.gson.JsonArray; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 
import com.rallydev.rest.RallyRestApi; 
import com.rallydev.rest.request.CreateRequest; 
import com.rallydev.rest.request.DeleteRequest; 
import com.rallydev.rest.request.GetRequest; 
import com.rallydev.rest.request.QueryRequest; 
import com.rallydev.rest.request.UpdateRequest; 
import com.rallydev.rest.response.CreateResponse; 
import com.rallydev.rest.response.DeleteResponse; 
import com.rallydev.rest.response.GetResponse; 
import com.rallydev.rest.response.QueryResponse; 
import com.rallydev.rest.response.UpdateResponse; 
import com.rallydev.rest.util.Fetch; 
import com.rallydev.rest.util.QueryFilter; 
import com.rallydev.rest.util.Ref; 

import java.io.RandomAccessFile; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 

import org.apache.commons.codec.binary.Base64; 

public class RestExample_AddAttachmentToUserStory { 

    public static void main(String[] args) throws URISyntaxException, IOException { 

     // Create and configure a new instance of RallyRestApi 
     // Connection parameters 
     String rallyURL = "https://rally1.rallydev.com"; 
     String wsapiVersion = "1.40"; 
     String applicationName = "RestExample_AddAttachmentToUserStory"; 

     // Credentials 
     String userName = "[email protected]"; 
     String userPassword = "topsecret"; 

     RallyRestApi restApi = new RallyRestApi(
         new URI(rallyURL), 
         userName, 
         userPassword); 
     restApi.setWsapiVersion(wsapiVersion); 
     restApi.setApplicationName(applicationName); 

     // User settings 
     String testerUserName = "[email protected]"; 

     // Workspace and Project Settings 
     String myWorkspace = "My Workspace"; 
     String myProject = "My Project"; 

     // FormattedID of Existing Test Case to Query 
     String existStoryFormattedID = "US43";  

     //Read User 
     QueryRequest userRequest = new QueryRequest("User"); 
     userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName")); 
     userRequest.setQueryFilter(new QueryFilter("UserName", "=", testerUserName)); 
     QueryResponse userQueryResponse = restApi.query(userRequest); 
     JsonArray userQueryResults = userQueryResponse.getResults(); 
     JsonElement userQueryElement = userQueryResults.get(0); 
     JsonObject userQueryObject = userQueryElement.getAsJsonObject(); 
     String userRef = userQueryObject.get("_ref").getAsString(); 

     // Get reference to Workspace of interest 
     QueryRequest workspaceRequest = new QueryRequest("Workspace"); 
     workspaceRequest.setFetch(new Fetch("Name", "Owner", "Projects")); 
     workspaceRequest.setQueryFilter(new QueryFilter("Name", "=", myWorkspace)); 
     QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest); 
     String workspaceRef = workspaceQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString(); 

     // Get reference to Project of interest 
     QueryRequest projectRequest = new QueryRequest("Project"); 
     projectRequest.setFetch(new Fetch("Name", "Owner", "Projects")); 
     projectRequest.setQueryFilter(new QueryFilter("Name", "=", myProject)); 
     QueryResponse projectQueryResponse = restApi.query(projectRequest); 
     String projectRef = projectQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();  

     // Query for existing User Story 
     QueryRequest existUserStoryRequest = new QueryRequest("HierarchicalRequirement"); 
     existUserStoryRequest.setFetch(new Fetch("FormattedID","Name")); 
     existUserStoryRequest.setQueryFilter(new QueryFilter("FormattedID", "=", existStoryFormattedID)); 
     QueryResponse userStoryQueryResponse = restApi.query(existUserStoryRequest); 
     JsonObject existUserStoryJsonObject = userStoryQueryResponse.getResults().get(0).getAsJsonObject(); 
     String existUserStoryRef = userStoryQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString(); 

     // Read In Image Content 
     String imageFilePath = "/Users/username/Documents/"; 
     String imageFileName = "image1.png"; 
     String fullImageFile = imageFilePath + imageFileName; 
     String imageBase64String; 
     long attachmentSize; 

     // Open file 
     RandomAccessFile myImageFileHandle = new RandomAccessFile(fullImageFile, "r"); 

     try { 
      // Get and check length 
      long longlength = myImageFileHandle.length(); 
      // Max upload size for Rally attachments is 5MB 
      long maxAttachmentLength = 5120000; 
      if (length > maxAttachmentLength) throw new IOException("File size too big for Rally attachment, > 5 MB"); 

      // Read file and return data 
      byte[] fileBytes = new byte[length]; 
      myImageFileHandle.readFully(fileBytes); 
      imageBase64String = Base64.encodeBase64String(fileBytes); 
      attachmentSize = length; 

      // First create AttachmentContent from image string 
      JsonObject myAttachmentContent = new JsonObject(); 
      myAttachmentContent.addProperty("Content", imageBase64String); 
      CreateRequest attachmentContentCreateRequest = new CreateRequest("AttachmentContent", myAttachmentContent); 
      CreateResponse attachmentContentResponse = restApi.create(attachmentContentCreateRequest); 
      String myAttachmentContentRef = attachmentContentResponse.getObject().get("_ref").getAsString(); 
      System.out.println("Attachment Content created: " + myAttachmentContentRef);    

      // Now create the Attachment itself 
      JsonObject myAttachment = new JsonObject(); 
      myAttachment.addProperty("Artifact", existUserStoryRef); 
      myAttachment.addProperty("Content", myAttachmentContentRef); 
      myAttachment.addProperty("Name", "AttachmentFromREST.png"); 
      myAttachment.addProperty("Description", "Attachment From REST"); 
      myAttachment.addProperty("ContentType","image/png"); 
      myAttachment.addProperty("Size", attachmentSize); 
      myAttachment.addProperty("User", userRef);   

      CreateRequest attachmentCreateRequest = new CreateRequest("Attachment", myAttachment); 
      CreateResponse attachmentResponse = restApi.create(attachmentCreateRequest); 
      String myAttachmentRef = attachmentResponse.getObject().get("_ref").getAsString(); 
      System.out.println("Attachment created: " + myAttachmentRef); 

      if (attachmentResponse.wasSuccessful()) { 
       System.out.println("Successfully created Attachment"); 
      } else { 
       String[] attachmentContentErrors; 
       attachmentContentErrors = attachmentResponse.getErrors(); 
         System.out.println("Error occurred creating Attachment: "); 
       for (int i=0; i<attachmentContentErrors.length;i++) { 
         System.out.println(attachmentContentErrors[i]); 
       }     
      } 
     } catch (Exception e) { 
       System.out.println("Exception occurred while attempting to create Content and/or Attachment: "); 
       e.printStackTrace();    
     } 

     finally { 
      //Release all resources 
      restApi.close(); 
     }     
    } 
} 
+1

谢谢你非常努力! – wgp

相关问题