This commit is contained in:
Tienan Chen 2013-09-18 21:48:12 +08:00
parent bfe56a07d1
commit ad9f668898
1 changed files with 106 additions and 133 deletions

View File

@ -16,7 +16,6 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.bench4q.web.model.master.OperateScriptServerResponseModel;
import org.bench4q.web.communication.HttpRequester;
import org.bench4q.web.communication.HttpRequester.HttpResponse;
import org.bench4q.web.entity.fromMaster.Script;
import org.bench4q.web.model.master.MonitorModel;
@ -41,6 +40,16 @@ import org.bench4q.web.model.ScriptIndexModel;
@SessionAttributes({ "accessToken", "testplanID" })
public class TestPlanActionController extends BaseActionController {
private String baseUrl = this.masterIP + "testPlan";
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
private String _marshalTestPlanToString(TestPlanModel testPlanModel)
throws JAXBException {
StringWriter stringWriter = new StringWriter();
@ -76,11 +85,22 @@ public class TestPlanActionController extends BaseActionController {
@ModelAttribute("accessToken") String accessToken, ModelMap model)
throws JAXBException, IOException, InterruptedException {
System.out.println("enter runTestPlan");
String urlString = masterIP + "testPlan/runTestPlanWithTestPlanModel";
int port = 5556;
String content;
HttpRequester httpRequester = new HttpRequester();
Map<String, String> properties = new HashMap<String, String>();
HttpResponse httpResponse = this.getHttpRequester().sendPostXml(
this.getBaseUrl() + "/runTestPlanWithTestPlanModel",
_marshalTestPlanToString(createTestPlan(request, port)),
this.makeAccessTockenMap(accessToken));
TestPlanResultModel runTestPlanResultModel = extractRunTestPlanResultModel(httpResponse
.getContent());
model.addAttribute("testplanID", runTestPlanResultModel.getTestPlanId()
.toString());
System.out.println("finshirun");
return "result.jsp";
}
private TestPlanModel createTestPlan(HttpServletRequest request, int port) {
List<RunningScriptModel> list = new ArrayList<RunningScriptModel>();
List<MonitorModel> ipList = new ArrayList<MonitorModel>();
System.out.println(request.getParameter("scriptID"));
@ -114,107 +134,100 @@ public class TestPlanActionController extends BaseActionController {
TestPlanModel testPlan = new TestPlanModel();
testPlan.setRunningScriptModels(list);
testPlan.setMonitorModles(ipList);
content = _marshalTestPlanToString(testPlan);
properties.put(this.AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
HttpResponse httpResponse = httpRequester.sendPostXml(urlString,
content, properties);
TestPlanResultModel runTestPlanResultModel = extractRunTestPlanResultModel(httpResponse
.getContent());
model.addAttribute("testplanID", runTestPlanResultModel.getTestPlanId()
.toString());
System.out.println("finshirun");
// return new ModelAndView("result");
return "result.jsp";
return testPlan;
}
@RequestMapping("getRunningInfo")
public @ResponseBody
RunningIndexModel getRunningIofo(HttpServletRequest request,
@ResponseBody
public RunningIndexModel getRunningIofo(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@ModelAttribute("testplanID") String testplanID){
@ModelAttribute("testplanID") String testplanID) {
System.out.println("enter getrunninginfo");
Map<String, String> params = new HashMap<String, String>();
params.put("testPlanId", testplanID);
Map<String, String> properties = new HashMap<String, String>();
properties.put(this.AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
HttpRequester httpRequester = new HttpRequester();
String urlString = masterIP + "testPlan/getRunningInfo";
HttpResponse httpResponse=null;
RunningIndexModel runningIndexModel=new RunningIndexModel();
try{
httpResponse = httpRequester.sendGet(urlString, params,
properties);
}catch (IOException e) {
System.out.println("getting the response from /getRunninInfo is wrong:"+ e);
HttpResponse httpResponse = null;
RunningIndexModel runningIndexModel = new RunningIndexModel();
try {
httpResponse = this.getHttpRequester().sendGet(
this.getBaseUrl() + "/getRunningInfo",
makeParamsMap("testPlanId", testplanID),
this.makeAccessTockenMap(accessToken));
TestPlanResultModel testPlanResultModel = extractRunTestPlanResultModel(httpResponse
.getContent());
List<RunningScriptModel> runningScriptModelList = testPlanResultModel
.getRunningScriptModels();
List<ScriptIndexModel> scriptIndexModels = new ArrayList<ScriptIndexModel>();
Iterator<RunningScriptModel> itr = runningScriptModelList
.iterator();
while (itr.hasNext()) {
RunningScriptModel runningScriptModel = itr.next();
ScriptIndexModel scriptIndexModel = new ScriptIndexModel();
scriptIndexModel.setScriptId(runningScriptModel.getScriptId());
String scriptName = getScriptNameById(
runningScriptModel.getScriptId(), accessToken);
scriptIndexModel.setScriptName(scriptName);
scriptIndexModels.add(scriptIndexModel);
}
runningIndexModel.setMonitorModels(testPlanResultModel
.getMonitorModels());
runningIndexModel
.setTestPlanId(testPlanResultModel.getTestPlanId());
runningIndexModel.setScriptIndexModels(scriptIndexModels);
return runningIndexModel;
} catch (JAXBException e) {
System.out.println("extracting the testPlanResultModel is wrong:"
+ e);
return null;
}
try{
TestPlanResultModel testPlanResultModel = extractRunTestPlanResultModel(httpResponse
.getContent());
List<RunningScriptModel> runningScriptModelList=testPlanResultModel.getRunningScriptModels();
List<ScriptIndexModel> scriptIndexModels=new ArrayList<ScriptIndexModel>();
Iterator<RunningScriptModel> itr=runningScriptModelList.iterator();
while(itr.hasNext()){
RunningScriptModel runningScriptModel=itr.next();
ScriptIndexModel scriptIndexModel=new ScriptIndexModel();
scriptIndexModel.setScriptId(runningScriptModel.getScriptId());
String scriptName=getScriptNameById(runningScriptModel.getScriptId(),accessToken);
scriptIndexModel.setScriptName(scriptName);
scriptIndexModels.add(scriptIndexModel);
}
runningIndexModel.setMonitorModels(testPlanResultModel.getMonitorModels());
runningIndexModel.setTestPlanId(testPlanResultModel.getTestPlanId());
runningIndexModel.setScriptIndexModels(scriptIndexModels);
return runningIndexModel;
}catch(JAXBException e){
System.out.println("extracting the testPlanResultModel is wrong:"+e);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private Map<String, String> makeParamsMap(String key, String value) {
Map<String, String> params = new HashMap<String, String>();
params.put(key, value);
return params;
}
@RequestMapping("getScriptBriefStatus")
public @ResponseBody
ScriptBriefResultModel getAgentBriefStatus(HttpServletRequest request,
@ResponseBody
public ScriptBriefResultModel getAgentBriefStatus(
HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@ModelAttribute("testplanID") String testplanID)
throws IOException, JAXBException {
@ModelAttribute("testplanID") String testplanID) {
System.out.println("getscriptbriefstatus");
String scriptID = request.getParameter("scriptID");
if (scriptID != null)
System.out.println(scriptID);
else {
if (scriptID == null) {
System.out.println("the scriptID is null");
return null;
}
;
Map<String, String> params = new HashMap<String, String>();
params.put("testPlanId", testplanID);
System.out.println(scriptID);
Map<String, String> params = makeParamsMap("testPlanId", testplanID);
params.put("scriptId", scriptID);
String urlString = masterIP + "testPlan/getScriptBrief";
HttpRequester httpRequester = new HttpRequester();
Map<String, String> properties = new HashMap<String, String>();
properties.put(this.AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
HttpResponse httpResponse = httpRequester.sendGet(urlString, params,
properties);
ScriptBriefResultModel scriptBriefResultModel = extractScriptBriefResultModel(httpResponse
.getContent());
if (scriptBriefResultModel == null) {
System.out.println("the scriptBriefResultModel return is null");
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
this.getBaseUrl() + "/getScriptBrief", params,
this.makeAccessTockenMap(accessToken));
ScriptBriefResultModel scriptBriefResultModel = extractScriptBriefResultModel(httpResponse
.getContent());
if (scriptBriefResultModel == null) {
System.out.println("the scriptBriefResultModel return is null");
return null;
}
return scriptBriefResultModel;
} catch (IOException exception) {
System.out.println(FAIL_WITH_EXCEPTION);
return null;
} catch (JAXBException e) {
System.out.println(FAIL_WITH_EXCEPTION);
return null;
}
return scriptBriefResultModel;
}
@RequestMapping("getProcessorStatus")
public @ResponseBody
ProcessorModel getPeocessStatus(HttpServletRequest request,
@ResponseBody
public ProcessorModel getPeocessStatus(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@ModelAttribute("testplanID") String testplanID) {
System.out.println("enter getProcessorStatus");
@ -272,63 +285,23 @@ public class TestPlanActionController extends BaseActionController {
}
private String getScriptNameById(int scriptId, String accessToken) {
Integer script_id = scriptId;
String scriptIdString = script_id.toString();
Map<String, String> params = new HashMap<String, String>();
params.put("scriptId", scriptIdString);
OperateScriptServerResponseModel operateScriptServerResponseModel = null;
String urlString = masterIP + "RecordScript/queryScriptById";
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
urlString, params, this.makeAccessTockenMap(accessToken));
operateScriptServerResponseModel = extractoperateScriptServerResponseModel(httpResponse
.getContent());
} catch (Exception e) {
System.out
.println("The response of getting the scriptName by ID is wrong "
+ e.toString());
}
List<Script> scripts = operateScriptServerResponseModel.getScripts();
String scriptName = "";
Iterator<Script> itr = scripts.iterator();
while (itr.hasNext()) {
if (scriptName.length() == 0)
scriptName += itr.next().getName();
else
scriptName += "," + itr.next().getName();
}
if (scriptName.length() > 0)
return scriptName;
else
return null;
}
public String listToString(List<Script> scripts) {
if (scripts == null) {
return "";
}
Iterator<Script> it = scripts.iterator();
Script script;
String str = "";
while (it.hasNext()) {
script = it.next();
if (str.isEmpty()) {
str = str + "[{" + "name:\"" + script.getName()
+ "\",createDateTime:\""
+ script.getCreateDateTime().getTime() + "\",ID:\""
+ script.getId() + "\"}";
} else {
str = str + ",{" + "name:\"" + script.getName()
+ "\",createDateTime:\""
+ script.getCreateDateTime().getTime() + "\",ID:\""
+ script.getId() + "\"}";
urlString,
makeParamsMap("scriptId", String.valueOf(scriptId)),
this.makeAccessTockenMap(accessToken));
if (httpResponse == null || httpResponse.getContent().isEmpty()) {
return null;
}
List<Script> scripts = extractoperateScriptServerResponseModel(
httpResponse.getContent()).getScripts();
return scripts == null || scripts.isEmpty() ? null : scripts.get(0)
.getName();
} catch (Exception e) {
System.out.println(FAIL_WITH_EXCEPTION);
return null;
}
return str + "]";
}
}