From 2b7766dd6ecf6fc011c917a3e99cf538f8cfd6eb Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Thu, 20 Sep 2018 10:00:37 +0300 Subject: Test coverage-Corrections Increase test coverage Change-Id: I8268d31486b3410fbe8791c6d8c4eed4b237462c Issue-ID: SDC-1673 Signed-off-by: siddharth0905 Signed-off-by: vempo --- .../dao/impl/ItemPermissionsDaoFactoryImpl.java | 11 +- .../dao/impl/ItemPermissionsDaoImpl.java | 116 +++++------ .../dao/impl/PermissionsServicesImpl.java | 109 +++++----- .../dao/impl/UserPermissionsDaoFactoryImpl.java | 2 +- .../dao/impl/UserPermissionsDaoImpl.java | 19 +- .../itempermissions/servlet/PermissionsFilter.java | 6 +- .../dao/impl/PermissionsServicesImplTest.java | 224 +++++++++++++-------- .../servlet/PermissionsFilterTest.java | 90 +++++++++ 8 files changed, 361 insertions(+), 216 deletions(-) create mode 100644 openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilterTest.java (limited to 'openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src') diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java index 87c0338c25..d2430a6166 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java @@ -1,6 +1,5 @@ package org.openecomp.sdc.itempermissions.dao.impl; - import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDaoFactory; @@ -10,10 +9,10 @@ import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDaoFactory; */ public class ItemPermissionsDaoFactoryImpl extends ItemPermissionsDaoFactory { - private static ItemPermissionsDao INSTANCE =new ItemPermissionsDaoImpl(); + private static ItemPermissionsDao INSTANCE = new ItemPermissionsDaoImpl(); - @Override - public ItemPermissionsDao createInterface() { - return INSTANCE; - } + @Override + public ItemPermissionsDao createInterface() { + return INSTANCE; + } } diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java index 6421c283f3..c9c4198081 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java @@ -13,75 +13,79 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openecomp.sdc.itempermissions.dao.impl; import com.datastax.driver.core.ResultSet; import com.datastax.driver.mapping.Result; import com.datastax.driver.mapping.annotations.Accessor; import com.datastax.driver.mapping.annotations.Query; -import org.openecomp.core.nosqldb.api.NoSqlDb; -import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; -import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; -import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; import java.util.Collection; import java.util.Optional; import java.util.Set; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; + /** * Created by ayalaben on 6/20/2017. */ public class ItemPermissionsDaoImpl implements ItemPermissionsDao { - private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); - private static ItemPermissionsAccessor accessor = - noSqlDb.getMappingManager().createAccessor(ItemPermissionsAccessor.class); - - @Override - public Collection listItemPermissions(String itemId) { - return accessor.getItemPermissions(itemId).all(); - } - - @Override - public void updateItemPermissions(String itemId, String permission, Set addedUsersIds, - Set removedUsersIds) { - addedUsersIds.forEach(userId -> accessor.addPermission(itemId, userId, permission)); - removedUsersIds.stream() - .filter(userId -> getUserItemPermission(itemId, userId) - .map(userPermissionOnItem -> userPermissionOnItem.equals(permission)) - .orElse(false)) - .forEach(userId -> accessor.deletePermission(itemId, userId)); - } - - @Override - public Optional getUserItemPermission(String itemId, String userId) { - ResultSet result = accessor.getUserItemPermission(itemId, userId); - return result.getAvailableWithoutFetching() < 1 - ? Optional.empty() - : Optional.of(result.one().getString(0)); - } - - @Override - public void deleteItemPermissions(String itemId) { - accessor.deleteItemPermissions(itemId); - } - - - @Accessor - interface ItemPermissionsAccessor { - @Query("select * from dox.item_permissions WHERE item_id = ?") - Result getItemPermissions(String itemId); - - @Query("select permission from dox.item_permissions WHERE item_id = ? AND user_id=?") - ResultSet getUserItemPermission(String itemId, String userId); - - @Query("delete from dox.item_permissions where item_id = ? and user_id = ?") - void deletePermission(String itemId, String userId); - - @Query("insert into dox.item_permissions (item_id,user_id,permission) values (?,?,?)") - void addPermission(String itemId, String userId, String permission); - - @Query("delete from dox.item_permissions where item_id=?") - void deleteItemPermissions(String itemId); - - } + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static ItemPermissionsAccessor accessor = + noSqlDb.getMappingManager().createAccessor(ItemPermissionsAccessor.class); + + @Override + public Collection listItemPermissions(String itemId) { + return accessor.getItemPermissions(itemId).all(); + } + + @Override + public void updateItemPermissions(String itemId, String permission, Set addedUsersIds, + Set removedUsersIds) { + addedUsersIds.forEach(userId -> accessor.addPermission(itemId, userId, permission)); + removedUsersIds.stream() + .filter(userId -> getUserItemPermission(itemId, userId) + .map(userPermissionOnItem -> userPermissionOnItem.equals(permission)) + .orElse(false)) + .forEach(userId -> accessor.deletePermission(itemId, userId)); + } + + @Override + public Optional getUserItemPermission(String itemId, String userId) { + ResultSet result = accessor.getUserItemPermission(itemId, userId); + return result.getAvailableWithoutFetching() < 1 + ? Optional.empty() + : Optional.of(result.one().getString(0)); + } + + @Override + public void deleteItemPermissions(String itemId) { + accessor.deleteItemPermissions(itemId); + } + + + @Accessor + interface ItemPermissionsAccessor { + + @Query("select * from dox.item_permissions WHERE item_id = ?") + Result getItemPermissions(String itemId); + + @Query("select permission from dox.item_permissions WHERE item_id = ? AND user_id=?") + ResultSet getUserItemPermission(String itemId, String userId); + + @Query("delete from dox.item_permissions where item_id = ? and user_id = ?") + void deletePermission(String itemId, String userId); + + @Query("insert into dox.item_permissions (item_id,user_id,permission) values (?,?,?)") + void addPermission(String itemId, String userId, String permission); + + @Query("delete from dox.item_permissions where item_id=?") + void deleteItemPermissions(String itemId); + + } } diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java index ec914b1530..d0e41d8c91 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java @@ -13,8 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openecomp.sdc.itempermissions.dao.impl; +import java.util.Collection; +import java.util.Optional; +import java.util.Set; + import org.openecomp.sdc.common.session.SessionContextProviderFactory; import org.openecomp.sdc.itempermissions.PermissionsRules; import org.openecomp.sdc.itempermissions.PermissionsServices; @@ -22,83 +27,79 @@ import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; import org.openecomp.sdc.itempermissions.dao.UserPermissionsDao; import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; -import java.util.Collection; -import java.util.Optional; -import java.util.Set; - /** * Created by ayalaben on 6/22/2017. */ public class PermissionsServicesImpl implements PermissionsServices { - private ItemPermissionsDao itemPermissionsDao; + private ItemPermissionsDao itemPermissionsDao; - private UserPermissionsDao userPermissionsDao; + private UserPermissionsDao userPermissionsDao; - private PermissionsRules permissionsRules; + private PermissionsRules permissionsRules; - private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; + private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; - public PermissionsServicesImpl(PermissionsRules permissionsRules, - ItemPermissionsDao itemPermissionsDao, - UserPermissionsDao userPermissionsDao) { - this.itemPermissionsDao = itemPermissionsDao; - this.permissionsRules = permissionsRules; - this.userPermissionsDao = userPermissionsDao; - } + public PermissionsServicesImpl(PermissionsRules permissionsRules, + ItemPermissionsDao itemPermissionsDao, + UserPermissionsDao userPermissionsDao) { + this.itemPermissionsDao = itemPermissionsDao; + this.permissionsRules = permissionsRules; + this.userPermissionsDao = userPermissionsDao; + } - @Override - public Collection listItemPermissions(String itemId) { - return itemPermissionsDao.listItemPermissions(itemId); - } + @Override + public Collection listItemPermissions(String itemId) { + return itemPermissionsDao.listItemPermissions(itemId); + } - @Override - public Set listUserPermittedItems(String userId, String permission) { - return userPermissionsDao.listUserPermittedItems(userId, permission); - } + @Override + public Set listUserPermittedItems(String userId, String permission) { + return userPermissionsDao.listUserPermittedItems(userId, permission); + } - @Override - public void updateItemPermissions(String itemId, String permission, Set addedUsersIds, - Set removedUsersIds) { + @Override + public void updateItemPermissions(String itemId, String permission, Set addedUsersIds, + Set removedUsersIds) { - String currentUserId = SessionContextProviderFactory.getInstance() - .createInterface().get().getUser().getUserId(); + String currentUserId = SessionContextProviderFactory.getInstance() + .createInterface().get().getUser().getUserId(); - permissionsRules.executeAction(itemId, currentUserId, CHANGE_PERMISSIONS); + permissionsRules.executeAction(itemId, currentUserId, CHANGE_PERMISSIONS); - permissionsRules.updatePermission(itemId, currentUserId, permission, addedUsersIds, - removedUsersIds); + permissionsRules.updatePermission(itemId, currentUserId, permission, addedUsersIds, + removedUsersIds); - itemPermissionsDao.updateItemPermissions(itemId, permission, - addedUsersIds, removedUsersIds); + itemPermissionsDao.updateItemPermissions(itemId, permission, + addedUsersIds, removedUsersIds); - userPermissionsDao.updatePermissions(itemId, permission, - addedUsersIds, removedUsersIds); + userPermissionsDao.updatePermissions(itemId, permission, + addedUsersIds, removedUsersIds); - } + } - @Override - public boolean isAllowed(String itemId, String userId, String action) { - return itemPermissionsDao.getUserItemPermission(itemId, userId) - .map(permission -> permissionsRules.isAllowed(permission, action)) - .orElse(false); - } + @Override + public boolean isAllowed(String itemId, String userId, String action) { + return itemPermissionsDao.getUserItemPermission(itemId, userId) + .map(permission -> permissionsRules.isAllowed(permission, action)) + .orElse(false); + } - @Override - public void execute(String itemId, String userId, String action) { - permissionsRules.executeAction(itemId, userId, action); - } + @Override + public void execute(String itemId, String userId, String action) { + permissionsRules.executeAction(itemId, userId, action); + } - @Override - public Optional getUserItemPermission(String itemId, String userId) { - return itemPermissionsDao.getUserItemPermission(itemId, userId); - } + @Override + public Optional getUserItemPermission(String itemId, String userId) { + return itemPermissionsDao.getUserItemPermission(itemId, userId); + } - @Override - public void deleteItemPermissions(String itemId) { - itemPermissionsDao.deleteItemPermissions(itemId); - } + @Override + public void deleteItemPermissions(String itemId) { + itemPermissionsDao.deleteItemPermissions(itemId); + } } diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoFactoryImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoFactoryImpl.java index 86a64ddf1d..4c23743ea5 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoFactoryImpl.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoFactoryImpl.java @@ -21,7 +21,7 @@ import org.openecomp.sdc.itempermissions.dao.UserPermissionsDaoFactory; public class UserPermissionsDaoFactoryImpl extends UserPermissionsDaoFactory { - private static UserPermissionsDao INSTANCE =new UserPermissionsDaoImpl(); + private static UserPermissionsDao INSTANCE = new UserPermissionsDaoImpl(); @Override public UserPermissionsDao createInterface() { diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoImpl.java index dad8f25535..8609f2f4a8 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoImpl.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/UserPermissionsDaoImpl.java @@ -19,14 +19,15 @@ package org.openecomp.sdc.itempermissions.dao.impl; import com.datastax.driver.core.ResultSet; import com.datastax.driver.mapping.annotations.Accessor; import com.datastax.driver.mapping.annotations.Query; -import org.openecomp.core.nosqldb.api.NoSqlDb; -import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; -import org.openecomp.sdc.itempermissions.dao.UserPermissionsDao; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.itempermissions.dao.UserPermissionsDao; + public class UserPermissionsDaoImpl implements UserPermissionsDao { private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); @@ -36,23 +37,23 @@ public class UserPermissionsDaoImpl implements UserPermissionsDao { @Override public Set listUserPermittedItems(String userId, String permission) { - ResultSet resultSet = accessor.getUserPermissionItems(userId,permission); - if(resultSet.isExhausted()){ + ResultSet resultSet = accessor.getUserPermissionItems(userId, permission); + if (resultSet.isExhausted()) { return new HashSet<>(); } else { - return resultSet.one().getSet(0,String.class); + return resultSet.one().getSet(0, String.class); } } @Override public void updatePermissions(String itemId, String permission, Set addedUsersIds, - Set removedUsersIds) { + Set removedUsersIds) { Set itemSet = Collections.singleton(itemId); addedUsersIds.forEach(userId -> - accessor.addItem(itemSet, userId,permission)); + accessor.addItem(itemSet, userId, permission)); removedUsersIds.forEach(userId -> - accessor.removeItem(itemSet,userId,permission)); + accessor.removeItem(itemSet, userId, permission)); } @Accessor diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java index 30472d6e80..9fe045f25d 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java @@ -48,7 +48,11 @@ public class PermissionsFilter implements Filter { private static final String EDIT_ITEM = "Edit_Item"; public PermissionsFilter() { - this.permissionsServices = PermissionsServicesFactory.getInstance().createInterface(); + this(PermissionsServicesFactory.getInstance().createInterface()); + } + + PermissionsFilter(PermissionsServices permissionsServices) { + this.permissionsServices = permissionsServices; } @Override diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java index 9574d4175a..0e27dcae4c 100644 --- a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java @@ -1,8 +1,21 @@ package org.openecomp.sdc.itempermissions.dao.impl; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.openecomp.sdc.common.session.SessionContextProviderFactory; @@ -14,99 +27,132 @@ import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.*; - /** * Created by ayalaben on 7/9/2017 */ public class PermissionsServicesImplTest { - private static final String ITEM1_ID = "1"; - private static final String PERMISSION = "Owner"; - private static final String USER2_ID = "testUser2"; - private static final String USER1_ID = "testUser1"; - private static final String ACTION_SUBMIT = "Submit_Item"; - private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; - - static { - SessionContextProviderFactory.getInstance().createInterface().create("testUser1", "dox"); - } - - @Mock - private ItemPermissionsDao permissionsDaoMock; - @Mock - private UserPermissionsDao userPermissionsDao; // do not delete. needed for permissionService - @Mock - private PermissionsRules permissionsRules; - @InjectMocks - @Spy - private PermissionsServicesImpl permissionsServices; - - - @BeforeMethod - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testListItemPermissionsWhenNone() { - Collection permissions = - permissionsServices.listItemPermissions(ITEM1_ID); - Assert.assertEquals(permissions.size(), 0); - } - - - @Test - public void testListItemPermissions(){ - doReturn(Arrays.asList( - createPermissionEntity(ITEM1_ID, USER1_ID, PERMISSION), - createPermissionEntity(ITEM1_ID, USER2_ID, PERMISSION))) - .when(permissionsDaoMock).listItemPermissions(anyObject()); - - Collection actual = - permissionsServices.listItemPermissions(ITEM1_ID); - Assert.assertEquals(actual.size(), 2); - } - - - @Test - public void testIsAllowed(){ - when(permissionsDaoMock.getUserItemPermission(ITEM1_ID,USER1_ID)).thenReturn(Optional.of(PERMISSION)); - when(permissionsRules.isAllowed(PERMISSION,ACTION_SUBMIT)).thenReturn(true); - - Boolean result = permissionsServices.isAllowed(ITEM1_ID,USER1_ID,ACTION_SUBMIT); - - Assert.assertTrue(result); - - } - @Test - public void testUpdatePermissions(){ - - Set addedUsers = new HashSet<>(); - addedUsers.add(USER2_ID); - - permissionsServices.updateItemPermissions(ITEM1_ID,PERMISSION,addedUsers, new HashSet<>()); - - verify(permissionsRules).executeAction(ITEM1_ID,USER1_ID,CHANGE_PERMISSIONS); - verify(permissionsRules).updatePermission(ITEM1_ID,USER1_ID,PERMISSION,addedUsers, new HashSet<>()); - verify(permissionsDaoMock).updateItemPermissions(ITEM1_ID,PERMISSION,addedUsers,new - HashSet<>()); - } - - - public static ItemPermissionsEntity createPermissionEntity(String itemId, String - userId, String permission) { - ItemPermissionsEntity permissionsEntity = new ItemPermissionsEntity(); - permissionsEntity.setItemId(itemId); - permissionsEntity.setUserId(userId); - permissionsEntity.setPermission(permission); - return permissionsEntity; - } + private static final String ITEM1_ID = "1"; + private static final String PERMISSION = "Owner"; + private static final String USER2_ID = "testUser2"; + private static final String USER1_ID = "testUser1"; + private static final String ACTION_SUBMIT = "Submit_Item"; + private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; + + static { + SessionContextProviderFactory.getInstance().createInterface().create("testUser1", "dox"); + } + + @Mock + private ItemPermissionsDao permissionsDaoMock; + @Mock + private UserPermissionsDao userPermissionsDao; // do not delete. needed for permissionService + @Mock + private PermissionsRules permissionsRules; + @InjectMocks + @Spy + private PermissionsServicesImpl permissionsServices; + + + @BeforeMethod + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testListUserPermittedItems() { + Set userPermissionSet = Collections.emptySet(); + + Mockito.when(userPermissionsDao.listUserPermittedItems(anyString(), anyString())).thenReturn(userPermissionSet); + + Set permissionsSet = permissionsServices.listUserPermittedItems(USER1_ID, PERMISSION); + + Assert.assertEquals(0, permissionsSet.size()); + } + + @Test + public void testExecute() { + Mockito.doNothing().when(permissionsRules).executeAction(anyString(), anyString(), anyString()); + + permissionsServices.execute(ITEM1_ID, USER1_ID, PERMISSION); + + Mockito.verify(permissionsRules, times(1)).executeAction(anyString(), anyString(), anyString()); + } + + @Test + public void testDeleteItemPermissions() { + Mockito.doNothing().when(permissionsDaoMock).deleteItemPermissions(anyString()); + + permissionsServices.deleteItemPermissions(ITEM1_ID); + + Mockito.verify(permissionsDaoMock, times(1)).deleteItemPermissions(anyString()); + } + + @Test + public void testGetUserItemPermission() { + Mockito.when(permissionsDaoMock.getUserItemPermission(anyString(), anyString())) + .thenReturn(Optional.of(PERMISSION)); + + Optional permission = permissionsServices.getUserItemPermission(ITEM1_ID, USER1_ID); + + Assert.assertTrue(permission.isPresent()); + Assert.assertEquals(permission.get(), PERMISSION); + } + + @Test + public void testListItemPermissionsWhenNone() { + Collection permissions = + permissionsServices.listItemPermissions(ITEM1_ID); + Assert.assertEquals(permissions.size(), 0); + } + + + @Test + public void testListItemPermissions() { + doReturn(Arrays.asList( + createPermissionEntity(ITEM1_ID, USER1_ID, PERMISSION), + createPermissionEntity(ITEM1_ID, USER2_ID, PERMISSION))) + .when(permissionsDaoMock).listItemPermissions(anyObject()); + + Collection actual = + permissionsServices.listItemPermissions(ITEM1_ID); + Assert.assertEquals(actual.size(), 2); + } + + + @Test + public void testIsAllowed() { + when(permissionsDaoMock.getUserItemPermission(ITEM1_ID, USER1_ID)).thenReturn(Optional.of(PERMISSION)); + when(permissionsRules.isAllowed(PERMISSION, ACTION_SUBMIT)).thenReturn(true); + + boolean result = permissionsServices.isAllowed(ITEM1_ID, USER1_ID, ACTION_SUBMIT); + + Assert.assertTrue(result); + + } + + @Test + public void testUpdatePermissions() { + + Set addedUsers = new HashSet<>(); + addedUsers.add(USER2_ID); + + permissionsServices.updateItemPermissions(ITEM1_ID, PERMISSION, addedUsers, new HashSet<>()); + + verify(permissionsRules).executeAction(ITEM1_ID, USER1_ID, CHANGE_PERMISSIONS); + verify(permissionsRules).updatePermission(ITEM1_ID, USER1_ID, PERMISSION, addedUsers, new HashSet<>()); + verify(permissionsDaoMock).updateItemPermissions(ITEM1_ID, PERMISSION, addedUsers, new + HashSet<>()); + } + + + private static ItemPermissionsEntity createPermissionEntity(String itemId, String + userId, String permission) { + ItemPermissionsEntity permissionsEntity = new ItemPermissionsEntity(); + permissionsEntity.setItemId(itemId); + permissionsEntity.setUserId(userId); + permissionsEntity.setPermission(permission); + return permissionsEntity; + } } \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilterTest.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilterTest.java new file mode 100644 index 0000000000..6bb51c30dd --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/test/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilterTest.java @@ -0,0 +1,90 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.itempermissions.servlet; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.HttpMethod; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class PermissionsFilterTest { + + @Mock + private PermissionsServices permissionsServicesMock; + + @InjectMocks + @Spy + private PermissionsFilter permissionsFilter; + + @BeforeMethod + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testDoFilter() throws ServletException, IOException { + HttpServletRequest httpServletRequest = Mockito.spy(HttpServletRequest.class); + HttpServletResponse httpServletResponse = Mockito.spy(HttpServletResponse.class); + FilterChain filterChain = Mockito.mock(FilterChain.class); + + initializeMocking(httpServletRequest, httpServletResponse, filterChain); + Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api/v1.0"); + + permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain); + + Mockito.verify(filterChain, Mockito.times(0)).doFilter(Mockito.any(), Mockito.any()); + } + + @Test + public void testDoFilterPass() throws ServletException, IOException { + HttpServletRequest httpServletRequest = Mockito.spy(HttpServletRequest.class); + HttpServletResponse httpServletResponse = Mockito.spy(HttpServletResponse.class); + FilterChain filterChain = Mockito.mock(FilterChain.class); + + initializeMocking(httpServletRequest, httpServletResponse, filterChain); + Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api"); + permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain); + + Mockito.verify(filterChain, Mockito.times(1)).doFilter(Mockito.any(), Mockito.any()); + } + + private void initializeMocking(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, + FilterChain filterChain) throws ServletException, IOException { + PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream()); + Mockito.when(httpServletRequest.getMethod()).thenReturn(HttpMethod.POST); + Mockito.when(httpServletRequest.getHeader("USER_ID")).thenReturn("cs0008"); + Mockito.when(httpServletResponse.getWriter()).thenReturn(printWriter); + Mockito.doNothing().when(filterChain).doFilter(Mockito.any(), Mockito.any()); + Mockito.when(permissionsServicesMock.isAllowed( + Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(false); + } +} -- cgit 1.2.3-korg