view test/RequestTest.java @ 131:2b6e9496fb0c draft default tip

modified updateUserConsensusStatus
author one
date Fri, 26 Apr 2013 02:47:01 +0900
parents ed77c5258568
children
line wrap: on
line source

import java.util.ArrayList;

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 RequestTest {
	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[] users = {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;
		}
		
		createClaim(userA, users);

		
/*
		JsonNode user1Cons = getUserInfo(user1,"claims/");
		for (int i=0; i<user1Cons.size(); i++) {
			int id = user1Cons.get(i).asInt();
			System.out.println("id = "+id);
			editClaimInfo(user1, allUsersArray, id);
		}
*/		
		

		
		/*
		String[] users1 = {user2,user3};
		String[] u = {user2,user3};
		
		createClaim(user1, u);
*/			
/*		
		JsonNode user2Requests = getUserInfo(user2, "requests/");
		for (int i=0; i<user2Requests.size(); i++) {
			int id = user2Requests.get(0).asInt();
			String[] users = {user1,user3};
			createMention(user2, users, NodeModel.L_REFUTATION, id);
		}
		JsonNode user1Requests = getUserInfo(user1, "requests/");		
		for (int i=0; i< user1Requests.size(); i++) {
			int id = user1Requests.get(0).asInt();
			createMention(user1, u, NodeModel.L_REFUTATION, id);
		}
		 user2Requests = getUserInfo(user2, "requests/");
		for (int i=0; i<user2Requests.size(); i++) {
			int id = user2Requests.get(0).asInt();
			String[] users = {user1,user3};
			createMention(user2, users, NodeModel.L_REFUTATION, id);
		}
*/		

/*
		JsonNode user1Claim = getUserInfo(user1, "requests/");
		if (user2Claim.size() != 0) {
			int id = user1Claim.get(0).asInt();
			String[] users = {user2};
			createMention(user1, users, NodeModel.L_SUGGESTION, id);
			updateUserConsensusStatus(id, user1, NodeModel.AGREED);			
		}
*/

//		allAgreed(user1);
//		allAgreed(user2);

//		getClaimTree(6);

		
/*
		createClaim(user1, users1);
		String[] users2 = {user2};
		createClaim(user1, users1);
		JsonNode user1Claim =  getUserInfo(user1,"consensus/");
		getClaimInfo(user1Claim.get(0).asInt());
		editClaimInfo(user1, users2, user1Claim.get(0).asInt());
		getClaimInfo(user1Claim.get(0).asInt()); 		
		for (int i=0; i<user1Claim.size(); i++) {
			int claimId = user1Claim.get(i).asInt();
			String[] users = {user1};
			createMention(user2, users, NodeModel.L_QUESTION, claimId);
			createMention(user3, users, NodeModel.L_REFUTATION, claimId);
		}
		JsonNode user2Claim =  getUserInfo(user2,"claims/");
		for (int i=0; i<user2Claim.size(); i++) {
			int claimId = user2Claim.get(user2Claim.size()-1).asInt();
			String[] users = {user2};
			createMention(user1, users, NodeModel.L_SUGGESTION, claimId);
		}
		getUserInfo(user1,"requests/");
		getUserInfo(user1,"claims/");
		getUserInfo(user1,"consensus/");
		getUserInfo(user2,"requests/");
		getUserInfo(user2,"claims/");
		getUserInfo(user2,"consensus/");
		getUserInfo(user3,"claims/");
		getUserInfo(user3,"consensus/");
		allAgreed(user1);
		allAgreed(user2);
		allAgreed(user3);
		allAgreed(user4);
		/*
		for (int i=0; i<user1Claim.size(); i++) {
			int id = user1Claim.get(i).asInt();
			getClaimTree(id); 
			getUserConsensusStatus(id, user2);
		}
		*/
	}
	
	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(String author, String[] users, int id) {
		ObjectNode jobj =  createEditClaimParameter(author,users);
		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(String author, String[] users, String type, Object id) {
		ObjectNode jobj = createClaimParameter(author, users);
		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(String author, String[] users) {
		ObjectNode jobj = createClaimParameter(author, 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 createEditClaimParameter(String author, String[] users) {
		ObjectNode toulmin = Json.newObject();
		toulmin.put(NodeModel.TITLE, "Edit Test");
		toulmin.put(NodeModel.CONTENTS, "Edited Contents");
		toulmin.put(NodeModel.QUALIFIER, "Edited qualifier");
		toulmin.put(NodeModel.WARRANT,"Edited warrant");
		toulmin.put(NodeModel.BACKING, "Edited backing");
		toulmin.put(NodeModel.DATA,"Edited DATA");
		toulmin.put(NodeModel.REBUTTLE,"Edited REBUTTLE");
		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.MAJORITY );
		return jobj;
	}
	
	
	public static ObjectNode createClaimParameter(String author, String[] users) {
		ObjectNode toulmin = Json.newObject();
		toulmin.put(NodeModel.TITLE, "アプリでGraphDBを利用する。");
		toulmin.put(NodeModel.CONTENTS, "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。");
		toulmin.put(NodeModel.QUALIFIER, "絶対");
		toulmin.put(NodeModel.WARRANT,"GraphDBの実用例 etc...");
		toulmin.put(NodeModel.BACKING, "GraphDBの最新動向 etc...");
		toulmin.put(NodeModel.DATA,"GraphDBの実用例 etc...");
		toulmin.put(NodeModel.REBUTTLE,"");

		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));
	}
}