Search in sources :

Example 1 with APIManagerFactory

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

the class ThreatProtectionPoliciesApiServiceImplTestCase method testThreatProtectionPoliciesPost.

@Test
public void testThreatProtectionPoliciesPost() throws Exception {
    PowerMockito.mockStatic(APIManagerFactory.class);
    APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
    Mockito.when(apiManagerFactory.getAPIMgtAdminService()).thenReturn(adminService);
    ThreatProtectionPolicyDTO dto = createThreatProtectionPolicyDTO();
    ThreatProtectionPoliciesApiServiceImpl apiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = apiService.threatProtectionPoliciesPost(dto, getRequest());
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    // Error path
    Mockito.doThrow(APIManagementException.class).when(adminService).addThreatProtectionPolicy(Mockito.any());
    response = apiService.threatProtectionPoliciesPost(createThreatProtectionPolicyDTO(), getRequest());
    Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with APIManagerFactory

use of org.wso2.carbon.apimgt.core.impl.APIManagerFactory 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 3 with APIManagerFactory

use of org.wso2.carbon.apimgt.core.impl.APIManagerFactory 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 4 with APIManagerFactory

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

the class ThreatProtectionApiServiceImplTestCase method threatProtectionPoliciesGetExceptionTestCase.

@Test
public void threatProtectionPoliciesGetExceptionTestCase() throws Exception {
    PowerMockito.mockStatic(APIManagerFactory.class);
    APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
    APIManagementException apiManagementException = new APIManagementException("Error");
    PowerMockito.when(apiManagerFactory.getAPIMgtAdminService()).thenThrow(apiManagementException);
    ThreatProtectionPoliciesApiServiceImpl threatProtectionApiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = threatProtectionApiService.threatProtectionPoliciesGet(getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with APIManagerFactory

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

the class ApiStoreSdkGenerationManagerTestCase method testGenerateSdkForApi.

@Test
public void testGenerateSdkForApi() throws APIManagementException, ApiStoreSdkGenerationException {
    String apiId = UUID.randomUUID().toString();
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(swaggerPetStore);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    String pathToZip = sdkGenerationManager.generateSdkForApi(apiId, LANGUAGE, USER);
    File sdkZipFile = new File(pathToZip);
    Assert.assertTrue(sdkZipFile.exists() && sdkZipFile.length() > MIN_SDK_SIZE);
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)27 Response (javax.ws.rs.core.Response)23 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)17 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)6 ArrayList (java.util.ArrayList)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 API (org.wso2.carbon.apimgt.core.models.API)5 Request (org.wso2.msf4j.Request)4 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)3 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)3 APIManagerFactory (org.wso2.carbon.apimgt.impl.APIManagerFactory)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)2 File (java.io.File)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1