All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.java.com.cloudant.tests.DesignDocumentTest Maven / Gradle / Ivy

There is a newer version: 2.20.1
Show newest version
/*
 * 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> rewrites = new ArrayList>();
                Map rewrite = new HashMap();
                rewrite.put("from", "/index.php");
                rewrite.put("to", "index.html");
                rewrite.put("method", "GET");
                rewrite.put("query", new HashMap());
                rewrites.add(0, rewrite);

                JsonArray rewriteJson = (JsonArray) gson.toJsonTree(rewrites);
                designDocument.setRewrites(rewriteJson);
                break;
            case indexes:
                Map> indexes = new HashMap>();

                Map index = new HashMap();
                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;
    }

    public static class OneOffEqualityTests {
        @Test
        public void testDesignDocEqualsForAllFields() {
            Assert.assertEquals(getDesignDocument(), getDesignDocument());
        }
    }

    @RunWith(Parameterized.class)
    public static final class ParameterizedEqualityTests {

        /**
         * Parameters for these tests so we run each test multiple times.
         * We run with a single key or a complex key and both ascending and descending.
         */
        @Parameterized.Parameters(name = "Field:{0}")
        public static Collection data() {
            return EnumSet.allOf(Field.class);
        }

        @Parameterized.Parameter
        public Field field;

        /**
         * Tests the design docs are equal for each field in turn.
         */
        @Test
        public void testDesignDocEqualsForEachField() {
            Assert.assertEquals(getDesignDocumentWithFields(EnumSet.of(field)),
                    getDesignDocumentWithFields(EnumSet.of(field)));
        }

        /**
         * Tests the design docs are not equal when each field is empty in one of the compared docs.
         *
         * @throws Exception
         */
        @Test
        public void testDesignDocNotEqualEmpty() throws Exception {
            Assert.assertNotEquals(getDesignDocument(), getDesignDocumentWithFields(EnumSet
                    .complementOf(EnumSet.of(field))));
        }

        /**
         * Tests the design docs are not equal when each field is different in one of the
         * compared docs.
         *
         * @throws Exception
         */
        @Test
        public void testDesignDocNotEqualDifferent() throws Exception {
            Assert.assertNotEquals(getDesignDocument(), getDesignDocumentWithDifferent(field));
        }

    }

    private static void indexes(DesignDocument designDocument) {
        Map> indexes = new HashMap>();

        Map index = new HashMap();
        index.put("index", "function(doc){....}");
        indexes.put("animal", index);

        JsonObject indexesJson = (JsonObject) gson.toJsonTree(indexes);
        designDocument.setIndexes(indexesJson);
    }

    private static void rewriteRule(DesignDocument designDocument) {
        List> rewrites = new ArrayList>();
        Map rewrite = new HashMap();
        rewrite.put("from", "/");
        rewrite.put("to", "index.html");
        rewrite.put("method", "GET");
        rewrite.put("query", new HashMap());
        rewrites.add(0, rewrite);

        JsonArray rewriteJson = (JsonArray) gson.toJsonTree(rewrites);
        designDocument.setRewrites(rewriteJson);
    }

    private static void filterFunction(DesignDocument designDocument) {
        Map filterFunctions = new HashMap();
        filterFunctions.put("myFilter", "function(doc,req){return false;}");
        designDocument.setFilters(filterFunctions);
    }

    private static void updateFunction(DesignDocument designDocument) {
        Map updateFunctions = new HashMap();
        updateFunctions.put("myUpdate", "function(doc,req){return [doc, 'Edited World!'];}");
        designDocument.setUpdates(updateFunctions);
    }

    private static void showFunction(DesignDocument designDocument) {
        Map showFunctions = new HashMap();
        showFunctions.put("myShow", "function(doc,req){ if(doc){return \"hello world!\"}");
        designDocument.setShows(showFunctions);
    }

    private static void listFunction(DesignDocument designDocument) {
        Map listFunctions = new HashMap();
        listFunctions.put("myList", "function(head,req){ send(toJson(getRow); }");
        designDocument.setLists(listFunctions);
    }

    private static void mrView(DesignDocument designDocument) {
        DesignDocument.MapReduce mapReduce = new DesignDocument.MapReduce();
        mapReduce.setMap("function(doc){emit(doc.hello);}");
        mapReduce.setReduce("_stats");
        mapReduce.setDbCopy("myOtherDB");
        Map views = new HashMap();
        views.put("helloWorldView", mapReduce);
        designDocument.setViews(views);
    }

    private static void validateDocUpdate(DesignDocument designDocument) {
        designDocument.setValidateDocUpdate("throw({ forbidden: 'Error message here.' });");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy