use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.
the class NewAPIVersionEmailNotifierTest method testShouldNotLoadMessageWhenErrorOccurs.
@Test
public void testShouldNotLoadMessageWhenErrorOccurs() throws RegistryException {
Resource resource = Mockito.mock(Resource.class);
NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {
@Override
protected Registry getConfigSystemRegistry(int tenantId) throws RegistryException {
return registry;
}
};
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true).thenThrow(RegistryException.class);
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
Mockito.when(resource.getContent()).thenReturn("".getBytes());
try {
NotificationDTO notification = emailNotifier.loadMessageTemplate(notificationDTO);
Assert.assertFalse(!notification.getMessage().equals(""));
emailNotifier.loadMessageTemplate(notificationDTO);
Assert.fail("Should fail with an exception");
} catch (NotificationException e) {
// exception is expection
return;
}
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.
the class NewAPIVersionEmailNotifierTest method testShouldReturnNotifiersWhenPropertiesValid.
@Test
public void testShouldReturnNotifiersWhenPropertiesValid() throws APIManagementException {
final ClaimsRetriever claimsRetriever = Mockito.mock(ClaimsRetriever.class);
final SortedMap<String, String> claims = new TreeMap<String, String>();
claims.put(NotifierConstants.EMAIL_CLAIM, "admin@wso2.com");
NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {
@Override
protected ClaimsRetriever getClaimsRetriever(String claimsRetrieverImplClass) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
return claimsRetriever;
}
};
Mockito.doNothing().when(claimsRetriever).init();
Mockito.when(claimsRetriever.getClaims(Mockito.anyString())).thenReturn(claims);
try {
Assert.assertTrue(emailNotifier.getNotifierSet(notificationDTO).size() > 0);
} catch (NotificationException e) {
Assert.fail("Should not throw any exceptions");
}
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO 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.notification.NotificationDTO in project carbon-apimgt by wso2.
the class APIProviderImpl method sendEmailNotification.
/**
* This method used to send notifications to the previous subscribers of older versions of a given API
*
* @param api
* @throws APIManagementException
*/
private void sendEmailNotification(API api) throws APIManagementException {
try {
JSONObject tenantConfig = APIUtil.getTenantConfig(tenantDomain);
String isNotificationEnabled = "false";
if (tenantConfig.containsKey(NotifierConstants.NOTIFICATIONS_ENABLED)) {
isNotificationEnabled = (String) tenantConfig.get(NotifierConstants.NOTIFICATIONS_ENABLED);
}
if (JavaUtils.isTrueExplicitly(isNotificationEnabled)) {
List<APIIdentifier> apiIdentifiers = getOldPublishedAPIList(api);
for (APIIdentifier oldAPI : apiIdentifiers) {
Properties prop = new Properties();
prop.put(NotifierConstants.API_KEY, oldAPI);
prop.put(NotifierConstants.NEW_API_KEY, api.getId());
Set<Subscriber> subscribersOfAPI = apiMgtDAO.getSubscribersOfAPI(oldAPI);
prop.put(NotifierConstants.SUBSCRIBERS_PER_API, subscribersOfAPI);
NotificationDTO notificationDTO = new NotificationDTO(prop, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
notificationDTO.setTenantID(tenantId);
notificationDTO.setTenantDomain(tenantDomain);
new NotificationExecutor().sendAsyncNotifications(notificationDTO);
}
}
} catch (NotificationException e) {
log.error(e.getMessage(), e);
}
}
Aggregations