1 | |
package org.jbehave.core.io.rest.redmine; |
2 | |
|
3 | |
import org.jbehave.core.io.rest.LoadFromREST; |
4 | |
import org.jbehave.core.io.rest.RESTClient.Type; |
5 | |
|
6 | |
import com.google.gson.Gson; |
7 | |
import com.google.gson.JsonParser; |
8 | |
import com.thoughtworks.xstream.XStream; |
9 | |
|
10 | |
import static java.text.MessageFormat.format; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
public class LoadFromRedmine extends LoadFromREST { |
16 | |
|
17 | |
private static final String REDMINE_URI = "{0}.{1}"; |
18 | |
|
19 | |
public LoadFromRedmine(Type type) { |
20 | 4 | this(type, null, null); |
21 | 4 | } |
22 | |
|
23 | |
public LoadFromRedmine(Type type, String username, String password) { |
24 | 4 | super(type, username, password); |
25 | 4 | } |
26 | |
|
27 | |
protected String uri(String resourcePath, Type type) { |
28 | 2 | return format(REDMINE_URI, resourcePath, type.name().toLowerCase()); |
29 | |
} |
30 | |
|
31 | |
protected String text(String entity, Type type) { |
32 | 1 | switch (type) { |
33 | |
case JSON: |
34 | 1 | Gson gson = new Gson(); |
35 | 1 | return gson.fromJson(jsonMember(entity, "wiki_page"), |
36 | |
WikiPage.class).text; |
37 | |
case XML: |
38 | 1 | XStream xstream = new XStream(); |
39 | 1 | xstream.alias("wiki_page", WikiPage.class); |
40 | 1 | xstream.ignoreUnknownElements(); |
41 | 1 | return ((WikiPage) xstream.fromXML(entity)).text; |
42 | |
default: |
43 | 0 | return entity; |
44 | |
} |
45 | |
} |
46 | |
|
47 | |
private String jsonMember(String entity, String memberName) { |
48 | 1 | return new JsonParser().parse(entity).getAsJsonObject().get(memberName) |
49 | |
.toString(); |
50 | |
} |
51 | |
|
52 | 1 | private static class WikiPage { |
53 | |
String text; |
54 | |
} |
55 | |
} |