/* Copyright 2017, Reinhard Herzog (Fraunhofer IOSB)

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 nato.ivct.commander;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.LoggerFactory;

public class CmdStartTc implements Command {
	// private MessageProducer producer;
	private String sut;
	private String badge;
	private String tc;

	public static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CmdStartTc.class);

	public CmdStartTc(String _sut, String _badge, String _tc, String _runFolder) {
		sut = _sut;
		tc = _tc;
		badge = _badge;
	}

	@SuppressWarnings("unchecked")
	@Override
	/*
	 * The Structure of start test case command message looks like the
	 * following:
	 *
	 * { "sequence":"0",
	 *   "commandType":"startTestCase",
	 *   "testScheduleName":"HelloWorld",
	 *   "sutName":"hw_iosb",
	 *   "sutDir":"C:\\projects\\MSG134\\IVCT_Runtime\\IVCTsut\\hw_iosb",
	 *   "tsRunFolder":"C:\\projects\\MSG134\\IVCT_Runtime\\Badges\\HelloWorld",
	 *   "tcParam":{
	 *     "rtiHostName":"localhost",
	 *     "federationName":"HelloWorld",
	 *     "sutFederateName":"A" },
	 *   "testCaseId":"TC0002"}
	 *
	 * @see nato.ivct.commander.Command#execute()
	 */
	public void execute() {
		// build the start parameter
		try {
			JSONParser parser = new JSONParser();
			JSONObject startCmd = new JSONObject();
			String sutHome = Factory.props.getProperty(Factory.IVCT_SUT_HOME_ID);
			String paramFileName = sutHome + '/' + sut + '/' + badge + "/TcParam.json";
			startCmd.put("commandType", "startTestCase");
			startCmd.put("sequence", Integer.toString(Factory.newCmdCount()));
			startCmd.put("sutName", sut);
			startCmd.put("sutDir", sutHome + '/' + sut);
			startCmd.put("badge", badge);
			startCmd.put("testCaseId", tc);

			String paramFileContentString = Factory.readWholeFile(paramFileName);
			if (paramFileContentString != null) {
			String tmpString = Factory.replaceMacro(paramFileContentString);
			JSONObject jsonParam = (JSONObject) parser.parse(tmpString);
			startCmd.put("tcParam", jsonParam);
			} else {
				LOGGER.error("File not found: " + paramFileName);
			}

			// send the start message
			Factory.sendToJms(startCmd.toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			LOGGER.error("error in starting test case <" + badge + '/' + tc + ">");
			e.printStackTrace();
		}

	}

	public String getSut() {
		return sut;
	}

	public void setSut(String sut) {
		this.sut = sut;
	}

	public String getTc() {
		return tc;
	}

	public void setTc(String tc) {
		this.tc = tc;
	}

	public String getBadge() {
		return badge;
	}

	public void setBadge(String _badge) {
		this.badge = _badge;
	}

}

@@ -1313,25 +1313,10 @@
 * %7B 
-%22sequence%22:%220%22,
 %0D%0A
+
 %09 * 
@@ -1345,24 +1345,47 @@
 TestCase%22,%0D%0A
+%09 *   %22sequence%22:%220%22,%0D%0A
 %09 *   %22testS
@@ -1454,34 +1454,33 @@
 %22sutDir%22:%22C:
-%5C%5C
+/
 projects
 %5C%5CMSG134%5C%5CIV
@@ -1463,34 +1463,25 @@
 %22C:/projects
-%5C%5CMSG134%5C%5C
+/
 IVCT_Runtime
@@ -1484,19 +1484,17 @@
 time
-%5C%5C
+/
 IVCTsut
-%5C%5C
+/
 hw_i
@@ -1511,34 +1511,27 @@
    %22
-tsRunFolder
+badge
 %22:%22C:
-%5C%5C
+/
 projects
 %5C%5CMS
@@ -1530,18 +1530,9 @@
 ects
-%5C%5CMSG134%5C%5C
+/
 IVCT
@@ -1543,18 +1543,16 @@
 time
-%5C%5C
+/
 Badges
-%5C%5C
+/
 Hell
@@ -1549,32 +1549,68 @@
 adges/HelloWorld
+-1.0.0%22,%0D%0A%09 *   %22testCaseId%22:%22TC0002
 %22,%0D%0A%09 *   %22tcPar
@@ -1723,25 +1723,24 @@
 e%22:%22A%22 %7D
-,
 %0D%0A%09 *   
 %22testCas
@@ -1731,37 +1731,16 @@
 %0D%0A%09 *   
-%22testCaseId%22:%22TC0002%22
 %7D%0D%0A%09 *%0D%0A

update comment