Search in sources :

Example 11 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class PolicyExportManagerTest method testCreateArchiveFromExecutionPlans.

@Test(description = "testing creating archive from execution plans")
public void testCreateArchiveFromExecutionPlans() throws APIManagementException {
    APIMgtAdminService apiMgtAdminService = Mockito.mock(APIMgtAdminService.class);
    List<APIPolicy> apiPolicies = new ArrayList<>();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    List<CustomPolicy> customPolicies = new ArrayList<>();
    apiPolicies.add(SampleTestObjectCreator.createDefaultAPIPolicy());
    applicationPolicies.add(SampleTestObjectCreator.createDefaultApplicationPolicy());
    subscriptionPolicies.add(SampleTestObjectCreator.createDefaultSubscriptionPolicy());
    customPolicies.add(SampleTestObjectCreator.createDefaultCustomPolicy());
    Mockito.when(apiMgtAdminService.getApiPolicies()).thenReturn(apiPolicies);
    Mockito.when(apiMgtAdminService.getApplicationPolicies()).thenReturn(applicationPolicies);
    Mockito.when(apiMgtAdminService.getCustomRules()).thenReturn(customPolicies);
    Mockito.when(apiMgtAdminService.getSubscriptionPolicies()).thenReturn(subscriptionPolicies);
    PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
    String path = policyExportManager.createArchiveFromExecutionPlans("exportDir", exportRootDirectory, "policies");
    String unzippedPath = exportRootDirectory + File.separator + "export-policies";
    APIFileUtils.extractArchive(path, unzippedPath);
    Assert.assertEquals(new File(APIFileUtils.getDirectoryList(unzippedPath).iterator().next()).listFiles().length == 6, true, "Exported policy count is not equal to ");
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) File(java.io.File) Test(org.testng.annotations.Test)

Example 12 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class EndpointsApiServiceImplTestCase method endpointsGetTest.

@Test
public void endpointsGetTest() throws Exception {
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    Endpoint endpointOne = SampleTestObjectCreator.createUniqueEndpoint();
    Endpoint endpointTwo = SampleTestObjectCreator.createUniqueEndpoint();
    Endpoint endpointThree = SampleTestObjectCreator.createUniqueEndpoint();
    List<Endpoint> endpointList = new ArrayList<>();
    endpointList.add(endpointOne);
    endpointList.add(endpointTwo);
    endpointList.add(endpointThree);
    Mockito.when(apiMgtAdminService.getAllEndpoints()).thenReturn(endpointList);
    EndpointsApiServiceImpl endpointsApiService = new EndpointsApiServiceImpl();
    Response response = endpointsApiService.endpointsGet(null, null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(((EndpointListDTO) response.getEntity()).getList().size(), 3);
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) EndpointListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.EndpointListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method getResponse.

private Response getResponse(String apiContext, String apiVersion) throws Exception {
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
    Mockito.when(apiMgtAdminService.getAPISubscriptions(LIMIT)).thenReturn(createSubscriptionValidationDataList());
    return subscriptionsApiService.subscriptionsGet(apiContext, apiVersion, LIMIT, null, getRequest());
}
Also used : APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)

Example 14 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class ThreatProtectionApiServiceImplTestCase method threatProtectionPoliciesGetTestCase.

@Test
public void threatProtectionPoliciesGetTestCase() throws Exception {
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
    PowerMockito.when(apiManagerFactory.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    List<ThreatProtectionPolicy> list = new ArrayList<>();
    list.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
    list.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
    list.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
    PowerMockito.when(apiMgtAdminService.getThreatProtectionPolicyList()).thenReturn(list);
    ThreatProtectionPoliciesApiServiceImpl threatProtectionApiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = threatProtectionApiService.threatProtectionPoliciesGet(getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    int apiCount = ((ThreatProtectionPolicyListDTO) response.getEntity()).getList().size();
    Assert.assertEquals(apiCount, 3);
}
Also used : Response(javax.ws.rs.core.Response) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) ArrayList(java.util.ArrayList) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with APIMgtAdminService

use of org.wso2.carbon.apimgt.core.api.APIMgtAdminService in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Retrieve subscriptions
 *
 * @param apiContext Context of the API
 * @param apiVersion API version
 * @param limit      Limit value
 * @return Subscriptions of the API
 * @throws NotFoundException If failed to retrieve subscriptions
 */
@Override
public Response subscriptionsGet(String apiContext, String apiVersion, Integer limit, String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<SubscriptionValidationData> subscriptionsOfApi;
        if (StringUtils.isEmpty(apiContext) || StringUtils.isEmpty(apiVersion)) {
            APIUtils.logDebug("API Context or version is null or empty. Retrieving subscriptions of all APIs", log);
            subscriptionsOfApi = apiMgtAdminService.getAPISubscriptions(limit);
        } else {
            subscriptionsOfApi = apiMgtAdminService.getAPISubscriptionsOfApi(apiContext, apiVersion);
        }
        SubscriptionListDTO subscriptionsList = new SubscriptionListDTO();
        subscriptionsList.setList(MappingUtil.convertToSubscriptionListDto(subscriptionsOfApi));
        return Response.ok(subscriptionsList).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions.";
        HashMap<String, String> paramList = new HashMap<String, String>();
        if (!StringUtils.isEmpty(apiContext)) {
            paramList.put(APIMgtConstants.ExceptionsConstants.API_CONTEXT, apiContext);
        }
        if (!StringUtils.isEmpty(apiVersion)) {
            paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, apiVersion);
        }
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)47 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)46 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)36 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)17 Response (javax.ws.rs.core.Response)16 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 HashMap (java.util.HashMap)10 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)9 ArrayList (java.util.ArrayList)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)6 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 Map (java.util.Map)5 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)5 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)5 Label (org.wso2.carbon.apimgt.core.models.Label)3 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)3 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)3 File (java.io.File)2