package jp.co.denso.fa.jmscaosql.jms;

import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.ArrayList;

import jp.co.denso.fa.jcaosql.CaoSQLMessage;
import jp.co.denso.fa.jmscaosql.model.CaoSQLItemModel;
import jp.co.denso.fa.jmscaosql.model.CaoSQLMessageModel;
import junit.framework.TestCase;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonConverterTest extends TestCase {

	public void testCaoSQLItemModelToJson() throws Exception {
		CaoSQLItemModel itemModel = new CaoSQLItemModel();
		itemModel.setActivation(true);
		itemModel.setAttribute(123);
		itemModel.setDateTime(null);
		itemModel.setDescription("説明書き");

		itemModel.setElements(new String[] { "tamachi", "hamamatucho" });
		itemModel.setIndex(0);
		itemModel.setLinkActivation(false);
		itemModel.setLinkItemNames(new String[] { "item1", "item2" });
		itemModel.setMicrosecond(1);
		itemModel.setName("test");
		itemModel.setSettingData("settingData_hoge");
		itemModel.setState(10);

		itemModel.setValue(new ArrayList<Integer>());

		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(itemModel);
		System.out.println(json);
	}

	public void testJsonToCaoSQLItemModel() throws Exception {
		String json = "{\"activation\":true,\"attribute\":123,\"dateTime\":null,\"description\":\"説明書き\",\"elements\":[\"tamachi\",\"hamamatucho\"],\"index\":0,\"linkActivation\":false,\"linkItemNames\":[\"item1\",\"item2\"],\"microsecond\":1,\"name\":\"test\",\"settingData\":\"settingData_hoge\",\"state\":10,\"tag\":null,\"value\":null,\"valueEx\":null}";
		ObjectMapper mapper = new ObjectMapper();
		CaoSQLItemModel hoge = mapper.readValue(json, CaoSQLItemModel.class);

		System.out.println(hoge);
	}

	public void testCaoSQLMessageModelToJson() throws Exception {
		CaoSQLMessageModel messageModel = new CaoSQLMessageModel(null, "メッセージ説明", "宛先", 123, 333, "so-su",
				"valueのあたい");

		ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
		String json = mapper.writeValueAsString(messageModel);

		System.out.println(json);
	}

	public void testCaoSQLMessageToJson() throws Exception {
		CaoSQLMessage message = new CaoSQLMessage(new Timestamp(System.currentTimeMillis()), "メッセージ説明", "宛先", 123,
				333, "so-su", "valueのあたい");

		ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
		String json = mapper.writeValueAsString(message);
		System.out.println(json);
	}
}
