Search in sources :

Example 1 with TestUtils.spyConnection

use of org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerImplTest method testLockFunctionalityForUser.

@Test(dataProvider = "LockFunctionalityForUserData")
public void testLockFunctionalityForUser(String functionalityIdentifier, String userId, int tenantId, boolean expected) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    long functionalityUnlockTime = System.currentTimeMillis() + 300000;
    String functionalityLockReasonCode = "Lock code";
    String functionalityLockReason = "Lock Reason 2";
    try {
        try (Connection connection = TestUtils.getConnection()) {
            Connection spyConnection = TestUtils.spyConnection(connection);
            when(dataSource.getConnection()).thenReturn(spyConnection);
            try {
                mockIsUserStoreManager(userId);
                userFunctionalityManager.lock(userId, tenantId, functionalityIdentifier, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), expected);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockReason);
            } catch (UserFunctionalityManagementException e) {
                log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
            } catch (UserStoreException e) {
                log.error("Error while checking userid in userstore", e);
            }
        }
    } catch (SQLException e) {
        log.error("SQL Exception", e);
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with TestUtils.spyConnection

use of org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerImplTest method testUnlockFunctionalityForUser.

@Test(dataProvider = "UnlockFunctionalityForUserData")
public void testUnlockFunctionalityForUser(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try {
        try (Connection connection = TestUtils.getConnection()) {
            Connection spyConnection = TestUtils.spyConnection(connection);
            when(dataSource.getConnection()).thenReturn(spyConnection);
            try {
                mockIsUserStoreManager(userId);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), isFunctionalityLocked);
                userFunctionalityManager.unlock(userId, tenantId, functionalityIdentifier);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), false);
            } catch (UserFunctionalityManagementException e) {
                log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
            } catch (UserStoreException e) {
                log.error("Error while checking userid in userstore", e);
            }
        }
    } catch (SQLException e) {
        log.error("SQL Exception", e);
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with TestUtils.spyConnection

use of org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerDAOImplTest method testDeleteFunctionality.

@Test
public void testDeleteFunctionality() {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try (Connection connection = TestUtils.getConnection()) {
        Connection spyConnection = TestUtils.spyConnection(connection);
        when(dataSource.getConnection()).thenReturn(spyConnection);
        FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(false, 0, null, null);
        try {
            userFunctionalityManagerDAO.addFunctionalityLock("userId", 1, "functionalityIdentifier1", functionalityLockStatus);
            userFunctionalityManagerDAO.deleteMappingForUser("userId", 1, "functionalityIdentifier1");
            userFunctionalityManagerDAO.deleteMappingForUser("userId", 2, "functionalityIdentifier2");
            assertNull(userFunctionalityManagerDAO.getFunctionalityLockStatus("userId", 1, "functionalityIdentifier1"));
        } catch (UserFunctionalityManagementServerException e) {
            log.error("FunctionalityManagementServer Exception", e);
        }
    } catch (SQLException e) {
    // Mock behaviour. Hence ignored.
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with TestUtils.spyConnection

use of org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerDAOImplTest method testAddFunctionality.

@Test(dataProvider = "TestFunctionalityData")
public void testAddFunctionality(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked, int functionalityUnlockTime, String functionalityLockReasonCode, String functionalityLockReason) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try (Connection connection = TestUtils.getConnection()) {
        Connection spyConnection = TestUtils.spyConnection(connection);
        when(dataSource.getConnection()).thenReturn(spyConnection);
        FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
        try {
            userFunctionalityManagerDAO.addFunctionalityLock(userId, tenantId, functionalityIdentifier, functionalityLockStatus);
        } catch (UserFunctionalityManagementServerException e) {
            log.error(String.format("Error while adding functionality: %s", functionalityIdentifier), e);
        }
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), functionalityLockStatus.getLockStatus());
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getUnlockTime(), functionalityLockStatus.getUnlockTime());
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockStatus.getLockReason());
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReasonCode(), functionalityLockStatus.getLockReasonCode());
    } catch (SQLException e) {
    // Mock behaviour. Hence ignored.
    } catch (UserFunctionalityManagementServerException e) {
        assertEquals(e.getMessage(), String.format("Error occurred while adding the functionality: %s, for user: %s, for tenant" + " id: %d, having the parameters, functionality lock status: %b, functionality " + "unlock time: %d, functionality lock reason code: %s, functionality lock reason: " + "%s.", functionalityIdentifier, userId, tenantId, isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason));
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with TestUtils.spyConnection

use of org.wso2.carbon.identity.template.mgt.util.TestUtils.spyConnection in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerDAOImplTest method testGetFunctionalityLockStatus.

@Test(dataProvider = "TestFunctionalityData")
public void testGetFunctionalityLockStatus(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked, int functionalityUnlockTime, String functionalityLockReasonCode, String functionalityLockReason) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try (Connection connection = TestUtils.getConnection()) {
        Connection spyConnection = TestUtils.spyConnection(connection);
        when(dataSource.getConnection()).thenReturn(spyConnection);
        FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
        try {
            userFunctionalityManagerDAO.addFunctionalityLock(userId, tenantId, functionalityIdentifier, functionalityLockStatus);
        } catch (UserFunctionalityManagementServerException e) {
            log.error(String.format("Error while adding functionality: %s", functionalityIdentifier), e);
        }
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), isFunctionalityLocked);
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getUnlockTime(), functionalityUnlockTime);
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReasonCode(), functionalityLockReasonCode);
        Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockReason);
    } catch (SQLException e) {
    // Mock behaviour. Hence ignored.
    } catch (UserFunctionalityManagementServerException e) {
        assertEquals(e.getMessage(), String.format("Error occurred while retrieving functionality lock status from DB for " + "functionality id: %s, user Id: %s and tenant Id: %d.", functionalityIdentifier, userId, tenantId));
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Connection (java.sql.Connection)13 DataSource (javax.sql.DataSource)13 SQLException (java.sql.SQLException)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Test (org.testng.annotations.Test)12 UserFunctionalityManagementServerException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException)7 FunctionalityLockStatus (org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus)7 UserFunctionalityManagementException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 BeforeMethod (org.testng.annotations.BeforeMethod)1