Search in sources :

Example 11 with DesignDocument

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

the class DesignDocumentsTest method deleteDesignDocWithIndex.

/**
 * Test that a design document with an index can be deleted.
 *
 * @throws Exception
 */
@Test
public void deleteDesignDocWithIndex() throws Exception {
    // Put a design document with indices
    DesignDocument ddocWithIndices = fileToDesignDocument("views101");
    designManager.put(ddocWithIndices);
    // Now delete the design doc with indices
    designManager.remove("_design/views101");
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) Test(org.junit.jupiter.api.Test)

Example 12 with DesignDocument

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

the class DesignDocumentsTest method deserializeQueryDesignDoc.

/**
 * Test that deserializing a query language design document results in the correct form of the
 * map function.
 *
 * @throws Exception
 * @see #serializeJavascriptView()
 * @see #deserializeJavascriptView()
 * @see #serializeQueryDesignDoc()
 */
@Test
public void deserializeQueryDesignDoc() throws Exception {
    // Put the query design document
    designManager.put(fileToDesignDocument("query"));
    // Get the query design document
    DesignDocument queryDDoc = designManager.get("testQuery");
    Map<String, DesignDocument.MapReduce> views = queryDDoc.getViews();
    assertEquals(1, views.size(), "There should be one view");
    for (DesignDocument.MapReduce mrView : views.values()) {
        assertTrue(mrView.getMap().startsWith("{"), "The map function should be a javascript " + "function in a JSON form, " + "so start with {");
        assertTrue(mrView.getMap().endsWith("}"), "The map function should be a javascript " + "function in a JSON form, " + "so end with }");
        assertEquals("{\"fields\":{\"Person_dob\":\"asc\"}}", mrView.getMap(), "The map " + "function string should be an object form");
    }
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) Test(org.junit.jupiter.api.Test)

Example 13 with DesignDocument

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

the class DesignDocumentsTest method deserializeJavascriptView.

/**
 * Test that for javascript language MR view (i.e. the map function is a JSON string) that the
 * map function is correctly deserialized as a Java String.
 * The javascript string must be enclosed in \", for compatibility with JSON objects that might
 * be stored in maps we have to internally switch between the two, but the library did not
 * originally deal with both cases so for backwards compatibility must return the JSON strings
 * without the leading and trailing " when they are java strings.
 *
 * @throws Exception
 * @see #serializeJavascriptView()
 * @see #serializeQueryDesignDoc()
 * @see #deserializeQueryDesignDoc()
 */
@Test
public void deserializeJavascriptView() throws Exception {
    DesignDocument queryDDoc = fileToDesignDocument("example");
    Map<String, DesignDocument.MapReduce> views = queryDDoc.getViews();
    for (DesignDocument.MapReduce mrView : views.values()) {
        assertFalse(mrView.getMap().startsWith("\""), "The map function should not start with" + " \"");
        assertFalse(mrView.getMap().endsWith("\""), "The map function should not end with \"");
    }
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) Test(org.junit.jupiter.api.Test)

Example 14 with DesignDocument

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

the class DesignDocumentManager method fromFile.

/**
 * Deserialize a javascript design document file to a DesignDocument object.
 *
 * @param file the design document javascript file (UTF-8 encoded)
 * @return {@link DesignDocument}
 * @throws FileNotFoundException if the file does not exist or cannot be read
 */
public static DesignDocument fromFile(File file) throws FileNotFoundException {
    assertNotEmpty(file, "Design js file");
    DesignDocument designDocument;
    Gson gson = new Gson();
    InputStreamReader reader = null;
    try {
        reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
        // Deserialize JS file contents into DesignDocument object
        designDocument = gson.fromJson(reader, DesignDocument.class);
        return designDocument;
    } catch (UnsupportedEncodingException e) {
        // UTF-8 should be supported on all JVMs
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DesignDocument(com.cloudant.client.api.model.DesignDocument) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileInputStream(java.io.FileInputStream)

Example 15 with DesignDocument

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

the class DesignDocumentManager method put.

/**
 * Synchronizes a design document to the Database.
 * <p>This method will first try to find a document in the database with the same {@code _id}
 * as the given document, if it is not found then the given document will be saved to the
 * database.</p>
 * <p>If the document was found in the database, it will be compared with the given document
 * using {@link com.cloudant.client.api.model.DesignDocument#equals(Object)}. Both the
 * {@code _rev} value and content must match for the given documents to be considered equal.
 * This method will <strong>not</strong> update the revision of the local design document.</p>
 * <ul>
 * <li>If the documents are <i>equal</i> then no action is taken.</li>
 * <li>If the documents are <i>not equal</i> then the given document will be saved to the
 * database therefore updating the existing document.</li>
 * </ul>
 * <p>If the design document's {@code _id} is not prefixed with {@code _design/}, then the
 * {@code _design/} prefix will be added.</p>
 *
 * @param document the design document to synchronize
 * @return {@link Response} as a result of a document save or update, or returns {@code null}
 * if no action was taken and the document in the database is up-to-date with the given
 * document.
 */
public Response put(DesignDocument document) {
    CouchDbUtil.assertNotEmpty(document, "DesignDocument");
    // Ensure the _design prefix
    ensureDesignPrefixObject(document);
    DesignDocument documentFromDb;
    try {
        documentFromDb = get(document.getId());
    } catch (NoDocumentException e) {
        return db.save(document);
    }
    if (!document.equals(documentFromDb)) {
        document.setRevision(documentFromDb.getRevision());
        return db.update(document);
    }
    return null;
}
Also used : DesignDocument(com.cloudant.client.api.model.DesignDocument) NoDocumentException(com.cloudant.client.org.lightcouch.NoDocumentException)

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