@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 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.
    }
  }
  private Product getTrialExtensionProduct(TrialInstanceUpdateDto trialInstanceUpdateDto) {
    Product product = null;
    if (isNullOrEmpty(trialInstanceUpdateDto.getProductShortName())) {
      if (trialInstanceUpdateDto.getProductId() != null) {
        if (productDao.exists(trialInstanceUpdateDto.getProductId())) {
          product = productDao.getReference(trialInstanceUpdateDto.getProductId());
        }
      }
    } else {
      product = productDao.findByShortName(trialInstanceUpdateDto.getProductShortName());
    }

    return product;
  }
  @Test
  public void testTrialProductChild() throws Exception {
    assertEquals(3, trialProductChildDao.count());
    ProductVersion productVersion =
        productVersionDao.findByProductAndName(productDao.findByShortName("EAM"), "EAM-BC-3");
    TrialEnvironment environment =
        trialEnvironmentDao
            .findByProductVersionAndRegionAndAvailable(
                productVersion, regionDao.findByShortName("NA"), true)
            .get(0);
    for (TrialProductChild tpc :
        trialProductChildDao.findByRegion(regionDao.findByShortName("NA"))) {

      logger.error("tpc.id:" + tpc.getId());
      logger.error("tpc.getChildVersion().getName():" + tpc.getChildVersion().getName());
      logger.error("tpc.getParentVersion().getName():" + tpc.getParentVersion().getName());
    }
    assertEquals(
        "3 records for region:NA",
        3,
        trialProductChildDao.findByRegion(regionDao.findByShortName("NA")).size());
    assertEquals(
        "3 records with WS as parent",
        3,
        trialProductChildDao
            .findByRegionAndParentVersion(
                regionDao.findByShortName("NA"), productVersionDao.findByName("WS-BC-3"))
            .size());
    assertEquals(
        "1 record with EAM as child",
        1,
        trialProductChildDao
            .findByRegionAndChildVersion(regionDao.findByShortName("NA"), productVersion)
            .size());
    assertEquals(
        "2 records should come back for EAM delete",
        2,
        trialObjectsCreatorComponent
            .deleteEnvironmentAndRelations(
                productVersion, regionDao.findByShortName("NA"), environment)
            .size());
  }
  @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());
  }