@Test
  public void testGetActualUrl() {
    ProductVersion productVersion =
        productVersionDao.findByProductAndName(productDao.findByShortName("EAM"), "EAM-BC-3");
    User user = userDao.findByUsername(testUserName);
    Region region = regionDao.getReference(2L);
    final TrialDto trialDto =
        trialService.launchTrial(getRequestStub(), productVersion, user, region, Locale.US);
    String guid = trialDto.getGuid();
    String url = trialDto.getUrl();
    assertNotNull(guid);
    assertNotNull(url);
    final TrialInstance actual = trialInstanceDao.findById(trialDto.getId());
    assertEquals(guid, actual.getGuid());
    assertEquals(url, actual.getUrl());

    RedirectUrlDto redirectUrlDto = trialService.getActualUrl(guid);
    assertNotNull(redirectUrlDto);
    assertEquals(actual.getUrl(), redirectUrlDto.getRedirectUrl());
    List<UserTracking> data =
        userTrackingDao.findByTrackingTypeAndUser(TrackingType.PROXY_URL_HIT, user);
    assertNotNull(data);
    assertEquals(1, data.size());
    assertEquals(actual.getId(), data.get(0).getTargetObject());

    redirectUrlDto = trialService.getActualUrl(guid);
    assertEquals(actual.getUrl(), redirectUrlDto.getRedirectUrl());
    data = userTrackingDao.findByTrackingTypeAndUser(TrackingType.PROXY_URL_HIT, user);
    assertNotNull(data);
    assertEquals(2, data.size());
    assertEquals(actual.getId(), data.get(0).getTargetObject());
    assertEquals(actual.getId(), data.get(1).getTargetObject());
  }
  @Test
  public void testAddTrialEnvironment() throws Exception {
    loginTestUser();
    List<TrialEnvironment> environments = trialService.getEnvironments(1L);
    int enviroSize = environments.size();

    User user = new User();
    user.setUsername("*****@*****.**");
    user.setFirstName("Test");
    user.setLastName("Test");
    user.setCreatedAt(new Date());
    user.getRoles().add(Role.ROLE_EXTERNAL);
    user.setPassword(securityService.encodePassword("test", user.getCreatedAt()));
    userDao.save(user);

    Product product = new Product();
    product.setShortName("Test");
    product.setName("Test Long");

    productService.addProduct(product);

    ProductVersion productVersion = new ProductVersion();
    productVersion.setName("Test-version");
    productVersion.setCreatedAt(new Date());
    productVersion.setProduct(product);
    productVersion.setIeOnly(true);
    productVersionDao.save(productVersion);
    product.getProductVersions().add(productVersion);
    productDao.save(product);

    emService.flush();

    TrialDto trial = new TrialDto();
    trial.setProductId(product.getId());
    trial.setProductVersionId(productVersion.getId());
    trial.setUserId(user.getId());
    trial.setUrl("http://test/hahahaha");
    trial.setUsername("test");
    trial.setPassword("haha");
    trialService.createTrialEnvironment(trial);
    emService.flush();

    environments = trialService.getEnvironments(product.getId());
    assertEquals(enviroSize + 1, environments.size());
  }
  @Test
  public void testRequestTrialApprove() {
    final MockHttpServletRequest requestStub = super.getRequestStub();

    List<ProductVersion> productVersions = new ArrayList<ProductVersion>();

    productVersions.add(
        productVersionDao.findByProductAndName(productDao.findByShortName("EAM"), "EAM-BC-3"));
    productVersions.add(
        productVersionDao.findByProductAndName(productDao.findByShortName("XM"), "XM-BC-3"));
    User user = userDao.findByUsername(testUserName);
    Region region = regionDao.findById(2L);
    Locale locale = Locale.US;
    String comment = "Test comment.";

    final NullEmailProvider emailProvider = new NullEmailProvider();
    trialEmailComponent.setEmailProvider(emailProvider);

    TrialRequest trialRequest =
        trialService.requestTrial(requestStub, productVersions, user, region, locale, comment);
    List<NullEmailProvider.EmailInfo> asyncEmails = emailProvider.getAsyncEmails();
    assertEquals("Verify email count", 2, asyncEmails.size());
    NullEmailProvider.EmailInfo emailInfo = asyncEmails.get(0);
    assertEquals(
        messageProvider.getMessage(
            StringDefs.MESSAGE_TRIAL_REQUEST_SUBJECT,
            trialEmailComponent.productNamesStrungForEmail(productVersions)),
        emailInfo.subject);
    assertTrue(
        emailInfo.text.contains(
            "Products: " + trialEmailComponent.productNamesStrungForEmail(productVersions)));
    assertTrue(emailInfo.text.contains("User: "******"Comment: " + comment));
    assertTrue(emailInfo.text.contains("Region: " + region.getName()));
    assertEquals(StringDefs.BC_LEADS_EMAIL, emailInfo.address);
    String done = trialService.approveTrialRequest(requestStub, trialRequest.getRequestKey());
    assertEquals("Done", done);

    try {
      trialService.deleteTrialRequest(trialRequest.getRequestKey());
      fail("Delete should fail, as it has already been deleted.");
    } catch (DataAccessException dae) {
      // eat.
    }
  }
  @Test
  public void testRequestTrialSize() {

    final MockHttpServletRequest requestStub = super.getRequestStub();
    List<ProductVersion> productVersions = new ArrayList<ProductVersion>();
    productVersions.add(
        productVersionDao.findByProductAndName(productDao.findByShortName("EAM"), "EAM-BC-3"));
    User user = userDao.findByUsername(testUserName);
    Region region = regionDao.findById(1L);
    Locale locale = Locale.US;
    String comment = "Test comment.";

    final NullEmailProvider emailProvider = new NullEmailProvider();
    trialEmailComponent.setEmailProvider(emailProvider);

    TrialRequest trialRequest =
        trialService.requestTrial(requestStub, productVersions, user, region, locale, comment);

    List<NullEmailProvider.EmailInfo> asyncEmails = emailProvider.getAsyncEmails();
    assertEquals(2, asyncEmails.size());
    NullEmailProvider.EmailInfo emailInfo = asyncEmails.get(0);
    assertEquals(
        messageProvider.getMessage(
            StringDefs.MESSAGE_TRIAL_REQUEST_SUBJECT,
            trialEmailComponent.productNamesStrungForEmail(productVersions)),
        emailInfo.subject);
    assertTrue(
        emailInfo.text.contains(
            "Products: " + trialEmailComponent.productNamesStrungForEmail(productVersions)));
    assertTrue(emailInfo.text.contains("User: "******"Comment: " + comment));
    assertTrue(emailInfo.text.contains("Region: " + region.getName()));
    logger.debug("\n----- EMAIL BODY --- \n" + emailInfo.text + "\n---------------");

    loginAdminUser();

    assertEquals(1, trialService.getTrialRequests().size());
    trialService.deleteTrialRequest(trialRequest.getRequestKey());
    assertEquals(0, trialService.getTrialRequests().size());
  }