Search in sources :

Example 6 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getSubscriptionBlockCondition.

/**
 * Get details of the subscription block condition by condition value and tenant domain
 *
 * @param conditionValue condition value of the block condition
 * @param tenantDomain   tenant domain of the block condition
 * @return Block condition
 * @throws APIManagementException
 */
public BlockConditionsDTO getSubscriptionBlockCondition(String conditionValue, String tenantDomain) throws APIManagementException {
    Connection connection = null;
    PreparedStatement selectPreparedStatement = null;
    ResultSet resultSet = null;
    BlockConditionsDTO blockCondition = null;
    try {
        String query = SQLConstants.ThrottleSQLConstants.GET_SUBSCRIPTION_BLOCK_CONDITION_BY_VALUE_AND_DOMAIN_SQL;
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(true);
        selectPreparedStatement = connection.prepareStatement(query);
        selectPreparedStatement.setString(1, conditionValue);
        selectPreparedStatement.setString(2, tenantDomain);
        resultSet = selectPreparedStatement.executeQuery();
        if (resultSet.next()) {
            blockCondition = new BlockConditionsDTO();
            blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
            blockCondition.setConditionType(resultSet.getString("TYPE"));
            blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
            blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
            blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
            blockCondition.setUUID(resultSet.getString("UUID"));
        }
    } catch (SQLException e) {
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                handleException("Failed to rollback getting Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, ex);
            }
        }
        handleException("Failed to get Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
    }
    return blockCondition;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 7 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getBlockCondition.

/**
 * Get details of a block condition by Id
 *
 * @param conditionId id of the condition
 * @return Block conditoin represented by the UUID
 * @throws APIManagementException
 */
public BlockConditionsDTO getBlockCondition(int conditionId) throws APIManagementException {
    Connection connection = null;
    PreparedStatement selectPreparedStatement = null;
    ResultSet resultSet = null;
    BlockConditionsDTO blockCondition = null;
    try {
        String query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITION_SQL;
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(true);
        selectPreparedStatement = connection.prepareStatement(query);
        selectPreparedStatement.setInt(1, conditionId);
        resultSet = selectPreparedStatement.executeQuery();
        if (resultSet.next()) {
            blockCondition = new BlockConditionsDTO();
            blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
            blockCondition.setConditionType(resultSet.getString("TYPE"));
            blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
            blockCondition.setConditionId(conditionId);
            blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
            blockCondition.setUUID(resultSet.getString("UUID"));
        }
    } catch (SQLException e) {
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                handleException("Failed to rollback getting Block condition with id " + conditionId, ex);
            }
        }
        handleException("Failed to get Block condition with id " + conditionId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
    }
    return blockCondition;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 8 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testDeleteBlockCondition.

@Test
public void testDeleteBlockCondition() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    Mockito.when(apimgtDAO.getBlockCondition(1111)).thenReturn(blockConditionsDTO);
    Mockito.when(apimgtDAO.deleteBlockCondition(1111)).thenReturn(false, true);
    // deleteState false
    assertFalse(apiProvider.deleteBlockCondition(1111));
    // deleteState true
    assertTrue(apiProvider.deleteBlockCondition(1111));
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testDeleteBlockConditionByUUID.

@Test
public void testDeleteBlockConditionByUUID() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    blockConditionsDTO.setConditionType("testType");
    blockConditionsDTO.setConditionValue("USER");
    blockConditionsDTO.setConditionId(1111);
    Mockito.when(apimgtDAO.getBlockConditionByUUID("testId")).thenReturn(blockConditionsDTO);
    Mockito.when(apimgtDAO.deleteBlockCondition(1111)).thenReturn(false, true);
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.when(MultitenantUtils.getTenantAwareUsername("User")).thenReturn("testValue");
    // deleteState false
    assertFalse(apiProvider.deleteBlockConditionByUUID("testId"));
    // deleteState true
    assertTrue(apiProvider.deleteBlockConditionByUUID("testId"));
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetBlockCondition.

@Test
public void testGetBlockCondition() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    Mockito.when(apimgtDAO.getBlockCondition(Mockito.anyInt())).thenReturn(blockConditionsDTO);
    assertNotNull(apiProvider.getBlockCondition(Mockito.anyInt()));
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

BlockConditionsDTO (org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)24 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 ParseException (org.json.simple.parser.ParseException)4 ArrayList (java.util.ArrayList)3 JSONObject (org.json.simple.JSONObject)2 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1