use of org.wso2.carbon.apimgt.impl.TestUtils.mockRegistryAndUserRealm in project carbon-apimgt by wso2.
the class APIProviderImplTest method init.
@Before
public void init() throws Exception {
System.setProperty("carbon.home", APIProviderImplTest.class.getResource("/").getFile());
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.mockStatic(GatewayArtifactsMgtDAO.class);
PowerMockito.mockStatic(ScopesDAO.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(RegistryUtils.class);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.mockStatic(WorkflowExecutorFactory.class);
PowerMockito.mockStatic(LifecycleBeanPopulator.class);
PowerMockito.mockStatic(KeyManagerHolder.class);
PowerMockito.mockStatic(Caching.class);
PowerMockito.mockStatic(PaginationContext.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(APIGatewayManager.class);
PowerMockito.mockStatic(CertificateManagerImpl.class);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
apimgtDAO = Mockito.mock(ApiMgtDAO.class);
gatewayArtifactsMgtDAO = Mockito.mock(GatewayArtifactsMgtDAO.class);
scopesDAO = Mockito.mock(ScopesDAO.class);
keyManager = Mockito.mock(KeyManager.class);
apiPersistenceInstance = Mockito.mock(APIPersistence.class);
certificateManager = Mockito.mock(CertificateManagerImpl.class);
Mockito.when(keyManager.getResourceByApiId(Mockito.anyString())).thenReturn(null);
Mockito.when(keyManager.registerNewResource(Mockito.any(API.class), Mockito.any(Map.class))).thenReturn(true);
KeyManagerDto keyManagerDto = new KeyManagerDto();
keyManagerDto.setName("default");
keyManagerDto.setKeyManager(keyManager);
keyManagerDto.setIssuer("https://localhost");
Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
tenantKeyManagerDtoMap.put("default", keyManagerDto);
PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
PowerMockito.when(CertificateManagerImpl.getInstance()).thenReturn(certificateManager);
PowerMockito.when(APIUtil.isAPIManagementEnabled()).thenReturn(false);
PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn("admin");
Mockito.when(APIUtil.replaceEmailDomain(Mockito.anyString())).thenReturn("admin");
PrivilegedCarbonContext prcontext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prcontext);
PowerMockito.doNothing().when(prcontext).setUsername(Mockito.anyString());
PowerMockito.doNothing().when(prcontext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
artifactManager = Mockito.mock(GenericArtifactManager.class);
registry = Mockito.mock(Registry.class);
PowerMockito.when(APIUtil.getArtifactManager(any(Registry.class), Mockito.anyString())).thenReturn(artifactManager);
artifact = Mockito.mock(GenericArtifact.class);
gatewayManager = Mockito.mock(APIGatewayManager.class);
Mockito.when(APIGatewayManager.getInstance()).thenReturn(gatewayManager);
TestUtils.mockRegistryAndUserRealm(-1234);
TestUtils.mockAPICacheClearence();
TestUtils.mockAPIMConfiguration();
mockDocumentationCreation();
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
Mockito.when(config.getApiRecommendationEnvironment()).thenReturn(null);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
TestUtils.initConfigurationContextService(true);
superTenantDomain = "carbon.super";
}
use of org.wso2.carbon.apimgt.impl.TestUtils.mockRegistryAndUserRealm 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
}
Aggregations