Skip to content

Commit

Permalink
Merge pull request #5964 from UdeshAthukorala/policy-fail-2
Browse files Browse the repository at this point in the history
Improve policy persistence manager failure test cases
  • Loading branch information
UdeshAthukorala committed Sep 29, 2024
2 parents 27d17b5 + 5258898 commit 165ca70
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO;
import org.wso2.carbon.identity.entitlement.internal.EntitlementConfigHolder;
import org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent;
import org.wso2.carbon.identity.entitlement.persistence.cache.CacheBackedPolicyDAO;
import org.wso2.carbon.registry.core.CollectionImpl;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand All @@ -59,7 +62,7 @@
*/
@WithCarbonHome
@WithRealmService(injectToSingletons = {EntitlementConfigHolder.class}, initUserStoreManager = true)
public class RegistryPolicyPersistenceManagerFailureTest {
public class PolicyPersistenceManagerFailureTest {

static final String SAMPLE_POLICY_STRING_1 =
"<Policy xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" PolicyId=\"sample_policy1\" RuleCombiningAlgId=\"urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:first-applicable\" Version=\"1.0\"><Target><AnyOf><AllOf><Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\"><AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">GET</AttributeValue><AttributeDesignator AttributeId=\"urn:oasis:names:tc:xacml:1.0:action:action-id\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:action\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"true\"></AttributeDesignator></Match><Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\"><AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">resourceA</AttributeValue><AttributeDesignator AttributeId=\"urn:oasis:names:tc:xacml:1.0:resource:resource-id\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"true\"></AttributeDesignator></Match></AllOf></AnyOf></Target><Rule Effect=\"Permit\" RuleId=\"rule1\"><Condition><Apply FunctionId=\"urn:oasis:names:tc:xacml:1.0:function:and\"><Apply FunctionId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\"><Apply FunctionId=\"urn:oasis:names:tc:xacml:1.0:function:string-one-and-only\"><AttributeDesignator AttributeId=\"http://wso2.org/claims/country\" Category=\"http://wso2.org/identity/user\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"true\"></AttributeDesignator></Apply><AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">Sri Lanka</AttributeValue></Apply><Apply FunctionId=\"urn:oasis:names:tc:xacml:1.0:function:string-is-in\"><AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">Engineer</AttributeValue><AttributeDesignator AttributeId=\"http://wso2.org/claims/role\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"true\"></AttributeDesignator></Apply></Apply></Condition></Rule><Rule Effect=\"Deny\" RuleId=\"rule2\"></Rule></Policy>";
Expand All @@ -68,11 +71,15 @@ public class RegistryPolicyPersistenceManagerFailureTest {
PolicyDTO samplePAPPolicy1;
PolicyStoreDTO samplePDPPolicy1;

@Mock
private CacheBackedPolicyDAO mockedPolicyDAO;

@Mock
private Registry mockedRegistry;

MockedStatic<EntitlementServiceComponent> entitlementServiceComponent;
private RegistryPolicyPersistenceManager registryPolicyPersistenceManager;
private JDBCPolicyPersistenceManager jdbcPolicyPersistenceManager;

@BeforeMethod
public void setUp() throws Exception {
Expand All @@ -95,6 +102,8 @@ public void setUp() throws Exception {
registryPolicyPersistenceManager = new RegistryPolicyPersistenceManager();
storeProps.setProperty("policyStorePath", "/repository/identity/entitlement/policy/pdp/");
registryPolicyPersistenceManager.init(storeProps);
jdbcPolicyPersistenceManager = new JDBCPolicyPersistenceManager();
setPrivateStaticFinalField(JDBCPolicyPersistenceManager.class, "policyDAO", mockedPolicyDAO);

samplePAPPolicy1 = new PolicyDTO(SAMPLE_POLICY_ID_1);
samplePAPPolicy1.setPolicy(SAMPLE_POLICY_STRING_1);
Expand All @@ -106,6 +115,72 @@ public void tearDown() throws Exception {

entitlementServiceComponent.close();
registryPolicyPersistenceManager = null;
setPrivateStaticFinalField(JDBCPolicyPersistenceManager.class, "policyDAO",
CacheBackedPolicyDAO.getInstance());
}

@Test
public void testAddOrUpdatePolicyWhenDatabaseErrorHappened() throws Exception {

doThrow(new EntitlementException("")).when(mockedPolicyDAO).insertPolicy(any(), anyInt());
assertThrows(EntitlementException.class,
() -> jdbcPolicyPersistenceManager.addOrUpdatePolicy(samplePAPPolicy1, true));
}

@Test
public void testGetPAPPolicyWhenDatabaseErrorHappened() throws Exception {

when(mockedPolicyDAO.getPAPPolicy(anyString(), anyInt())).thenThrow(new EntitlementException(""));
when(mockedRegistry.resourceExists(anyString())).thenReturn(true);
assertThrows(EntitlementException.class,
() -> jdbcPolicyPersistenceManager.getPAPPolicy(samplePAPPolicy1.getPolicyId()));
}

@Test
public void testGetActivePoliciesWhenDatabaseErrorHappened() throws Exception {

when(mockedPolicyDAO.getAllPDPPolicies(anyInt())).thenThrow(new EntitlementException(""));
String[] activePolicies = jdbcPolicyPersistenceManager.getActivePolicies();
assertEquals(activePolicies.length, 0);
}

@Test
public void testGetOrderedPolicyIdentifiersWhenDatabaseErrorHappened() throws Exception {

when(mockedPolicyDAO.getAllPDPPolicies(anyInt())).thenThrow(new EntitlementException(""));
String[] orderedPolicies = jdbcPolicyPersistenceManager.getOrderedPolicyIdentifiers();
assertEquals(orderedPolicies.length, 0);
}

@Test
public void testGetPolicyIdentifiersWhenDatabaseErrorHappened() throws Exception {

when(mockedPolicyDAO.getPublishedPolicyIds(anyInt())).thenThrow(new EntitlementException(""));
assertNull(jdbcPolicyPersistenceManager.getPolicyIdentifiers());
}

@Test
public void testGetSearchAttributesWhenDatabaseErrorHappened() throws Exception {

when(mockedPolicyDAO.getAllPDPPolicies(anyInt())).thenThrow(new EntitlementException(""));
Map<String, Set<AttributeDTO>> attributes = jdbcPolicyPersistenceManager.getSearchAttributes(null, null);
assertEquals(attributes.size(), 0);
}

@Test
public void testRemovePolicyWhenDatabaseErrorHappened() throws Exception {

doThrow(new EntitlementException("")).when(mockedPolicyDAO).deletePAPPolicy(anyString(), anyInt());
assertThrows(EntitlementException.class,
() -> jdbcPolicyPersistenceManager.removePolicy(samplePAPPolicy1.getPolicyId()));
}

@Test
public void testAddPdPPolicyWhenDatabaseErrorHappened() throws Exception {

doThrow(new EntitlementException("")).when(mockedPolicyDAO).insertOrUpdatePolicy(any(), anyInt());
assertThrows(EntitlementException.class,
() -> jdbcPolicyPersistenceManager.addPolicy(samplePDPPolicy1));
}

@Test
Expand Down Expand Up @@ -165,8 +240,8 @@ public void testGetActivePoliciesWhenResourceNotExists() throws Exception {
@Test
public void testGetOrderedPolicyIdentifiersWhenRegistryErrorHappened() throws Exception {

when(mockedRegistry.resourceExists(anyString())).thenReturn(false);
String[] orderedPolicies = registryPolicyPersistenceManager.getActivePolicies();
when(mockedRegistry.resourceExists(anyString())).thenThrow(new RegistryException(""));
String[] orderedPolicies = registryPolicyPersistenceManager.getOrderedPolicyIdentifiers();
assertEquals(orderedPolicies.length, 0);
}

Expand Down Expand Up @@ -259,4 +334,17 @@ private PolicyStoreDTO getPDPPolicy(String id, String policy, String version, bo
policyStoreDTO.setSetOrder(setOrder);
return policyStoreDTO;
}

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 @@ -67,10 +67,12 @@ public abstract class SubscriberPersistenceManagerTest {
static final String SAMPLE_SUBSCRIBER_PASSWORD_2 = "admin_password2";
static final String SAMPLE_ENCRYPTED_PASSWORD1 = "encrypted_admin_password1";
static final String SAMPLE_ENCRYPTED_PASSWORD2 = "encrypted_admin_password2";
static final String NEW_MODULE_NAME = "New Updated Module";

public PublisherDataHolder sampleHolder1;
public PublisherDataHolder sampleHolder2;
public PublisherDataHolder updatedSampleHolder1;
private PublisherDataHolder moduleNameUpdatedSampleHolder1;
public PublisherDataHolder invalidSampleHolder;

@BeforeClass
Expand Down Expand Up @@ -99,6 +101,9 @@ public void setUp() {
updatedSampleHolder1 =
createSampleHolder(SAMPLE_SUBSCRIBER_ID_1, SAMPLE_SUBSCRIBER_URL_2, SAMPLE_SUBSCRIBER_USERNAME_2,
SAMPLE_SUBSCRIBER_PASSWORD_2);
moduleNameUpdatedSampleHolder1 = createSampleHolder(SAMPLE_SUBSCRIBER_ID_1, SAMPLE_SUBSCRIBER_URL_1,
SAMPLE_SUBSCRIBER_USERNAME_1, SAMPLE_SUBSCRIBER_PASSWORD_1);
moduleNameUpdatedSampleHolder1.setModuleName(NEW_MODULE_NAME);
invalidSampleHolder = createSampleHolder(null, null, null, null);
}

Expand Down Expand Up @@ -193,6 +198,17 @@ public void testUpdateSubscriber() throws Exception {
SAMPLE_SUBSCRIBER_PASSWORD_2);
}

@Test(priority = 3)
public void testUpdateSubscriberModuleName() throws Exception {

subscriberPersistenceManager.addSubscriber(sampleHolder1);
subscriberPersistenceManager.updateSubscriber(moduleNameUpdatedSampleHolder1);

PublisherDataHolder subscriberFromStorage =
subscriberPersistenceManager.getSubscriber(SAMPLE_SUBSCRIBER_ID_1, false);
assertEquals(subscriberFromStorage.getModuleName(), moduleNameUpdatedSampleHolder1.getModuleName());
}

@Test(priority = 3)
public void testUpdateInvalidSubscriber() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<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"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.PolicyPersistenceManagerFailureTest"/>
<class name="org.wso2.carbon.identity.entitlement.policy.finder.JDBCPolicyPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.policy.finder.RegistryPolicyPersistenceManagerTest"/>
<class name="org.wso2.carbon.identity.entitlement.policy.finder.HybridPolicyPersistenceManagerTest"/>
Expand All @@ -39,7 +40,6 @@
<class name="org.wso2.carbon.identity.entitlement.persistence.JDBCSimplePAPStatusDataHandlerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.RegistrySimplePAPStatusDataHandlerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.HybridPAPStatusDataHandlerTest"/>
<class name="org.wso2.carbon.identity.entitlement.persistence.RegistryPolicyPersistenceManagerFailureTest"/>
</classes>
</test>
</suite>

0 comments on commit 165ca70

Please sign in to comment.