Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用Java Rally REST API将图像文件附加到Rally中的测试用例结果?_Java_Rest_Rally - Fatal编程技术网

是否可以使用Java Rally REST API将图像文件附加到Rally中的测试用例结果?

是否可以使用Java Rally REST API将图像文件附加到Rally中的测试用例结果?,java,rest,rally,Java,Rest,Rally,我正在尝试使用JavaRallyRESTAPI将图像文件附加到Rally中的测试结果(失败的屏幕截图)。图像文件附加到测试用例本身,但不附加到测试结果。尝试提供testCaseResult引用而不是testCaseReference,但收到错误 “无法为项目设置值:无法在属性项目中使用类型TestCaseResult” 有什么帮助吗? 感谢下面是如何做到这一点的示例。该示例创建一个新的测试用例结果,然后添加附件。诀窍是在感兴趣的TestCaseResult的ref附件上设置“TestCaseRe

我正在尝试使用JavaRallyRESTAPI将图像文件附加到Rally中的测试结果(失败的屏幕截图)。图像文件附加到测试用例本身,但不附加到测试结果。尝试提供testCaseResult引用而不是testCaseReference,但收到错误 “无法为项目设置值:无法在属性项目中使用类型TestCaseResult” 有什么帮助吗?
感谢

下面是如何做到这一点的示例。该示例创建一个新的测试用例结果,然后添加附件。诀窍是在感兴趣的TestCaseResult的ref附件上设置“TestCaseResult”属性。这与向故事添加附件不同,例如,在故事中,属性是“工件”

