Search in sources :

Example 31 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddThreatProtectionPolicy.

@Test(description = "Test adding threat protection policy")
public void testAddThreatProtectionPolicy() throws APIManagementException {
    ThreatProtectionDAO threatProtectionDAO = Mockito.mock(ThreatProtectionDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(threatProtectionDAO);
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    Mockito.doThrow(APIMgtDAOException.class).when(threatProtectionDAO).updatePolicy(policy);
    Mockito.doThrow(APIMgtDAOException.class).when(threatProtectionDAO).addPolicy(policy);
    // Error path
    try {
        adminService.addThreatProtectionPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error adding threat protection policy");
    }
    try {
        policy.setUuid("");
        adminService.addThreatProtectionPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error adding threat protection policy");
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 32 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testUpdateApplicationPolicy.

@Test(description = "Test update application policy")
public void testUpdateApplicationPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    adminService.updateApplicationPolicy(applicationPolicy);
    Mockito.verify(policyDAO, Mockito.times(1)).updateApplicationPolicy(applicationPolicy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).updateApplicationPolicy(applicationPolicy);
    try {
        adminService.updateApplicationPolicy(applicationPolicy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't update Application policy for uuid: " + applicationPolicy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 33 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testUpdateCustomRule.

@Test(description = "Test updating a custom rule")
public void testUpdateCustomRule() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
    adminService.updateCustomRule(customPolicy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).updateCustomPolicy(customPolicy);
    try {
        adminService.updateCustomRule(customPolicy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't update custom policy with UUID: " + customPolicy.getUuid());
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 34 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testRegisterGatewayLabels.

@Test(description = "Register gateway labels")
public void testRegisterGatewayLabels() throws APIManagementException {
    LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
    List<Label> labels = new ArrayList<>();
    Label label1 = SampleTestObjectCreator.createLabel("testLabel1", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    Label label2 = SampleTestObjectCreator.createLabel("testLabel2", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    labels.add(label1);
    List<String> labelNames = new ArrayList<>();
    labelNames.add(label1.getName());
    List<Label> existingLabels = new ArrayList<>();
    existingLabels.add(label1);
    existingLabels.add(label2);
    Mockito.when(labelDAO.getLabelsByName(labelNames)).thenReturn(existingLabels);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(labelDAO);
    adminService.registerGatewayLabels(labels, "false");
    Mockito.verify(labelDAO, Mockito.times(1)).addLabels(labels);
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) Test(org.testng.annotations.Test)

Example 35 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddPolicy.

@Test(description = "Add policy")
public void testAddPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy policy = SampleTestObjectCreator.createDefaultAPIPolicy();
    adminService.addApiPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addApiPolicy(policy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).addApiPolicy(policy);
    try {
        adminService.addApiPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add API policy for uuid: " + policy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Aggregations

APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)67 Response (javax.ws.rs.core.Response)65 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)65 Test (org.junit.Test)59 Test (org.testng.annotations.Test)58 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)46 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 ArrayList (java.util.ArrayList)28 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)20 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)17 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)14 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)11 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)11 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)11 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)10 Label (org.wso2.carbon.apimgt.core.models.Label)10 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)9 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)6 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)6