您的位置:首页 > 编程语言 > Java开发

redmine-java-api的使用方法简介

2017-08-14 21:32 716 查看
redmine是一个项目管理系统,对于测试人员来讲,日常工作中的大部分时间都用来测试,搜索bug,提交bug,验证问题。

随着自动化测试的不断发展,我们有专门的apk去收集测试数据,通过redmine的api接口实现测试数据与redmine上bug的关联。

目前实现bug的批量相似查找,自动提交与bug备注的功能。

首先贴上github上redmine的开源接口:https://github.com/taskadapter/redmine-java-api

主要使用了以下三个接口:


1、Multi-values search for issues-使用多个过滤条件搜索bug

Params params = new Params()
.add("set_filter", "1")
.add("f[]", "summary")
.add("op[summary]", "~")
.add("v[summary]", "another")
.add("f[]", "description")
.add("op[description]", "~")
.add("v[description][]", "abc");

result = issueManager.getIssues(params);


Params也是redmine-java-api提供的类。新建一个该类的实例,添加搜索的过滤条件。

首先,这个类是11 Aug 2016由alexeyOnGitHub提交上去的,

在maven上可以下载最新的3.0.1版本:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.taskadapter%22%20AND%20a%3A%22redmine-java-api%22

最新版本有一个问题就是不兼容jdk-1.7,使用jdk-1.8可以运行。由于本人的项目使用的是jdk-1.7,所以就得自己去做兼容。

把Params这个类以及相关联更改都添加到原有的redmine-java-api-2.6.0上面去。

其实就是根据https://github.com/taskadapter/redmine-java-api/commit/69723c4463f41bec077d9282cb3903c17100b31c 这一个提交改了下2.6.0的代码,然后自己打了一个jar包放在项目中去用。


2、Create an issue-创建bug

Issue issue = IssueFactory.createWithSubject("test123");
Version ver = VersionFactory.create(512);
issue.setTargetVersion(ver);
IssueCategory cat = IssueCategoryFactory.create(673);
issue.setCategory(cat);
ProjectManager projectManager = manager.getProjectManager();
Project projectByKey = projectManager.getProjectByKey("testid");
issue.setProject(projectByKey);
manager.getIssueManager().createIssue(issue);

3、备注bug

更新Issue的接口如下:

PUT /issues/[id].json
{
"issue": {
"subject": "Subject changed",
"notes": "The subject was changed"
}
}


现在的需求是根据bugId去添加一条备注:

public boolean updateissueById(int bugid, String comment, String apikey) throws IOException {
String data="{\"issue\":" +
"{\"id\":"+bugid+"," +
"\"notes\": \""+comment+"\"}}";
LOGGER.info("json:\n"+data);
LOGGER.info("备注bug:bugid:"+bugid);
String url = redmineServerHost(redmineHostType)+"issues/" + bugid + ".json";
return getPutInformation(url, data, apikey);
}
建立连接向redmine发送PUT请求的代码:

public static boolean getPutInformation(String  path,String obj, String apiKey) throws IOException{
//创建连接
URL url = new URL(path);
HttpURLConnection connection ;
StringBuffer sbuffer=null;
if("".equals(apiKey)){
apiKey = Constants.StabbilityConfig.REDMINE_APIKEY;
}
try {
//添加 请求内容
connection= (HttpURLConnection) url.openConnection();
//设置http连接属性
connection.setDoOutput(true);// http正文内,因此需要设为true, 默认情况下是false;
connection.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true;
connection.setRequestMethod("PUT"); // 可以根据需要 提交 GET、POST、DELETE、PUT等http提供的功能
//connection.setUseCaches(false);//设置缓存,注意设置请求方法为post不能用缓存
// connection.setInstanceFollowRedirects(true);

//            connection.setRequestProperty("Host", "*******");  //设置请 求的服务器网址,域名,例如***.**.***.***
connection.setRequestProperty("Content-Type", " application/json");//设定 请求格式 json,也可以设定xml格式的
connection.setRequestProperty("Accept-Charset", "utf-8");  //设置编码语言
//            connection.setRequestProperty("X-Auth-Token", "token");  //设置请求的token
connection.setRequestProperty("Connection", "keep-alive");  //设置连接的状态
connection.setRequestProperty("Transfer-Encoding", "chunked");//设置传输编码
connection.setRequestProperty("Content-Length", obj.toString().getBytes().length + "");  //设置文件请求的长度
connection.setReadTimeout(10000);//设置读取超时时间
connection.setConnectTimeout(10000);//设置连接超时时间
connection.connect();
OutputStream out = connection.getOutputStream();//向对象输出流写出数据,这些数据将存到内存缓冲区中
out.write(obj.toString().getBytes());            //out.write(new String("测试数据").getBytes());            //刷新对象输出流,将任何字节都写入潜在的流中
out.flush();
// 关闭流对象,此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中
out.close();
//读取响应
if (connection.getResponseCode()==200){
// 从服务器获得一个输入流
InputStreamReader   inputStream =new InputStreamReader(connection.getInputStream());//调用HttpURLConnection连接对象的getInputStream()函数, 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
BufferedReader reader = new BufferedReader(inputStream);
String lines;
sbuffer= new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbuffer.append(lines);
}
reader.close();
connection.disconnect();//断开连接
return true;
}else{
System.out.print("请求失败"+connection.getResponseCode());
connection.disconnect();//断开连接
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}


通过redmine-java-api接口能做的事情远不止如此,还可以对bug进行更加深度的管理,例如bug的自动化验证,修改问题优先级等。有现成的接口,但是目前还有点小问题,后面有精力了再继续做。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  测试平台