package org.bench4q.web.test.masterMessager;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.*;

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.AfterClass;
import org.junit.BeforeClass;
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 AgentManagerMessagerTest extends MessagerTestBase {
	private AgentManagerMessager agentManagerMessager;
	private String baseUrl = "/agentManage";

	public AgentManagerMessager getAgentManagerMessager() {
		return agentManagerMessager;
	}

	@Autowired
	public void setAgentManagerMessager(
			AgentManagerMessager agentManagerMessager) {
		this.agentManagerMessager = agentManagerMessager;
	}

	@BeforeClass
	public static void setUp() {
		startServer();
	}

	@AfterClass
	public static void clear() {
		stopServer();
	}

	@Test
	public void test_addAgent() {
		String url = baseUrl + "/addAgentToPool";

		this.getWireMock().register(
				post(urlEqualTo(url))
						.withRequestBody(
								equalToXml(MarshalHelper
										.tryMarshal(createAgentModel())))
						.willReturn(
								aResponse()
										.withStatus(200)
										.withHeader("Content-Type",
												"application/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.agentManagerMessager.addAgent(null,
				MarshalHelper.tryMarshal(createAgentModel())));
	}

	private AgentModel createAgentModel() {
		AgentModel agentModel = new AgentModel();
		agentModel.setMaxLoad(500);
		agentModel.setPort(6565);
		return agentModel;
	}

	@Test
	public void test_deleteAgent() {
		String url = baseUrl + "/removeAgentFromPool?hostName=&agentId=";
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.agentManagerMessager.deleteAgent(null, "", ""));
		assertTrue(this.agentManagerMessager.deleteAgent(null, "", "")
				.isSuccess());
	}

	@Test
	public void test_loadAgents() {
		String url = baseUrl + "/queryAgentList";
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.agentManagerMessager.loadAgents(null));
		assertTrue(this.agentManagerMessager.loadAgents(null).isSuccess());
	}

	private String createResponse() {
		AgentResponseModel agentResponseModel = new AgentResponseModel();
		agentResponseModel.setSuccess(true);
		return MarshalHelper.tryMarshal(agentResponseModel);

	}

}
