package org.bench4q.master.unitTest.factory;

import static org.junit.Assert.*;

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

import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.plugin.BehaviorInfo;
import org.bench4q.master.domain.entity.plugin.FieldType;
import org.bench4q.master.domain.entity.plugin.ParamInfo;
import org.bench4q.master.domain.entity.plugin.PluginInfo;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginEntityFactory;
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 Test_PluginEntityFactory {

	private PluginEntityFactory pluginEntityFactory;

	public PluginEntityFactory getPluginEntityFactory() {
		return pluginEntityFactory;
	}

	@Autowired
	public void setPluginEntityFactory(PluginEntityFactory pluginEntityFactory) {
		this.pluginEntityFactory = pluginEntityFactory;
	}

	@Test
	public void test_createPluginFormString() throws IOException {
		String fileSeparator = System.getProperty("file.separator");
		String filePath = System.getProperty("user.dir") + fileSeparator
				+ "pluginUI" + fileSeparator + "CsvProvider.xml";
		File file = new File(filePath);
		String uiContent = FileUtils.readFileToString(file);
		assertNotNull(uiContent);
		PluginUI pluginUI = pluginEntityFactory
				.createPluginUIWithOutId(uiContent);
		assertNotNull(pluginUI);
		assertNotNull(pluginUI.getBehaviorInfos());
		assertNotNull(pluginUI.getPluginInfo());
		PluginInfo pluginInfo = pluginUI.getPluginInfo();
		assertEquals(pluginInfo.getParams().size(), 7);
		Set<ParamInfo> pluginParamInfos = pluginInfo.getParams();
		FieldType fieldType = null;
		for (ParamInfo pluginParamInfo : pluginParamInfos) {
			if (pluginParamInfo.getParamType() instanceof FieldType)
				fieldType = (FieldType) pluginParamInfo.getParamType();
		}
		assertTrue(fieldType.getSize() == 7);
		assertEquals(2, pluginUI.getBehaviorInfos().size());
		int timerCount = 0;
		System.out.println(pluginUI.getBehaviorInfos().size());
		for (BehaviorInfo behaviorInfo : pluginUI.getBehaviorInfos()) {
			if (behaviorInfo.getBehaviorType().equals("timer"))
				timerCount++;
		}
		assertEquals(1, timerCount);

	}
}
