Commit 1c10ec40 authored by hengwen's avatar hengwen

Add API for checking simulation status

parent 8688064d
......@@ -53,21 +53,11 @@ public class SimulationView {
return "simulation failed";
}
@GetMapping("/{id}/status")
public Map<String, SimulationService.SimulationStatus> getStatus(@PathVariable("id") int simulationID) {
if (SimulationService.getInstances()!= null && SimulationService.getInstances().containsKey(simulationID)) {
return new HashMap<String, SimulationService.SimulationStatus>() {{
put("status", SimulationService.getInstances().get(simulationID).getStatus());
}};
}
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
String.format(
"Not found. Please start the simulation using POST /simulation/%d/start first before checking the status",
simulationID, simulationID
)
);
@GetMapping("/{id}/finished")
public Map<String, Boolean> getStatus(@PathVariable("id") int simulationID) {
return new HashMap<String, Boolean>() {{
put("result", SimulationService.isSimulationFinished(simulationID));
}};
}
@GetMapping("/{id}/result")
......
......@@ -5,8 +5,6 @@ import org.junit.Test;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import server.restful.registry.RegistryFactory;
import server.restful.service.SimulationService;
public class SimulationViewTest extends AbstractViewTest{
......@@ -24,27 +22,4 @@ public class SimulationViewTest extends AbstractViewTest{
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertTrue(result.getResponse().getContentAsString().length() > 10);
}
@Test
public void testSimulationStatus() throws Exception{
// simulation 1 not exist
RequestBuilder builder = MockMvcRequestBuilders.get("/simulation/1/status");
MvcResult result = null;
result = mvc.perform(builder).andReturn();
Assert.assertEquals(404, result.getResponse().getStatus());
// simulation 2 should be ready after we initialized its simulation service instance
new SimulationService(2, RegistryFactory.getZookeeperRegistry());
builder = MockMvcRequestBuilders.get("/simulation/2/status");
result = mvc.perform(builder).andReturn();
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertTrue(result.getResponse().getContentAsString().contains("READY"));
// simulation 2 should failed, since there are no available sub-simulators and rmi servers
SimulationService.getInstances().get(2).run();
builder = MockMvcRequestBuilders.get("/simulation/2/status");
result = mvc.perform(builder).andReturn();
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertTrue(result.getResponse().getContentAsString().contains("FAILED"));
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment