package org.bench4q.web.test.masterMessager;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import static org.junit.Assert.*;

import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.OrganizeRecordPortResponseModel;
import org.bench4q.web.masterMessager.RecordPortMessager;

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 PortMessageTest extends MessagerTestBase {

	private RecordPortMessager recordPortMessager;

	public RecordPortMessager getRecordPortMessager() {
		return recordPortMessager;
	}

	@Autowired
	public void setRecordPortMessager(RecordPortMessager recordPortMessager) {
		this.recordPortMessager = recordPortMessager;
	}

	@BeforeClass
	public static void setUp() {

		startServer();
	}

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

	@Test
	public void test_addPort() throws IOException, JAXBException {

		this.getWireMock().register(post(urlEqualTo("/RecordPort/addPortToPortPool"))
				.withRequestBody(containing("port")).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		OrganizeRecordPortResponseModel responseModel = recordPortMessager
				.addPort(null, "8090");
		assertNotNull(responseModel);
		assertTrue(responseModel.isSuccess());
	}

	@Test
	public void test_deletePort() throws JAXBException {
		this.getWireMock().register(post(urlEqualTo("/RecordPort/removePortFromPool"))
				.withRequestBody(containing("port")).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		OrganizeRecordPortResponseModel responseModel = recordPortMessager
				.deletePort(null, "8090");
		assertNotNull(responseModel);
		assertTrue(responseModel.isSuccess());
	}

	@Test
	public void test_loadPorts() throws JAXBException {
		this.getWireMock().register(post(urlEqualTo("/RecordPort/loadPortList"))
				.willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		OrganizeRecordPortResponseModel responseModel = recordPortMessager
				.loadPorts(null);
		assertNotNull(responseModel);
		assertTrue(responseModel.isSuccess());
		assertNotNull(responseModel.getPortModels());
	}

	private String createResponse() throws JAXBException {
		OrganizeRecordPortResponseModel organizeRecordPortResponseModel = new OrganizeRecordPortResponseModel();
		organizeRecordPortResponseModel.setSuccess(true);

		String response = MarshalHelper.marshal(
				OrganizeRecordPortResponseModel.class,
				organizeRecordPortResponseModel);
		return response;
	}

}
