Search in sources :

Example 36 with APIProvider

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

the class APIProviderImplTest method testGetSubscriberClaims.

@Test
public void testGetSubscriberClaims() throws APIManagementException, UserStoreException, XMLStreamException {
    String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2";
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    // Mock retrieving the tenant domain
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
    UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
    PowerMockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    PowerMockito.when(userStoreManager.isExistingUser("admin")).thenReturn(true);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    claimValues.put("claim3", "http://wso2.org/claim3");
    PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
    PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configurationService);
    PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.getFirstProperty(APIConstants.API_PUBLISHER_SUBSCRIBER_CLAIMS)).thenReturn(configuredClaims);
    Map subscriberClaims = apiProvider.getSubscriberClaims("admin");
    assertNotNull(subscriberClaims);
    assertEquals(configuredClaims.split(",").length, subscriberClaims.size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) TreeMap(java.util.TreeMap) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with APIProvider

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

the class APIProviderImplTest method testUpdateAPIProductForStateChange.

@Test
public void testUpdateAPIProductForStateChange() throws Exception {
    String provider = "admin";
    PowerMockito.mockStatic(MultitenantUtils.class);
    Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(APIConstants.SUPER_TENANT_DOMAIN);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    APIProduct apiProduct = createMockAPIProduct(provider);
    prepareForLCStateChangeOfAPIProduct(apiProvider, apiProduct);
    PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
    Organization organization = new Organization(APIConstants.SUPER_TENANT_DOMAIN);
    Mockito.when(apiPersistenceInstance.updateAPIProduct(organization, publisherAPIProduct)).thenReturn(publisherAPIProduct);
    apiProvider.updateAPIProductForStateChange(apiProduct, APIConstants.CREATED, APIConstants.PUBLISHED);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 38 with APIProvider

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

the class ApiMgtDAO method getAPIProviderByNameAndVersion.

/**
 * This method is used to get the API provider by giving API name, API version and tenant domain
 *
 * @param apiName    API name
 * @param apiVersion API version
 * @param tenant     tenant domain
 * @return API provider
 * @throws APIManagementException if failed to get the API provider by giving API name, API version, tenant domain
 */
public String getAPIProviderByNameAndVersion(String apiName, String apiVersion, String tenant) throws APIManagementException {
    if (StringUtils.isBlank(apiName) || StringUtils.isBlank(apiVersion) || StringUtils.isBlank(tenant)) {
        String msg = "API name, version, tenant cannot be null when fetching provider";
        log.error(msg);
        throw new APIManagementException(msg);
    }
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    String apiProvider = null;
    String getAPIProviderQuery = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenant)) {
            // in this case, the API should be fetched from super tenant
            getAPIProviderQuery = SQLConstants.GET_API_PROVIDER_WITH_NAME_VERSION_FOR_SUPER_TENANT;
            prepStmt = connection.prepareStatement(getAPIProviderQuery);
        } else {
            // in this case, the API should be fetched from the respective tenant
            getAPIProviderQuery = SQLConstants.GET_API_PROVIDER_WITH_NAME_VERSION_FOR_GIVEN_TENANT;
            prepStmt = connection.prepareStatement(getAPIProviderQuery);
            prepStmt.setString(3, "%" + tenant + "%");
        }
        prepStmt.setString(1, apiName);
        prepStmt.setString(2, apiVersion);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            apiProvider = rs.getString("API_PROVIDER");
        }
        if (StringUtils.isBlank(apiProvider)) {
            String msg = "Unable to find provider for API: " + apiName + " in the database";
            log.warn(msg);
        }
    } catch (SQLException e) {
        handleException("Error while locating API: " + apiName + " from the database", e);
    }
    return apiProvider;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 39 with APIProvider

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

the class APIProviderImplTest method testDeleteWorkflowTask.

@Test
public void testDeleteWorkflowTask() throws APIManagementException, WorkflowException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor apiStateChangeWFExecutor = Mockito.mock(WorkflowExecutor.class);
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateChangeWFExecutor);
    WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(workflowDTO);
    APIIdentifier identifier = new APIIdentifier("admin", "API1", "1.0.0", apiUUID);
    apiProvider.deleteWorkflowTask(identifier);
    Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 40 with APIProvider

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

the class APIProviderImplTest method testGetAPIRevisions.

/**
 * This method tests adding a new API Revision and then retrieving API Revisions by API UUID
 *
 * @throws APIManagementException
 */
@Test
public void testGetAPIRevisions() throws APIManagementException, APIPersistenceException, ArtifactSynchronizerException {
    ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
    ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
    APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
    APIRevision apiRevision = new APIRevision();
    apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    apiRevision.setDescription("test description revision 1");
    Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
    Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
    Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
    Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
    Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
    PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
    try {
        apiProvider.addAPIRevision(apiRevision, superTenantDomain);
        apiProvider.getAPIRevisions("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ArtifactSaver(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) JSONObject(org.json.simple.JSONObject) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) DuplicateAPIException(org.wso2.carbon.apimgt.api.model.DuplicateAPIException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)207 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)198 API (org.wso2.carbon.apimgt.api.model.API)92 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 Test (org.junit.Test)82 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)82 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)73 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)65 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)36 URISyntaxException (java.net.URISyntaxException)34 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)32 URI (java.net.URI)31 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)31 JSONObject (org.json.simple.JSONObject)29 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)29 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)29 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)28 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)28 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)23