use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class ApiPurgeTest method testOrganizationRemoval.
@Test
public void testOrganizationRemoval() throws APIManagementException, APIPersistenceException {
PowerMockito.mockStatic(OrganizationPurgeDAO.class);
PowerMockito.when(OrganizationPurgeDAO.getInstance()).thenReturn(organizationPurgeDAO);
PowerMockito.mockStatic(GatewayArtifactsMgtDAO.class);
PowerMockito.when(GatewayArtifactsMgtDAO.getInstance()).thenReturn(gatewayArtifactsMgtDAO);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true);
APIIdentifier api = Mockito.mock(APIIdentifier.class);
ArrayList<APIIdentifier> apiIdentifierList = new ArrayList<>();
apiIdentifierList.add(api);
Mockito.doReturn(apiIdentifierList).when(organizationPurgeDAO).getAPIIdList("testOrg");
Mockito.doNothing().when(organizationPurgeDAO).deleteOrganizationAPIList(Mockito.any());
Mockito.doNothing().when(gatewayArtifactsMgtDAO).removeOrganizationGatewayArtifacts(Mockito.any());
Mockito.doNothing().when(apiPersistenceInstance).deleteAllAPIs(any(Organization.class));
Mockito.doReturn(true).when(organizationPurgeDAO).apiOrganizationExist(Mockito.anyString());
ApiPurge apiPurge = new ApiPurgeWrapper(apiPersistenceInstance);
LinkedHashMap<String, String> subtaskResult = apiPurge.purge("testOrg");
for (Map.Entry<String, String> entry : subtaskResult.entrySet()) {
Assert.assertEquals(entry.getKey() + " is not successful", APIConstants.OrganizationDeletion.COMPLETED, entry.getValue());
}
Mockito.verify(organizationPurgeDAO, Mockito.times(1)).getAPIIdList("testOrg");
Mockito.verify(organizationPurgeDAO, Mockito.times(1)).deleteOrganizationAPIList(Mockito.any());
Mockito.verify(gatewayArtifactsMgtDAO, Mockito.times(1)).removeOrganizationGatewayArtifacts(Mockito.any());
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testGetVerbInfoDTOFromAPIData.
/*
* Test method for getVerbInfoDTOFromAPIData()
* */
@Test
public void testGetVerbInfoDTOFromAPIData() throws Exception {
String context = "/";
String apiVersion = "1.0";
String requestPath = "/menu";
String httpMethod = "GET";
VerbInfoDTO verbDTO = getDefaultVerbInfoDTO();
verbDTO.setRequestKey("//1.0/:https");
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), verbDTO);
// If isAPIResourceValidationEnabled==true
apiKeyValidator.setGatewayAPIResourceValidationEnabled(true);
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
VerbInfoDTO verbInfoDTO1 = new VerbInfoDTO();
verbInfoDTO1.setHttpVerb("get");
Mockito.when(APIUtil.getAPIInfoDTOCacheKey("", "1.0")).thenReturn("abc");
Mockito.when((VerbInfoDTO) CacheProvider.getResourceCache().get("abc")).thenReturn(verbInfoDTO1);
MessageContext messageContext = Mockito.mock(MessageContext.class);
VerbInfoDTO verbInfoDTOFromAPIData = apiKeyValidator.getVerbInfoDTOFromAPIData(messageContext, context, apiVersion, requestPath, httpMethod);
Assert.assertEquals("", verbDTO, verbInfoDTOFromAPIData);
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetPoliciesExcludingUnAuthenticatedTier.
@Test
public void testGetPoliciesExcludingUnAuthenticatedTier() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException, XMLStreamException {
SubscriptionPolicy[] policies3 = { new SubscriptionPolicy("policy4"), new SubscriptionPolicy("policy5"), new SubscriptionPolicy("policy6"), new SubscriptionPolicy(APIConstants.UNAUTHENTICATED_TIER), new SubscriptionPolicy(APIConstants.UNLIMITED_TIER) };
PowerMockito.mockStatic(APIUtil.class);
BDDMockito.when(APIUtil.getTenantId(Mockito.anyString())).thenReturn(-1234);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies3);
ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(false);
Assert.assertEquals(3, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_SUB).length);
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIProviderImplTest method testEmailSentWhenPropergateAPIStatusChangeToGateways.
@Test
public void testEmailSentWhenPropergateAPIStatusChangeToGateways() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setOrganization("carbon.super");
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
TestUtils.mockRegistryAndUserRealm(-1);
Resource resource = PowerMockito.mock(Resource.class);
JSONObject tenantConfig = PowerMockito.mock(JSONObject.class);
NotificationExecutor notificationExecutor = PowerMockito.mock(NotificationExecutor.class);
NotificationDTO notificationDTO = PowerMockito.mock(NotificationDTO.class);
UserRegistry configRegistry = PowerMockito.mock(UserRegistry.class);
RegistryService registryService = PowerMockito.mock(RegistryService.class);
Mockito.when(apimgtDAO.addAPI(api, -1, "testOrg")).thenReturn(1);
Mockito.doNothing().when(apimgtDAO).addURITemplates(1, api, -1);
Mockito.doNothing().when(keyManager).attachResourceScopes(api, api.getUriTemplates());
Mockito.when(artifactManager.newGovernanceArtifact(Matchers.any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Map<String, String> failedToPubGWEnv = new HashMap<String, String>();
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()).thenReturn(configurationService);
PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
PowerMockito.when(apiManagerConfiguration.getApiRecommendationEnvironment()).thenReturn(null);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
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.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
apiProvider.addAPI(api);
PowerMockito.when(APIUtil.replaceEmailDomain(apiId.getProviderName())).thenReturn("admin");
PowerMockito.when(APIUtil.replaceEmailDomainBack(api.getId().getProviderName())).thenReturn("admin");
PowerMockito.when(apimgtDAO.getPublishedDefaultVersion(api.getId())).thenReturn("1.0.0");
// Change to PUBLISHED state
// Existing APIs of the provider
API api1 = new API(new APIIdentifier("admin", "API1", "0.0.5"));
api1.setStatus(APIConstants.PUBLISHED);
API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
prepareForGetAPIsByProvider(artifactManager, apiProvider, "admin", api1, api2);
PowerMockito.when(registryService.getConfigSystemRegistry(-1)).thenReturn(configRegistry);
Mockito.when(resource.getContent()).thenReturn(getTenantConfigContent());
PowerMockito.when(tenantConfig.get(NotifierConstants.NOTIFICATIONS_ENABLED)).thenReturn("true");
PowerMockito.when(tenantConfig.get(APIConstants.EXTENSION_HANDLER_POSITION)).thenReturn("bottom");
PowerMockito.whenNew(NotificationDTO.class).withAnyArguments().thenReturn(notificationDTO);
PowerMockito.whenNew(NotificationExecutor.class).withAnyArguments().thenReturn(notificationExecutor);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(registry);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
apiProvider.propergateAPIStatusChangeToGateways(APIConstants.PUBLISHED, api);
// Mockito.verify(notificationExecutor).sendAsyncNotifications(notificationDTO); Not valid. notification logic moved outside
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAllPaginatedAPIs.
@Test
public void testGetAllPaginatedAPIs() throws RegistryException, UserStoreException, APIManagementException, XMLStreamException {
APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
API api1 = new API(apiId1);
api1.setContext("/test");
APIIdentifier apiId2 = new APIIdentifier("admin", "API2", "1.0.0");
API api2 = new API(apiId2);
api2.setContext("/test1");
PaginationContext paginationCtx = Mockito.mock(PaginationContext.class);
PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationCtx);
Mockito.when(paginationCtx.getLength()).thenReturn(2);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry userReg = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(userReg);
PowerMockito.when(APIUtil.getArtifactManager(userReg, APIConstants.API_KEY)).thenReturn(artifactManager);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_APIS_PER_PAGE)).thenReturn("2");
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tm = Mockito.mock(TenantManager.class);
PowerMockito.when(sh.getRealmService()).thenReturn(realmService);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tm);
PowerMockito.when(tm.getTenantId("carbon.super")).thenReturn(-1234);
GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
List<GovernanceArtifact> governanceArtifacts = new ArrayList<GovernanceArtifact>();
governanceArtifacts.add(genericArtifact1);
governanceArtifacts.add(genericArtifact2);
List<GovernanceArtifact> governanceArtifacts1 = new ArrayList<GovernanceArtifact>();
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.anyMap(), any(Registry.class), Mockito.anyString())).thenReturn(governanceArtifacts, governanceArtifacts1);
Map<String, Object> result = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
List<API> apiList = (List<API>) result.get("apis");
Assert.assertEquals(2, apiList.size());
Assert.assertEquals("API1", apiList.get(0).getId().getApiName());
Assert.assertEquals("API2", apiList.get(1).getId().getApiName());
Assert.assertEquals(2, result.get("totalLength"));
// No APIs available
Map<String, Object> result1 = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
List<API> apiList1 = (List<API>) result1.get("apis");
Assert.assertEquals(0, apiList1.size());
// Registry Exception while retrieving artifacts
Mockito.when(artifactManager.findGenericArtifacts(Matchers.anyMap())).thenThrow(RegistryException.class);
try {
apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
} catch (APIManagementException e) {
Assert.assertEquals("Failed to get all APIs", e.getMessage());
}
}
Aggregations