Skip to content

Commit

Permalink
basic java tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scivey committed Nov 30, 2015
1 parent 9c6c8ee commit cdcc632
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 35 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ test-clients:
$(MAKE) -C clients/python test
$(MAKE) -C clients/nodejs test
$(MAKE) -C clients/ruby test
$(MAKE) -C clients/java test

publish-clients: test-clients
$(MAKE) -C clients/python publish
Expand Down
3 changes: 3 additions & 0 deletions clients/java/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
publish:
cd client && sbt publishSigned

test:
cd client && sbt test

.PHONY: publish
7 changes: 5 additions & 2 deletions clients/java/client/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ crossPaths := false
// This forbids including Scala related libraries into the dependency
autoScalaLibrary := false
sourceDirectories in Compile += file("src")
// mainClass in (Compile, run) := Some("org.relevanced.client.Main")

// library dependencies. (org name) % (project name) % (version)
libraryDependencies ++= Seq(
"org.apache.thrift" % "libthrift" % "0.9.2",
"org.slf4j" % "slf4j-jdk14" % "1.7.12"
"org.slf4j" % "slf4j-jdk14" % "1.7.12",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
)

testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v")

publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Expand Down
33 changes: 0 additions & 33 deletions clients/java/client/src/main/java/org/relevanced/client/Main.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import org.relevanced.client.protocol.CreateCentroidRequest;
import org.relevanced.client.protocol.CreateCentroidResponse;
import org.relevanced.client.protocol.CreateDocumentResponse;
import org.relevanced.client.protocol.DeleteCentroidRequest;
import org.relevanced.client.protocol.DeleteCentroidResponse;
import org.relevanced.client.protocol.DeleteDocumentRequest;
import org.relevanced.client.protocol.DeleteDocumentResponse;
import org.relevanced.client.protocol.MultiDeleteDocumentsRequest;
import org.relevanced.client.protocol.MultiDeleteDocumentsResponse;
import org.relevanced.client.protocol.JoinCentroidRequest;
Expand Down Expand Up @@ -60,6 +64,16 @@ public AddDocumentsToCentroidResponse addDocumentsToCentroid(String centroidId,
return addDocumentsToCentroid(centroidId, docIds, false);
}

public AddDocumentsToCentroidResponse addDocumentToCentroid(String centroidId, String documentId, Boolean ignoreAlreadyInCentroid) throws TException {
List<String> documentIds = new ArrayList<String>();
documentIds.add(documentId);
return addDocumentsToCentroid(centroidId, documentIds, ignoreAlreadyInCentroid);
}

public AddDocumentsToCentroidResponse addDocumentToCentroid(String centroidId, String documentId) throws TException {
return addDocumentToCentroid(centroidId, documentId, false);
}

public CreateCentroidResponse createCentroid(String id, Boolean ignoreExisting) throws TException {
CreateCentroidRequest request = new CreateCentroidRequest();
request.id = id;
Expand All @@ -79,6 +93,28 @@ public CreateDocumentResponse createDocumentWithID(String id, String text) throw
return createDocumentWithID(id, text, Language.EN);
}

public DeleteCentroidResponse deleteCentroid(String id, Boolean ignoreMissing) throws TException {
DeleteCentroidRequest request = new DeleteCentroidRequest();
request.id = id;
request.ignoreMissing = ignoreMissing;
return deleteCentroid(request);
}

public DeleteCentroidResponse deleteCentroid(String id) throws TException {
return deleteCentroid(id, false);
}

public DeleteDocumentResponse deleteDocument(String id, Boolean ignoreMissing) throws TException {
DeleteDocumentRequest request = new DeleteDocumentRequest();
request.id = id;
request.ignoreMissing = ignoreMissing;
return deleteDocument(request);
}

public DeleteDocumentResponse deleteDocument(String id) throws TException {
return deleteDocument(id, false);
}

public double getTextSimilarity(String centroidId, String text) throws TException {
return getTextSimilarity(centroidId, text, Language.EN);
}
Expand Down Expand Up @@ -154,6 +190,16 @@ public RemoveDocumentsFromCentroidResponse removeDocumentsFromCentroid(String ce
return removeDocumentsFromCentroid(centroidId, docIds, false);
}

public RemoveDocumentsFromCentroidResponse removeDocumentFromCentroid(String centroidId, String documentId, Boolean ignoreNotInCentroid) throws TException {
List<String> documentIds = new ArrayList<String>();
documentIds.add(documentId);
return removeDocumentsFromCentroid(centroidId, documentIds, ignoreNotInCentroid);
}

