annotate test/RequestTest.java @ 16:7cdc9d19834f

modified createClaim
author one
date Tue, 02 Oct 2012 13:52:31 +0900
parents 0adbec4c7091
children c8ad59a52c7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
1 import javax.ws.rs.core.MediaType;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
2
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
3 import models.NodeModel;
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
4
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
5 import org.json.JSONException;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
6 import org.json.JSONObject;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
7
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
8 import com.sun.jersey.api.client.Client;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
9 import com.sun.jersey.api.client.ClientResponse;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
10 import com.sun.jersey.api.client.WebResource;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
11
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
12
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
13 public class RequestTest {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
14 final static String SERVER_ROOT_URI = "http://localhost:9000";
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
16 public static void main(String[] args) throws JSONException {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
17
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
18 createUser("taro");
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
19 /*
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
20 getUser("taro");
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
21 getUserInfo("taro","requests/");
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
22 getUserInfo("taro","claims/");
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
23 getUserInfo("taro","consensus/");
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
24 */
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
25 createClaim("taro");
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
26
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
27
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
28
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
29 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
30
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
31 public static void createClaim(String author) throws JSONException {
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
32 JSONObject toulmin = new JSONObject();
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
33 toulmin.put(NodeModel.TITLE, "アプリでGraphDBを利用する。");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
34 toulmin.put(NodeModel.CONTENTS, "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
35 toulmin.put(NodeModel.QUALIFIER, "絶対");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
36 toulmin.put(NodeModel.WARRANT,"GraphDBの実用例 etc...");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
37 toulmin.put(NodeModel.BACKING, "GraphDBの最新動向 etc...");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
38 toulmin.put(NodeModel.DATA,"GraphDBの実用例 etc...");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
39 toulmin.put(NodeModel.REBUTTLE,"");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
40
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
41 JSONObject jobj = new JSONObject();
16
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
42 jobj.put(NodeModel.TOULMIN, toulmin);
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
43 jobj.put(NodeModel.AUTHOR, author);
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
44 String[] users = {"akifumi","yosiaki"};
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
45 jobj.put(NodeModel.USERS,users);
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
46 jobj.put(NodeModel.TYPE, "unanimously");
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
47
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
48 final String uri = SERVER_ROOT_URI + "/claims/create";
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
49 WebResource resource = Client.create().resource(uri);
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
50 ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON)
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
51 .entity(jobj.toString())
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
52 .post(ClientResponse.class);
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
53 System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus()));
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
54 System.out.println(response.getEntity(String.class));
7cdc9d19834f modified createClaim
one
parents: 15
diff changeset
55
15
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
56 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
57
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
58 public static void getUserInfo(String name, String preUri) {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
59 final String uri = SERVER_ROOT_URI + "/users/"+preUri+name;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
60 WebResource resource = Client.create().resource(uri);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
61 ClientResponse response = resource.get(ClientResponse.class);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
62 System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus()));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
63 System.out.println(response.getEntity(String.class));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
64 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
65
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
66 public static void createUser(String name) {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
67 final String uri = SERVER_ROOT_URI + "/users/create/"+name;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
68 WebResource resource = Client.create().resource(uri);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
69 ClientResponse response = resource.put(ClientResponse.class);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
70 System.out.println(String.format("PUT on [%s], status code [%d]", uri, response.getStatus()));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
71 System.out.println(response.getEntity(String.class));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
72
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
73 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
74
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
75 public static void getUser(String name) {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
76 final String uri = SERVER_ROOT_URI + "/users/browse/"+name;
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
77 WebResource resource = Client.create().resource(uri);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
78 ClientResponse response = resource.get(ClientResponse.class);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
79 System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus()));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
80 System.out.println(response.getEntity(String.class));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
81 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
82
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
83
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
84 public static void postName() throws JSONException {
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
85 final String uri = SERVER_ROOT_URI + "/hello";
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
86 WebResource resource = Client.create().resource(uri);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
87 JSONObject jobj = new JSONObject();
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
88 jobj.put("name","taro");
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
89 // ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
90 ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON)
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
91 .entity(jobj.toString())
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
92 .post(ClientResponse.class);
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
93 System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus()));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
94 System.out.println(response.getEntity(String.class));
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
95
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
96 }
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
97
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
98
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
99
0adbec4c7091 add jersey libraries and RequestTest.java
one
parents:
diff changeset
100 }