Search in sources :

Example 1 with DesignDocument

use of com.cloudant.client.api.model.DesignDocument in project java-cloudant by cloudant.

the class DesignDocumentTest method getDesignDocumentWithDifferent.

private static DesignDocument getDesignDocumentWithDifferent(Field f) {
    DesignDocument designDocument = getDesignDocument();
    switch(f) {
        case mrView:
            DesignDocument.MapReduce mapReduce = new DesignDocument.MapReduce();
            mapReduce.setMap("function(doc){emit(doc.hello);}");
            mapReduce.setReduce("_count");
            mapReduce.setDbCopy("myOtherDB");
            designDocument.getViews().put("view2", mapReduce);
            break;
        case listFunction:
            designDocument.getLists().put("myList2", "function(head,req){ send(toJson(getRow)" + "; }");
            break;
        case showFunction:
            designDocument.getShows().put("myShow2", "function(doc,req){ if(doc){return " + "\"hello " + "world!\"}");
            break;
        case updateFunction:
            designDocument.getUpdates().put("myUpdate2", "function(doc,req){return [doc, " + "'Edited" + " " + "World!'];}");
            break;
        case filterFunction:
            designDocument.getFilters().put("myOtherFilter", "function(doc,req){return false;" + "}");
            break;
        case rewriteRule:
            List<Map<String, Object>> rewrites = new ArrayList<Map<String, Object>>();
            Map<String, Object> rewrite = new HashMap<String, Object>();
            rewrite.put("from", "/index.php");
            rewrite.put("to", "index.html");
            rewrite.put("method", "GET");
            rewrite.put("query", new HashMap<String, String>());
            rewrites.add(0, rewrite);
            JsonArray rewriteJson = (JsonArray) gson.toJsonTree(rewrites);
            designDocument.setRewrites(rewriteJson);
            break;
        case indexes:
            Map<String, Map<String, String>> indexes = new HashMap<String, Map<String, String>>();
            Map<String, String> index = new HashMap<String, String>();
            index.put("index", "function(doc){....}");
            indexes.put("movie", index);
            JsonObject indexesJson = (JsonObject) gson.toJsonTree(indexes);
            designDocument.setIndexes(indexesJson);
            break;
        case validateDocUpdate:
            designDocument.setValidateDocUpdate("throw({ unauthorized: 'Error message here.' " + "});");
            break;
        default:
            break;
    }
    return designDocument;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) DesignDocument(com.cloudant.client.api.model.DesignDocument) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DesignDocument

use of com.cloudant.client.api.model.DesignDocument in project java-cloudant by cloudant.

the class DesignDocumentsTest method updateDesignDocIndex.

@Test
public void updateDesignDocIndex() throws Exception {
    DesignDocument designDoc1 = DesignDocumentManager.fromFile(new File(String.format("%s/views101_design_doc.js", rootDesignDir)));
    designDoc1.setId("_design/MyAmazingDdoc");
    JsonObject indexes = designDoc1.getIndexes();
    designDoc1.setIndexes(null);
    Response response = designManager.put(designDoc1);
    designDoc1.setRevision(response.getRev());
    designDoc1.setIndexes(indexes);
    response = designManager.put(designDoc1);
    Utils.assertOKResponse(response);
}
Also used : Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) DesignDocument(com.cloudant.client.api.model.DesignDocument) JsonObject(com.google.gson.JsonObject) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with DesignDocument

use of com.cloudant.client.api.model.DesignDocument in project java-cloudant by cloudant.

the class DesignDocumentsTest method serializeJavascriptView.

/**
 * Test that when setting a javascript map function it is correctly serialized and deserialized.
 *
 * @throws Exception
 * @see #deserializeJavascriptView()
 * @see #serializeQueryDesignDoc()
 * @see #deserializeQueryDesignDoc()
 */
@Test
public void serializeJavascriptView() throws Exception {
    // Create and write a design document with a javascript lang map function
    String testDDocName = "testJSMapFn";
    String mapFunction = "function(doc){emit([doc.contentArray[0].boolean,doc.contentArray[0]" + ".creator,doc.contentArray[0].created],doc);}";
    DesignDocument ddoc = new DesignDocument();
    ddoc.setId(testDDocName);
    Map<String, DesignDocument.MapReduce> views = new HashMap<String, DesignDocument.MapReduce>();
    DesignDocument.MapReduce mr = new DesignDocument.MapReduce();
    mr.setMap(mapFunction);
    mr.setReduce("_count");
    views.put("testView", mr);
    ddoc.setViews(views);
    Response r = designManager.put(ddoc);
    // Retrieve the doc and check that the javascript function is correct
    DesignDocument retrievedDDoc = designManager.get(testDDocName, r.getRev());
    assertNotNull(retrievedDDoc, "There should be a retrieved design doc");
    Map<String, DesignDocument.MapReduce> retrievedViews = retrievedDDoc.getViews();
    assertNotNull(retrievedViews, "There should be views defined on the design doc");
    DesignDocument.MapReduce mrView = retrievedViews.get("testView");
    assertNotNull(mrView, "There should be a testView in the retrieved design doc");
    assertEquals(mapFunction, mrView.getMap(), "The map function string should be the " + "expected string");
}
Also used : Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) DesignDocument(com.cloudant.client.api.model.DesignDocument) Test(org.junit.jupiter.api.Test)

Example 4 with DesignDocument

use of com.cloudant.client.api.model.DesignDocument in project java-cloudant by cloudant.

the class Utils method putDesignDocs.

/**
 * Test utility to put design documents under the testing resource folder in to the database.
 *
 * @param db        database to put the design docs
 * @param directory location of design docs
 */
public static void putDesignDocs(Database db, File directory) throws FileNotFoundException {
    // Get design documents from directory
    List<DesignDocument> designDocuments = DesignDocumentManager.fromDirectory(directory);
    DesignDocumentManager designDocumentManager = db.getDesignDocumentManager();
    DesignDocument[] designDocArray = designDocuments.toArray(new DesignDocument[designDocuments.size()]);
    // Put documents into database
    designDocumentManager.put(designDocArray);
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) DesignDocumentManager(com.cloudant.client.api.DesignDocumentManager)

Example 5 with DesignDocument

use of com.cloudant.client.api.model.DesignDocument in project java-cloudant by cloudant.

the class SearchTests method setUp.

@BeforeAll
public static void setUp() throws Exception {
    // replicate the animals db for search tests
    com.cloudant.client.api.Replication r = account.replication();
    r.source("https://clientlibs-test.cloudant.com/animaldb");
    r.createTarget(true);
    r.target(dbResource.getDbURIWithUserInfo());
    r.trigger();
    // sync the design doc for faceted search
    File designDocViews101 = new File(String.format("%s/views101_design_doc.js", new File(System.getProperty("user.dir") + "/src/test/resources/design-files")));
    DesignDocument designDoc = DesignDocumentManager.fromFile(designDocViews101);
    db.getDesignDocumentManager().put(designDoc);
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) File(java.io.File) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

DesignDocument (com.cloudant.client.api.model.DesignDocument)15 Test (org.junit.jupiter.api.Test)10 Response (com.cloudant.client.api.model.Response)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 JsonObject (com.google.gson.JsonObject)2 File (java.io.File)2 HashMap (java.util.HashMap)2 DesignDocumentManager (com.cloudant.client.api.DesignDocumentManager)1 NoDocumentException (com.cloudant.client.org.lightcouch.NoDocumentException)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1