Search in sources :

Example 26 with APIMgtAdminService

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

the class ResourcesApiServiceImpl method resourcesGet.

@Override
public Response resourcesGet(String apiContext, String apiVersion, String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<UriTemplate> resourcesOfApi = new ArrayList<>();
        if (!StringUtils.isEmpty(apiContext) && !StringUtils.isEmpty(apiVersion)) {
            resourcesOfApi = apiMgtAdminService.getAllResourcesForApi(apiContext, apiVersion);
        }
        ResourcesListDTO resourcesListDTO = new ResourcesListDTO();
        resourcesListDTO.setList(MappingUtil.convertToResourceListDto(resourcesOfApi));
        return Response.ok(resourcesListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving resources.";
        Map<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);
        }
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) Map(java.util.Map) ResourcesListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ResourcesListDTO)

Example 27 with APIMgtAdminService

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

the class ThreatProtectionPoliciesApiServiceImpl method threatProtectionPoliciesGet.

/**
 * Get a list of all threat protection policies
 *
 * @param request
 * @return List of threat protection policies
 * @throws NotFoundException
 */
@Override
public Response threatProtectionPoliciesGet(Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<ThreatProtectionPolicy> policyList = apiMgtAdminService.getThreatProtectionPolicyList();
        ThreatProtectionPolicyListDTO listDTO = new ThreatProtectionPolicyListDTO();
        for (ThreatProtectionPolicy policy : policyList) {
            listDTO.addListItem(MappingUtil.toThreatProtectionPolicyDTO(policy));
        }
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
    }
    return Response.status(500).entity("Internal Server Error.").build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 28 with APIMgtAdminService

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

the class ApisApiServiceImplTestCase method apisGetApisNullStatusTestCase.

@Test
public void apisGetApisNullStatusTestCase() throws Exception {
    String labels = "ZONE_ONE,ZONE_TWO";
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Response response = apisApiService.apisGet(labels, null, getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with APIMgtAdminService

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

the class ApisApiServiceImplTestCase method apisGetTestCase.

@Test
public void apisGetTestCase() throws Exception {
    String labels = "ZONE_ONE,ZONE_TWO";
    String[] gatewayLabels = labels.split(",");
    List<String> labelList = new ArrayList<String>(Arrays.asList(gatewayLabels));
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    List<API> apiList = new ArrayList<>();
    apiList.add(SampleTestObjectCreator.createUniqueAPI().build());
    apiList.add(SampleTestObjectCreator.createUniqueAPI().build());
    apiList.add(SampleTestObjectCreator.createUniqueAPI().build());
    Mockito.when(apiMgtAdminService.getAPIsByStatus(labelList, "Published")).thenReturn(apiList);
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Response response = apisApiService.apisGet(labels, "Published", getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Integer count = ((APIListDTO) response.getEntity()).getCount();
    Assert.assertEquals(count.intValue(), 3);
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with APIMgtAdminService

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

the class ApisApiServiceImplTestCase method apisGetApisNullLabelsTestCase.

@Test
public void apisGetApisNullLabelsTestCase() throws Exception {
    String labels = null;
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Response response = apisApiService.apisGet(labels, "Published", getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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