Search in sources :

Example 6 with GatewaySourceGenerator

use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator 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 7 with GatewaySourceGenerator

use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator 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 8 with GatewaySourceGenerator

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

the class APIPublisherImplTestCase method testAddExistingScopeToApi2.

@Test(description = "Add existing Scope to API")
public void testAddExistingScopeToApi2() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway, keyManager);
    String oldSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Scope scope = new Scope("api_delete", "api_delete");
    Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
    Mockito.when(keyManager.registerScope(scope)).thenReturn(false);
    try {
        apiPublisher.addScopeToTheApi(api.getId(), scope);
        Assert.fail();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Scope already registered");
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 9 with GatewaySourceGenerator

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

the class APIPublisherImplTestCase method testApiLevelEndpointAddWhileDbGetError.

@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class })
public void testApiLevelEndpointAddWhileDbGetError() throws APIManagementException, LifecycleException {
    /**
     * this test method verify the API Add with correct API object get invoked correctly
     */
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint").applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build();
    Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
    Map<String, Endpoint> endpointMap = new HashMap();
    endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint);
    endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, null, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint);
    Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenThrow(APIMgtDAOException.class);
    Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false);
    apiPublisher.addAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName());
}
Also used : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) WorkflowExtensionsConfigBuilder(org.wso2.carbon.apimgt.core.workflow.WorkflowExtensionsConfigBuilder) 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) 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 10 with GatewaySourceGenerator

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

the class APIPublisherImplTestCase method testUpdateAPIWithStatusUnchangedWhenContextExists.

@Test(description = "Test UpdateAPI with Status unchanged but context exist", expectedExceptions = APIManagementException.class)
public void testUpdateAPIWithStatusUnchangedWhenContextExists() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
    String uuid = api.getId();
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy apiPolicy = new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(apiPolicy);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO);
    Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.build());
    Mockito.when(apiDAO.isAPIContextExists("testContext")).thenReturn(true);
    Mockito.when(identityProvider.getRoleId(ADMIN_ROLE)).thenReturn(ADMIN_ROLE_ID);
    Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
    apiPublisher.updateAPI(api.context("testContext"));
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) 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) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) 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) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Aggregations

GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)71 Test (org.testng.annotations.Test)65 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)65 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)59 API (org.wso2.carbon.apimgt.core.models.API)59 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)34 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)34 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)34 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)28 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)28 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)24 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)22 HashMap (java.util.HashMap)20 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)20 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)17 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)16 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 FileInputStream (java.io.FileInputStream)12 Scope (org.wso2.carbon.apimgt.core.models.Scope)12 WorkflowExtensionsConfigBuilder (org.wso2.carbon.apimgt.core.workflow.WorkflowExtensionsConfigBuilder)11