Search in sources :

Example 31 with APIGateway

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

the class APIPublisherImplTestCase method testUpdateNullAPI.

@Test(description = "Test UpdateAPI with null api returned by uuid", expectedExceptions = APIManagementException.class)
public void testUpdateNullAPI() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
    String uuid = api.getId();
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(null);
    apiPublisher.updateAPI(api);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 32 with APIGateway

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

the class APIPublisherImplTestCase method testAddUpdateAPIFromWSDLFile.

@Test(description = "Test adding an API from WSDL file")
public void testAddUpdateAPIFromWSDLFile() throws APIManagementException, LifecycleException, IOException {
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap());
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APILifecycleManager apiLifecycleManager = getDefaultMockedAPILifecycleManager();
    PolicyDAO policyDAO = getDefaultMockedPolicyDAO();
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
    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.addAPIFromWSDLFile(apiBuilder, SampleTestObjectCreator.createDefaultWSDL11ContentInputStream(), true);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiDAO, Mockito.times(1)).addOrUpdateWSDL(apiBuilder.getId(), IOUtils.toByteArray(SampleTestObjectCreator.createDefaultWSDL11ContentInputStream()), USER);
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
    apiPublisher.updateAPIWSDL(apiBuilder.getId(), SampleTestObjectCreator.createAlternativeWSDL11ContentInputStream());
    Mockito.verify(apiDAO, Mockito.times(1)).addOrUpdateWSDL(apiBuilder.getId(), IOUtils.toByteArray(SampleTestObjectCreator.createAlternativeWSDL11ContentInputStream()), USER);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 33 with APIGateway

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

the class APIPublisherImplTestCase method testAddApi.

@Test(description = "Test add api with production endpoint")
public void testAddApi() throws APIManagementException, LifecycleException {
    /**
     * this test method verify the API Add with correct API object get invoked correctly
     */
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(SampleTestObjectCreator.getMockEndpointMap());
    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);
    // Error path
    // When an APIMgtDAOException is being thrown when the API is created
    Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).addAPI(apiBuilder.build());
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while creating the API - " + apiBuilder.getName());
    }
    // Error path
    // When an GatewayException is being thrown when an error occurred while adding API to the Gateway
    Mockito.doThrow(GatewayException.class).when(gateway).addAPI(apiBuilder.build());
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while adding API - " + apiBuilder.getName() + " to gateway");
    }
    // Error path
    // When an APITemplateException is being thrown when generating API configuration for API
    Mockito.when(gatewaySourceGenerator.getConfigStringFromTemplate(Mockito.any())).thenThrow(APITemplateException.class);
    try {
        apiPublisher.addAPI(apiBuilder);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error generating API configuration for API " + apiBuilder.getName());
    }
}
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) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) 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) Test(org.testng.annotations.Test)

Example 34 with APIGateway

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

the class APIPublisherImplTestCase method testAddApiDefinitionErrorIOException.

@Test(description = "Test IO exception when adding api definition from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorIOException() throws APIManagementException, LifecycleException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
    Mockito.doThrow(new IOException()).when(httpURLConnection).getResponseCode();
    apiPublisher.addApiFromDefinition(httpURLConnection);
    Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) HttpURLConnection(java.net.HttpURLConnection) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) IOException(java.io.IOException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 35 with APIGateway

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

the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.

@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
    Map<String, Endpoint> endpointMap = new HashMap<>();
    Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
    uriTemplateBuilder.endpoint(resourceEndpointMap);
    uriTemplateBuilder.templateId("getApisApiIdGet");
    uriTemplateBuilder.uriTemplate("/apis/");
    uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
    uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
    uriTemplateBuilder.httpVerb("GET");
    uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.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);
    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 : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) 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) Test(org.testng.annotations.Test)

Aggregations

APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)135 Test (org.testng.annotations.Test)129 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)100 API (org.wso2.carbon.apimgt.core.models.API)87 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)73 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)59 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)55 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)48 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)38 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)35 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)32 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)31 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)29 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)28 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)28 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)26 HashMap (java.util.HashMap)22 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)22 BeforeTest (org.testng.annotations.BeforeTest)19 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)19