/**
   * Create a one time activation key for use with a kickstart
   *
   * @param creator of the key
   * @param ksdata associated with the key
   * @param server being kickstarted (can be null)
   * @param session associated with the kickstart (NOT NULL)
   * @param note to add to key
   * @param usageLimit to apply to the key. null for unlimited.
   * @return ActivationKey that has been saved to the DB.
   */
  public static ActivationKey createKickstartActivationKey(
      User creator,
      KickstartData ksdata,
      Server server,
      KickstartSession session,
      Long usageLimit,
      String note) {

    // Now create ActivationKey
    ActivationKey key =
        ActivationKeyManager.getInstance().createNewReActivationKey(creator, server, note, session);
    key.addEntitlement(ServerConstants.getServerGroupTypeProvisioningEntitled());
    key.setDeployConfigs(false);
    key.setUsageLimit(usageLimit);
    if (KickstartVirtualizationType.paraHost()
        .equals(ksdata.getKickstartDefaults().getVirtualizationType())) {
      // we'll have to setup the key for virt
      key.addEntitlement(ServerConstants.getServerGroupTypeVirtualizationEntitled());
    }
    ActivationKeyFactory.save(key);

    // Add child channels to the key
    if (ksdata.getChildChannels() != null && ksdata.getChildChannels().size() > 0) {
      Iterator i = ksdata.getChildChannels().iterator();
      log.debug("Add the child Channels");
      while (i.hasNext()) {
        key.addChannel((Channel) i.next());
      }
    }

    log.debug("** Saving new token");
    ActivationKeyFactory.save(key);
    log.debug("** Saved new token: " + key.getId());
    return key;
  }
  public void testOrgDefautRegistrationToken() throws Exception {
    User user = UserTestUtils.findNewUser("testUser", "testOrg", true);
    Org orig = user.getOrg();
    orig.setName("org created by OrgFactory test: " + TestUtils.randomString());
    // build the channels set
    Channel channel1 = ChannelFactoryTest.createTestChannel(orig);
    flushAndEvict(channel1);
    orig.addOwnedChannel(channel1);
    orig = OrgFactory.save(orig);
    assertTrue(orig.getId().longValue() > 0);

    assertNull(orig.getToken());
    ActivationKey key = ActivationKeyTest.createTestActivationKey(user);
    // Token is hidden behind activation key so we have to look it up
    // manually:
    Token token = TokenFactory.lookupById(key.getId());
    orig.setToken(token);
    orig = OrgFactory.save(orig);
    Long origId = orig.getId();
    flushAndEvict(orig);

    Org lookup = OrgFactory.lookupById(origId);
    assertEquals(token.getId(), lookup.getToken().getId());
    lookup.setToken(null);
    flushAndEvict(lookup);

    lookup = OrgFactory.lookupById(origId);
    assertNull(lookup.getToken());
  }
  // See BZ: 191007
  public void testCreateWithCustomGroups() throws Exception {
    Server s =
        ServerFactoryTest.createTestServer(
            user, true, ServerConstants.getServerGroupTypeEnterpriseEntitled());
    ServerGroup testGroup = ServerGroupTestUtils.createManaged(user);
    s.getManagedGroups().add((ManagedServerGroup) testGroup);

    // Three, one for the server entitlement, one for the user permission to the
    // server, one as the testGroup.
    assertEquals(1, s.getManagedGroups().size());
    ActivationKey key = createTestActivationKey(user, s);
    assertNotNull(key);
    key = (ActivationKey) reload(key);
    assertNotNull(key.getId());
  }