@Transactional
  public Long createCluster(String clusterName) throws Exception {
    // required to populate the database with stacks
    injector.getInstance(AmbariMetaInfo.class);

    ResourceTypeDAO resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);

    ResourceTypeEntity resourceTypeEntity =  new ResourceTypeEntity();
    resourceTypeEntity.setId(ResourceType.CLUSTER.getId());
    resourceTypeEntity.setName(ResourceType.CLUSTER.name());
    resourceTypeEntity = resourceTypeDAO.merge(resourceTypeEntity);

    ResourceEntity resourceEntity = new ResourceEntity();
    resourceEntity.setResourceType(resourceTypeEntity);

    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
    StackDAO stackDAO = injector.getInstance(StackDAO.class);

    StackEntity stackEntity = stackDAO.find("HDP", "2.0.6");
    assertNotNull(stackEntity);

    ClusterEntity clusterEntity = new ClusterEntity();
    clusterEntity.setClusterName(clusterName);
    clusterEntity.setClusterInfo("test_cluster_info1");
    clusterEntity.setResource(resourceEntity);
    clusterEntity.setDesiredStack(stackEntity);

    clusterDAO.create(clusterEntity);

    ClusterStateEntity clusterStateEntity = new ClusterStateEntity();
    clusterStateEntity.setCurrentStack(stackEntity);
    clusterStateEntity.setClusterEntity(clusterEntity);
    getEntityManager().persist(clusterStateEntity);

    clusterEntity = clusterDAO.findByName(clusterEntity.getClusterName());
    assertNotNull(clusterEntity);
    assertTrue(clusterEntity.getClusterId() > 0);

    clusterEntity.setClusterStateEntity(clusterStateEntity);
    clusterDAO.merge(clusterEntity);

    // because this test method goes around the Clusters business object, we
    // forcefully will refresh the internal state so that any tests which
    // incorrect use Clusters after calling this won't be affected
    Clusters clusters = injector.getInstance(Clusters.class);
    Method method = ClustersImpl.class.getDeclaredMethod("loadClustersAndHosts");
    method.setAccessible(true);
    method.invoke(clusters);

    return clusterEntity.getClusterId();
  }
Creates an empty cluster with an ID using the specified cluster name.

@return the cluster ID.
ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java