package uk.org.mygrid.cagrid.servicewrapper.serviceinvoker;

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

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.junit.Before;
import org.junit.Test;

import uk.ac.ebi.www.wsncbiblast.Data;
import uk.ac.ebi.www.wsncbiblast.InputParams;
import uk.org.mygrid.cagrid.servicewrapper.serviceinvoker.ncbiblast.DummyNCBIBlastInvoker;
import uk.org.mygrid.cagrid.servicewrapper.serviceinvoker.ncbiblast.NCBIBlastInput;
import uk.org.mygrid.cagrid.servicewrapper.serviceinvoker.ncbiblast.NCBIBlastInvoker;
import uk.org.mygrid.cagrid.servicewrapper.serviceinvoker.ncbiblast.NCBIBlastInvokerImpl;

public class NCBIInvokerTest {

	private static final Namespace NCBIBLAST_NS = Namespace.getNamespace("http://www.ebi.ac.uk/schema"); // sic
	private static final String EXPIRED_JOB_ID = "iprscan-20090529-1051066486";
	private NCBIBlastInvoker invoker;

	@Before
	public void findInvoker() throws InvokerException {
	//	invoker = new DummyNCBIBlastInvoker();		
		invoker = new NCBIBlastInvokerImpl();		
		
		// Only use the real InterProScanInvoker in integration tests		
	}

	
	@SuppressWarnings("unchecked")
	@Test
	public void invokeInterProScan() throws Exception {
		NCBIBlastInput analyticalServiceInput = new NCBIBlastInput();
		uk.ac.ebi.www.wsncbiblast.InputParams inputParams = uk.ac.ebi.www.wsncbiblast.InputParams.Factory.newInstance();
		inputParams.setEmail("mannen@soiland-reyes.com");
		inputParams.setDatabase("uniprot");
		inputParams.setProgram("blastp");
		analyticalServiceInput.setParams(inputParams);
		
		Data[] content = new Data[1];
		content[0] = Data.Factory.newInstance();
		content[0].setContent("uniprot:wap_rat");
		content[0].setType("sequence");
		analyticalServiceInput.setContent(content);
		
		String jobID = invoker.runJob(analyticalServiceInput );
		String status = invoker.checkStatus(jobID);
		assertTrue("Expected status RUNNING/PENDING, but was " + status, status
				.equals("RUNNING")
				|| status.equals("PENDING"));
		
		try {
			invoker.poll(jobID);
			fail("premature poll did not fail");
		} catch (InvokerException ex) {
			// expected			
		}
		
		while (status.equals("RUNNING") || status.equals("PENDING")) {
			System.out.println(status);
			Thread.sleep(450);
			status = invoker.checkStatus(jobID);
		}
		assertEquals("Status was not DONE", "DONE", status);
		
		Document pollXML = invoker.poll(jobID);
		Element rootElement = pollXML.getRootElement();
		assertEquals("EBIApplicationResult", rootElement.getName());
		assertEquals(NCBIBLAST_NS,
				rootElement.getNamespace());
		
		
		Element hit = rootElement.getChild("SequenceSimilaritySearchResult", NCBIBLAST_NS).getChild("hits", NCBIBLAST_NS).getChild("hit", NCBIBLAST_NS);
		assertNotNull("Hit element not returned from service", hit);
		
		// What we searched for
		assertEquals("WAP_RAT", hit.getAttributeValue("id"));
		Element alignment = hit.getChild("alignments", NCBIBLAST_NS).getChild("alignment", NCBIBLAST_NS);
		assertEquals("Unexpected number of bits in hit", "298", alignment.getChildText("bits", NCBIBLAST_NS));
		
	}
	
	@Test
	public void getStatusNotFound() throws Exception {
		assertEquals("NOT_FOUND", invoker.checkStatus(EXPIRED_JOB_ID));
	}

	@Test(expected=InvokerException.class)
	public void pollExpiredFails() throws Exception {
		invoker.poll(EXPIRED_JOB_ID);
	}
	
}

@@ -230,32 +230,8 @@
 l;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -304,16 +304,53 @@
 espace;%0A
+import org.jdom.output.DOMOutputter;%0A
 import o
@@ -411,28 +411,73 @@
 ebi.
-www.wsncbiblast.Data
+schema.EBIApplicationResultDocument;%0Aimport uk.ac.ebi.schema.THit
 ;%0Aim
@@ -507,27 +507,20 @@
 biblast.
-InputParams
+Data
 ;%0Aimport
@@ -777,98 +777,8 @@
 ker;
-%0Aimport uk.org.mygrid.cagrid.servicewrapper.serviceinvoker.ncbiblast.NCBIBlastInvokerImpl;
 %0A%0Apu
@@ -1092,18 +1092,16 @@
 tion %7B%0A%09
-//
 %09invoker
@@ -1134,16 +1134,18 @@
 er();%09%09%0A
+//
 %09%09invoke
@@ -3219,16 +3219,502 @@
 S));%0A%09%09%0A
+%09%09DOMOutputter domOutputter = new DOMOutputter();%0A%09%09EBIApplicationResultDocument appResults = EBIApplicationResultDocument.Factory.parse(domOutputter.output(pollXML));%0A%09%09THit thit = appResults.getEBIApplicationResult().getSequenceSimilaritySearchResult().getHits().getHitArray(0);%0A%09%09assertEquals(%22WAP_RAT%22, thit.getId());%0A%09%09// Why is %22bits%22 a floating point number?%0A%09%09assertEquals(%22Unexpected number of bits in hit%22, 298.0, thit.getAlignments().getAlignmentArray(0).getBits(), 0.1);%0A%09%09%0A
 %09%7D%0A%09%0A%09@T

test locally with dummy