test.java.com.cloudant.tests.DesignDocumentTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudant-client Show documentation
Show all versions of cloudant-client Show documentation
Official Cloudant client for Java
/*
* Copyright (c) 2016 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.cloudant.tests;
import com.cloudant.client.api.model.DesignDocument;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Rhys Short on 24/02/2016.
*/
@RunWith(Enclosed.class)
public class DesignDocumentTest {
static Gson gson = new GsonBuilder().create();
enum Field {
mrView,
listFunction,
showFunction,
updateFunction,
filterFunction,
rewriteRule,
indexes,
validateDocUpdate
}
private static DesignDocument getDesignDocument() {
return getDesignDocumentWithFields(EnumSet.allOf(Field.class));
}
private static DesignDocument getDesignDocumentWithFields(EnumSet fields) {
DesignDocument designDocument = new DesignDocument();
for (Field f : fields) {
switch (f) {
case mrView:
mrView(designDocument);
break;
case listFunction:
listFunction(designDocument);
break;
case showFunction:
showFunction(designDocument);
break;
case updateFunction:
updateFunction(designDocument);
break;
case filterFunction:
filterFunction(designDocument);
break;
case rewriteRule:
rewriteRule(designDocument);
break;
case indexes:
indexes(designDocument);
break;
case validateDocUpdate:
validateDocUpdate(designDocument);
break;
default:
break;
}
}
return designDocument;
}
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