Search in sources :

Example 11 with BlockConditionsDTO

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

the class APIProviderImplTest method testUpdateBlockCondition.

@Test
public void testUpdateBlockCondition() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.updateBlockConditionState(1, "testState")).thenReturn(false, true);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    blockConditionsDTO.setConditionType("testType");
    blockConditionsDTO.setConditionValue("USER");
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.when(MultitenantUtils.getTenantAwareUsername("User")).thenReturn("testValue");
    Mockito.when(apimgtDAO.getBlockCondition(1)).thenReturn(blockConditionsDTO);
    // updateState false
    assertFalse(apiProvider.updateBlockCondition(1, "testState"));
    // updateState true
    assertTrue(apiProvider.updateBlockCondition(1, "testState"));
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with BlockConditionsDTO

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

the class APIProviderImpl method deleteBlockCondition.

@Override
public boolean deleteBlockCondition(int conditionId) throws APIManagementException {
    BlockConditionsDTO blockCondition = apiMgtDAO.getBlockCondition(conditionId);
    boolean deleteState = apiMgtDAO.deleteBlockCondition(conditionId);
    if (deleteState && blockCondition != null) {
        unpublishBlockCondition(blockCondition);
    }
    return deleteState;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)

Example 13 with BlockConditionsDTO

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

the class APIProviderImpl method updateBlockCondition.

@Override
public boolean updateBlockCondition(int conditionId, String state) throws APIManagementException {
    boolean updateState = apiMgtDAO.updateBlockConditionState(conditionId, state);
    BlockConditionsDTO blockConditionsDTO = apiMgtDAO.getBlockCondition(conditionId);
    if (updateState) {
        publishBlockingEventUpdate(blockConditionsDTO);
    }
    return updateState;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)

Example 14 with BlockConditionsDTO

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

the class APIProviderImpl method addBlockCondition.

@Override
public String addBlockCondition(String conditionType, String conditionValue) throws APIManagementException {
    if (APIConstants.BLOCKING_CONDITIONS_USER.equals(conditionType)) {
        conditionValue = MultitenantUtils.getTenantAwareUsername(conditionValue);
        conditionValue = conditionValue + "@" + tenantDomain;
    }
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    blockConditionsDTO.setConditionType(conditionType);
    blockConditionsDTO.setConditionValue(conditionValue);
    blockConditionsDTO.setTenantDomain(tenantDomain);
    blockConditionsDTO.setEnabled(true);
    blockConditionsDTO.setUUID(UUID.randomUUID().toString());
    BlockConditionsDTO createdBlockConditionsDto = apiMgtDAO.addBlockConditions(blockConditionsDTO);
    if (createdBlockConditionsDto != null) {
        publishBlockingEvent(createdBlockConditionsDto, "true");
    }
    return createdBlockConditionsDto.getUUID();
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)

Example 15 with BlockConditionsDTO

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

the class APIMgtDAOTest method testAddUpdateDeleteBlockCondition.

@Test
public void testAddUpdateDeleteBlockCondition() throws Exception {
    Subscriber subscriber = new Subscriber("blockuser1");
    subscriber.setTenantId(-1234);
    subscriber.setEmail("abc@wso2.com");
    subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
    apiMgtDAO.addSubscriber(subscriber, null);
    Policy applicationPolicy = getApplicationPolicy("testAddUpdateDeleteBlockCondition");
    applicationPolicy.setTenantId(-1234);
    apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
    Application application = new Application("testAddUpdateDeleteBlockCondition", subscriber);
    application.setTier("testAddUpdateDeleteBlockCondition");
    application.setId(apiMgtDAO.addApplication(application, "blockuser1", "testOrg"));
    APIIdentifier apiId = new APIIdentifier("testAddUpdateDeleteBlockCondition", "testAddUpdateDeleteBlockCondition", "1.0.0");
    API api = new API(apiId);
    api.setContext("/testAddUpdateDeleteBlockCondition");
    api.setContextTemplate("/testAddUpdateDeleteBlockCondition/{version}");
    api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
    api.setUUID(UUID.randomUUID().toString());
    apiMgtDAO.addAPI(api, -1234, "testOrg");
    BlockConditionsDTO apiBlockConditionDto = new BlockConditionsDTO();
    apiBlockConditionDto.setConditionValue("/testAddUpdateDeleteBlockCondition");
    apiBlockConditionDto.setConditionType(APIConstants.BLOCKING_CONDITIONS_API);
    apiBlockConditionDto.setUUID(UUID.randomUUID().toString());
    apiBlockConditionDto.setTenantDomain("carbon.super");
    BlockConditionsDTO apiUUID = apiMgtDAO.addBlockConditions(apiBlockConditionDto);
    BlockConditionsDTO applicationBlockCondition = new BlockConditionsDTO();
    applicationBlockCondition.setConditionValue("blockuser1:testAddUpdateDeleteBlockCondition");
    applicationBlockCondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_APPLICATION);
    applicationBlockCondition.setUUID(UUID.randomUUID().toString());
    applicationBlockCondition.setTenantDomain("carbon.super");
    BlockConditionsDTO applicationUUID = apiMgtDAO.addBlockConditions(applicationBlockCondition);
    assertNotNull(applicationUUID);
    BlockConditionsDTO ipBlockcondition = new BlockConditionsDTO();
    ipBlockcondition.setConditionValue("127.0.0.1");
    ipBlockcondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_IP);
    ipBlockcondition.setUUID(UUID.randomUUID().toString());
    ipBlockcondition.setTenantDomain("carbon.super");
    BlockConditionsDTO ipUUID = apiMgtDAO.addBlockConditions(ipBlockcondition);
    assertNotNull(ipUUID);
    BlockConditionsDTO userBlockcondition = new BlockConditionsDTO();
    userBlockcondition.setConditionValue("admin");
    userBlockcondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_USER);
    userBlockcondition.setUUID(UUID.randomUUID().toString());
    userBlockcondition.setTenantDomain("carbon.super");
    BlockConditionsDTO userUUID = apiMgtDAO.addBlockConditions(userBlockcondition);
    assertNotNull(apiMgtDAO.getBlockConditionByUUID(apiUUID.getUUID()));
    assertNotNull(userUUID);
    assertNotNull(apiMgtDAO.updateBlockConditionState(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId(), "FALSE"));
    assertNotNull(apiMgtDAO.updateBlockConditionStateByUUID(userUUID.getUUID(), "FALSE"));
    apiMgtDAO.deleteBlockCondition(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId());
    apiMgtDAO.getBlockCondition(apiMgtDAO.getBlockConditionByUUID(ipUUID.getUUID()).getConditionId());
    List<BlockConditionsDTO> blockConditions = apiMgtDAO.getBlockConditions("carbon.super");
    for (BlockConditionsDTO blockConditionsDTO : blockConditions) {
        apiMgtDAO.deleteBlockConditionByUUID(blockConditionsDTO.getUUID());
    }
    apiMgtDAO.deleteApplication(application);
    apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, applicationPolicy.getPolicyName(), -1234);
    apiMgtDAO.deleteAPI(api.getUuid());
    deleteSubscriber(subscriber.getId());
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) API(org.wso2.carbon.apimgt.api.model.API) Application(org.wso2.carbon.apimgt.api.model.Application) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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