package eu.geoknow.generator.workflow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Test;
import com.ontos.ldiw.vocabulary.LDIWO;
import eu.geoknow.generator.component.beans.Service;
import eu.geoknow.generator.configuration.FrameworkConfiguration;
import eu.geoknow.generator.configuration.FrameworkManager;
import eu.geoknow.generator.exceptions.UnknownException;
import eu.geoknow.generator.workflow.beans.JobExecutionWrapper;
import eu.geoknow.generator.workflow.beans.JobsRegistered;
import eu.geoknow.generator.workflow.beans.Registration;
/**
* To run these tests we assume that spring-batch-admin-xxxxx is running somewhere in
* BatchAdminClient.setServiceUrl(serviceUrl).
*
* @author alejandragarciarojas
*
*/
public class BatchAdminClientIT {
private static final Logger log = Logger.getLogger(BatchAdminClientIT.class);
String springBatchServiceUri;
String sbaDir;
public BatchAdminClientIT() throws Exception {
FrameworkConfiguration config = FrameworkConfiguration.getInstance();
FrameworkManager manager = new FrameworkManager();
Service sba = manager.getFrameworkService(config.getResourceNamespace() + "SpringBatchService");
springBatchServiceUri = sba.getServiceUrl();
sbaDir = sba.getProperties().get(LDIWO.springBatchAdminJobsDir.getURI());
log.info("springBatchServiceUri : " + springBatchServiceUri);
log.info("sbaDir : " + sbaDir);
// remove last slash if existing
if (springBatchServiceUri.endsWith("/")) {
springBatchServiceUri =
springBatchServiceUri.substring(0, springBatchServiceUri.length() - 1);
}
}
@Test
public void registeXmlJobTest() throws Exception {
Calendar calendar = new GregorianCalendar();
String id = "testXml_" + calendar.getTimeInMillis();
String description = "some description";
String xml =
JobFactory.getInstance().createOneStepServiceJobXml(id, description, "http://...",
"application/json", "post", "{some:json}");
assertTrue(xml.startsWith("<?xml version="));
Registration response = BatchAdminClient.registerJob(id, xml, springBatchServiceUri, sbaDir);
assertEquals(id, response.getName());
}
@Test
public void getAllJobsTest() throws Exception {
JobsRegistered jobs = BatchAdminClient.getRegistedJobs(springBatchServiceUri);
assertNotSame(0, jobs.getJobs().getRegistrations().size());
}
@Test
public void getAJob() throws Exception {
Registration job = BatchAdminClient.getJob("job1", springBatchServiceUri);
assertEquals("job1", job.getName());
}
@Test(expected = UnknownException.class)
public void getUnexistingExecution() throws Exception {
List<JobExecutionWrapper> execution =
BatchAdminClient.getExecutionDetail("mmxy", "91919", springBatchServiceUri);
}
@Test
public void executeJobAndGetExecutionsAndStop() throws Exception {
// since the following line mostly fails, I commented it out
/*
* JobExecution exec = BatchAdminClient.runJob("limesJobSampleJson",springBatchServiceUri);
* assertEquals("START", exec.getStatus().substring(0, 5));
*
* JobExecutions executions = BatchAdminClient.getAllExecutions(springBatchServiceUri);
* assertNotSame(0, executions.getJobExecutions().size());
*
* // run more times to test job parameters passing exec =
* BatchAdminClient.runJob("limesJobSampleJson",springBatchServiceUri); assertEquals("START",
* exec.getStatus().substring(0, 5));
*
*
*
* JobExecutionWrapper execWr =
* BatchAdminClient.stopExecution(exec.getId(),springBatchServiceUri); assertEquals("STOPPING",
* execWr.getJobExecution().getStatus());
*/
}
}