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.plugin.PluginResponseModel;
import org.bench4q.web.masterMessager.PluginMessager;

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("file:src/test/resources/bench4qweb-servlet.xml")
public class PluginMessageTest extends MessagerTestBase {
	private String pluginName = "pluginName";
	private PluginMessager pluginMessager;
	private String baseUrl = "/plugin";

	public PluginMessager getPluginMessager() {
		return pluginMessager;
	}

	@Autowired
	public void setPluginMessager(PluginMessager pluginMessager) {
		this.pluginMessager = pluginMessager;
	}

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

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

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

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

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

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

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

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

	public String createResponse() {
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		pluginResponseModel.setSuccess(true);
		return MarshalHelper.tryMarshal(pluginResponseModel);
	}

}
