@SuppressWarnings("unchecked")
  public static D2CanaryDistributionStrategy toConfig(CanaryDistributionStrategy properties)
  {
    D2CanaryDistributionStrategy config = new D2CanaryDistributionStrategy();
    StrategyType type = strategyTypes.get(properties.getStrategy());
    if (type == null)
    {
      LOG.warn("Unknown strategy type from CanaryDistributionStrategy: " + properties.getStrategy() + ". Fall back to DISABLED.");
      type = StrategyType.DISABLED;
    }
    config.setStrategy(type);

    try
    {
      switch (type)
      {
        case PERCENTAGE:
          Double scope = getValidScope(PropertyUtil.checkAndGetValue(properties.getPercentageStrategyProperties(), PropertyKeys.PERCENTAGE_SCOPE,
                                        Number.class, "PercentageStrategyProperties").doubleValue());
          PercentageStrategyProperties toPercentageProperties = new PercentageStrategyProperties();
          toPercentageProperties.setScope(scope);
          config.setPercentageStrategyProperties(toPercentageProperties);
          break;
        case TARGET_HOSTS:
          List<String> hosts = PropertyUtil.checkAndGetValue(properties.getTargetHostsStrategyProperties(), PropertyKeys.TARGET_HOSTS, List.class,
                                                      "TargetHostsStrategyProperties");
          TargetHostsStrategyProperties toTargetHostsProperties = new TargetHostsStrategyProperties();
          toTargetHostsProperties.setTargetHosts(new StringArray(hosts));
          config.setTargetHostsStrategyProperties(toTargetHostsProperties);
          break;
        case TARGET_APPLICATIONS:
          Map<String, Object> fromTargetAppsProperties = properties.getTargetApplicationsStrategyProperties();
          List<String> apps = PropertyUtil.checkAndGetValue(fromTargetAppsProperties, PropertyKeys.TARGET_APPLICATIONS, List.class,
                                                      "TargetApplicationsStrategyProperties");
          Double appScope = getValidScope(PropertyUtil.checkAndGetValue(fromTargetAppsProperties, PropertyKeys.PERCENTAGE_SCOPE,
                                        Number.class, "TargetApplicationsStrategyProperties").doubleValue());

          TargetApplicationsStrategyProperties toTargetAppsProperties = new TargetApplicationsStrategyProperties();
          toTargetAppsProperties.setTargetApplications(new StringArray(apps));
          toTargetAppsProperties.setScope(appScope);
          config.setTargetApplicationsStrategyProperties(toTargetAppsProperties);
          break;
        case DISABLED:
          break;
        default:
          throw new IllegalStateException("Unexpected strategy type: " + type);
      }
    }
    catch (Exception e)
    {
      LOG.warn("Error in converting distribution strategy. Fall back to DISABLED.", e);
      config.setStrategy(StrategyType.DISABLED);
    }
    return config;
  }
@Test(dataProvider = "getEdgeCasesDistributionPropertiesAndConfigs")
  public void testToConfigEdgeCases(String strategyType, Map<String, Object> percentageProperties, Map<String, Object> targetHostsProperties,
                                Map<String, Object> targetAppsProperties, D2CanaryDistributionStrategy expected)
  {
    CanaryDistributionStrategy input = new CanaryDistributionStrategy(strategyType, percentageProperties, targetHostsProperties, targetAppsProperties);
    Assert.assertEquals(CanaryDistributionStrategyConverter.toConfig(input), expected);
  }