package org.bench4q.master.unitTest.controller;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.service.PluginService;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.unitTest.testHelper.Test_PluginHelper;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.plugin.PluginResponseModel;
import org.junit.After;
import org.junit.Assert;
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 = { "classpath:service-test-context.xml" })
public class PluginControllerTest extends TestBase {
	private final String URLSTRING = BASE_URL + "/plugin";
	private Test_PluginHelper test_PluginHelper;
	private PluginService pluginService;
	private String pluginName;
	private String fileName;
	private Logger logger = Logger.getLogger(PluginControllerTest.class);
	@SuppressWarnings("unused")
	private int paramInPluginCount;

	public String getURLSTRING() {
		return URLSTRING;
	}

	public Test_PluginHelper getTest_PluginHelper() {
		return test_PluginHelper;
	}

	@Autowired
	public void setTest_PluginHelper(Test_PluginHelper test_PluginHelper) {
		this.test_PluginHelper = test_PluginHelper;
	}

	public PluginService getPluginService() {
		return pluginService;
	}

	@Autowired
	public void setPluginService(PluginService pluginService) {
		this.pluginService = pluginService;
	}

	@Before
	public void setUp() throws IOException, JAXBException {
		this.pluginName = "hbase";
		this.fileName = "hbase.xml";
		this.paramInPluginCount = 7;
		this.getTest_PluginHelper().addPlugin(fileName,
				this.getPluginService(), pluginName);
		this.setAccessTocken(this.login());
	}

	@Test
	public void testAddPlugin() throws JAXBException, IOException {
		this.getPluginService().deletePlugin(pluginName);
		this.setAccessTocken(this.login());
		String url = URLSTRING + "/addPlugin";
		File file = new File(Test_PluginHelper.filePath(fileName));
		String pluginContent = FileUtils.readFileToString(file);
		HttpResponse httpResponse = this.httpRequester.sendPutXml(url,
				pluginContent, makeAccessTockenMap(this.getAccessTocken()));
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.tryUnmarshal(PluginResponseModel.class,
						httpResponse.getContent());
		Assert.assertNotNull(pluginResponseModel);
		Assert.assertTrue(pluginResponseModel.isSuccess());
		this.getPluginService().deletePlugin(pluginName);
	}

	@Test
	public void testLoadPluginNameList() throws JAXBException, IOException {
		this.getPluginService().deletePlugin(pluginName);
		String url = URLSTRING + "/loadPluginNameList";
		int insertBefore = this.getPluginService().loadPluginUIs().size();
		this.test_PluginHelper.addPlugin(fileName, pluginService, pluginName);
		HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
				makeAccessTockenMap(this.getAccessTocken()));
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.tryUnmarshal(PluginResponseModel.class,
						httpResponse.getContent());
		System.out.println(httpResponse.getContent());
		assertNotNull(pluginResponseModel);
		Assert.assertTrue(pluginResponseModel.isSuccess());
		Assert.assertTrue(pluginResponseModel.getPluginList().size() > 0);
		assertEquals(insertBefore + 1, pluginResponseModel.getPluginList()
				.size());
		this.getPluginService().deletePlugin(pluginName);
	}

	@Test
	public void testLoadPluginList() throws IOException, JAXBException {
		this.getPluginService().deletePlugin(pluginName);
		int insertBefore = this.getPluginService().loadPluginUIs().size();
		this.getTest_PluginHelper().addPlugin(fileName,
				this.getPluginService(), pluginName);
		String url = URLSTRING + "/loadPluginList";
		HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
				makeAccessTockenMap(this.getAccessTocken()));
		System.out.println(httpResponse.getContent());
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.tryUnmarshal(PluginResponseModel.class,
						httpResponse.getContent());
		assertNotNull(pluginResponseModel);
		assertEquals(insertBefore + 1, pluginResponseModel.getPluginUIModels()
				.size());
		this.getPluginService().deletePlugin(pluginName);
	}

	@Test
	public void testDeletePlugin() throws IOException {
		this.getPluginService().deletePlugin(pluginName);
		this.getTest_PluginHelper().addPlugin(fileName,
				this.getPluginService(), pluginName);
		String url = URLSTRING + "/deletePlugin" + "/" + this.pluginName;
		HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
				makeAccessTockenMap(this.getAccessTocken()));
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.tryUnmarshal(PluginResponseModel.class,
						httpResponse.getContent());
		assertNotNull(pluginResponseModel);
		assertTrue(pluginResponseModel.isSuccess());
	}

	@Test
	public void testGetBehaviors() throws IOException, JAXBException {
		this.getPluginService().deletePlugin(pluginName);
		this.getTest_PluginHelper().addPlugin(fileName,
				this.getPluginService(), pluginName);
		String url = URLSTRING + "/loadBehaviors/" + this.pluginName;
		HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
				makeAccessTockenMap(this.getAccessTocken()));
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.tryUnmarshal(PluginResponseModel.class,
						httpResponse.getContent());
		assertNotNull(pluginResponseModel);
		assertTrue(pluginResponseModel.isSuccess());
		this.getPluginService().deletePlugin(pluginName);
		System.out.println(MarshalHelper.marshal(PluginResponseModel.class,
				pluginResponseModel));
		this.getPluginService().deletePlugin(pluginName);
	}

	@Test
	public void testGetParamType() throws IOException, JAXBException {
		this.getTest_PluginHelper().addPlugin(fileName,
				this.getPluginService(), pluginName);
		String url = URLSTRING + "/loadPluginList";
		Map<String, String> properties = makeAccessTockenMap(this
				.getAccessTocken());
		HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
				properties);
		PluginResponseModel pluginResponseModel = null;
		try {
			pluginResponseModel = (PluginResponseModel) MarshalHelper
					.unmarshal(PluginResponseModel.class,
							httpResponse.getContent());
		} catch (Exception e) {
			logger.info(ExceptionLog.getStackTrace(e));
		}
		assertNotNull(pluginResponseModel);

	}

	@After
	public void clear() throws IOException {

	}

}
