fix the bug in upload script file

This commit is contained in:
fanfuxiaoran 2014-06-06 11:38:22 +08:00
parent 9b01a16ccd
commit 56c316203a
14 changed files with 77 additions and 495 deletions

View File

@ -1,11 +1,12 @@
package org.bench4q.web.api;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.RunningScriptModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.masterMessager.ScriptMessager;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,7 +18,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import com.github.tomakehurst.wiremock.admin.NewStubMappingTask;
import com.google.gson.Gson;
@Controller
@ -56,7 +56,8 @@ public class ScriptController extends BaseController {
@RequestMapping("deleteScript")
@ResponseBody
public Map<String, Object> deleteScript(
@ModelAttribute("accessToken") String accessToken, @RequestParam String scriptId) {
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) {
Map<String, Object> map = new HashMap<String, Object>();
return processScriptResponse(map, this.getScriptMessager()
.deleteScript(accessToken, scriptId));
@ -72,14 +73,16 @@ public class ScriptController extends BaseController {
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().queryScriptById(accessToken, scriptId);
if (operateScriptServerResponseModel.isSuccess()) {
ScriptModel scriptModel=operateScriptServerResponseModel.getScriptModels().get(0);
RunScenarioModel runScenarioModel=
(RunScenarioModel)MarshalHelper.tryUnmarshal(RunScenarioModel.class, scriptModel.getScriptContent());
if(runScenarioModel==null){
return fail(map, "error script");
}
Gson gson=new Gson();
map = success(map);
ScriptModel scriptModel = operateScriptServerResponseModel
.getScriptModels().get(0);
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.tryUnmarshal(RunScenarioModel.class,
scriptModel.getScriptContent());
if (runScenarioModel == null) {
return fail(map, "error script");
}
Gson gson = new Gson();
map = success(map);
map.put("script", gson.toJson(runScenarioModel));
return map;
} else {
@ -89,25 +92,46 @@ public class ScriptController extends BaseController {
}
}
@RequestMapping("uploadScriptFile")
@ResponseBody
public Map<String, Object> uploadScriptFile(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptName,
@RequestParam MultipartFile scriptFile) throws IOException {
String script = new String(scriptFile.getBytes());
Map<String, Object> map = new HashMap<String, Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().uploadScriptFile(accessToken, scriptName,
script);
if (operateScriptServerResponseModel.isSuccess()) {
success(map);
map.put("message", (Object) new String("upload script:"
+ scriptName + " success!"));
return map;
} else {
return fail(map,
operateScriptServerResponseModel.getFailCauseString());
}
}
@RequestMapping("uploadScript")
@ResponseBody
public Map<String, Object> saveScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptName, @RequestParam String script,
@RequestParam(required = false) MultipartFile[] paramFiles) {
Map<String, Object> map=new HashMap<String,Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel=
this.getScriptMessager().uploadScript(accessToken, scriptName,
Map<String, Object> map = new HashMap<String, Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().uploadScript(accessToken, scriptName,
script, paramFiles);
if(operateScriptServerResponseModel.isSuccess())
{
if (operateScriptServerResponseModel.isSuccess()) {
success(map);
map.put("message", (Object) new String(
"upload script:" + scriptName + " success!"));
map.put("message", (Object) new String("upload script:"
+ scriptName + " success!"));
return map;
}
else {
return fail(map, operateScriptServerResponseModel.getFailCauseString());
} else {
return fail(map,
operateScriptServerResponseModel.getFailCauseString());
}
}
@ -131,7 +155,7 @@ public class ScriptController extends BaseController {
.getScriptMessager().startScriptRecordServer(accessToken);
if (operateScriptServerResponseModel.isSuccess()) {
map = success(map);
map.put("server", operateScriptServerResponseModel);
map.put("server", operateScriptServerResponseModel);
return map;
} else {
map = fail(map,
@ -165,7 +189,7 @@ public class ScriptController extends BaseController {
scriptName, port, scriptRecordUUID));
}
private Map<String, Object> processScriptResponse(Map<String, Object> map,
OperateScriptServerResponseModel operateScriptServerResponseModel) {
if (operateScriptServerResponseModel.isSuccess()) {

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;
import org.bench4q.share.models.master.UserModel;
@ -16,7 +15,6 @@ import org.bench4q.web.validation.ValidateResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

View File

@ -52,6 +52,32 @@ public class ScriptMessager extends MasterMessager {
}
public OperateScriptServerResponseModel uploadScriptFile(
String accessToken, String scriptName, String scenarioModel) {
String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName;
Map<String, String> params = new HashMap<String, String>();
params.put("scenarioModel", scenarioModel);
HttpResponse httpResponse = null;
List<String> stringPartsList = new LinkedList<String>();
stringPartsList.add(scenarioModel);
try {
httpResponse = this.getHttpRequester().postFilesMulti(
makeAccessTockenMap(accessToken), url, "paramFiles[]",
null, "scenarioModel", stringPartsList);
System.out.println(httpResponse.getContent());
if (!validateHttpResponse(httpResponse))
return null;
return (OperateScriptServerResponseModel) MarshalHelper.unmarshal(
OperateScriptServerResponseModel.class,
httpResponse.getContent());
} catch (JAXBException | IOException e) {
this.handleException(httpResponse, e);
return createFailOperateScriptServerResponseModel();
}
}
public OperateScriptServerResponseModel uploadScript(String accessToken,
String scriptName, String scenarioModel, MultipartFile[] paramFiles) {
String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName;
@ -110,7 +136,7 @@ public class ScriptMessager extends MasterMessager {
.getOperateScriptServerResponseModelByPost(url, params,
accessToken);
if (!operateScriptServerResponseModel.isSuccess()
|| operateScriptServerResponseModel.getScriptModels().size() !=1) {
|| operateScriptServerResponseModel.getScriptModels().size() != 1) {
operateScriptServerResponseModel.setSuccess(false);
operateScriptServerResponseModel
.setFailCauseString("error get script by id:" + scriptId);

View File

@ -63,7 +63,6 @@ public class TestPlanController extends BaseController {
this.testPlanService = testPlanService;
}
// not add to testPlan taskList=====not need
@RequestMapping(value = "runTestPlan", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> runTestPlan(

View File

@ -1 +1 @@
masterAddress=133.133.12.1:7979/
masterAddress=127.0.0.1:7979/

View File

@ -38,9 +38,9 @@ function startServer() {
$('#uploadScript').click(function(){
var myModalContent=$('#myModal').html();
$(".modal-footer").html("");
var uploadForm='<form method="post" enctype="multipart/form-data" action="uploadScript">'+
var uploadForm='<form method="post" enctype="multipart/form-data" action="uploadScriptFile">'+
'<p>script name:<input class="input-mini" type="text" name="scriptName" ></p>'+
'<p>script to upload: <input type="file" name="script"/></p>'+
'<p>script to upload: <input type="file" name="scriptFile"/></p>'+
'<p><input type="submit"/></p></form>';
var status="<div id='status'><h3></h3><div>";
$('#scriptInfo').html("");

View File

@ -1,32 +0,0 @@
package org.benc4q.test.newapi;
import org.bench4q.web.tool.test.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
public abstract class ControllerTestBase {
protected String baseUrl = "http://127.0.0.1:8080";
private LoginHelper loginHelper;
private HttpRequest httpRequest;
public LoginHelper getLoginHelper() {
return loginHelper;
}
@Autowired
private void setLoginHelper(LoginHelper loginHelper) {
this.loginHelper = loginHelper;
}
public HttpRequest getHttpRequest() {
return httpRequest;
}
@Autowired
public void setHttpRequest(HttpRequest httpRequest) {
this.httpRequest = httpRequest;
}
}

View File

@ -1,40 +0,0 @@
package org.benc4q.test.newapi;
import org.junit.Before;
import org.junit.Test;
public class PluginControllerTest extends ControllerTestBase {
private String accessToken;
@Before
public void setUp() {
this.accessToken = this.getLoginHelper().login();
}
@Test
public void testAddPlugin() {
}
@Test
public void testDeletePlugin() {
}
@Test
public void loadPluginNameList() {
}
@Test
public void loadPluginUIList() {
}
@Test
public void loadBehaviorList() {
}
}

View File

@ -1,10 +0,0 @@
package org.bench4q.web.tool.test;
import com.google.gson.Gson;
public class GsonHelper {
public static Gson getGson() {
return new Gson();
}
}

View File

@ -1,48 +0,0 @@
package org.bench4q.web.tool.test;
import org.apache.log4j.Logger;
import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.models.master.UserModel;
import org.bench4q.web.api.AuthorizeActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
@Component
public class LoginHelper {
private final String userName = "admin";
private final String password = "admin";
private AuthorizeActionController authorizeActionController;
private Logger logger = Logger.getLogger(LoginHelper.class);
public AuthorizeActionController getAuthorizeActionController() {
return authorizeActionController;
}
@Autowired
public void setAuthorizeActionController(
AuthorizeActionController authorizeActionController) {
this.authorizeActionController = authorizeActionController;
}
public String login() {
UserModel userModel = new UserModel();
userModel.setUserName(userName);
userModel.setPassword(password);
try {
if (this.authorizeActionController == null) {
System.out.println(" null");
}
ModelMap modelMap = new ModelMap();
authorizeActionController.adminLogin(userModel, modelMap);
String accessToken = (String) modelMap.get("accessToken");
return accessToken;
} catch (CustomGenericException e) {
logger.info(ExceptionLog.getStackTrace(e));
return null;
}
}
}

View File

@ -1,90 +0,0 @@
package org.bench4q.web.tool.test;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import junit.framework.Assert;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.api.ScriptActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
@Component
public class ScriptHelper {
private String filePath = "src/test/resources/script.xml";
private ScriptService scriptService;
private ScriptModel scriptModel;
private ScriptActionController scriptActionController;
public ScriptService getScriptService() {
return scriptService;
}
@Autowired
public void setScriptService(ScriptService scriptService) {
this.scriptService = scriptService;
}
public ScriptActionController getScriptActionController() {
return scriptActionController;
}
@Autowired
public void setScriptActionController(
ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
}
public RunScenarioModel getRunScenarioModelFormFile() throws JAXBException,
IOException {
String scriptContent = FileUtils.readFileToString(new File(filePath));
return (RunScenarioModel) ObjectXmlExchange.fromXml(
RunScenarioModel.class, scriptContent);
}
public void deleteScript(String accessToken) throws CustomGenericException {
BaseResponseModel baseResponseModel = this.getScriptActionController()
.deletescript(accessToken,
(new Integer(this.scriptModel.getId())).toString());
Gson goGson = new Gson();
Logger.getLogger(ScriptHelper.class).info(
goGson.toJson(baseResponseModel));
if (baseResponseModel.isSuccess())
Logger.getLogger(ScriptHelper.class).info("delete script");
}
public ScriptModel upLoadScriptAndGetId(String accessToken)
throws JAXBException, IOException, CustomGenericException {
String scriptName = "testScript" + UUID.randomUUID();
String scriptContent = MarshalHelper.marshal(RunScenarioModel.class,
getRunScenarioModelFormFile());
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptService().uploadScript(accessToken, scriptContent,
scriptName);
Assert.assertTrue(operateScriptServerResponseModel.isSuccess());
ScriptModel scriptModel = this.getScriptService().queryScriptByName(
scriptName, accessToken);
this.scriptModel = scriptModel;
return scriptModel;
}
}

View File

@ -1,87 +0,0 @@
package org.bench4q.web.tool.test;
import static org.junit.Assert.*;
import java.util.List;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.api.ScriptActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.tool.test.LoginHelper;
import org.bench4q.web.tool.test.TestPlanHelper;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class TestLoginHelper {
private LoginHelper loginHelper;
private ScriptActionController scriptActionController;
private TestPlanHelper startTestPlan;
public LoginHelper getLoginHelper() {
return loginHelper;
}
@Autowired
public void setLoginHelper(LoginHelper loginHelper) {
this.loginHelper = loginHelper;
}
public TestPlanHelper getStartTestPlan() {
return startTestPlan;
}
@Autowired
public void setStartTestPlan(TestPlanHelper startTestPlan) {
this.startTestPlan = startTestPlan;
}
public ScriptActionController getScriptActionController() {
return scriptActionController;
}
@Autowired
public void setScriptActionController(
ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
}
@Test
public void testLoginHelper() {
String accessToken = this.getLoginHelper().login();
assertNotNull(accessToken);
}
@SuppressWarnings("unchecked")
@Test
public void testLoginHelperRight() throws CustomGenericException {
String accessToken = this.getLoginHelper().login();
BaseResponseModel baseResponseModel = this
.getScriptActionController().loadScript(accessToken);
List<ScriptModel> scriptModels=(List<ScriptModel>)
baseResponseModel.getData();
assertTrue(baseResponseModel.isSuccess());
assertTrue(scriptModels.size()>0);
}
@Test
public void testLoginInStartTest() throws CustomGenericException{
String accessToken = this.getLoginHelper().login();
this.getStartTestPlan().startTest(accessToken);
assertNotNull(this.getStartTestPlan().getTestPlanRunId());
}
@After
public void clean() throws CustomGenericException{
String accessToken = this.getLoginHelper().login();
this.getStartTestPlan().cleanUpTest(accessToken);
}
}

View File

@ -1,109 +0,0 @@
package org.bench4q.web.tool.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.models.master.TestPlanResultModel;
import org.bench4q.web.api.TestPlanActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.TestPlanRequestModel;
import org.bench4q.web.model.WebScriptModel;
import org.bench4q.web.model.WebTestPlanResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
@Component
public class TestPlanHelper {
public static final String MONITOR_IP = "133.133.12.3";
private ScriptHelper scriptHelper;
private TestPlanActionController testPlanActionController;
private String testPlanRunId;
public TestPlanActionController getTestPlanActionController() {
return testPlanActionController;
}
@Autowired
public void setTestPlanActionController(
TestPlanActionController testPlanActionController) {
this.testPlanActionController = testPlanActionController;
}
public ScriptHelper getScriptHelper() {
return scriptHelper;
}
@Autowired
public void setScriptHelper(ScriptHelper scriptHelper) {
this.scriptHelper = scriptHelper;
}
public String getTestPlanRunId() {
return testPlanRunId;
}
private void setTestPlanRunId(String testPlanRunId) {
this.testPlanRunId = testPlanRunId;
}
public void startTest(String accessToken) {
try {
TestPlanResultModel testPlanResultModel = (TestPlanResultModel) this
.getTestPlanActionController()
.runTestPlan(accessToken, new ModelMap(),
createTestPlanWithMonitor(accessToken)).getData();
this.setTestPlanRunId(testPlanResultModel.getTestPlanId()
.toString());
} catch (Exception e) {
Logger.getLogger(TestPlanHelper.class).info(
ExceptionLog.getStackTrace(e));
}
}
public TestPlanRequestModel createTestPlanWithMonitor(String accessToken)
throws JAXBException, IOException, CustomGenericException {
List<String> ipList = new ArrayList<>();
ipList.add(MONITOR_IP);
TestPlanRequestModel testPlanRequestModel = new TestPlanRequestModel();
testPlanRequestModel.setIpList(ipList);
testPlanRequestModel.setTestPlanName("forTest");
testPlanRequestModel
.setScriptList(generateWebScriptModels(accessToken));
return testPlanRequestModel;
}
private List<WebScriptModel> generateWebScriptModels(String accessToken)
throws JAXBException, IOException, CustomGenericException {
List<WebScriptModel> scriptList = new ArrayList<WebScriptModel>();
WebScriptModel webScriptModel = new WebScriptModel();
webScriptModel.setCooldown(1);
webScriptModel.setExecuteRange(50);
webScriptModel.setId(this.getScriptHelper()
.upLoadScriptAndGetId(accessToken).getId());
webScriptModel.setLoad(30);
webScriptModel.setWarmup(1);
scriptList.add(webScriptModel);
return scriptList;
}
public void cleanUpTest(String accessToken) throws CustomGenericException {
/*this.getScriptHelper().deleteScript(accessToken);*/
}
public WebTestPlanResultModel queryTestPlanResultModel(
String testPlanRunId, String accessToken)
throws CustomGenericException, JAXBException {
WebTestPlanResultModel webTestPlanResultModel = (WebTestPlanResultModel) this
.getTestPlanActionController()
.queryTestPlan(accessToken, testPlanRunId).getData();
return webTestPlanResultModel;
}
}

View File

@ -1,49 +0,0 @@
package org.bench4q.web.tool.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.web.service.CommunicateWithMaster;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class Test_CommunicateWithMaster {
private CommunicateWithMaster communicateWithMaster;
private LoginHelper loginHelper;
public CommunicateWithMaster getCommunicateWithMaster() {
return communicateWithMaster;
}
@Autowired
public void setCommunicateWithMaster(
CommunicateWithMaster communicateWithMaster) {
this.communicateWithMaster = communicateWithMaster;
}
public LoginHelper getLoginHelper() {
return loginHelper;
}
@Autowired
public void setLoginHelper(LoginHelper loginHelper) {
this.loginHelper = loginHelper;
}
@Test
public void test_uploadScriptWithParamFiles() {
List<File> paramFiles = new ArrayList<File>();
String filePath = "ScriptParameters"
+ System.getProperty("file.separator") + "param1.txt";
TestHelper.createFileIfNotExist(new File(filePath));
paramFiles.add(new File(filePath));
}
}