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;
}
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);
}
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");
}
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);
}
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);
}
Aggregations