Mercurial > hg > Members > nobuyasu > Consensus
view test/ETDemo.java @ 64:7121a35a61b9
modified InitialData.java
author | one |
---|---|
date | Tue, 13 Nov 2012 05:26:27 +0900 |
parents | a4540fee6db6 |
children |
line wrap: on
line source
import java.util.HashMap; import javax.ws.rs.core.MediaType; import models.NodeModel; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.node.ArrayNode; import org.codehaus.jackson.node.ObjectNode; import play.libs.Json; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class ETDemo { final static String SERVER_ROOT_URI = "http://localhost:9000"; public static void main(String[] args) { String userA = "Yana"; String userB = "Tatsumi"; String userC = "Shiomi"; String userD = "Ikuhiro"; createUser(userA); createUser(userB); createUser(userC); String[] otherUserA = {userB,userC,userD}; String[] otherUserB = {userA,userC,userD}; String[] otherUserC = {userA,userB,userD}; String[] otherUserD = {userA,userB,userC}; JsonNode allUsers = getAllUsers(); String[] allUsersArray = new String[allUsers.size()]; for (int i=0;i<allUsers.size(); i++) { String id = allUsers.get(i).asText(); allUsersArray[i] = id; } HashMap<String, Long> idHash = new HashMap<String, Long>(); createClaimRoot(userA, otherUserA); createClaim1(userA, otherUserA); createClaim2(userB, otherUserB); } public static JsonNode getAllUsers() { final String uri = SERVER_ROOT_URI + "/users/all"; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.toJson(resStr); } public static void allAgreed(String name) { JsonNode requestsJson = getUserInfo(name,"requests/"); for (int i=0; i<requestsJson.size(); i++) { int id = requestsJson.get(i).asInt(); updateUserConsensusStatus(id, name, NodeModel.AGREED); } } public static JsonNode editClaimInfo(ObjectNode jobj, String author, String[] users, int id) { final String uri = SERVER_ROOT_URI + "/claims/edit/"+id; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON) .entity(jobj.toString()) .post(ClientResponse.class); System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.toJson(resStr); } public static JsonNode getClaimTree(int id) { final String uri = SERVER_ROOT_URI + "/consensus/browse/"+id; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.parse(resStr); } public static void updateUserConsensusStatus(int id, String name, String status) { final String uri = SERVER_ROOT_URI + "/claims/answer/"+id+"/"+name+"/"+status; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.post(ClientResponse.class); System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); } public static JsonNode getUserConsensusStatus(int id, String name) { final String uri = SERVER_ROOT_URI + "/claims/answer/"+id+"/"+name; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.parse(resStr); } public static JsonNode getClaimInfo(int id) { final String uri = SERVER_ROOT_URI + "/claims/browse/"+id; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.parse(resStr); } public static void createMention(ObjectNode jobj, String author, String[] users, String type, Object id) { final String uri = SERVER_ROOT_URI + "/claims/"+type+"/"+id+"/create"; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON) .entity(jobj.toString()) .post(ClientResponse.class); System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); } public static JsonNode createClaim(ObjectNode jobj, String author, String[] users) { final String uri = SERVER_ROOT_URI + "/claims/create"; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON) .entity(jobj.toString()) .post(ClientResponse.class); System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.toJson(resStr); } public static ObjectNode createToulmin(String title, String contents, String rebuttle, String qualifier, String data, String warrant, String backing) { ObjectNode toulmin = Json.newObject(); toulmin.put(NodeModel.TITLE, title); toulmin.put(NodeModel.CONTENTS, contents); toulmin.put(NodeModel.REBUTTLE, rebuttle); toulmin.put(NodeModel.QUALIFIER, qualifier); toulmin.put(NodeModel.DATA, data); toulmin.put(NodeModel.WARRANT, warrant); toulmin.put(NodeModel.BACKING, backing); return toulmin; } public static ObjectNode createClaimRoot(String author, String[] users) { String title = "Aのパソコンを買う"; String contents = ""; String rebuttle = ""; String qualifier = ""; String data = ""; String warrant = ""; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static ObjectNode createClaim1(String author, String[] users) { String title = "壊れたマシンと同じ程度のものがあればいい。"; String contents = "壊れたマシンと同じ程度のものがあればいいです。。"; String rebuttle = "仕事の内容が大幅に変わらない限りは"; String qualifier = ""; String data = "壊れてしまったパソコンは、Windows7の入ったノートブックで15インチです。"; String warrant = "困っていないので"; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static ObjectNode createClaim2(String author, String[] users) { String title = "MacProの最新版を買うのはどうか"; String contents = "MacProの最新版を買う"; String rebuttle = ""; String qualifier = ""; String data = "次のプロジェクトで画面デザインの仕事が入る予定です。"; String warrant = ""; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static ObjectNode createClaim3(String author, String[] users) { String title = "MacProの最新版を買います。"; String contents = "MacProの最新版を買います。"; String rebuttle = ""; String qualifier = ""; String data = "当社のグラフィックアプリケーションはMacバージョンです。"; String warrant = "Macでしか動かないので"; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static ObjectNode createClaim4(String author, String[] users) { String title = "MacProの最速マシンは高いのでやめましょう"; String contents = ""; String rebuttle = ""; String qualifier = ""; String data = "予算は250,000円までです。"; String warrant = "予算以内に収めなければなりません。"; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static ObjectNode createClaim5(String author, String[] users) { String title = "MacProの最速マシンである必要はない。"; String contents = ""; String rebuttle = ""; String qualifier = ""; String data = "Macのタワー型ならグラフィックの性能は高いです。"; String warrant = "画面デザインなら最速でなくても良いです。。"; String backing = ""; ObjectNode toulmin = createToulmin(title, contents, rebuttle, qualifier, data, warrant, backing); ObjectNode jobj = Json.newObject(); jobj.put(NodeModel.TOULMIN, toulmin); jobj.put(NodeModel.L_AUTHOR, author); ArrayNode usersArray = jobj.putArray(NodeModel.USERS); for (String u : users) usersArray.add(u); jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY); return jobj; } public static JsonNode getUserInfo(String name, String preUri) { final String uri = SERVER_ROOT_URI + "/users/"+preUri+name; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); System.out.println(resStr); return Json.parse(resStr); } public static void createUser(String name) { final String uri = SERVER_ROOT_URI + "/users/create/"+name; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.put(ClientResponse.class); System.out.println(String.format("PUT on [%s], status code [%d]", uri, response.getStatus())); System.out.println(response.getEntity(String.class)); } public static JsonNode getUser(String name) { final String uri = SERVER_ROOT_URI + "/users/browse/"+name; WebResource resource = Client.create().resource(uri); ClientResponse response = resource.get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus())); String resStr = response.getEntity(String.class); return Json.parse(resStr); } public static void postName() { final String uri = SERVER_ROOT_URI + "/hello"; WebResource resource = Client.create().resource(uri); ObjectNode jobj = Json.newObject(); jobj.put("name","taro"); // ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON) .entity(jobj.toString()) .post(ClientResponse.class); System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus())); System.out.println(response.getEntity(String.class)); } }