@Test
public void testServicesWithRangerPluginRoleCommandOrder() throws AmbariException {

  URL rootDirectoryURL = StackManagerTest.class.getResource("/");
  Assert.notNull(rootDirectoryURL);
  File resourcesDirectory = new File(new File(rootDirectoryURL.getFile()).getParentFile().getParentFile(), "src/main/resources");
  File stackRoot = new File(resourcesDirectory, "stacks");
  File commonServices = new File(resourcesDirectory, "common-services");
  File extensions = null;
  try {
    URL extensionsURL = ClassLoader.getSystemClassLoader().getResource("extensions");
    if (extensionsURL != null) {
      extensions = new File(extensionsURL.getPath().replace("test-classes","classes"));
    }
  }
  catch (Exception e) {}
  MetainfoDAO metaInfoDao = createNiceMock(MetainfoDAO.class);
  StackDAO stackDao = createNiceMock(StackDAO.class);
  ExtensionDAO extensionDao = createNiceMock(ExtensionDAO.class);
  ExtensionLinkDAO linkDao = createNiceMock(ExtensionLinkDAO.class);
  ActionMetadata actionMetadata = createNiceMock(ActionMetadata.class);
  Configuration config = createNiceMock(Configuration.class);
  ExtensionEntity extensionEntity = createNiceMock(ExtensionEntity.class);
  expect(config.getSharedResourcesDirPath()).andReturn(
    ClassLoader.getSystemClassLoader().getResource("").getPath()).anyTimes();
  expect(
      extensionDao.find(EasyMock.anyObject(String.class),
          EasyMock.anyObject(String.class))).andReturn(extensionEntity).atLeastOnce();
  List<ExtensionLinkEntity> list = Collections.emptyList();
  expect(
      linkDao.findByStack(EasyMock.anyObject(String.class),
          EasyMock.anyObject(String.class))).andReturn(list).atLeastOnce();
  replay(config, metaInfoDao, stackDao, extensionDao, linkDao, actionMetadata);
  OsFamily osFamily = new OsFamily(config);
  StackManager stackManager = new StackManager(stackRoot, commonServices, extensions, osFamily,
      false, metaInfoDao, actionMetadata, stackDao, extensionDao, linkDao);
  String rangerUserSyncRoleCommand = Role.RANGER_USERSYNC + "-" + RoleCommand.START;
  String rangerAdminRoleCommand = Role.RANGER_ADMIN + "-" + RoleCommand.START;
  String zookeeperServerRoleCommand = Role.ZOOKEEPER_SERVER + "-" + RoleCommand.START;
  String nodeManagerRoleCommand = Role.NODEMANAGER + "-" + RoleCommand.START;
  String mySqlServerRoleCommand = Role.MYSQL_SERVER + "-" + RoleCommand.START;
  // When
  StackInfo hdp = stackManager.getStack("HDP", "2.3");
  Map<String, Object> rco = hdp.getRoleCommandOrder().getContent();
  // Then
  // verify that services that have ranger plugin are after ranger admin in the role command order sequence
  // as these services require ranger admin and ranger user sync to up upfront
  Map<String, Object> generalDeps = (Map<String, Object>)rco.get("general_deps");
  Map<String, Object> optionalNoGlusterfs = (Map<String, Object>)rco.get("optional_no_glusterfs");
  // HDFS
  String nameNodeRoleCommand  = Role.NAMENODE +  "-" + RoleCommand.START;
  ArrayList<String> nameNodeBlockers = (ArrayList<String>)optionalNoGlusterfs.get(nameNodeRoleCommand);
  assertTrue(nameNodeRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, nameNodeBlockers.contains(rangerUserSyncRoleCommand));
  String dataNodeRoleCommand = Role.DATANODE +  "-" + RoleCommand.START;
  ArrayList<String> dataNodeBlockers = (ArrayList<String>)optionalNoGlusterfs.get(dataNodeRoleCommand);
  assertTrue(dataNodeRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, dataNodeBlockers.contains(rangerUserSyncRoleCommand));
  // YARN
  String resourceManagerCommandRoleCommand = Role.RESOURCEMANAGER +  "-" + RoleCommand.START;
  ArrayList<String> resourceManagerBlockers = (ArrayList<String>)generalDeps.get(resourceManagerCommandRoleCommand);
  assertTrue(resourceManagerCommandRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, resourceManagerBlockers.contains(rangerUserSyncRoleCommand));
  // HBase
  String hbaseRoleCommand = Role.HBASE_MASTER +  "-" + RoleCommand.START;
  ArrayList<String> hbaseBlockers = (ArrayList<String>)generalDeps.get(hbaseRoleCommand);
  assertTrue(hbaseRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, hbaseBlockers.contains(rangerUserSyncRoleCommand));
  assertTrue(hbaseRoleCommand + " should be dependent of " + zookeeperServerRoleCommand, hbaseBlockers.contains(zookeeperServerRoleCommand));
  // Knox
  String knoxRoleCommand = Role.KNOX_GATEWAY +  "-" + RoleCommand.START;
  ArrayList<String> knoxBlockers = (ArrayList<String>)generalDeps.get(knoxRoleCommand);
  assertTrue(knoxRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, knoxBlockers.contains(rangerUserSyncRoleCommand));
  // Kafka
  String kafkaRoleCommand = Role.KAFKA_BROKER +  "-" + RoleCommand.START;
  ArrayList<String> kafkaBlockers = (ArrayList<String>)generalDeps.get(kafkaRoleCommand);
  assertTrue(Role.KAFKA_BROKER + "-" + RoleCommand.START + " should be dependent of " + rangerUserSyncRoleCommand, kafkaBlockers.contains(rangerUserSyncRoleCommand));
  // Hive
  String hiveRoleCommand = Role.HIVE_SERVER +  "-" + RoleCommand.START;
  ArrayList<String> hiveBlockers = (ArrayList<String>)generalDeps.get(hiveRoleCommand);
  assertTrue(hiveRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, hiveBlockers.contains(rangerUserSyncRoleCommand));
  assertTrue(hiveRoleCommand + " should be dependent of " + nodeManagerRoleCommand, hiveBlockers.contains(nodeManagerRoleCommand));
  assertTrue(hiveRoleCommand + " should be dependent of " + mySqlServerRoleCommand, hiveBlockers.contains(mySqlServerRoleCommand));
  // Storm
  String stormRoleCommand = Role.NIMBUS +  "-" + RoleCommand.START;
  ArrayList<String> stormBlockers = (ArrayList<String>)generalDeps.get(stormRoleCommand);
  assertTrue(stormRoleCommand + " should be dependent of " + rangerUserSyncRoleCommand, stormBlockers.contains(rangerUserSyncRoleCommand));
  assertTrue(stormRoleCommand + " should be dependent of " + zookeeperServerRoleCommand, stormBlockers.contains(zookeeperServerRoleCommand));
  // Ranger KMS
  String kmsRoleCommand = Role.RANGER_KMS_SERVER +  "-" + RoleCommand.START;
  ArrayList<String> rangerKmsBlockers = (ArrayList<String>)generalDeps.get(kmsRoleCommand);
  assertTrue(kmsRoleCommand + " should be dependent of " + rangerAdminRoleCommand, rangerKmsBlockers.contains(rangerAdminRoleCommand));
  // Ranger User Sync
  ArrayList<String> rangerUserSyncBlockers = (ArrayList<String>)generalDeps.get(rangerUserSyncRoleCommand);
  assertTrue(rangerUserSyncRoleCommand + " should be dependent of " + rangerAdminRoleCommand, rangerUserSyncBlockers.contains(rangerAdminRoleCommand));
  assertTrue(rangerUserSyncRoleCommand + " should be dependent of " + kmsRoleCommand, rangerUserSyncBlockers.contains(kmsRoleCommand));
}