@Test
public void testDowngradeToBase() throws Exception {

  Cluster cluster = clusters.getCluster("c1");
  Map<String, Object> requestProps = new HashMap<>();
  requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
  requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.1.1.1");
  requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
  requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
  requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
  ResourceProvider upgradeResourceProvider = createProvider(amc);
  Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
  upgradeResourceProvider.createResources(request);
  List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
  assertEquals(1, upgrades.size());
  UpgradeEntity upgrade = upgrades.get(0);
  // now abort the upgrade so another can be created
  abortUpgrade(upgrade.getRequestId());
  // create another upgrade which should fail
  requestProps = new HashMap<>();
  requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
  requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2");
  requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
  requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
  request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
  try {
    upgradeResourceProvider.createResources(request);
    Assert.fail("Expected an exception going downgrade with no upgrade pack");
  } catch (Exception e) {
    // !!! expected
  }
  // fix the properties and try again
  requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
  requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2.0.0");
  requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
  requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
  requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.1.1.0");
  requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
  Map<String, String> requestInfoProperties = new HashMap<>();
  request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), requestInfoProperties);
  RequestStatus status = upgradeResourceProvider.createResources(request);
  assertEquals(1, status.getAssociatedResources().size());
  Resource r = status.getAssociatedResources().iterator().next();
  String id = r.getPropertyValue("Upgrade/request_id").toString();
  UpgradeEntity entity = upgradeDao.findUpgrade(Long.parseLong(id));
  assertNotNull(entity);
  assertEquals("2.1.1.0", entity.getFromVersion());
  assertEquals("2.2.0.0", entity.getToVersion());
  assertEquals(Direction.DOWNGRADE, entity.getDirection());
  StageDAO dao = injector.getInstance(StageDAO.class);
  List<StageEntity> stages = dao.findByRequestId(entity.getRequestId());
  Gson gson = new Gson();
  for (StageEntity se : stages) {
    Map<String, String> map = gson.<Map<String, String>>fromJson(se.getCommandParamsStage(), Map.class);
    assertTrue(map.containsKey("upgrade_direction"));
    assertEquals("downgrade", map.get("upgrade_direction"));
  }
}