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

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

There is a newer version: 2.20.1
Show newest version
/*
 * Copyright (c) 2015 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.model.ApiKey;
import com.cloudant.client.api.model.Permissions;
import com.cloudant.client.api.model.Shard;
import com.cloudant.client.org.lightcouch.CouchDbException;
import com.cloudant.test.main.RequiresCloudant;
import com.cloudant.test.main.RequiresCloudantLocal;
import com.cloudant.test.main.RequiresCloudantService;
import com.cloudant.test.main.RequiresCouch;
import com.cloudant.test.main.RequiresDB;
import com.cloudant.tests.util.CloudantClientResource;
import com.cloudant.tests.util.DatabaseResource;
import com.cloudant.tests.util.MockWebServerResources;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;

import java.net.MalformedURLException;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Category(RequiresDB.class)
public class DatabaseTest {

    public static CloudantClientResource clientResource = new CloudantClientResource();
    public static DatabaseResource dbResource = new DatabaseResource(clientResource);

    @ClassRule
    public static RuleChain chain = RuleChain.outerRule(clientResource).around(dbResource);
    @ClassRule
    public static MockWebServer mockWebServer = new MockWebServer();

    private static Database db;
    private static CloudantClient account;

    @BeforeClass
    public static void setUp() throws Exception {
        account = clientResource.get();
        db = dbResource.get();

        //replicate animaldb for tests
        com.cloudant.client.api.Replication r = account.replication();
        r.source("http://clientlibs-test.cloudant.com/animaldb");
        r.createTarget(true);
        r.target(dbResource.getDbURIWithUserInfo());
        r.trigger();
    }

    @Test
    @Category(RequiresCloudantService.class)
    public void permissions() {
        Map> userPerms = db.getPermissions();
        assertNotNull(userPerms);
        ApiKey key = account.generateApiKey();
        EnumSet p = EnumSet.of(Permissions._reader, Permissions._writer);
        db.setPermissions(key.getKey(), p);
        userPerms = db.getPermissions();
        assertNotNull(userPerms);
        assertEquals(1, userPerms.size());
        assertEquals(p, userPerms.get(key.getKey()));

        p = EnumSet.noneOf(Permissions.class);
        db.setPermissions(key.getKey(), p);
        userPerms = db.getPermissions();
        assertNotNull(userPerms);
        assertEquals(1, userPerms.size());
        assertEquals(p, userPerms.get(key.getKey()));
    }

    /**
     * Test that when called against a DB that is not a Cloudant service
     * an UnsupportedOperationException is thrown
     */
    @Test(expected = UnsupportedOperationException.class)
    @Category({RequiresCouch.class, RequiresCloudantLocal.class})
    public void testPermissionsException() {
        Map> userPerms = db.getPermissions();
    }

    @Test
    public void permissionsParsing() throws Exception {
        CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer)
                .build();
        Database db = client.database("notarealdb", false);

        // Mock up a request of all permissions
        mockWebServer.enqueue(MockWebServerResources.PERMISSIONS); // for GET _security
        mockWebServer.enqueue(MockWebServerResources.JSON_OK); // for PUT _security
        db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));

        // Mock up a failing request
        String testError = "test error";
        String testReason = "test reason";
        mockWebServer.enqueue(MockWebServerResources.PERMISSIONS); // for GET _security
        mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("{\"reason\":\"" +
                testReason + "\", \"error\":\"" + testError + "\"}"));
        try {
            db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
        } catch (CouchDbException e) {
            assertEquals("", testError, e.getError());
            assertEquals("", testReason, e.getReason());
        }
    }

    @Test
    @Category(RequiresCloudant.class)
    public void shards() {
        List shards = db.getShards();
        assert (shards.size() > 0);
        for (Shard s : shards) {
            assertNotNull(s.getRange());
            assertNotNull(s.getNodes());
            assertNotNull(s.getNodes().hasNext());
        }
    }

    @Test
    @Category(RequiresCloudant.class)
    public void shard() {
        Shard s = db.getShard("snipe");
        assertNotNull(s);
        assertNotNull(s.getRange());
        assertNotNull(s.getNodes());
        assert (s.getNodes().hasNext());
    }


    @Test
    @Category(RequiresCloudant.class)
    public void QuorumTests() {

        db.save(new Animal("human"), 2);
        Animal h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params()
                .readQuorum(2));
        assertNotNull(h);
        assertEquals("human", h.getId());

        db.update(h.setClass("inhuman"), 2);
        h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params().readQuorum
                (2));
        assertEquals("inhuman", h.getclass());

        db.post(new Animal("test"), 2);
        h = db.find(Animal.class, "test", new com.cloudant.client.api.model.Params().readQuorum(3));
        assertEquals("test", h.getId());


    }

    //Test case for issue #31
    @Test
    public void customGsonDeserializerTest() throws MalformedURLException {
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        CloudantClient account  = CloudantClientHelper.getClientBuilder()
                .gsonBuilder(builder)
                .build();

        Database db = account.database(dbResource.getDatabaseName(), false);

        Map h = new HashMap();
        h.put("_id", "serializertest");
        h.put("date", "2015-01-23T18:25:43.511Z");
        db.save(h);

        db.find(Foo.class, "serializertest"); // should not throw a JsonSyntaxException

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy