@Test
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    ResourceBlock newResourceBlock = addResourceBlock();

    _persistence.clearCache();

    ResourceBlockModelImpl existingResourceBlockModelImpl =
        (ResourceBlockModelImpl) _persistence.findByPrimaryKey(newResourceBlock.getPrimaryKey());

    Assert.assertEquals(
        existingResourceBlockModelImpl.getCompanyId(),
        existingResourceBlockModelImpl.getOriginalCompanyId());
    Assert.assertEquals(
        existingResourceBlockModelImpl.getGroupId(),
        existingResourceBlockModelImpl.getOriginalGroupId());
    Assert.assertTrue(
        Validator.equals(
            existingResourceBlockModelImpl.getName(),
            existingResourceBlockModelImpl.getOriginalName()));
    Assert.assertTrue(
        Validator.equals(
            existingResourceBlockModelImpl.getPermissionsHash(),
            existingResourceBlockModelImpl.getOriginalPermissionsHash()));
  }
  @Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

    ResourceBlock existingResourceBlock =
        _persistence.fetchByPrimaryKey(newResourceBlock.getPrimaryKey());

    Assert.assertEquals(existingResourceBlock, newResourceBlock);
  }
Exemple #3
0
  protected void convertResourcePermissions(String name, String tableName, String pkColumnName)
      throws Exception {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      con = DataAccess.getConnection();

      ps = con.prepareStatement("select " + pkColumnName + ", companyId from " + tableName);

      rs = ps.executeQuery();

      while (rs.next()) {
        long primKey = rs.getLong(pkColumnName);
        long companyId = rs.getLong("companyId");

        ResourceBlock resourceBlock = convertResourcePermissions(companyId, name, primKey);

        if (_log.isInfoEnabled() && ((resourceBlock.getResourceBlockId() % 100) == 0)) {

          _log.info("Processed 100 resource blocks for " + name);
        }
      }
    } finally {
      DataAccess.cleanUp(con, ps, rs);
    }

    List<ResourcePermission> resourcePermissions =
        ResourcePermissionLocalServiceUtil.getScopeResourcePermissions(_SCOPES);

    for (ResourcePermission resourcePermission : resourcePermissions) {
      int scope = resourcePermission.getScope();

      if (!name.equals(resourcePermission.getName())) {
        continue;
      }

      if ((scope == ResourceConstants.SCOPE_COMPANY)
          || (scope == ResourceConstants.SCOPE_GROUP_TEMPLATE)) {

        ResourceBlockLocalServiceUtil.setCompanyScopePermissions(
            resourcePermission.getCompanyId(),
            name,
            resourcePermission.getRoleId(),
            resourcePermission.getActionIds());
      } else if (scope == ResourceConstants.SCOPE_GROUP) {
        ResourceBlockLocalServiceUtil.setGroupScopePermissions(
            resourcePermission.getCompanyId(),
            GetterUtil.getLong(resourcePermission.getPrimaryKey()),
            name,
            resourcePermission.getRoleId(),
            resourcePermission.getActionIds());
      }
    }
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    ResourceBlock resourceBlock = _persistence.create(pk);

    Assert.assertNotNull(resourceBlock);

    Assert.assertEquals(resourceBlock.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

    _persistence.remove(newResourceBlock);

    ResourceBlock existingResourceBlock =
        _persistence.fetchByPrimaryKey(newResourceBlock.getPrimaryKey());

    Assert.assertNull(existingResourceBlock);
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newResourceBlock.getPrimaryKey());

    Map<Serializable, ResourceBlock> resourceBlocks = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, resourceBlocks.size());
    Assert.assertEquals(newResourceBlock, resourceBlocks.get(newResourceBlock.getPrimaryKey()));
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newResourceBlock.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, ResourceBlock> resourceBlocks = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, resourceBlocks.size());
    Assert.assertEquals(newResourceBlock, resourceBlocks.get(newResourceBlock.getPrimaryKey()));
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

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

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("resourceBlockId", newResourceBlock.getResourceBlockId()));

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

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

    ResourceBlock existingResourceBlock = result.get(0);

    Assert.assertEquals(existingResourceBlock, newResourceBlock);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    ResourceBlock newResourceBlock = addResourceBlock();

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

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

    Object newResourceBlockId = newResourceBlock.getResourceBlockId();

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

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

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

    Object existingResourceBlockId = result.get(0);

    Assert.assertEquals(existingResourceBlockId, newResourceBlockId);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    ResourceBlock newResourceBlock1 = addResourceBlock();
    ResourceBlock newResourceBlock2 = addResourceBlock();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newResourceBlock1.getPrimaryKey());
    primaryKeys.add(newResourceBlock2.getPrimaryKey());

    Map<Serializable, ResourceBlock> resourceBlocks = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, resourceBlocks.size());
    Assert.assertEquals(newResourceBlock1, resourceBlocks.get(newResourceBlock1.getPrimaryKey()));
    Assert.assertEquals(newResourceBlock2, resourceBlocks.get(newResourceBlock2.getPrimaryKey()));
  }
  protected ResourceBlock addResourceBlock() throws Exception {
    long pk = RandomTestUtil.nextLong();

    ResourceBlock resourceBlock = _persistence.create(pk);

    resourceBlock.setMvccVersion(RandomTestUtil.nextLong());

    resourceBlock.setCompanyId(RandomTestUtil.nextLong());

    resourceBlock.setGroupId(RandomTestUtil.nextLong());

    resourceBlock.setName(RandomTestUtil.randomString());

    resourceBlock.setPermissionsHash(RandomTestUtil.randomString());

    resourceBlock.setReferenceCount(RandomTestUtil.nextLong());

    _resourceBlocks.add(_persistence.update(resourceBlock));

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

    ResourceBlock newResourceBlock = _persistence.create(pk);

    newResourceBlock.setMvccVersion(RandomTestUtil.nextLong());

    newResourceBlock.setCompanyId(RandomTestUtil.nextLong());

    newResourceBlock.setGroupId(RandomTestUtil.nextLong());

    newResourceBlock.setName(RandomTestUtil.randomString());

    newResourceBlock.setPermissionsHash(RandomTestUtil.randomString());

    newResourceBlock.setReferenceCount(RandomTestUtil.nextLong());

    _resourceBlocks.add(_persistence.update(newResourceBlock));

    ResourceBlock existingResourceBlock =
        _persistence.findByPrimaryKey(newResourceBlock.getPrimaryKey());

    Assert.assertEquals(existingResourceBlock.getMvccVersion(), newResourceBlock.getMvccVersion());
    Assert.assertEquals(
        existingResourceBlock.getResourceBlockId(), newResourceBlock.getResourceBlockId());
    Assert.assertEquals(existingResourceBlock.getCompanyId(), newResourceBlock.getCompanyId());
    Assert.assertEquals(existingResourceBlock.getGroupId(), newResourceBlock.getGroupId());
    Assert.assertEquals(existingResourceBlock.getName(), newResourceBlock.getName());
    Assert.assertEquals(
        existingResourceBlock.getPermissionsHash(), newResourceBlock.getPermissionsHash());
    Assert.assertEquals(
        existingResourceBlock.getReferenceCount(), newResourceBlock.getReferenceCount());
  }