java - 使用 Java Rally REST API 附加文件

标签 java rest rally

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

最佳答案

下面是如何在 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5c0c6d0c7f5d6dad8c5d4dbcc9bd6dad8" rel="noreferrer noopener nofollow">[email protected]</a>";
        String userPassword = "topsecret";

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

        // User settings
        String testerUserName = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1a0f0f1a1813161e150f0e081e093b1814160b1a150255181416" rel="noreferrer noopener nofollow">[email protected]</a>";

        // 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();
        }                
    }
}

关于java - 使用 Java Rally REST API 附加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14219534/

相关文章:

java - SQL IN 或 NOT IN 取决于 boolean 参数

java - 尝试循环更新日期和时间

spring - 用 Spring 玩框架 2.1

javascript - 是否有关于如何填充 Revision.Description 以及在什么条件下填充的文档?

java - Rally REST WS 查询 : Getting ObjectIDs of One-To-Many children using API v2. 0

java - Spring 记得我没有使用自定义过滤器和 Java 配置

python - Django 中的 Web 服务

java - 如何将 Apache Shiro 与 AngularJS 集成

javascript - 逐步使用 Node.js 创建自定义应用程序

java - 如何在JmonkeyEngine3中通过物理控制旋转空间?