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());
}
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);
}
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;
}
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);
}
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());
}
}
Aggregations