view test/RequestTest.java @ 24:81d1d7c7bcde

create getConsensus action. but this action can not test because there is no createMention action.
author one
date Wed, 03 Oct 2012 01:46:43 +0900
parents cad8e054fe4b
children 97b249d9fad1
line wrap: on
line source

import javax.ws.rs.core.MediaType;

import models.NodeModel;

import org.json.JSONException;
import org.json.JSONObject;

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) throws JSONException {
		String user1 = "akifumi";
		String user2 = "takaaki";
		String user3 = "yosiaki";

		createUser(user1);
		createUser(user2);
		createUser(user3);

		String[] users = {user2,user3};
		createClaim(user1, users);
		createClaim(user1, users);
/*
		HashSet<Object> set = new HashSet<Object>();
		set.add("1");
		set.add("2");
		set.add("3");
		Iterator<Object> iter = set.iterator();
		while (iter.hasNext() ) {
			Object o = iter.next();
			System.out.println("o = "+o);
		}

		Iterator<Object> iter2 = set.iterator();
		while (iter2.hasNext() ) {
			Object o = iter2.next();
			System.out.println("o = "+o);
			if (set.remove("2")) {
				iter2 = set.iterator();
			}
		}*/
		
/*
		getUser(user1);
		getUserInfo(user2,"claims/");
*/

		getUserInfo(user2,"requests/");
		getUserInfo(user3,"requests/");
//		getUserInfo(user3,"claims/");
		getUserInfo(user3,"consensus/");

	}
	
	public static void createClaim(String author, String[] users) throws JSONException {
		JSONObject toulmin = new JSONObject();
		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,"");

		JSONObject jobj = new JSONObject();
		jobj.put(NodeModel.TOULMIN, toulmin);
		jobj.put(NodeModel.L_AUTHOR, author);
		jobj.put(NodeModel.USERS,users);
		jobj.put(NodeModel.TYPE, "unanimously");

		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()));
		System.out.println(response.getEntity(String.class));		
		
	}
	
	public static void 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()));
		System.out.println(response.getEntity(String.class));
	}
	
	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 void 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()));
		System.out.println(response.getEntity(String.class));
	}

	
	public static void postName() throws JSONException {
		final String uri = SERVER_ROOT_URI + "/hello";
		WebResource resource = Client.create().resource(uri);
		JSONObject jobj = new JSONObject();
		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));
		
	}
	
	
	
}