/*
 * Copyright 2014-2016 MarkLogic Corporation
 *
 * 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.marklogic.client.functionaltest;

import static org.junit.Assert.assertEquals;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.DatabaseClientFactory.Authentication;
import com.marklogic.client.FailedRequestException;

public class TestRuntimeDBselection extends BasicJavaClientREST {
	private static String dbName = "TestRuntimeDB";
	private static String [] fNames = {"TestRuntimeDB-1"};
	
	//
	private  DatabaseClient client ;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		System.out.println("In setup");
		configureRESTServer(dbName, fNames, false);
	     createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
	     createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
	    
		 //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		System.out.println("In tear down" );
		cleanupRESTServer(dbName, fNames);
		deleteRESTUser("eval-user");
		deleteUserRole("test-eval");
	}

	@Test
	public void testRuntimeDBclientWithDefaultUser() throws KeyManagementException, NoSuchAlgorithmException, Exception {
		if 	(!IsSecurityEnabled()) {
		associateRESTServerWithDefaultUser(getRestServerName(),"eval-user","application-level");
		int restPort = getRestServerPort();
		client = DatabaseClientFactory.newClient("localhost", restPort, dbName);
		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
		client.newServerEval().xquery(insertJSON).eval();
		String query1 = "fn:count(fn:doc())";
		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",1,response);
		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
		client.newServerEval().javascript(query2).eval();
		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",0,response2);
		client.release();
		associateRESTServerWithDefaultUser(getRestServerName(),"nobody","digest");
		}
	}
	//Issue 184 exists 
	@Test
	public void testRuntimeDBclientWithDifferentAuthType() throws KeyManagementException, NoSuchAlgorithmException, Exception {
		if 	(!IsSecurityEnabled()) {
		associateRESTServerWithDefaultUser(getRestServerName(),"nobody","basic");
		int restPort = getRestServerPort();
		
		client = getDatabaseClientOnDatabase("localhost", restPort, dbName,"eval-user","x",Authentication.BASIC);
		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
		client.newServerEval().xquery(insertJSON).eval();
		String query1 = "fn:count(fn:doc())";
		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",1,response);
		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
		client.newServerEval().javascript(query2).eval();
		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",0,response2);
		client.release();
		associateRESTServerWithDefaultUser(getRestServerName(),"nobody","digest");
		}
	}
	//issue 141 user with no privileges for eval
	/*
	 * If ssl is enabled, then there throw an exception, since the test is expecting it, with BASIC
	 */
	@Test(expected=FailedRequestException.class)
	public void testRuntimeDBclientWithNoPrivUser() throws KeyManagementException, NoSuchAlgorithmException, Exception {
		int restPort = getRestServerPort();
		if 	(IsSecurityEnabled()) throw new FailedRequestException("Illegal");
		else
			client = DatabaseClientFactory.newClient("localhost", restPort, dbName,"rest-admin","x",Authentication.BASIC);
		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
		client.newServerEval().xquery(insertJSON).eval();
		String query1 = "fn:count(fn:doc())";
		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",1,response);
		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
		client.newServerEval().javascript(query2).eval();
		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
		assertEquals("count of documents ",0,response2);
		client.release();
		
	}
}

@@ -864,32 +864,58 @@
 t.BeforeClass;%0D%0A
+import org.junit.Ignore;%0D%0A
 import org.junit
@@ -1324,13 +1324,8 @@
 %0A%09%0D%0A
-%09//%0D%0A
 %09pri
@@ -2061,21 +2061,109 @@
 %0A%09%7D%0D%0A%0D%0A%09
-@Test
+// Ignoring the test due to enhanced security and need for a authentication digest.%0D%0A%09@Ignore
 %0D%0A%09publi

Test changes for authentications.