package us.jubat.clustering;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.msgpack.rpc.Client;

import us.jubat.common.Datum;
import us.jubat.testutil.JubaServer;
import us.jubat.testutil.JubatusClientTest;

public class ClusteringClientTest extends JubatusClientTest {
	private ClusteringClient client;

	public ClusteringClientTest() {
		super(JubaServer.clustering);
	}

	@Before
	public void setUp() throws Exception {
		server.start(server.getConfigPath());
		client = new ClusteringClient(server.getHost(), server.getPort(), NAME,
				TIMEOUT_SEC);
	}

	@After
	public void tearDown() throws Exception {
		server.stop();
	}

	@Test
	public void testGet_config() throws IOException {
		String config = client.getConfig();
		assertThat(formatAsJson(config),
				is(formatAsJson(server.getConfigData())));
	}

	@Test
	public void testSave_and_Load() {
		String id = "clustering.test_java-client.model";
		assertThat(client.save(id), is(true));
		assertThat(client.load(id), is(true));
	}

	@Test
	public void testGet_status() {
		Map<String, Map<String, String>> status = client.getStatus();
		assertThat(status, is(notNullValue()));
		assertThat(status.size(), is(1));
	}

	@Test
	public void testGet_client() {
		assertThat(client.getClient(), is(instanceOf(Client.class)));
		assertThat(client.getClient(), is(notNullValue()));
	}

	private Datum generateDatum() {
		Datum datum = new Datum();

		for (int i = 1; i <= 10; i++) {
			datum.addString("key/str" + Integer.toString(i), "val/str"
					+ Integer.toString(i));
		}

		for (int i = 1; i <= 10; i++) {
			datum.addNumber("key/num" + Integer.toString(i), i);
		}

		return datum;
	}

}

@@ -233,16 +233,67 @@
 eption;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -1653,16 +1653,1697 @@
 ));%0A%09%7D%0A%0A
+  @Test%0A  public void testPush() %7B%0A    List%3CDatum%3E data_list = new ArrayList%3CDatum%3E();%0A%09%09data_list.add(generateDatum());%0A    assertThat(client.push(data_list), is(true));%0A  %7D%0A%0A  @Test%0A  public void testGet_revision() %7B%0A      assertThat(client.getRevision(), is(instanceOf(Integer.class)));%0A  %7D%0A%0A  @Test%0A  public void testGet_core_members() %7B%0A    List%3CDatum%3E data_list = new ArrayList%3CDatum%3E();%0A%09%09data_list.add(generateDatum());%0A    client.push(data_list);%0A    List%3CList%3CWeightedDatum%3E%3E members = client.getCoreMembers();%0A    assertThat(members.size(), is(10));%0A    assertThat(members.get(0).size(), is(1));%0A    assertThat(members.get(0).get(0), instanceOf(WeightedDatum.class));%0A  %7D%0A%0A  @Test%0A  public void testGet_k_center() %7B%0A    for (int i = 0; i %3C 100; i++) %7B%0A      List%3CDatum%3E data_list = new ArrayList%3CDatum%3E();%0A      data_list.add(generateDatum());%0A      client.push(data_list);%0A    %7D%0A    List%3CDatum%3E centers = client.getKCenter();%0A    assertThat(centers.size(), is(10));%0A    assertThat(centers.get(0), instanceOf(Datum.class));%0A  %7D%0A%0A  @Test%0A  public void testGet_nearest_center() %7B%0A    for (int i = 0; i %3C 100; i++) %7B%0A      List%3CDatum%3E data_list = new ArrayList%3CDatum%3E();%0A      data_list.add(generateDatum());%0A      client.push(data_list);%0A    %7D%0A    Datum center = client.getNearestCenter(generateDatum());%0A    assertThat(center, instanceOf(Datum.class));%0A  %7D%0A%0A  @Test%0A  public void testGet_nearest_members() %7B%0A    List%3CDatum%3E data_list = new ArrayList%3CDatum%3E();%0A    data_list.add(generateDatum());%0A    client.push(data_list);%0A    List%3CWeightedDatum%3E members = client.getNearestMembers(generateDatum());%0A    assertThat(members.get(0), instanceOf(WeightedDatum.class));%0A  %7D%0A%0A
 %09private

Complete test for clustering