diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/AgentManagerMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/AgentManagerMessager.java similarity index 84% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/AgentManagerMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/AgentManagerMessager.java index e0bb47c0..96f4d5c1 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/AgentManagerMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/AgentManagerMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -9,20 +9,22 @@ import javax.xml.bind.JAXBException; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.AgentResponseModel; +import org.springframework.stereotype.Component; -public class AgentManagerMessenger extends MasterMessenger { +@Component +public class AgentManagerMessager extends MasterMessager { - public AgentManagerMessenger() { + public AgentManagerMessager() { super(MasterAddressManamger.getMasterAddress() + "/agentManage"); } public AgentResponseModel addAgent(String accessToken, - String agentModelContnet) { + String agentModelContent) { String url = this.getBaseUrl() + "/addAgentToPool"; HttpResponse httpResponse = null; try { httpResponse = this.getHttpRequester().sendPostXml(url, - agentModelContnet, makeAccessTockenMap(accessToken)); + agentModelContent, makeAccessTockenMap(accessToken)); if (!validateHttpResponse(httpResponse)) return null; return (AgentResponseModel) MarshalHelper.unmarshal( diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterAddressManamger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterAddressManamger.java similarity index 91% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterAddressManamger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterAddressManamger.java index 1a6de16c..505b881a 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterAddressManamger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterAddressManamger.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.File; import java.io.FileInputStream; diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterMessager.java similarity index 87% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterMessager.java index c5f8d082..be2dd118 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MasterMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MasterMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.util.HashMap; import java.util.List; @@ -12,12 +12,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component -public abstract class MasterMessenger { +public abstract class MasterMessager { private final String AUTH_HEADER_PROPERTY = "Authorization"; private final String ACCES_TOCKEN_STARTER = "Bearer "; protected String baseUrl; protected HttpRequester httpRequester; - protected Logger logger = Logger.getLogger(MasterMessenger.class); + protected Logger logger = Logger.getLogger(MasterMessager.class); public String getBaseUrl() { return baseUrl; @@ -36,7 +36,7 @@ public abstract class MasterMessenger { this.httpRequester = httpRequester; } - public MasterMessenger(String baseUrl) { + public MasterMessager(String baseUrl) { this.setBaseUrl(baseUrl); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MonitorMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java similarity index 93% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MonitorMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java index 2e0b1157..e33888f0 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/MonitorMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -15,8 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Component -public class MonitorMessenger extends MasterMessenger { - public MonitorMessenger() { +public class MonitorMessager extends MasterMessager { + public MonitorMessager() { super(MasterAddressManamger.getMasterAddress() + "/monitorController"); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/PluginMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/PluginMessager.java similarity index 92% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/PluginMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/PluginMessager.java index 473a5ec9..b6fbb23a 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/PluginMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/PluginMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.Map; @@ -11,8 +11,8 @@ import org.bench4q.share.models.master.plugin.PluginResponseModel; import org.springframework.stereotype.Component; @Component -public class PluginMessenger extends MasterMessenger { - public PluginMessenger() { +public class PluginMessager extends MasterMessager { + public PluginMessager() { super(MasterAddressManamger.getMasterAddress() + "/plugin"); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/RecordPortMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/RecordPortMessager.java similarity index 91% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/RecordPortMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/RecordPortMessager.java index cd3b961b..dd6b084d 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/RecordPortMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/RecordPortMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -11,9 +11,9 @@ import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.OrganizeRecordPortResponseModel; import org.springframework.stereotype.Component; @Component -public class RecordPortMessenger extends MasterMessenger { +public class RecordPortMessager extends MasterMessager { - public RecordPortMessenger() { + public RecordPortMessager() { super(MasterAddressManamger.getMasterAddress() + "/RecordPort"); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/ScriptMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/ScriptMessager.java similarity index 94% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/ScriptMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/ScriptMessager.java index 0731bf66..85cf208c 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/ScriptMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/ScriptMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -15,8 +15,8 @@ import org.springframework.stereotype.Component; import org.springframework.web.multipart.commons.CommonsMultipartFile; @Component -public class ScriptMessenger extends MasterMessenger { - public ScriptMessenger() { +public class ScriptMessager extends MasterMessager { + public ScriptMessager() { super(MasterAddressManamger.getMasterAddress() + "/script"); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/TestPlanMessegner.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/TestPlanMessager.java similarity index 95% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/TestPlanMessegner.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/TestPlanMessager.java index 9113bf50..3c69da4f 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/TestPlanMessegner.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/TestPlanMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -19,9 +19,9 @@ import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; import org.springframework.stereotype.Component; @Component -public class TestPlanMessegner extends MasterMessenger { +public class TestPlanMessager extends MasterMessager { - public TestPlanMessegner() { + public TestPlanMessager() { super("/testPlan"); } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/UserMessenger.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/UserMessager.java similarity index 90% rename from Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/UserMessenger.java rename to Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/UserMessager.java index 0452de6a..a8a45a16 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessenger/UserMessenger.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/UserMessager.java @@ -1,4 +1,4 @@ -package org.bench4q.web.masterMessenger; +package org.bench4q.web.masterMessager; import java.io.IOException; import java.util.HashMap; @@ -13,9 +13,9 @@ import org.bench4q.share.models.master.RegisterResponseModel; import org.springframework.stereotype.Component; @Component -public class UserMessenger extends MasterMessenger { +public class UserMessager extends MasterMessager { - public UserMessenger() { + public UserMessager() { super(MasterAddressManamger.getMasterAddress() + "/user"); } @@ -72,7 +72,7 @@ public class UserMessenger extends MasterMessenger { String accessToken, String url, Map params) { HttpResponse httpResponse = null; try { - httpResponse = this.getHttpRequester().sendPost(url, params, + httpResponse = this.getHttpRequester().sendGet(url, params, makeAccessTockenMap(accessToken)); if (!validateHttpResponse(httpResponse)) return null; diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/AgentController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/AgentController.java index cfb2d94a..53eaf6d3 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/AgentController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/AgentController.java @@ -2,10 +2,11 @@ package org.bench4q.web.newapi; import java.util.HashMap; import java.util.Map; + import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.AgentModel; import org.bench4q.share.models.master.AgentResponseModel; -import org.bench4q.web.masterMessenger.AgentManagerMessenger; +import org.bench4q.web.masterMessager.AgentManagerMessager; import org.bench4q.web.validation.AgentValidate; import org.bench4q.web.validation.ValidateResponseModel; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +24,7 @@ public class AgentController extends BaseController { private final String SERVER_ERROR = "server error"; private AgentValidate agentValidate; - private AgentManagerMessenger agentManagerMessenger; + private AgentManagerMessager agentManagerMessenger; private AgentValidate getAgentValidate() { return agentValidate; @@ -34,13 +35,13 @@ public class AgentController extends BaseController { this.agentValidate = agentValidate; } - private AgentManagerMessenger getAgentManagerMessenger() { + private AgentManagerMessager getAgentManagerMessenger() { return agentManagerMessenger; } @Autowired private void setAgentManagerMessenger( - AgentManagerMessenger agentManagerMessenger) { + AgentManagerMessager agentManagerMessenger) { this.agentManagerMessenger = agentManagerMessenger; } @@ -89,9 +90,11 @@ public class AgentController extends BaseController { Map map = new HashMap(); AgentResponseModel agentResponseModel = this.getAgentManagerMessenger() .loadAgents(accessToken); - map = processAgentResposeModel(agentResponseModel, map); - if ((Boolean) map.get("success")) { + if (agentResponseModel.isSuccess()) { + map = success(map); map.put("agents", agentResponseModel.getAgents()); + } else { + map = fail(map, agentResponseModel.getFailCauseString()); } return map; } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/validation/AgentValidate.java b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/AgentValidate.java index 9c817ce0..18f992b9 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/validation/AgentValidate.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/AgentValidate.java @@ -1,5 +1,8 @@ package org.bench4q.web.validation; +import org.springframework.stereotype.Component; + +@Component public class AgentValidate { public ValidateResponseModel validateAddedAgent(String hostName, diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ValidateHelper.java b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ValidateHelper.java index ae6ceb2f..1eadab74 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ValidateHelper.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ValidateHelper.java @@ -3,6 +3,7 @@ package org.bench4q.web.validation; import java.util.regex.Matcher; import java.util.regex.Pattern; + public class ValidateHelper { public static boolean validateStringInput(String input) { diff --git a/Bench4Q-Web/src/main/resources/ipConfig.properties b/Bench4Q-Web/src/main/resources/ipConfig.properties index 93407696..e69de29b 100644 --- a/Bench4Q-Web/src/main/resources/ipConfig.properties +++ b/Bench4Q-Web/src/main/resources/ipConfig.properties @@ -1 +0,0 @@ -masterAddress=133.133.12.1:7979/ \ No newline at end of file diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/AgentMessagerTest.java b/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/AgentMessagerTest.java new file mode 100644 index 00000000..8a28b89b --- /dev/null +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/AgentMessagerTest.java @@ -0,0 +1,113 @@ +package org.bench4q.web.test.masterMessager; + +import static org.junit.Assert.*; + +import java.util.List; + +import javax.xml.bind.JAXBException; + +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.master.AgentModel; +import org.bench4q.share.models.master.AgentResponseModel; +import org.bench4q.web.masterMessager.AgentManagerMessager; +import org.junit.Before; +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 AgentMessagerTest { + + private String accessToken; + private MessagerLoginTool messagerLoginTool; + private AgentManagerMessager agentManagerMessager; + + public MessagerLoginTool getMessagerLoginTool() { + return messagerLoginTool; + } + + @Autowired + public void setMessagerLoginTool(MessagerLoginTool messagerLoginTool) { + this.messagerLoginTool = messagerLoginTool; + } + + public AgentManagerMessager getAgentManagerMessager() { + return agentManagerMessager; + } + + @Autowired + public void setAgentManagerMessager( + AgentManagerMessager agentManagerMessager) { + this.agentManagerMessager = agentManagerMessager; + } + + @Before + public void setUp() { + this.accessToken = this.getMessagerLoginTool().getAccessToken(); + } + + @Test + public void test_addAndDeleteAgent() throws JAXBException { + AgentModel agentModel = createAgentModel(); + + AgentResponseModel agentResponseModel = this.addAgent(agentModel); + assertNotNull(agentResponseModel); + assertTrue(agentResponseModel.isSuccess()); + agentResponseModel = this.deleteAgent(agentModel); + assertNotNull(agentResponseModel); + assertTrue(agentResponseModel.isSuccess()); + } + + @Test + public void test_loadAgents() throws JAXBException { + int countBeforeInsert = this.getAgentManagerMessager() + .loadAgents(this.accessToken).getAgents().size(); + AgentModel agentModel = this.createAgentModel(); + assertTrue(this.addAgent(agentModel).isSuccess()); + int countAfterInsert = this.getAgentManagerMessager() + .loadAgents(this.accessToken).getAgents().size(); + assertEquals(countBeforeInsert + 1, countAfterInsert); + assertTrue(this.deleteAgent(agentModel).isSuccess()); + + } + + private AgentResponseModel addAgent(AgentModel agentModel) + throws JAXBException { + + String agentModelContent = MarshalHelper.marshal(AgentModel.class, + agentModel); + AgentResponseModel agentResponseModel = this.getAgentManagerMessager() + .addAgent(this.accessToken, agentModelContent); + return agentResponseModel; + } + + private AgentResponseModel deleteAgent(AgentModel agentModel) { + return this.getAgentManagerMessager().deleteAgent(this.accessToken, + this.getAgentId(agentModel.getHostName()), + agentModel.getHostName()); + + } + + private AgentModel createAgentModel() { + AgentModel agentModel = new AgentModel(); + agentModel.setHostName("133.133.1.1"); + agentModel.setPort(5555); + agentModel.setMaxLoad(500); + return agentModel; + } + + private String getAgentId(String hostName) { + List agentModels = this.getAgentManagerMessager() + .loadAgents(this.accessToken).getAgents(); + for (AgentModel agentModel : agentModels) { + if (agentModel.getHostName().equals(hostName)) { + return new Integer(agentModel.getId()).toString(); + } + } + return null; + } +} diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/MessagerLoginTool.java b/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/MessagerLoginTool.java new file mode 100644 index 00000000..46229075 --- /dev/null +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/test/masterMessager/MessagerLoginTool.java @@ -0,0 +1,32 @@ +package org.bench4q.web.test.masterMessager; + +import org.bench4q.share.models.master.AuthorizeResponseModel; +import org.bench4q.web.masterMessager.UserMessager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class MessagerLoginTool { + private UserMessager userMessager; + private final String userName = "admin"; + private final String password = "admin"; + + private UserMessager getUserMessager() { + return userMessager; + } + + @Autowired + private void setUserMessager(UserMessager userMessager) { + this.userMessager = userMessager; + } + + public String getAccessToken() { + AuthorizeResponseModel authorizeResponseModel = this.getUserMessager() + .adminLogin(userName, password); + if (authorizeResponseModel != null) + return authorizeResponseModel.getAccessToken(); + else + return null; + } + +} diff --git a/Bench4Q-Web/src/test/resources/ipConfig.properties b/Bench4Q-Web/src/test/resources/ipConfig.properties index 63e5d3c0..f480ff2b 100644 --- a/Bench4Q-Web/src/test/resources/ipConfig.properties +++ b/Bench4Q-Web/src/test/resources/ipConfig.properties @@ -1 +1 @@ -masterAddress=127.0.0.1:7979/ \ No newline at end of file +masterAddress=133.133.12.1:7979 \ No newline at end of file