import com.google.gson.JsonArray;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
导入com.rallydev.rest.RallyRestApi;
导入com.rallydev.rest.request.CreateRequest;
导入com.rallydev.rest.request.DeleteRequest;
导入com.rallydev.rest.request.GetRequest;
导入com.rallydev.rest.request.QueryRequest;
导入com.rallydev.rest.request.UpdateRequest;
导入com.rallydev.rest.response.CreateResponse;
导入com.rallydev.rest.response.deleteSponse;
导入com.rallydev.rest.response.GetResponse;
导入com.rallydev.rest.response.QueryResponse;
导入com.rallydev.rest.response.UpdateResponse;
导入com.rallydev.rest.util.Fetch;
导入com.rallydev.rest.util.QueryFilter;
导入com.rallydev.rest.util.Ref;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.net.URI;
导入java.net.URISyntaxException;
导入org.apache.commons.codec.binary.Base64;
公共类RestExample_CreateTestCaseResultAddAttachment{
公共静态void main(字符串[]args)抛出URISyntaxException、IOException{
//创建并配置RallyRestApi的新实例
//连接参数
字符串rallyURL=”https://rally1.rallydev.com";
字符串wsapiVersion=“v2.0”;
字符串applicationName=“RestExample\u createTestCaseResultadAttachment”;
//证书
字符串用户名=”user@company.com";
字符串userPassword=“topsecret”;
RallyRestApi restApi=新的RallyRestApi(
新URI(rallyURL),
用户名,
用户密码);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
//工作空间和项目设置
字符串myWorkspace=“/workspace/12345678910”;
字符串myProject=“/project/12345678911”;
//我们想要添加结果的测试用例
字符串testCaseFormattedID=“TC40”;
//测试人员的用户名
字符串TesterAllyid=”tester@testit.com";
//对已创建TestCaseResult的引用
字符串testCaseResultRef=“”;
//要附加的图像的文件句柄
随机访问文件myImageFileHandle;
字符串imageFilePath=“/home/username/Pictures/”;
字符串imageFileName=“image1.jpg”;
字符串fullImageFile=imageFilePath+imageFileName;
字符串imageBase64String;
长附件尺寸;
//打开文件
myImageFileHandle=新的随机访问文件(fullImageFile,“r”);
//读用户
QueryRequest userRequest=新的QueryRequest(“用户”);
setFetch(新的Fetch(“用户名”、“订阅”、“显示名”));
setQueryFilter(新的QueryFilter(“用户名”,“=”,testerAllyid));
QueryResponse userQueryResponse=restApi.query(userRequest);
JsonArray userQueryResults=userQueryResponse.getResults();
JsonObject userQueryObject=userQueryResults.get(0.getAsJsonObject();
字符串userRef=userQueryObject.get(“_ref”).getAsString();
//查询要向其添加结果的测试用例
QueryRequest testCaseRequest=新的QueryRequest(“TestCase”);
setFetch(新的Fetch(“FormattedID”,“Name”));
setQueryFilter(新的QueryFilter(“FormattedID”,“=”,testCaseFormattedID));
QueryResponse testCaseQueryResponse=restApi.query(testCaseRequest);
JsonObject testCaseJsonObject=testCaseQueryResponse.getResults().get(0.getAsJsonObject();
字符串testCaseRef=testCaseQueryResponse.getResults().get(0.getAsJsonObject().get(“_ref”).getAsString();
//查询要添加测试用例的测试集
QueryRequest testSetQuery=新的QueryRequest(“TestSet”);
setFetch(新的Fetch(“FormattedID”、“Name”、“TestCases”);
setWorkspace(myWorkspace);
setProject(myProject);
setQueryFilter(新的QueryFilter(“FormattedID”,“=”,“TS5”);
QueryResponse testSetQueryResponse=restApi.query(testSetQuery);
JsonObject testSetJsonObject=testSetQueryResponse.getResults().get(0.getAsJsonObject();
字符串testSetRef=testSetJsonObject.get(“_ref”).getAsString();
System.out.println(“测试集参考:”+testSetRef);
试试{
//添加测试用例结果
System.out.println(“创建测试用例结果…”);
JsonObject newTestCaseResult=新JsonObject();
newTestCaseResult.addProperty(“裁决”、“非决定性”);
newTestCaseResult.addProperty(“日期”,“2013-09-04T18:00:00.000Z”);
newTestCaseResult.addProperty(“注释”,“自动硒测试运行”);
newTestCaseResult.addProperty(“构建”、“2013.09.04.0020101”);
newTestCaseResult.addProperty(“Tester”,userRef);
newTestCaseResult.addProperty(“TestCase”,testCaseRef);
newTestCaseResult.addProperty(“TestSet”,testSetRef);
CreateRequest CreateRequest=新的CreateRequest(“testcaseresult”,newTestCaseResult);
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.IOException;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;

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

public class RestExample_CreateTestCaseResultAddAttachment {

    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 = "v2.0";
        String applicationName = "RestExample_CreateTestCaseResultAddAttachment";

        // Credentials
        String userName = "user@company.com";
        String userPassword = "topsecret";

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

        // Workspace and Project Settings
        String myWorkspace = "/workspace/12345678910";
        String myProject = "/project/12345678911";

        // Test Case to which we want to add a result
        String testCaseFormattedID = "TC40";

        // User name of tester
        String testerRallyID = "tester@testit.com";

        // Reference to created TestCaseResult
        String testCaseResultRef = "";

        // File handle for image to attach
        RandomAccessFile myImageFileHandle;
        String imageFilePath = "/home/username/Pictures/";
        String imageFileName = "image1.jpg";
        String fullImageFile = imageFilePath + imageFileName;
        String imageBase64String;
        long attachmentSize;

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

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

        // Query for Test Case to which we want to add results
        QueryRequest testCaseRequest = new QueryRequest("TestCase");
        testCaseRequest.setFetch(new Fetch("FormattedID","Name"));
        testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", testCaseFormattedID));
        QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
        JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
        String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();        

        // Query for Test Set to which we want to add Test Case
        QueryRequest testSetQuery = new QueryRequest("TestSet");
        testSetQuery.setFetch(new Fetch("FormattedID","Name","TestCases"));
        testSetQuery.setWorkspace(myWorkspace);
        testSetQuery.setProject(myProject);
        testSetQuery.setQueryFilter(new QueryFilter("FormattedID", "=", "TS5"));
        QueryResponse testSetQueryResponse = restApi.query(testSetQuery);
        JsonObject testSetJsonObject = testSetQueryResponse.getResults().get(0).getAsJsonObject();
        String testSetRef = testSetJsonObject.get("_ref").getAsString();
        System.out.println("Test Set Ref: " + testSetRef);

        try {           

            //Add a Test Case Result                
            System.out.println("Creating Test Case Result...");
            JsonObject newTestCaseResult = new JsonObject();
            newTestCaseResult.addProperty("Verdict", "Inconclusive");
            newTestCaseResult.addProperty("Date", "2013-09-04T18:00:00.000Z");
            newTestCaseResult.addProperty("Notes", "Automated Selenium Test Runs");
            newTestCaseResult.addProperty("Build", "2013.09.04.0020101");
            newTestCaseResult.addProperty("Tester", userRef);
            newTestCaseResult.addProperty("TestCase", testCaseRef);
            newTestCaseResult.addProperty("TestSet", testSetRef);

            CreateRequest createRequest = new CreateRequest("testcaseresult", newTestCaseResult);
            CreateResponse createResponse = restApi.create(createRequest);            

            if (createResponse.wasSuccessful()) {

                System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));          

                //Read Test Case Result
                testCaseResultRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
                System.out.println(String.format("\nReading Test Case Result %s...", testCaseResultRef));
                GetRequest getRequest = new GetRequest(testCaseResultRef);
                getRequest.setFetch(new Fetch("Date", "Verdict"));
                GetResponse getResponse = restApi.get(getRequest);
                JsonObject obj = getResponse.getObject();
                System.out.println(String.format("Read Test Case Result. Date = %s, Verdict = %s",
                        obj.get("Date").getAsString(), obj.get("Verdict").getAsString()));

                try {
                    // Get and check length
                    long longLength = myImageFileHandle.length();
                    long maxLength = 5000000;
                    if (longLength >= maxLength) throw new IOException("File size >= 5 MB Upper limit for Rally.");
                    int fileLength = (int) longLength;            

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

                    // 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("TestCaseResult", testCaseResultRef);
                    myAttachment.addProperty("Content", myAttachmentContentRef);
                    myAttachment.addProperty("Name", "AttachmentFromREST.jpg");
                    myAttachment.addProperty("Description", "Attachment From REST");
                    myAttachment.addProperty("ContentType","image/jpg");
                    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();            
                }                           

            } else {
                String[] createErrors;
                createErrors = createResponse.getErrors();
                System.out.println("Error occurred creating Test Case Result: ");
                for (int j=0; j<createErrors.length;j++) {
                    System.out.println(createErrors[j]);
                }
            }                        

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

}