public static PasswordPolicy addPasswordPolicyRel(
      Organization organization, ServiceContext serviceContext) throws Exception {

    PasswordPolicy passwordPolicy = PasswordPolicyTestUtil.addPasswordPolicy(serviceContext);

    PasswordPolicyRelLocalServiceUtil.addPasswordPolicyRel(
        passwordPolicy.getPasswordPolicyId(),
        organization.getModelClassName(),
        organization.getOrganizationId());

    return passwordPolicy;
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    PasswordPolicy newPasswordPolicy = addPasswordPolicy();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            PasswordPolicy.class, PasswordPolicy.class.getClassLoader());

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("passwordPolicyId", newPasswordPolicy.getPasswordPolicyId()));

    List<PasswordPolicy> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    PasswordPolicy existingPasswordPolicy = result.get(0);

    Assert.assertEquals(existingPasswordPolicy, newPasswordPolicy);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    PasswordPolicy newPasswordPolicy = addPasswordPolicy();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            PasswordPolicy.class, PasswordPolicy.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("passwordPolicyId"));

    Object newPasswordPolicyId = newPasswordPolicy.getPasswordPolicyId();

    dynamicQuery.add(
        RestrictionsFactoryUtil.in("passwordPolicyId", new Object[] {newPasswordPolicyId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingPasswordPolicyId = result.get(0);

    Assert.assertEquals(existingPasswordPolicyId, newPasswordPolicyId);
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, PasswordPolicy passwordPolicy) throws Exception {

    long userId = portletDataContext.getUserId(passwordPolicy.getUserUuid());

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(
            passwordPolicy, PasswordPolicyPortletDataHandler.NAMESPACE);

    PasswordPolicy existingPasswordPolicy =
        PasswordPolicyLocalServiceUtil.fetchPasswordPolicyByUuidAndCompanyId(
            passwordPolicy.getUuid(), portletDataContext.getCompanyId());

    if (existingPasswordPolicy == null) {
      existingPasswordPolicy =
          PasswordPolicyLocalServiceUtil.fetchPasswordPolicy(
              portletDataContext.getCompanyId(), passwordPolicy.getName());
    }

    PasswordPolicy importedPasswordPolicy = null;

    if (existingPasswordPolicy == null) {
      serviceContext.setUuid(passwordPolicy.getUuid());

      importedPasswordPolicy =
          PasswordPolicyLocalServiceUtil.addPasswordPolicy(
              userId,
              passwordPolicy.isDefaultPolicy(),
              passwordPolicy.getName(),
              passwordPolicy.getDescription(),
              passwordPolicy.getChangeable(),
              passwordPolicy.isChangeRequired(),
              passwordPolicy.getMinAge(),
              passwordPolicy.getCheckSyntax(),
              passwordPolicy.isAllowDictionaryWords(),
              passwordPolicy.getMinAlphanumeric(),
              passwordPolicy.getMinLength(),
              passwordPolicy.getMinLowerCase(),
              passwordPolicy.getMinNumbers(),
              passwordPolicy.getMinSymbols(),
              passwordPolicy.getMinUpperCase(),
              passwordPolicy.getRegex(),
              passwordPolicy.isHistory(),
              passwordPolicy.getHistoryCount(),
              passwordPolicy.isExpireable(),
              passwordPolicy.getMaxAge(),
              passwordPolicy.getWarningTime(),
              passwordPolicy.getGraceLimit(),
              passwordPolicy.isLockout(),
              passwordPolicy.getMaxFailure(),
              passwordPolicy.getLockoutDuration(),
              passwordPolicy.getResetFailureCount(),
              passwordPolicy.getResetTicketMaxAge(),
              serviceContext);
    } else {
      importedPasswordPolicy =
          PasswordPolicyLocalServiceUtil.updatePasswordPolicy(
              existingPasswordPolicy.getPasswordPolicyId(),
              passwordPolicy.getName(),
              passwordPolicy.getDescription(),
              passwordPolicy.isChangeable(),
              passwordPolicy.isChangeRequired(),
              passwordPolicy.getMinAge(),
              passwordPolicy.isCheckSyntax(),
              passwordPolicy.isAllowDictionaryWords(),
              passwordPolicy.getMinAlphanumeric(),
              passwordPolicy.getMinLength(),
              passwordPolicy.getMinLowerCase(),
              passwordPolicy.getMinNumbers(),
              passwordPolicy.getMinSymbols(),
              passwordPolicy.getMinUpperCase(),
              passwordPolicy.getRegex(),
              passwordPolicy.isHistory(),
              passwordPolicy.getHistoryCount(),
              passwordPolicy.isExpireable(),
              passwordPolicy.getMaxAge(),
              passwordPolicy.getWarningTime(),
              passwordPolicy.getGraceLimit(),
              passwordPolicy.isLockout(),
              passwordPolicy.getMaxFailure(),
              passwordPolicy.getLockoutDuration(),
              passwordPolicy.getResetFailureCount(),
              passwordPolicy.getResetTicketMaxAge(),
              serviceContext);
    }

    portletDataContext.importClassedModel(
        passwordPolicy, importedPasswordPolicy, PasswordPolicyPortletDataHandler.NAMESPACE);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    PasswordPolicy newPasswordPolicy = _persistence.create(pk);

    newPasswordPolicy.setCompanyId(ServiceTestUtil.nextLong());

    newPasswordPolicy.setUserId(ServiceTestUtil.nextLong());

    newPasswordPolicy.setUserName(ServiceTestUtil.randomString());

    newPasswordPolicy.setCreateDate(ServiceTestUtil.nextDate());

    newPasswordPolicy.setModifiedDate(ServiceTestUtil.nextDate());

    newPasswordPolicy.setDefaultPolicy(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setName(ServiceTestUtil.randomString());

    newPasswordPolicy.setDescription(ServiceTestUtil.randomString());

    newPasswordPolicy.setChangeable(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setChangeRequired(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setMinAge(ServiceTestUtil.nextLong());

    newPasswordPolicy.setCheckSyntax(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setAllowDictionaryWords(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setMinAlphanumeric(ServiceTestUtil.nextInt());

    newPasswordPolicy.setMinLength(ServiceTestUtil.nextInt());

    newPasswordPolicy.setMinLowerCase(ServiceTestUtil.nextInt());

    newPasswordPolicy.setMinNumbers(ServiceTestUtil.nextInt());

    newPasswordPolicy.setMinSymbols(ServiceTestUtil.nextInt());

    newPasswordPolicy.setMinUpperCase(ServiceTestUtil.nextInt());

    newPasswordPolicy.setHistory(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setHistoryCount(ServiceTestUtil.nextInt());

    newPasswordPolicy.setExpireable(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setMaxAge(ServiceTestUtil.nextLong());

    newPasswordPolicy.setWarningTime(ServiceTestUtil.nextLong());

    newPasswordPolicy.setGraceLimit(ServiceTestUtil.nextInt());

    newPasswordPolicy.setLockout(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setMaxFailure(ServiceTestUtil.nextInt());

    newPasswordPolicy.setLockoutDuration(ServiceTestUtil.nextLong());

    newPasswordPolicy.setRequireUnlock(ServiceTestUtil.randomBoolean());

    newPasswordPolicy.setResetFailureCount(ServiceTestUtil.nextLong());

    newPasswordPolicy.setResetTicketMaxAge(ServiceTestUtil.nextLong());

    _persistence.update(newPasswordPolicy, false);

    PasswordPolicy existingPasswordPolicy =
        _persistence.findByPrimaryKey(newPasswordPolicy.getPrimaryKey());

    Assert.assertEquals(
        existingPasswordPolicy.getPasswordPolicyId(), newPasswordPolicy.getPasswordPolicyId());
    Assert.assertEquals(existingPasswordPolicy.getCompanyId(), newPasswordPolicy.getCompanyId());
    Assert.assertEquals(existingPasswordPolicy.getUserId(), newPasswordPolicy.getUserId());
    Assert.assertEquals(existingPasswordPolicy.getUserName(), newPasswordPolicy.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingPasswordPolicy.getCreateDate()),
        Time.getShortTimestamp(newPasswordPolicy.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingPasswordPolicy.getModifiedDate()),
        Time.getShortTimestamp(newPasswordPolicy.getModifiedDate()));
    Assert.assertEquals(
        existingPasswordPolicy.getDefaultPolicy(), newPasswordPolicy.getDefaultPolicy());
    Assert.assertEquals(existingPasswordPolicy.getName(), newPasswordPolicy.getName());
    Assert.assertEquals(
        existingPasswordPolicy.getDescription(), newPasswordPolicy.getDescription());
    Assert.assertEquals(existingPasswordPolicy.getChangeable(), newPasswordPolicy.getChangeable());
    Assert.assertEquals(
        existingPasswordPolicy.getChangeRequired(), newPasswordPolicy.getChangeRequired());
    Assert.assertEquals(existingPasswordPolicy.getMinAge(), newPasswordPolicy.getMinAge());
    Assert.assertEquals(
        existingPasswordPolicy.getCheckSyntax(), newPasswordPolicy.getCheckSyntax());
    Assert.assertEquals(
        existingPasswordPolicy.getAllowDictionaryWords(),
        newPasswordPolicy.getAllowDictionaryWords());
    Assert.assertEquals(
        existingPasswordPolicy.getMinAlphanumeric(), newPasswordPolicy.getMinAlphanumeric());
    Assert.assertEquals(existingPasswordPolicy.getMinLength(), newPasswordPolicy.getMinLength());
    Assert.assertEquals(
        existingPasswordPolicy.getMinLowerCase(), newPasswordPolicy.getMinLowerCase());
    Assert.assertEquals(existingPasswordPolicy.getMinNumbers(), newPasswordPolicy.getMinNumbers());
    Assert.assertEquals(existingPasswordPolicy.getMinSymbols(), newPasswordPolicy.getMinSymbols());
    Assert.assertEquals(
        existingPasswordPolicy.getMinUpperCase(), newPasswordPolicy.getMinUpperCase());
    Assert.assertEquals(existingPasswordPolicy.getHistory(), newPasswordPolicy.getHistory());
    Assert.assertEquals(
        existingPasswordPolicy.getHistoryCount(), newPasswordPolicy.getHistoryCount());
    Assert.assertEquals(existingPasswordPolicy.getExpireable(), newPasswordPolicy.getExpireable());
    Assert.assertEquals(existingPasswordPolicy.getMaxAge(), newPasswordPolicy.getMaxAge());
    Assert.assertEquals(
        existingPasswordPolicy.getWarningTime(), newPasswordPolicy.getWarningTime());
    Assert.assertEquals(existingPasswordPolicy.getGraceLimit(), newPasswordPolicy.getGraceLimit());
    Assert.assertEquals(existingPasswordPolicy.getLockout(), newPasswordPolicy.getLockout());
    Assert.assertEquals(existingPasswordPolicy.getMaxFailure(), newPasswordPolicy.getMaxFailure());
    Assert.assertEquals(
        existingPasswordPolicy.getLockoutDuration(), newPasswordPolicy.getLockoutDuration());
    Assert.assertEquals(
        existingPasswordPolicy.getRequireUnlock(), newPasswordPolicy.getRequireUnlock());
    Assert.assertEquals(
        existingPasswordPolicy.getResetFailureCount(), newPasswordPolicy.getResetFailureCount());
    Assert.assertEquals(
        existingPasswordPolicy.getResetTicketMaxAge(), newPasswordPolicy.getResetTicketMaxAge());
  }