Skip to content

Commit

Permalink
Add tests for Config Persistence Manager class to cover failure scena…
Browse files Browse the repository at this point in the history
…rios of db or registry
  • Loading branch information
UdeshAthukorala committed Sep 25, 2024
1 parent 502bd71 commit d011a78
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.wso2.carbon.identity.entitlement.persistence;

import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithRealmService;
import org.wso2.carbon.identity.entitlement.EntitlementException;
import org.wso2.carbon.identity.entitlement.internal.EntitlementConfigHolder;
import org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent;
import org.wso2.carbon.identity.entitlement.persistence.cache.CacheBackedConfigDAO;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.wso2.carbon.identity.entitlement.PDPConstants.Algorithms.DENY_OVERRIDES;
import static org.wso2.carbon.identity.entitlement.PDPConstants.Algorithms.PERMIT_OVERRIDES;
import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_ID;

/**
* This class tests the failure scenarios of Database or Registry in ConfigPersistenceManager implementations.
*/
@WithCarbonHome
@WithRealmService(injectToSingletons = {EntitlementConfigHolder.class}, initUserStoreManager = true)
public class ConfigPersistenceManagerFailureTest {

@Mock
private CacheBackedConfigDAO mockedConfigDAO;

@Mock
private Registry mockedRegistry;

MockedStatic<EntitlementServiceComponent> entitlementServiceComponent;

private JDBCConfigPersistenceManager jdbcConfigPersistenceManager;
private RegistryConfigPersistenceManager registryConfigPersistenceManager;

@BeforeMethod
public void setUp() throws Exception {

MockitoAnnotations.openMocks(this);
jdbcConfigPersistenceManager = new JDBCConfigPersistenceManager();
setPrivateStaticFinalField(JDBCConfigPersistenceManager.class, "configDAO", mockedConfigDAO);

entitlementServiceComponent = mockStatic(EntitlementServiceComponent.class);
entitlementServiceComponent.when(() -> EntitlementServiceComponent.getGovernanceRegistry(anyInt()))
.thenReturn(mockedRegistry);
registryConfigPersistenceManager = new RegistryConfigPersistenceManager();
}

@AfterMethod
public void tearDown() {

entitlementServiceComponent.close();
}

@Test
public void testGetGlobalPolicyAlgorithmWhenDatabaseErrorHappened() throws Exception {

when(mockedConfigDAO.getPolicyCombiningAlgorithm(anyInt())).thenThrow(new EntitlementException(""));
String globalPolicyAlgorithmName = jdbcConfigPersistenceManager.getGlobalPolicyAlgorithmName();
assertEquals(globalPolicyAlgorithmName, DENY_OVERRIDES);
}

@Test
public void testAddGlobalPolicyAlgorithmWhenResourceCheckFailed() throws Exception {

when(mockedConfigDAO.getPolicyCombiningAlgorithm(anyInt())).thenThrow(new EntitlementException(""));
jdbcConfigPersistenceManager.addOrUpdateGlobalPolicyAlgorithm(PERMIT_OVERRIDES);
verify(mockedConfigDAO, never()).updatePolicyCombiningAlgorithm(anyString(), anyInt());
verify(mockedConfigDAO, times(1)).insertPolicyCombiningAlgorithm(PERMIT_OVERRIDES, SUPER_TENANT_ID);
}

@Test
public void testAddGlobalPolicyAlgorithmWhenDatabaseErrorHappened() throws Exception {

when(mockedConfigDAO.getPolicyCombiningAlgorithm(anyInt())).thenReturn(null);
doThrow(new EntitlementException("")).when(mockedConfigDAO)
.insertPolicyCombiningAlgorithm(anyString(), anyInt());
assertThrows(EntitlementException.class,
() -> jdbcConfigPersistenceManager.addOrUpdateGlobalPolicyAlgorithm(PERMIT_OVERRIDES));
}

@Test
public void testGetGlobalPolicyAlgorithmWhenRegistryErrorHappened() throws Exception {

when(mockedRegistry.resourceExists(anyString())).thenThrow(new RegistryException(""));
String actualAlgorithm = registryConfigPersistenceManager.getGlobalPolicyAlgorithmName();
assertEquals(actualAlgorithm, DENY_OVERRIDES);
}

@Test
public void testAddGlobalPolicyAlgorithmWhenRegistryErrorHappened() throws Exception {

when(mockedRegistry.resourceExists(anyString())).thenThrow(new RegistryException(""));
assertThrows(EntitlementException.class,
() -> registryConfigPersistenceManager.addOrUpdateGlobalPolicyAlgorithm(PERMIT_OVERRIDES));
}

@Test
public void testDeleteGlobalPolicyAlgorithmWhenRegistryErrorHappened() throws Exception {

when(mockedRegistry.resourceExists(anyString())).thenReturn(true);
doThrow(new RegistryException("")).when(mockedRegistry).delete(anyString());
assertThrows(EntitlementException.class, () -> registryConfigPersistenceManager.deleteGlobalPolicyAlgorithm());
}

private static void setPrivateStaticFinalField(Class<?> clazz, String fieldName, Object newValue)
throws ReflectiveOperationException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);

Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(null, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<class name="org.wso2.carbon.identity.entitlement.persistence.JDBCConfigPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.RegistryConfigPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.HybridConfigPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.ConfigPersistenceManagerFailureTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.JDBCPolicyPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.RegistryPolicyPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.HybridPolicyPersistenceManagerTest"/>
Expand Down

0 comments on commit d011a78

Please sign in to comment.