Search in sources :

Example 36 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testSearchAPIsException.

@Test(description = "Exception when searching APIs")
public void testSearchAPIsException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(ALTERNATIVE_USER, identityProvider, apiDAO);
    // Error path
    // APIMgtDAOException
    Mockito.when(apiDAO.searchAPIs(new HashSet<>(), ALTERNATIVE_USER, QUERY_STRING, 1, 2)).thenThrow(APIMgtDAOException.class);
    try {
        apiPublisher.searchAPIs(2, 1, QUERY_STRING);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while Searching the API with query " + QUERY_STRING);
    }
    // Error path
    // IdentityProviderException
    Mockito.when(identityProvider.getIdOfUser(ALTERNATIVE_USER)).thenThrow(IdentityProviderException.class);
    try {
        apiPublisher.searchAPIs(2, 1, QUERY_STRING);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while calling SCIM endpoint to retrieve user " + ALTERNATIVE_USER + "'s information");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 37 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiWithRestrictedVisibility.

@Test(description = "Test add api with restricted visibility")
public void testAddApiWithRestrictedVisibility() throws APIManagementException, LifecycleException {
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add(ADMIN_ROLE);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap()).visibility(API.Visibility.RESTRICTED).visibleRoles(visibleRoles);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    String endpointId = apiBuilder.getEndpoint().get("production").getId();
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    apiPublisher.addAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 38 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddEndpointWhenEndpointExist.

@Test(description = "Add endpoint when endpoint name already exist")
public void testAddEndpointWhenEndpointExist() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, gatewaySourceGenerator, gateway);
    Endpoint endpointToAdd = new Endpoint.Builder(SampleTestObjectCreator.createMockEndpoint()).build();
    Mockito.when(apiDAO.getEndpointByName(endpointToAdd.getName())).thenReturn(endpointToAdd);
    try {
        apiPublisher.addEndpoint(endpointToAdd);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Endpoint already exist with name " + endpointToAdd.getName());
    }
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 39 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetSubscribersOfProviderException.

@Test(description = "Exception when getting subscriptions for a provider's APIs", expectedExceptions = APIManagementException.class)
public void testGetSubscribersOfProviderException() throws APIManagementException {
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiSubscriptionDAO);
    Mockito.when(apiSubscriptionDAO.getAPISubscriptionsForUser(1, 2, USER)).thenThrow(new APIMgtDAOException("Unable to fetch subscriptions APIs of provider " + USER));
    apiPublisher.getSubscribersOfProvider(1, 2, USER);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Test(org.testng.annotations.Test)

Example 40 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUploadDocumentationFileException.

@Test(description = "Exception when uploading Documentation File", expectedExceptions = APIManagementException.class)
public void testUploadDocumentationFileException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    Mockito.doThrow(new APIMgtDAOException("Unable to add documentation with file")).when(apiDAO).addDocumentFileContent(DOC_ID, null, "text/plain", USER);
    apiPublisher.uploadDocumentationFile(DOC_ID, null, "text/plain");
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)432 Test (org.testng.annotations.Test)353 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)233 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)202 API (org.wso2.carbon.apimgt.core.models.API)200 Test (org.junit.Test)164 Response (javax.ws.rs.core.Response)160 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)159 HashMap (java.util.HashMap)148 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)146 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)134 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)129 ArrayList (java.util.ArrayList)102 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)100 BeforeTest (org.testng.annotations.BeforeTest)82 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)80 Request (org.wso2.msf4j.Request)80 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)76 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)71