package com.cloudant.tests;

import static org.junit.Assert.assertNotNull;

import java.util.List;
import java.util.Properties;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.model.ApiKey;
import com.cloudant.client.api.model.Membership;
import com.cloudant.client.api.model.Task;
import com.cloudant.tests.util.Utils;

public class CloudantClientTests {

	private static final Log log = LogFactory.getLog(CloudantClientTests.class);
	private static CloudantClient account;
	public static CloudantClient cookieBasedClient;
	private static Properties props;

	@BeforeClass
	public static void setUpClass() {
		props = Utils.getProperties("cloudant.properties", log);
		account = new CloudantClient(props.getProperty("cloudant.account"),
				props.getProperty("cloudant.username"),
				props.getProperty("cloudant.password"));
		String cookie = account.getCookie();
		cookieBasedClient = new CloudantClient(
				props.getProperty("cloudant.account"), cookie);

	}

	@AfterClass
	public static void tearDownClass() {
		account.shutdown();
	}

	@Test
	public void apiKey() {
		ApiKey key = account.generateApiKey();
		assertNotNull(key);
		assertNotNull(key.getKey());
		assertNotNull(key.getPassword());
	}

	@Test
	public void activeTasks() {
		List<Task> tasks = account.getActiveTasks();
		assertNotNull(tasks);
	}

	@Test
	public void membership() {
		Membership mship = account.getMembership();
		assertNotNull(mship);
		assertNotNull(mship.getClusterNodes());
		assertNotNull(mship.getClusterNodes().hasNext());
		assertNotNull(mship.getAllNodes());
		assertNotNull(mship.getAllNodes().hasNext());
	}

	@Test
	public void cookieTest() {
		Membership membership = cookieBasedClient.getMembership();
		assertNotNull(membership);
	}

	@Test
	public void cookieAPITest() {
		boolean exceptionRaised = false;
		try{
			cookieBasedClient.generateApiKey();
			exceptionRaised = false ;
		}catch(Exception e){
			exceptionRaised = true ;
		}
		if(exceptionRaised == false){
			Assert.fail("Need to connect from cloudant using UserName & Password");
		}

	}

	@Test
	public void cookieNegativeTest() {
		String cookie = account.getCookie() + "XXX";
		boolean exceptionRaised = false;
		try {
			cookieBasedClient = new CloudantClient(
					props.getProperty("cloudant.account"), cookie);
			exceptionRaised = false;
		} catch (Exception e) {
			exceptionRaised = true;
		}
		if (exceptionRaised == false) {
			Assert.fail("could not connect to cloudant. Un-recognized cookie");
		}
	}

}

@@ -334,16 +334,57 @@
 t.Test;%0D
+%0Aimport org.lightcouch.CouchDbException;%0D
 %0A%0D%0Aimpor
@@ -2119,49 +2119,14 @@
  %7B%0D%0A
-%09%09boolean exceptionRaised = false;%0D%0A
 %09%09try
+ 
 %7B%0D%0A%09
@@ -2171,200 +2171,127 @@
 %0A%09%09%09
-exceptionRaised = false ;%0D%0A%09%09%7Dcatch(Exception e)%7B%0D%0A%09%09%09exceptionRaised = true ;%0D%0A%09%09%7D%0D%0A%09%09if(exceptionRaised == fals
+Assert.fail(%22Passed cloudant.com based generateapikey without creds%22);%0D%0A%09%09%7D%0D%0A%09%09catch(IllegalArgumentException 
 e)
+ 
 %7B%0D%0A%09%09%09
-Assert.fail(%22Need to connect from cloudant using UserName & Password%22);
 %0D%0A%09%09
@@ -2418,28 +2418,27 @@
 ionRaised = 
-fals
+tru
 e;%0D%0A%09%09try %7B%0D
@@ -2441,36 +2441,16 @@
 y %7B%0D%0A%09%09%09
-cookieBasedClient = 
 new Clou
@@ -2513,16 +2513,28 @@
  cookie)
+.getAllDbs()
 ;%0D%0A%09%09%09ex
@@ -2568,16 +2568,23 @@
  catch (
+CouchDb
 Exceptio
@@ -2591,16 +2591,66 @@
 n e) %7B%0D%0A
+%09%09%09if ( e.getMessage().contains(%22Forbidden%22) ) %7B%0D%0A
 %09%09%09excep
@@ -2669,16 +2669,22 @@
  true;%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09i
@@ -2741,12 +2741,8 @@
 uld 
-not 
 conn
@@ -2760,23 +2760,32 @@
 dant
-. Un-recognized
+ with random AuthSession
  coo

Update CloudantClientTests.java