package eu.admire.clienttoolkit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import eu.admire.clienttoolkit.core.Gateway;
import eu.admire.clienttoolkit.core.GatewayFactory;
import eu.admire.clienttoolkit.core.GatewayProcess;
import eu.admire.clienttoolkit.core.Result;
import eu.admire.clienttoolkit.core.ServerException;
import eu.admire.clienttoolkit.core.Util;
import eu.admire.clienttoolkit.core.ValidationResult;
import eu.admire.gateway.common.ProcessingStatus;
import eu.admire.gateway.common.errors.Errors;
public class GatewayClientRESTTest {
public static final String GATEWAY_ADDRESS =
// "http://admire3.epcc.ed.ac.uk:8444/AdmireGateway/services";
"http://localhost:8080/AdmireGateway/services/";
private static final String DISPEL_REQUEST =
"use uk.org.ogsadai.Echo;\n" +
"use eu.admire.Results;\n" +
"Echo echo = new Echo;\n" +
"|- \"Hello World2!\" -| => echo.input;\n" +
"Results result = new Results;\n" +
"|- \"results\" -| => result.name;\n" +
"echo.output => result.input;\n" +
"submit echo;";
@Test
public void testSubmittingDISPEL() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
GatewayProcess p = g.submit(DISPEL_REQUEST);
try
{
p.waitForResults();
Map<String, Result> results = p.getResults();
Result result = results.get("results");
assertNotNull(result);
String resultStr = Util.getAsString(result);
assertEquals("Received wrong result", "[Hello World2!]", resultStr);
Errors e = p.getErrors();
assertNull("Caught a compile time error", e.getCompiletime());
assertNull("Caught a runtime error", e.getRuntime());
assertNull("Caught a registration error", e.getRegistration());
assertNull("Caught a system error", e.getSystem());
assertEquals(ProcessingStatus.COMPLETED, p.getStatus());
// list all processes and check that this one in the list
List<String> processes = g.getGatewayProcesses();
boolean contained = false;
for (String process : processes)
{
if (p.getAddress().endsWith(process))
{
contained = true;
break;
}
}
assertTrue("Process not found in list of all processes.", contained);
}
finally
{
p.destroyProcess();
}
}
@Test
public void testDISPELNoResults() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
String dispelRequest =
"use uk.org.ogsadai.Echo;\n" +
"use uk.org.ogsadai.DeliverToNull;\n" +
"Echo echo = new Echo;\n" +
"|- \"Hello World2!\" -| => echo.input;\n" +
"DeliverToNull deliver = new DeliverToNull;\n" +
"echo.output => deliver.input;\n" +
"submit echo;";
GatewayProcess p = g.submit(dispelRequest);
try
{
p.waitForResults();
Map<String, Result> results = p.getResults();
assertTrue(results.isEmpty());
Errors e = p.getErrors();
assertNull("Caught a compile time error", e.getCompiletime());
assertNull("Caught a runtime error", e.getRuntime());
assertNull("Caught a registration error", e.getRegistration());
assertNull("Caught a system error", e.getSystem());
}
finally
{
p.destroyProcess();
}
}
@Test
public void testUnknownHost() throws Exception
{
Gateway g = GatewayFactory.get("http://unknown");
try
{
g.submit(DISPEL_REQUEST);
TestCase.fail("ServerException expected.");
}
catch (ServerException e)
{
// expected
// find the cause and check that it's UnknownHost
Throwable cause = e;
while (cause.getCause() != null)
{
cause = cause.getCause();
}
assertTrue(cause instanceof UnknownHostException);
}
}
@Test
public void getVersionTest() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
// just test that there is no error
String version = g.getVersion();
assertNotNull(version);
}
@Test
public void getLocalDataResourcesTest() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
// just test that there is no error
List<String> dataResources = g.getGatewayLocalDataResources();
assertNotNull(dataResources);
}
@Test
public void validateTest() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
ValidationResult v = g.validate(DISPEL_REQUEST);
assertTrue(v.getSubmittedGraphs().get(0).contains("<svg"));
//Don't expect any errors or objects for this
assertTrue(v.isValid());
assertTrue(v.getErrors().isEmpty());
assertEquals(0, v.getRegisteredObjects().size());
}
@Test
public void validationFailsTest() throws Exception
{
Gateway g = GatewayFactory.get(GATEWAY_ADDRESS);
//Give it rubbish, expect an error and no graph
String dispelRequest = "This is a nonsense request";
ValidationResult v = g.validate(dispelRequest);
assertFalse(v.isValid());
assertFalse(v.getErrors().trim().isEmpty());
assertEquals(0, v.getRegisteredObjects().size());
}
}