add test to agentMessager

This commit is contained in:
fanfuxiaoran 2014-04-30 10:34:06 +08:00
parent ec705fed2a
commit cd6696dc67
16 changed files with 190 additions and 37 deletions

View File

@ -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(

View File

@ -1,4 +1,4 @@
package org.bench4q.web.masterMessenger;
package org.bench4q.web.masterMessager;
import java.io.File;
import java.io.FileInputStream;

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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<String, String> params) {
HttpResponse httpResponse = null;
try {
httpResponse = this.getHttpRequester().sendPost(url, params,
httpResponse = this.getHttpRequester().sendGet(url, params,
makeAccessTockenMap(accessToken));
if (!validateHttpResponse(httpResponse))
return null;

View File

@ -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<String, Object> map = new HashMap<String, Object>();
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;
}

View File

@ -1,5 +1,8 @@
package org.bench4q.web.validation;
import org.springframework.stereotype.Component;
@Component
public class AgentValidate {
public ValidateResponseModel validateAddedAgent(String hostName,

View File

@ -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) {

View File

@ -1 +0,0 @@
masterAddress=133.133.12.1:7979/

View File

@ -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<AgentModel> agentModels = this.getAgentManagerMessager()
.loadAgents(this.accessToken).getAgents();
for (AgentModel agentModel : agentModels) {
if (agentModel.getHostName().equals(hostName)) {
return new Integer(agentModel.getId()).toString();
}
}
return null;
}
}

View File

@ -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;
}
}

View File

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