package org.sagebionetworks.doi;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Random;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.junit.Test;
import org.sagebionetworks.StackConfiguration;

public class EzidClientTest {

	@Test
	public void testConstructor() throws Exception {
		DoiClient client = new EzidClient();
		assertNotNull(client);
		Field field = EzidClient.class.getDeclaredField("client");
		assertNotNull(field);
		field.setAccessible(true);
		DefaultHttpClient httpClient = (DefaultHttpClient)field.get(client);
		assertNotNull(httpClient);
		assertEquals(Integer.valueOf(6000), httpClient.getParams().getParameter(CoreConnectionPNames.SO_TIMEOUT));
		assertEquals("Synapse", httpClient.getParams().getParameter(CoreProtocolPNames.USER_AGENT));
		AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "EZID", AuthPolicy.BASIC);
		Credentials credentials = httpClient.getCredentialsProvider().getCredentials(authScope);
		assertNotNull(credentials);
		assertEquals(StackConfiguration.getEzidUsername(), credentials.getUserPrincipal().getName());
		assertEquals(StackConfiguration.getEzidPassword(), credentials.getPassword());
	}

	@Test
	public void testCreate() throws Exception {
		final EzidMetadata metadata = new EzidMetadata();
		// This is an EZID test "domain"
		// Salt it with a random id
		Random r = new Random();
		String id = Integer.toHexString(r.nextInt());
		final String doi = "doi:10.5072/FK2." + id;
		metadata.setDoi(doi);
		final String target = "https://synapse.sagebase.org/";
		metadata.setTarget(target);
		final String creator = "Test, Something";
		metadata.setCreator(creator);
		final String title = "This is a test";
		metadata.setTitle(title);
		final String publisher = "Sage Bionetworks";
		metadata.setPublisher(publisher);
		final int year = Calendar.getInstance().get(Calendar.YEAR);
		metadata.setPublicationYear(year);
		// Create
		DoiClient client = new EzidClient();
		client.create(metadata);
	}
}

@@ -1650,67 +1650,52 @@
 %09// 
-This is an EZID test %22domain%22%0A%09%09// Salt it with a random id
+Create a random ID in the EZID test %22domain%22
 %0A%09%09R
@@ -1766,16 +1766,16 @@
 Int());%0A
-
 %09%09final 
@@ -2330,11 +2330,829 @@
 ata);%0A%09%7D
+%0A%0A%09@Test(expected=RuntimeException.class)%0A%09public void testCreateWithException() throws Exception %7B%0A%09%09final EzidMetadata metadata = new EzidMetadata();%0A%09%09Random r = new Random();%0A%09%09String id = Integer.toHexString(r.nextInt());%0A%09%09// Invalid doi%0A%09%09final String doi = %22doi:10.99999/test.invalid.%22 + id;%0A%09%09metadata.setDoi(doi);%0A%09%09final String target = %22https://synapse.sagebase.org/%22;%0A%09%09metadata.setTarget(target);%0A%09%09final String creator = %22Test, Something%22;%0A%09%09metadata.setCreator(creator);%0A%09%09final String title = %22This is a test%22;%0A%09%09metadata.setTitle(title);%0A%09%09final String publisher = %22Sage Bionetworks%22;%0A%09%09metadata.setPublisher(publisher);%0A%09%09final int year = Calendar.getInstance().get(Calendar.YEAR);%0A%09%09metadata.setPublicationYear(year);%0A%09%09// Create%0A%09%09DoiClient client = new EzidClient();%0A%09%09client.create(metadata);%0A%09%7D
 %0A%7D%0A

Add testing invalid doi