/*
 * Copyright 2019 Arcus Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iris.protocol.zwave.model;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestZWaveCommandClass {

	// Test that jackson can create and read the from the same file for command creation.
	@Test
	public void testJacksonCreateSaveAndLoad() throws Exception {
		File filename = File.createTempFile("TestZWaveCommandClass", ".json");
		filename.deleteOnExit();

		// Create a set command in json format.
		ZWaveCommandClass 	switchCommandClass 	= new ZWaveCommandClass("Switch Binary",(byte) 0x25);
		ZWaveCommand		setCommand			= new ZWaveCommand("set",(byte) 0x01);
		setCommand.addSendVariable("value");
		ZWaveCommand		getCommand			= new ZWaveCommand("get",(byte) 0x02);
		ZWaveCommand		reportCommand		= new ZWaveCommand("report",(byte) 0x03);
		reportCommand.addReceiveVariable("value");
		switchCommandClass.add(setCommand);
		switchCommandClass.add(getCommand);
		switchCommandClass.add(reportCommand);

		// Write out the command class
		ObjectMapper mapper = new ObjectMapper();
		mapper.writerWithDefaultPrettyPrinter().writeValue(new FileWriter(filename), switchCommandClass);

		// Read that set command in json format.
		ObjectMapper mapper2 = new ObjectMapper();
		@SuppressWarnings("unused")
		ZWaveCommandClass setCommand2 = mapper2.readValue(FileUtils.readFileToByteArray(filename), ZWaveCommandClass.class );
	}
}

