view test/RequestTest.java @ 38:a2abe67d7c7a

fix createMention action bug
author one
date Thu, 04 Oct 2012 01:05:19 +0900
parents bc3ac73320f9
children 870553e92e3e
line wrap: on
line source

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 user1 = "akifumi";
		String user2 = "takaaki";
		String user3 = "yosiaki";
		String user4 = "suzuki";
		createUser(user1);
		createUser(user2);
		createUser(user3);
		createUser(user4);
		String[] users1 = {user2,user3,user4};
		createClaim(user1, users1);
		String[] users2 = {user2};
		createClaim(user1, users2);
		JsonNode user1Claim =  getUserInfo(user1,"consensus/");
		getClaimInfo(user1Claim.get(0).asInt());
		editClaimInfo(user1, users2, user1Claim.get(0).asInt());
		getClaimInfo(user1Claim.get(0).asInt()); 		
		String[] users3 = {user1};
		for (int i=0; i<user1Claim.size(); i++) {
			int claimId = user1Claim.get(i).asInt();
			createMention(user2, users3, NodeModel.L_QUESTION, claimId);
			createMention(user3, users3, NodeModel.L_REFUTATION, claimId);
		}
		JsonNode user2Claim =  getUserInfo(user2,"claims/");		
		for (int i=0; i<user2Claim.size(); i++) {
			int claimId = user2Claim.get(i).asInt();
			System.out.println("claimId = "+claimId);
			String[] users = {user2};
			createMention(user1, users, NodeModel.L_SUGGESTION, claimId);
		}

/*		
		getUserInfo(user1,"requests/");
		getUserInfo(user1,"consensus/");
		getUserInfo(user2,"claims/");

		getUserInfo(user2,"consensus/");
		getUserInfo(user3,"claims/");
		getUserInfo(user3,"consensus/");
*/
		for (int i=0; i<user1Claim.size(); i++) {
			int id = user1Claim.get(i).asInt(); 
			getClaimTree(id); 
		}
	}
	
	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 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));
	}
}