Permalink
Browse files

first integration test to verify couchdb-lucene against local couchdb…

… instance.
  • Loading branch information...
1 parent 527bb45 commit 20b7d8b0213d21f344041a5765855cccc1d61bd2 Robert Newson committed Aug 17, 2009
View
4 pom.xml
@@ -67,8 +67,8 @@
</dependency>
</dependencies>
<issueManagement>
- <system>lighthouseapp</system>
- <url>http://rnewson.lighthouseapp.com/projects/27420-couchdb-lucene/tickets</url>
+ <system>github</system>
+ <url>http://github.com/rnewson/couchdb-lucene/issues</url>
</issueManagement>
<scm>
<connection>scm:git:git://github.com/rnewson/couchdb-lucene.git</connection>
View
62 src/main/java/com/github/rnewson/couchdb/lucene/Database.java
@@ -32,8 +32,10 @@
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
+import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
@@ -72,27 +74,39 @@ public Database(final String url) {
return (String[]) JSONArray.fromObject(get("_all_dbs")).toArray(EMPTY_ARR);
}
- public JSONObject getAllDocsBySeq(final String dbname, final long startkey) throws HttpException, IOException {
+ public JSONObject getAllDocsBySeq(final String dbname, final long startkey) throws IOException {
return JSONObject.fromObject(get(String.format("%s/_all_docs_by_seq?startkey=%s&include_docs=true",
encode(dbname), startkey)));
}
- public JSONObject getAllDocs(final String dbname, final String startkey, final String endkey) throws HttpException, IOException {
- return JSONObject.fromObject(get(String.format("%s/_all_docs?startkey=%%22%s%%22&endkey=%%22%s%%22&include_docs=true",
- encode(dbname), encode(startkey), encode(endkey))));
+ public boolean createDatabase(final String dbname) throws IOException {
+ return put(encode(dbname), null) == 201;
}
- public JSONObject getAllDocsBySeq(final String dbname, final long startkey, final int limit) throws HttpException,
- IOException {
+ public boolean deleteDatabase(final String dbname) throws IOException {
+ return delete(encode(dbname)) == 201;
+ }
+
+ public boolean saveDocument(final String dbname, final String id, final String body) throws IOException {
+ return put(String.format("%s/%s", encode(dbname), id), body) == 201;
+ }
+
+ public JSONObject getAllDocs(final String dbname, final String startkey, final String endkey) throws IOException {
+ return JSONObject.fromObject(get(String.format(
+ "%s/_all_docs?startkey=%%22%s%%22&endkey=%%22%s%%22&include_docs=true", encode(dbname),
+ encode(startkey), encode(endkey))));
+ }
+
+ public JSONObject getAllDocsBySeq(final String dbname, final long startkey, final int limit) throws IOException {
return JSONObject.fromObject(get(String.format("%s/_all_docs_by_seq?startkey=%d&limit=%d&include_docs=true",
encode(dbname), startkey, limit)));
}
- public JSONObject getDoc(final String dbname, final String id) throws HttpException, IOException {
+ public JSONObject getDoc(final String dbname, final String id) throws IOException {
return JSONObject.fromObject(get(String.format("%s/%s", encode(dbname), id)));
}
- public JSONObject getDocs(final String dbname, final String... ids) throws HttpException, IOException {
+ public JSONObject getDocs(final String dbname, final String... ids) throws IOException {
final JSONArray keys = new JSONArray();
for (final String id : ids) {
keys.add(id);
@@ -104,11 +118,11 @@ public JSONObject getDocs(final String dbname, final String... ids) throws HttpE
.toString()));
}
- public JSONObject getInfo(final String dbname) throws HttpException, IOException {
+ public JSONObject getInfo(final String dbname) throws IOException {
return JSONObject.fromObject(get(encode(dbname)));
}
- private String get(final String path) throws HttpException, IOException {
+ private String get(final String path) throws IOException {
return execute(new GetMethod(url(path)));
}
@@ -124,18 +138,38 @@ String encode(final String path) {
}
}
- private String post(final String path, final String body) throws HttpException, IOException {
+ private String post(final String path, final String body) throws IOException {
final PostMethod post = new PostMethod(url(path));
post.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
return execute(post);
}
- private synchronized String execute(final HttpMethodBase method) throws HttpException, IOException {
+ private int put(final String path, final String body) throws IOException {
+ final PutMethod method = new PutMethod(url(path));
+ if (body != null) {
+ method.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
+ }
+ try {
+ return CLIENT.executeMethod(method);
+ } finally {
+ method.releaseConnection();
+ }
+ }
+
+ private int delete(final String path) throws IOException {
+ final DeleteMethod method = new DeleteMethod(url(path));
try {
+ return CLIENT.executeMethod(method);
+ } finally {
+ method.releaseConnection();
+ }
+ }
+ private String execute(final HttpMethodBase method) throws IOException {
+ try {
final int sc = CLIENT.executeMethod(method);
- if (sc == 401) {
- throw new HttpException("Unauthorized.");
+ if (sc < 200 || sc > 299) {
+ throw new HttpException("Unexpected status code: " + sc);
}
final InputStream in = method.getResponseBodyAsStream();
try {
View
63 src/test/java/com/github/rnewson/couchdb/lucene/IntegrationTest.java
@@ -0,0 +1,63 @@
+package com.github.rnewson.couchdb.lucene;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.File;
+import java.io.IOException;
+
+import net.sf.json.JSONObject;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * This test class requires a running couchdb instance on localhost port 5984.
+ *
+ * @author rnewson
+ *
+ */
+public class IntegrationTest {
+
+ private final String base = "http://localhost:5984/";
+ private final String dbname = "lucenetestdb";
+
+ private Database db;
+
+ @Before
+ public void setup() throws IOException, InterruptedException {
+ final File dir = new File("target/output");
+ FileUtils.cleanDirectory(dir);
+ System.setProperty("couchdb.lucene.dir", dir.getAbsolutePath());
+ db = new Database(base);
+ try {
+ db.deleteDatabase(dbname);
+ db.createDatabase(dbname);
+ } catch (final IOException e) {
+ assumeTrue(false);
+ }
+ }
+
+ @After
+ public void teardown() throws IOException {
+ // db.deleteDatabase(dbname);
+ }
+
+ @Test
+ public void index() throws IOException, InterruptedException {
+ final String ddoc = "{\"fulltext\": {\"idx\": {\"index\":\"function(doc) {var ret=new Document(); ret.add(doc.content); return ret;}\"}}}";
+ assertThat(db.saveDocument(dbname, "_design/lucene", ddoc), is(true));
+ for (int i = 0; i < 50; i++) {
+ assertThat(db.saveDocument(dbname, "doc-" + i, "{\"content\":\"hello\"}"), is(true));
+ }
+ SECONDS.sleep(5);
+
+ final JSONObject indexState = db.getDoc(dbname, "_fti");
+ assertThat(indexState.getInt("doc_count"), is(51));
+ }
+
+}

0 comments on commit 20b7d8b

Please sign in to comment.