gt:                     AssignmentData ad = new AssignmentData();
context: package com.intuit.tank.transform.scriptGenerator;
  
  /*
   * #%L
   * Common
   * %%
   * Copyright (C) 2011 - 2015 Intuit Inc.
   * %%
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   * #L%
   */
  
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  import java.util.Set;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.Marshaller;
  
  import org.apache.commons.lang.math.NumberUtils;
  
  import com.intuit.tank.harness.data.AssignmentData;
  import com.intuit.tank.harness.data.ClearCookiesStep;
  import com.intuit.tank.harness.data.CookieStep;
  import com.intuit.tank.harness.data.HDRequest;
  import com.intuit.tank.harness.data.HDResponse;
  import com.intuit.tank.harness.data.HDScript;
  import com.intuit.tank.harness.data.HDScriptGroup;
  import com.intuit.tank.harness.data.HDScriptUseCase;
  import com.intuit.tank.harness.data.HDTestPlan;
  import com.intuit.tank.harness.data.HDTestVariables;
  import com.intuit.tank.harness.data.HDWorkload;
  import com.intuit.tank.harness.data.Header;
  import com.intuit.tank.harness.data.LogicStep;
  import com.intuit.tank.harness.data.RequestStep;
  import com.intuit.tank.harness.data.ResponseData;
  import com.intuit.tank.harness.data.SleepTimeStep;
  import com.intuit.tank.harness.data.TestStep;
  import com.intuit.tank.harness.data.ThinkTimeStep;
  import com.intuit.tank.harness.data.TimerStep;
  import com.intuit.tank.harness.data.ValidationData;
  import com.intuit.tank.harness.data.VariableStep;
  import com.intuit.tank.project.BaseJob;
  import com.intuit.tank.project.RequestData;
  import com.intuit.tank.project.Script;
  import com.intuit.tank.project.ScriptGroup;
  import com.intuit.tank.project.ScriptGroupStep;
  import com.intuit.tank.project.ScriptStep;
  import com.intuit.tank.project.TestPlan;
  import com.intuit.tank.project.Workload;
  import com.intuit.tank.script.RequestDataType;
  import com.intuit.tank.script.ScriptConstants;
  import com.intuit.tank.script.TimerAction;
  import com.intuit.tank.vm.api.enumerated.ValidationType;
  
  public class ConverterUtil {
      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ConverterUtil.class);
  
      public static HDWorkload convertScriptToHdWorkload(Script script) {
  
          String name = "TestPlan for " + script.getName();
          name += " (id_" + script.getId() + ")";
          HDWorkload hdWorkload = new HDWorkload();
  
          hdWorkload.setDescription(name);
          hdWorkload.setName(name);
  
          HDTestPlan tp = new HDTestPlan();
          hdWorkload.getPlans().add(tp);
          tp.setTestPlanName("Main");
          tp.setUserPercentage(100);
  
          HDScriptGroup hdScriptGroup = new HDScriptGroup();
          tp.getGroup().add(hdScriptGroup);
          hdScriptGroup.setLoop(1);
          hdScriptGroup.setName("Group-1");
          hdScriptGroup.setDescription("autoGen");
  
          HDScript testGroup = new HDScript();
          hdScriptGroup.getGroupSteps().add(testGroup);
          testGroup.setName(script.getName());
          testGroup.setLoop(1);
          testGroup.getUseCase().addAll(convertScript(script.getScriptSteps(), new StepCounter()));
  
          return hdWorkload;
  
      }
  
      public static HDWorkload convertWorkload(Workload workload, BaseJob job) {
  
          String name = "TestPlan for " + workload.getProject() != null ? " project " + workload.getProject().getName()
                  : " workload " + workload.getName();
          int id = workload.getProject() != null ? workload.getProject().getId() : workload.getId();
          name += " (id" + id + ")";
  
          HDWorkload hdWorkload = new HDWorkload();
  
          hdWorkload.setDescription(name);
          hdWorkload.setName(name);
          hdWorkload.setVariables(new HDTestVariables(job.isAllowOverride(), job.getVariables()));
          for (TestPlan plan : workload.getTestPlans()) {
              HDTestPlan tp = new HDTestPlan();
              tp.setTestPlanName(plan.getName());
              tp.setUserPercentage(plan.getUserPercentage());
              tp.getGroup().addAll(convertScriptGroups(plan.getScriptGroups(), new StepCounter()));
              hdWorkload.getPlans().add(tp);
          }
          return hdWorkload;
  
      }
  
      public static String getWorkloadXML(HDWorkload hdWorkload) {
          StringWriter sw;
          try {
              JAXBContext context = JAXBContext.newInstance(HDWorkload.class.getPackage().getName());
              Marshaller createMarshaller = context.createMarshaller();
              createMarshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
              sw = new StringWriter();
              createMarshaller.marshal(hdWorkload, sw);
              sw.flush();
              sw.close();
          } catch (Exception e) {
              e.printStackTrace();
              throw new RuntimeException(e);
          }
          return sw.toString();
  
      }
  
      private static List<HDScriptGroup> convertScriptGroups(List<ScriptGroup> scriptGroups, StepCounter sc) {
          List<HDScriptGroup> hdScriptGroups = new ArrayList<HDScriptGroup>();
          for (ScriptGroup scriptGroup : scriptGroups) {
              hdScriptGroups.add(convertScriptGroup(scriptGroup, sc));
          }
          return hdScriptGroups;
      }
  
      private static HDScriptGroup convertScriptGroup(ScriptGroup scriptGroup, StepCounter sc) {
          HDScriptGroup hdScriptGroup = new HDScriptGroup();
          hdScriptGroup.setName(scriptGroup.getName());
          hdScriptGroup.setDescription(scriptGroup.getName());
          hdScriptGroup.setLoop(scriptGroup.getLoop());
          hdScriptGroup.getGroupSteps().addAll(convertScriptGroupSteps(scriptGroup.getScriptGroupSteps(), sc));
          return hdScriptGroup;
      }
  
      private static List<HDScript> convertScriptGroupSteps(List<ScriptGroupStep> sgs, StepCounter sc) {
          List<HDScript> testGroupList = new ArrayList<HDScript>();
          for (ScriptGroupStep scriptGroupStep : sgs) {
              testGroupList.add(convertScriptGroupStep(scriptGroupStep, sc));
          }
          return testGroupList;
      }
  
      private static HDScript convertScriptGroupStep(ScriptGroupStep sgs, StepCounter sc) {
          HDScript testGroup = new HDScript();
          testGroup.setName(sgs.getScript().getName());
          testGroup.setLoop(sgs.getLoop());
  
          Script script = sgs.getScript();
          List<ScriptStep> steps = script.getScriptSteps();
          testGroup.getUseCase().addAll(convertScript(steps, sc));
          return testGroup;
      }
  
      private static List<HDScriptUseCase> convertScript(List<ScriptStep> scriptSteps, StepCounter sc) {
          List<HDScriptUseCase> useCaseList = new ArrayList<HDScriptUseCase>();
          String currentGroup = null;
          HDScriptUseCase useCase = null;
          for (ScriptStep scriptStep : scriptSteps) {
  
              String group = scriptStep.getScriptGroupName();
              if (group == null || group.length() == 0) {
                  // test step has not group name, so generate one:
                  group = "Group " + scriptStep.getStepIndex();
              }
              if (!group.equals(currentGroup)) {
                  useCaseList.add(useCase);
                  useCase = new HDScriptUseCase();
                  currentGroup = group;
              }
              useCase.getScriptSteps().add(convertScriptStep(scriptStep, sc.getNextStepIndex()));
          }
          if (useCase != null) {
              useCaseList.add(useCase);
          }
          return useCaseList;
      }
  
      private static TestStep convertScriptStep(ScriptStep scriptStep, int stepIndex) {
          TestStep testStep = null;
          if (ScriptConstants.SLEEP.equals(scriptStep.getType())) {
              testStep = convertSleepStep(scriptStep);
          } else if (ScriptConstants.THINK_TIME.equals(scriptStep.getType())) {
              testStep = convertThinkStep(scriptStep);
          } else if (ScriptConstants.VARIABLE.equals(scriptStep.getType())) {
              testStep = convertVariableStep(scriptStep);
          } else if (ScriptConstants.COOKIE.equals(scriptStep.getType())) {
              testStep = convertCookieStep(scriptStep);
          } else if (ScriptConstants.LOGIC.equals(scriptStep.getType())) {
              testStep = convertLogicStep(scriptStep);
          } else if (ScriptConstants.REQUEST.equals(scriptStep.getType())) {
              testStep = convertRequestStep(scriptStep);
          } else if (ScriptConstants.CLEAR.equals(scriptStep.getType())) {
              testStep = convertClearStep(scriptStep);
          } else if (ScriptConstants.TIMER.equals(scriptStep.getType())) {
              testStep = convertTimerStep(scriptStep);
          }
          testStep.setStepIndex(stepIndex);
          return testStep;
      }
  
      private static TestStep convertSleepStep(ScriptStep scriptStep) {
          SleepTimeStep sts = new SleepTimeStep();
          Set<RequestData> data = scriptStep.getData();
          for (RequestData requestData : data) {
              if (ScriptConstants.TIME.equals(requestData.getKey())) {
                  sts.setValue(requestData.getValue());
                  break;
              }
          }
          return sts;
      }
  
      private static TestStep convertLogicStep(ScriptStep scriptStep) {
          LogicStep sts = new LogicStep();
          sts.setName(scriptStep.getName());
          sts.setScriptGroupName(scriptStep.getScriptGroupName());
          Set<RequestData> data = scriptStep.getData();
          for (RequestData requestData : data) {
              if (ScriptConstants.SCRIPT.equals(requestData.getKey())) {
                  sts.setScript(requestData.getValue());
                  break;
              }
          }
          return sts;
      }
  
      private static TestStep convertCookieStep(ScriptStep scriptStep) {
          CookieStep sts = new CookieStep();
          Set<RequestData> data = scriptStep.getData();
          for (RequestData requestData : data) {
              if (ScriptConstants.COOKIE_NAME.equals(requestData.getKey())) {
                  sts.setCookieName(requestData.getValue());
              }
              if (ScriptConstants.COOKIE_VALUE.equals(requestData.getKey())) {
                  sts.setCookieValue(requestData.getValue());
              }
              if (ScriptConstants.COOKIE_DOMAIN.equals(requestData.getKey())) {
                  sts.setCookieDomain(requestData.getValue());
              }
              if (ScriptConstants.COOKIE_PATH.equals(requestData.getKey())) {
                  sts.setCookiePath(requestData.getValue());
              }
          }
          return sts;
      }
  
      private static TestStep convertClearStep(ScriptStep scriptStep) {
          ClearCookiesStep sts = new ClearCookiesStep();
          // Set<RequestData> data = scriptStep.getData();
          // for (RequestData requestData : data) {
          // if (ScriptConstants.TIME.equals(requestData.getKey())) {
          // sts.setValue(requestData.getValue());
          // break;
          // }
          // }
          return sts;
      }
  
      private static TestStep convertTimerStep(ScriptStep scriptStep) {
          TimerStep sts = new TimerStep();
          Set<RequestData> data = scriptStep.getData();
          for (RequestData requestData : data) {
              if (ScriptConstants.LOGGING_KEY.equals(requestData.getKey())) {
                  sts.setValue(requestData.getValue());
              } else if (ScriptConstants.IS_START.equals(requestData.getKey())) {
                  TimerAction action = TimerAction.valueOf(requestData.getValue());
                  sts.setStart(action == TimerAction.START);
              }
          }
          return sts;
      }
  
      private static TestStep convertRequestStep(ScriptStep scriptStep) {
          RequestStep rs = new RequestStep();
          rs.setName(scriptStep.getName() + " (" + scriptStep.getStepIndex() + ")");
          rs.setScriptGroupName(scriptStep.getScriptGroupName());
          rs.setOnFail(scriptStep.getOnFail());
  
          HDRequest hdRequest = new HDRequest();
          hdRequest.setMethod(scriptStep.getMethod());
          hdRequest.setReqFormat(scriptStep.getReqFormat());
          hdRequest.setLoggingKey(scriptStep.getLoggingKey());
          hdRequest.setPath(scriptStep.getSimplePath());
          hdRequest.setProtocol(scriptStep.getProtocol());
          hdRequest.setPayload(scriptStep.getPayload());
          hdRequest.setLabel(scriptStep.getLabel());
          String host = extractHost(scriptStep.getHostname());
          String port = extractPort(scriptStep.getHostname());
          hdRequest.setHost(host);
          hdRequest.setPort(port);
          hdRequest.setPostDatas(getPostData(scriptStep.getPostDatas()));
          hdRequest.setQueryString(getPostData(scriptStep.getQueryStrings()));
          hdRequest.setRequestHeaders(getRequestHeaders(scriptStep.getRequestheaders(), scriptStep.getRequestCookies()));
  
          HDResponse hdResponse = new HDResponse(scriptStep.getRespFormat());
  
          hdResponse.getAssignment().getHeaderVariable()
                  .addAll(getAssignments(RequestDataType.headerAssignment, scriptStep.getResponseData()));
          hdResponse.getAssignment().getCookieVariable()
                  .addAll(getAssignments(RequestDataType.cookieAssignment, scriptStep.getResponseData()));
          hdResponse.getAssignment().getBodyVariable()
                  .addAll(getAssignments(RequestDataType.bodyAssignment, scriptStep.getResponseData()));
  
          hdResponse.getValidation().getHeaderValidation()
                  .addAll(getValidations(RequestDataType.headerValidation, scriptStep.getResponseData()));
          hdResponse.getValidation().getCookieValidation()
                  .addAll(getValidations(RequestDataType.cookieValidation, scriptStep.getResponseData()));
          hdResponse.getValidation().getBodyValidation()
                  .addAll(getValidations(RequestDataType.bodyValidation, scriptStep.getResponseData()));
  
          rs.setRequest(hdRequest);
          rs.setResponse(hdResponse);
  
          return rs;
      }
  
      /**
       * @param hostname
       * @return
       */
      protected static String extractPort(String hostname) {
          int index = hostname.lastIndexOf(':');
          String port = null;
          if (index != -1 && index + 1 < hostname.length()) {
              String candidate = hostname.substring(index + 1);
              if (NumberUtils.isDigits(candidate)) {
                  port = candidate;
              } 
          }
          return port;
      }
  
      /**
       * @param hostname
       * @return
       */
      protected static String extractHost(String hostname) {
          int index = hostname.lastIndexOf(':');
          if (index != -1) {
              try {
                  String port = hostname.substring(index + 1);
                  if (NumberUtils.isDigits(port)) {
                      hostname = hostname.substring(0, index);
                  }
              } catch (Exception e) {
                  LOG.error("Error parsing host");
              }
          }
          return hostname;
      }
  
      private static Collection<? extends AssignmentData> getAssignments(RequestDataType type,
              Set<RequestData> requestDatas) {
          List<AssignmentData> assignmentDataList = new ArrayList<AssignmentData>();
          for (RequestData requestData : requestDatas) {
              if (isAssignment(requestData)) {
                  if (type.name().equals(requestData.getType())
                          || (type == RequestDataType.bodyAssignment
                          && requestData.getType().equals("responseData"))) {
  