package eu.dnetlib.pid.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.socialhistoryservices.pid.PidType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author alessia
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PidServiceClientTest {

	private static final Log log = LogFactory.getLog(PidServiceClientTest.class); // NOPMD by marko on 11/24/08 5:02 PM

	@Resource
	private PidServiceClient pidServiceClient;
	private final String namingAuthority = "12345.1";

	/**
	 * Test method for {@link eu.dnetlib.pid.service.PidServiceClient#createPid(java.lang.String)}.
	 */
	@Test
	public void testCreatePid() {
		final PidType pid = this.pidServiceClient.createPid(this.namingAuthority);
		log.debug(pid);
	}

	/**
	 * Test method for {@link eu.dnetlib.pid.service.PidServiceClient#deletePids(java.lang.String)}.
	 */
	@Test
	public void testDeletePids() {
		final int count = this.pidServiceClient.deletePids(this.namingAuthority);
		log.debug("deleted " + count + " pids");
	}

	/**
	 * Test method for {@link eu.dnetlib.pid.service.PidServiceClient#getPid(java.lang.String)}.
	 */
	@Test
	public void testGetPid() {
		final PidType pid = this.pidServiceClient.createPid(this.namingAuthority);
		final PidType res = this.pidServiceClient.getPid(pid.getPid());
		assertEquals(pid.getPid(), res.getPid());
	}

	/**
	 * Test method for
	 * {@link eu.dnetlib.pid.service.PidServiceClient#getPidByAttribute(java.lang.String, java.lang.String)}.
	 */
	@Test
	public void testGetPidByAttribute() {
		final String localID = "0";
		final String resolveURL = "http://isti.cnr.it";
		final PidType pid = this.pidServiceClient.getQuickPid(this.namingAuthority, localID, resolveURL);
		final List<PidType> resL = this.pidServiceClient.getPidByAttribute(this.namingAuthority, localID);
		assertTrue(resL.size() == 1);
		final List<PidType> resR = this.pidServiceClient.getPidByAttribute(this.namingAuthority, resolveURL);
		assertEquals(resL.get(0).getPid(), pid.getPid());
		assertEquals(resR.get(0).getPid(), pid.getPid());

	}

	/**
	 * Test method for
	 * {@link eu.dnetlib.pid.service.PidServiceClient#getQuickPid(java.lang.String, java.lang.String, java.lang.String)}
	 * .
	 */
	@Test
	public void testGetQuickPid() {
		final String localID = "100";
		final String resolveURL = "http://isti.cnr.it/1";
		final PidType pid = this.pidServiceClient.getQuickPid(this.namingAuthority, localID, resolveURL);
		log.debug(pid);
	}

	/**
	 * Test method for
	 * {@link eu.dnetlib.pid.service.PidServiceClient#getQuickPid(java.lang.String, java.lang.String, java.lang.String)}
	 * .
	 */
	@Test
	public void testGetQuickPid_NoResolveURL() {
		final String localID = "101";
		final PidType pid = this.pidServiceClient.getQuickPid(this.namingAuthority, localID, null);
		log.debug(pid);
	}

	/**
	 * Test method for
	 * {@link eu.dnetlib.pid.service.PidServiceClient#update(java.lang.String, java.lang.String, java.lang.String, java.util.List)}
	 * .
	 */
	@Test
	public void testUpdate() {
		final String resolveURL = "http://isti.cnr.it";
		final String localID = "2";
		final PidType pid = this.pidServiceClient.createPid(this.namingAuthority);
		final PidType res = this.pidServiceClient.update(pid.getPid(), resolveURL, localID, null);
		final PidType quickPid = this.pidServiceClient.getQuickPid(this.namingAuthority, localID, resolveURL + "/foo");
		assertEquals(res.getPid(), pid.getPid());
		assertEquals(res.getLocalIdentifier(), quickPid.getLocalIdentifier());
		assertFalse(res.getResolveUrl().equals(quickPid.getResolveUrl()));
	}

	/**
	 * Test method for {@link eu.dnetlib.pid.service.PidServiceClient#updatePid(eu.consortiumservices.pid.PidType)}.
	 */
	@Test
	public void testUpdatePid() {
		final String resolveURL = "http://isti.cnr.it";
		final String localID = "3";
		final PidType pid = this.pidServiceClient.createPid(this.namingAuthority);
		pid.setResolveUrl(resolveURL + "/xxx");
		pid.setLocalIdentifier(localID);
		final PidType updated = this.pidServiceClient.updatePid(pid);
		final PidType quickPid = this.pidServiceClient.getQuickPid(this.namingAuthority, localID, resolveURL + "/foo");
		assertEquals(updated.getPid(), pid.getPid());
		assertEquals(updated.getLocalIdentifier(), quickPid.getLocalIdentifier());
		assertFalse(updated.getResolveUrl().equals(quickPid.getResolveUrl()));
	}

	@Test
	public void testUpsert() {
		final String resolveURL = "http://isti.cnr.it";
		final String localID = "4";
		final PidType res = this.pidServiceClient.upsert(this.namingAuthority, "", resolveURL, localID, null);
	}

}
