protected Address addAddress() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Address address = _persistence.create(pk);

    address.setMvccVersion(RandomTestUtil.nextLong());

    address.setUuid(RandomTestUtil.randomString());

    address.setCompanyId(RandomTestUtil.nextLong());

    address.setUserId(RandomTestUtil.nextLong());

    address.setUserName(RandomTestUtil.randomString());

    address.setCreateDate(RandomTestUtil.nextDate());

    address.setModifiedDate(RandomTestUtil.nextDate());

    address.setClassNameId(RandomTestUtil.nextLong());

    address.setClassPK(RandomTestUtil.nextLong());

    address.setStreet1(RandomTestUtil.randomString());

    address.setStreet2(RandomTestUtil.randomString());

    address.setStreet3(RandomTestUtil.randomString());

    address.setCity(RandomTestUtil.randomString());

    address.setZip(RandomTestUtil.randomString());

    address.setRegionId(RandomTestUtil.nextLong());

    address.setCountryId(RandomTestUtil.nextLong());

    address.setTypeId(RandomTestUtil.nextLong());

    address.setMailing(RandomTestUtil.randomBoolean());

    address.setPrimary(RandomTestUtil.randomBoolean());

    address.setLastPublishDate(RandomTestUtil.nextDate());

    _addresses.add(_persistence.update(address));

    return address;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Address newAddress = _persistence.create(pk);

    newAddress.setMvccVersion(RandomTestUtil.nextLong());

    newAddress.setUuid(RandomTestUtil.randomString());

    newAddress.setCompanyId(RandomTestUtil.nextLong());

    newAddress.setUserId(RandomTestUtil.nextLong());

    newAddress.setUserName(RandomTestUtil.randomString());

    newAddress.setCreateDate(RandomTestUtil.nextDate());

    newAddress.setModifiedDate(RandomTestUtil.nextDate());

    newAddress.setClassNameId(RandomTestUtil.nextLong());

    newAddress.setClassPK(RandomTestUtil.nextLong());

    newAddress.setStreet1(RandomTestUtil.randomString());

    newAddress.setStreet2(RandomTestUtil.randomString());

    newAddress.setStreet3(RandomTestUtil.randomString());

    newAddress.setCity(RandomTestUtil.randomString());

    newAddress.setZip(RandomTestUtil.randomString());

    newAddress.setRegionId(RandomTestUtil.nextLong());

    newAddress.setCountryId(RandomTestUtil.nextLong());

    newAddress.setTypeId(RandomTestUtil.nextLong());

    newAddress.setMailing(RandomTestUtil.randomBoolean());

    newAddress.setPrimary(RandomTestUtil.randomBoolean());

    newAddress.setLastPublishDate(RandomTestUtil.nextDate());

    _addresses.add(_persistence.update(newAddress));

    Address existingAddress = _persistence.findByPrimaryKey(newAddress.getPrimaryKey());

    Assert.assertEquals(existingAddress.getMvccVersion(), newAddress.getMvccVersion());
    Assert.assertEquals(existingAddress.getUuid(), newAddress.getUuid());
    Assert.assertEquals(existingAddress.getAddressId(), newAddress.getAddressId());
    Assert.assertEquals(existingAddress.getCompanyId(), newAddress.getCompanyId());
    Assert.assertEquals(existingAddress.getUserId(), newAddress.getUserId());
    Assert.assertEquals(existingAddress.getUserName(), newAddress.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getCreateDate()),
        Time.getShortTimestamp(newAddress.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getModifiedDate()),
        Time.getShortTimestamp(newAddress.getModifiedDate()));
    Assert.assertEquals(existingAddress.getClassNameId(), newAddress.getClassNameId());
    Assert.assertEquals(existingAddress.getClassPK(), newAddress.getClassPK());
    Assert.assertEquals(existingAddress.getStreet1(), newAddress.getStreet1());
    Assert.assertEquals(existingAddress.getStreet2(), newAddress.getStreet2());
    Assert.assertEquals(existingAddress.getStreet3(), newAddress.getStreet3());
    Assert.assertEquals(existingAddress.getCity(), newAddress.getCity());
    Assert.assertEquals(existingAddress.getZip(), newAddress.getZip());
    Assert.assertEquals(existingAddress.getRegionId(), newAddress.getRegionId());
    Assert.assertEquals(existingAddress.getCountryId(), newAddress.getCountryId());
    Assert.assertEquals(existingAddress.getTypeId(), newAddress.getTypeId());
    Assert.assertEquals(existingAddress.getMailing(), newAddress.getMailing());
    Assert.assertEquals(existingAddress.getPrimary(), newAddress.getPrimary());
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getLastPublishDate()),
        Time.getShortTimestamp(newAddress.getLastPublishDate()));
  }
  private void createAccount(WebForm form, HttpServletRequest request) throws Exception {

    User user =
        APILocator.getUserAPI()
            .loadByUserByEmail(form.getEmail(), APILocator.getUserAPI().getSystemUser(), false);
    User defaultUser = APILocator.getUserAPI().getDefaultUser();
    Date today = new Date();

    if (user.isNew() || (!user.isNew() && user.getLastLoginDate() == null)) {

      // ### CREATE USER ###
      Company company = PublicCompanyFactory.getDefaultCompany();
      user.setEmailAddress(form.getEmail().trim().toLowerCase());
      user.setFirstName(form.getFirstName() == null ? "" : form.getFirstName());
      user.setMiddleName(form.getMiddleName() == null ? "" : form.getMiddleName());
      user.setLastName(form.getLastName() == null ? "" : form.getLastName());
      user.setNickName("");
      user.setCompanyId(company.getCompanyId());
      user.setPasswordEncrypted(true);
      user.setGreeting("Welcome, " + user.getFullName() + "!");

      // Set defaults values
      if (user.isNew()) {
        // if it's a new user we set random password
        String pass = PublicEncryptionFactory.getRandomPassword();
        user.setPassword(PublicEncryptionFactory.digestString(pass));
        user.setLanguageId(defaultUser.getLanguageId());
        user.setTimeZoneId(defaultUser.getTimeZoneId());
        user.setSkinId(defaultUser.getSkinId());
        user.setDottedSkins(defaultUser.isDottedSkins());
        user.setRoundedSkins(defaultUser.isRoundedSkins());
        user.setResolution(defaultUser.getResolution());
        user.setRefreshRate(defaultUser.getRefreshRate());
        user.setLayoutIds("");
        user.setActive(true);
        user.setCreateDate(today);
      }
      APILocator.getUserAPI().save(user, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CREATE USER ###

      // ### CREATE USER_PROXY ###
      UserProxy userProxy =
          com.dotmarketing.business.APILocator.getUserProxyAPI()
              .getUserProxy(user.getUserId(), APILocator.getUserAPI().getSystemUser(), false);
      userProxy.setPrefix("");
      userProxy.setTitle(form.getTitle());
      userProxy.setOrganization(form.getOrganization());
      userProxy.setUserId(user.getUserId());
      com.dotmarketing.business.APILocator.getUserProxyAPI()
          .saveUserProxy(userProxy, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CRETE USER_PROXY ###

      // saving user inode on web form
      form.setUserInode(userProxy.getInode());
      if (UtilMethods.isSet(form.getFormType())) {
        HibernateUtil.saveOrUpdate(form);
      }

      ///// WE CAN DO THIS! BUT WE NEED TO ADD CATEGORIES TO WEBFORM AND ALSO CHANGE THE PROCESSES
      // THAT
      //// CREATE THE EXCEL DOWNLOAD FROM WEB FORMS. I DIDN'T ADD IT SO I COMMENTED THIS CODE FOR
      // NOW
      // get the old categories, wipe them out
      /*
      List<Category> categories = InodeFactory.getParentsOfClass(userProxy, Category.class);
      for (int i = 0; i < categories.size(); i++) {
      	categories.get(i).deleteChild(userProxy);
      }
       */
      // Save the new categories
      /*String[] arr = form.getCategories();
      if (arr != null) {
      	for (int i = 0; i < arr.length; i++) {
      		Category node = (Category) InodeFactory.getInode(arr[i], Category.class);
      		node.addChild(userProxy);
      	}
      }*/

      // ### CREATE ADDRESS ###
      try {
        List<Address> addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
        Address address =
            (addresses.size() > 0 ? addresses.get(0) : PublicAddressFactory.getInstance());
        address.setStreet1(form.getAddress1() == null ? "" : form.getAddress1());
        address.setStreet2(form.getAddress2() == null ? "" : form.getAddress2());
        address.setCity(form.getCity() == null ? "" : form.getCity());
        address.setState(form.getState() == null ? "" : form.getState());
        address.setZip(form.getZip() == null ? "" : form.getZip());
        String phone = form.getPhone();
        address.setPhone(phone == null ? "" : phone);
        address.setUserId(user.getUserId());
        address.setCompanyId(company.getCompanyId());
        PublicAddressFactory.save(address);
      } catch (Exception ex) {
        Logger.error(this, ex.getMessage(), ex);
      }

      Role defaultRole =
          com.dotmarketing.business.APILocator.getRoleAPI()
              .loadRoleByKey(Config.getStringProperty("CMS_VIEWER_ROLE"));
      String roleId = defaultRole.getId();
      if (InodeUtils.isSet(roleId)) {
        com.dotmarketing.business.APILocator.getRoleAPI().addRoleToUser(roleId, user);
      }
    }
    // ### END CREATE ADDRESS ###

    // ### BUILD THE USER COMMENT ###
    addUserComments(user.getUserId(), form, request);
    // ### END BUILD THE USER COMMENT ###

    /* associate user with their clickstream request */
    if (Config.getBooleanProperty("ENABLE_CLICKSTREAM_TRACKING", false)) {
      ClickstreamFactory.setClickStreamUser(user.getUserId(), request);
    }
  }