public void testKeyGeneration() throws Exception {

    ActivationKey k = createTestActivationKey(user);
    String note = k.getNote();
    String key = k.getKey();

    TestUtils.saveAndFlush(k);

    ActivationKey k2 = ActivationKeyFactory.lookupByKey(key);
    assertEquals(key, k2.getKey());
    assertEquals(note, k2.getNote());

    ActivationKey k3 = ActivationKeyFactory.lookupByKey(TestUtils.randomString());
    assertNull(k3);

    // Make sure we got the entitlements correct
    Server server = k2.getServer();
    assertEquals(1, server.getEntitlements().size());
    assertEquals(1, k2.getEntitlements().size());

    Entitlement e = server.getEntitlements().iterator().next();
    ServerGroupType t2 = k2.getEntitlements().iterator().next();
    assertEquals(e.getLabel(), t2.getLabel());

    // test out ActivationKeyManager.findByServer while we're here...
    ActivationKey k4 =
        ActivationKeyManager.getInstance().findByServer(server, user).iterator().next();
    assertNotNull(k4);
    assertEquals(key, k4.getKey());

    try {
      k3 = ActivationKeyManager.getInstance().findByServer(null, user).iterator().next();
      String msg =
          "Permission check failed :(.."
              + " Activation key should not have existed"
              + " for a server of 'null' id. An exception "
              + "should have been raised for this.";
      fail(msg);
    } catch (Exception ie) {
      // great!.. Exception for passing in invalid keys always welcome
    }

    User user1 = UserTestUtils.findNewUser("testuser", "testorg");
    Server server2 = ServerFactoryTest.createTestServer(user1);
    try {
      k3 = ActivationKeyManager.getInstance().findByServer(server2, user1).iterator().next();
      String msg =
          "Permission check failed :(.."
              + " Activation key should not have existed"
              + " for a server of the associated id. An exception "
              + "should have been raised for this.";
      fail(msg);
    } catch (Exception ie) {
      // great!.. Exception for passing in invalid keys always welcome
    }
  }
  /**
   * 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 static ActivationKey createTestActivationKey(User u, Server s) {

    String note = "" + TestUtils.randomString() + " -- Java unit test activation key.";

    ActivationKey key = ActivationKeyManager.getInstance().createNewReActivationKey(u, s, note);
    ActivationKeyFactory.save(key);
    return key;
  }
 public void testBadKeys() throws Exception {
   ActivationKeyManager manager = ActivationKeyManager.getInstance();
   try {
     manager.createNewActivationKey(user, "A,B", "Cool", null, null, false);
     fail("Validator exception Not raised for an invalid name");
   } catch (ValidatorException ve) {
     // success . Name had invalid chars
   }
 }
 public void testDuplicateKeyCreation() throws Exception {
   String keyName = "Hey!";
   ActivationKeyManager.getInstance()
       .createNewActivationKey(user, keyName, null, null, null, false);
   try {
     ActivationKeyManager.getInstance()
         .createNewActivationKey(user, keyName, "Cool Duplicate", null, null, false);
     String msg = "Duplicate Key exception not raised..";
     fail(msg);
   } catch (ValidatorException e) {
     for (ValidatorError er : e.getResult().getErrors()) {
       if (er.getKey().equals("activation-key.java.exists")) {
         // sweet duplicate object exception
         return;
       }
     }
     throw e;
   }
 }
 public void testKeyTrimming() throws Exception {
   ActivationKeyManager manager = ActivationKeyManager.getInstance();
   String keyName = " Test Space  ";
   ActivationKey k =
       manager.createNewActivationKey(user, keyName, "Cool Duplicate", null, null, false);
   assertEquals(
       ActivationKey.makePrefix(user.getOrg()) + keyName.trim().replace(" ", ""), k.getKey());
   String newKey = keyName + " FOO  ";
   manager.changeKey(newKey, k, user);
   assertNotNull(ActivationKey.makePrefix(user.getOrg()) + newKey.trim());
 }
 public void setUp() throws Exception {
   super.setUp();
   manager = ActivationKeyManager.getInstance();
 }