public RemoveDocumentsFromCentroidResponse removeDocumentFromCentroid(String centroidId, String documentId) throws TException {
return removeDocumentFromCentroid(centroidId, documentId, false);
}

public static RelevancedBlockingClient connect(String host, int port) throws TException {
try {
TTransport transport = new TSocket(host, port);
Expand Down
135 changes: 135 additions & 0 deletions clients/java/client/src/test/java/RelevancedBlockingClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.relevanced.client.RelevancedBlockingClient;
import org.apache.thrift.TException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class RelevancedBlockingClientTest {
private void eraseEverything(RelevancedBlockingClient client) throws TException {
List<String> documentIds = client.listAllDocuments().documents;
List<String> centroidIds = client.listAllCentroids().centroids;
Boolean ignoreMissing = true;
client.multiDeleteCentroids(centroidIds, ignoreMissing);
client.multiDeleteDocuments(documentIds, ignoreMissing);
}

@Test
public void testCentroidCRUD() throws TException {
RelevancedBlockingClient client = RelevancedBlockingClient.connect("localhost", 8097);
eraseEverything(client);
client.createCentroid("something");
List<String> centroids = client.listAllCentroids().centroids;
List<String> expected = new ArrayList<String>();
expected.add("something");
assertEquals(expected, centroids);

List<String> toCreate = new ArrayList<String>();
toCreate.add("c1");
toCreate.add("c2");
toCreate.add("c3");
toCreate.add("c4");
client.multiCreateCentroids(toCreate);

centroids = client.listAllCentroids().centroids;
expected.clear();
expected.add("c1");
expected.add("c2");
expected.add("c3");
expected.add("c4");
expected.add("something");
assertEquals(expected, centroids);

client.deleteCentroid("c2");
centroids = client.listAllCentroids().centroids;
expected.clear();
expected.add("c1");
expected.add("c3");
expected.add("c4");
expected.add("something");
assertEquals(expected, centroids);

List<String> toDelete = new ArrayList<String>();
toDelete.add("c3");
toDelete.add("something");
client.multiDeleteCentroids(toDelete);

centroids = client.listAllCentroids().centroids;
expected.clear();
expected.add("c1");
expected.add("c4");
assertEquals(expected, centroids);
}

@Test
public void testDocumentCRUD() throws TException {
RelevancedBlockingClient client = RelevancedBlockingClient.connect("localhost", 8097);
eraseEverything(client);
client.createDocumentWithID("doc-1", "some text");
String createdID = client.createDocument("some other text").id;
List<String> docs = client.listAllDocuments().documents;
List<String> expected = new ArrayList<String>();
expected.add("doc-1");
expected.add(createdID);
Collections.sort(expected);
assertEquals(expected, docs);

client.deleteDocument(createdID);
expected.clear();
expected.add("doc-1");
docs = client.listAllDocuments().documents;
assertEquals(expected, docs);

client.createDocumentWithID("doc-2", "more text");
client.createDocumentWithID("doc-3", "more text");
client.createDocumentWithID("doc-4", "more text");

docs = client.listAllDocuments().documents;
expected.clear();
expected.add("doc-1");
expected.add("doc-2");
expected.add("doc-3");
expected.add("doc-4");
assertEquals(expected, docs);

List<String> toDelete = new ArrayList<String>();
toDelete.add("doc-2");
toDelete.add("doc-4");
client.multiDeleteDocuments(toDelete);

docs = client.listAllDocuments().documents;
expected.clear();
expected.add("doc-1");
expected.add("doc-3");
assertEquals(expected, docs);
}

@Test
public void testCentroidDocumentOperations() throws TException {
RelevancedBlockingClient client = RelevancedBlockingClient.connect("localhost", 8097);
eraseEverything(client);
client.createCentroid("c1");
client.createCentroid("c2");
client.createDocumentWithID("d1", "some text");
client.createDocumentWithID("d2", "some text");
client.createDocumentWithID("d3", "some text");

List<String> expectedCentroids = new ArrayList<String>();
List<String> expectedDocs = new ArrayList<String>();
expectedCentroids.add("c1");
expectedCentroids.add("c2");
expectedDocs.add("d1");
expectedDocs.add("d2");
expectedDocs.add("d3");
assertEquals(expectedCentroids, client.listAllCentroids().centroids);
assertEquals(expectedDocs, client.listAllDocuments().documents);

client.addDocumentToCentroid("c1", "d2");
expectedDocs.clear();
expectedDocs.add("d2");
assertEquals(expectedDocs, client.listAllDocumentsForCentroid("c1").documents);
}
}

0 comments on commit cdcc632

Please sign in to comment.