Skip to content
Permalink
Browse files
Retained JMS Configuration
  • Loading branch information
ebenezergraham committed Jun 24, 2019
1 parent 286c314 commit 6b0cd7aacdfe4776ec5df59ccef17b7259c0d978
Showing 17 changed files with 198 additions and 35 deletions.
@@ -59,4 +59,7 @@ public interface NotificationEventConstants {

String SELECTOR_SEND_EMAIL_NOTIFICATION = SELECTOR_NAME + " = '" + SEND_EMAIL_NOTIFICATION + "'";
String SELECTOR_SEND_SMS_NOTIFICATION = SELECTOR_NAME + " = '" + SEND_SMS_NOTIFICATION + "'";

String TEST = "jms-test";
String SELECTOR_TEST = SELECTOR_NAME + " = '" + TEST + "'";
}
@@ -53,8 +53,9 @@ public class AbstractNotificationTest extends SuiteTestEnvironment {
public final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer);
public static final String LOGGER_NAME = "test-logger";
public static final String TEST_USER = "homer";
public static final String TEST_ADDRESS = "egraham15@alustudent.com";
public static final String TEST_ADDRESS = "egraham15@alustudent.com"; // Replace with developer's dummy testing email
public static final String TEST_TEMPLATE= "sample";
public static final String SMS_TEST_NUMBER= "+23058409206"; // Replace with developers dummy testing number

@SuppressWarnings("WeakerAccess")
@Autowired
@@ -54,7 +54,6 @@ public void shouldSendAnEmail() throws InterruptedException,IOException {
TEST_ADDRESS,
TEST_TEMPLATE,
null);
//Assert.assertTrue(this.eventRecorder.wait(NotificationEventConstants.SEND_EMAIL_NOTIFICATION,TEST_ADDRESS ));
}

@Test
@@ -81,8 +80,6 @@ public void shouldSendFormattedEmail() throws InterruptedException, IOException
TEST_TEMPLATE,
templateVariables
);

//Assert.assertTrue(this.eventRecorder.wait(NotificationEventConstants.SEND_EMAIL_NOTIFICATION,TEST_ADDRESS ));
}

@Test(expected = NotFoundException.class)
@@ -114,6 +111,17 @@ public void checkEmailConfigurationEntityExist() throws InterruptedException {
Assert.assertTrue(this.emailService.emailConfigurationExists(emailConfiguration.getIdentifier()));
}

@Test
public void deleteConfiguration() throws InterruptedException {
logger.info("Delete Email configuration");

notificationManager.createEmailConfiguration(emailConfiguration);
Assert.assertTrue(eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier()));

notificationManager.deleteEmailConfiguration(emailConfiguration.getIdentifier());
Assert.assertTrue(eventRecorder.wait(NotificationEventConstants.DELETE_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier()));
}

@Test
public void shouldFindDefaultGateway() {
this.logger.info("Find Active Gateway");
@@ -84,8 +84,19 @@ public void checkIfConfigurationAlreadyExists() throws InterruptedException{
@Test
public void shouldSendAnSMS(){
this.logger.info("Send SMS Notification");
String to = this.notificationService.sendSMS("+23058409206",
int to = this.notificationService.sendSMS(SMS_TEST_NUMBER,
"Dear Valued Customer\n\nTalk is cheap show me the code\n\nBest Regards\nYour MFI");
Assert.assertNotNull(to);
}

@Test
public void deleteConfiguration() throws InterruptedException {
logger.info("Delete SMS configuration");

notificationManager.createSMSConfiguration(smsConfiguration);
Assert.assertTrue(eventRecorder.wait(NotificationEventConstants.POST_SMS_CONFIGURATION, smsConfiguration.getIdentifier()));

notificationManager.deleteSMSConfiguration(smsConfiguration.getIdentifier());
Assert.assertTrue(eventRecorder.wait(NotificationEventConstants.DELETE_SMS_CONFIGURATION, smsConfiguration.getIdentifier()));
}
}
@@ -108,6 +108,7 @@ public void onSendSmsTrigger(@Header(TenantHeaderFilter.TENANT_HEADER) final Str
public void onDeleteEmailConfiguration(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant,
final String payload) {
this.eventRecorder.event(tenant, NotificationEventConstants.DELETE_EMAIL_CONFIGURATION, payload, String.class);
logger.info("onDeleteEmailConfiguration received");
}

@JmsListener(
@@ -148,6 +149,6 @@ public void onUpdateSmsConfiguration(@Header(TenantHeaderFilter.TENANT_HEADER) f
public void onPostTemplate(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant,
final String payload) {
this.eventRecorder.event(tenant, NotificationEventConstants.POST_TEMPLATE, payload, String.class);
}
}

}
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.cn.notification.listener;

import org.apache.fineract.cn.lang.config.TenantHeaderFilter;
import org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants;
import org.apache.fineract.cn.notification.service.ServiceConstants;
import org.apache.fineract.cn.test.listener.EventRecorder;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import static org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants.SELECTOR_TEST;
import static org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants.TEST;

@SuppressWarnings("unused")
@Component
public class EventListener {
private final EventRecorder eventRecorder;
private final Logger logger;


@Autowired
public EventListener(final EventRecorder eventRecorder,
@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger) {
this.logger = logger;
this.eventRecorder = eventRecorder;
}
static int counter = 0;
@JmsListener(
destination = TEST,
selector = SELECTOR_TEST
)
public void jmsTest(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant,
final String payload) {
counter++;
logger.info("Received {}",counter);
this.eventRecorder.event(tenant, NotificationEventConstants.TEST, payload, String.class);
}
}
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.cn.notification.listener;

import org.apache.fineract.cn.notification.AbstractNotificationTest;
import org.apache.fineract.cn.notification.service.internal.service.EventHelper;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

import static org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants.TEST;

public class TestJMS extends AbstractNotificationTest {

@Autowired
EventHelper eventHelper;


public TestJMS() {
super();
}

@Test
public void testJMSConcurrency() throws InterruptedException,IOException {
this.logger.info("Send JMS event");
eventHelper.sendEvent(TEST,"test-tenant","payload");
Assert.assertTrue(eventRecorder.wait(TEST,"payload"));
}
}
@@ -63,7 +63,7 @@ public String process(final CreateEmailConfigurationCommand createEmailConfigura
public String process(final UpdateEmailConfigurationCommand updateEmailConfigurationCommand) {
EmailConfiguration emailConfiguration = updateEmailConfigurationCommand.getEmailConfiguration();
final EmailGatewayConfigurationEntity newEntity = EmailConfigurationMapper.map(emailConfiguration);
this.emailGatewayConfigurationRepository.deleteEmailGatewayConfigurationEntityBy(newEntity.getIdentifier());
this.emailGatewayConfigurationRepository.deleteEmailGatewayConfigurationEntityByIdentifier(newEntity.getIdentifier());
this.emailGatewayConfigurationRepository.save(newEntity);

return emailConfiguration.getIdentifier();
@@ -73,7 +73,7 @@ public String process(final UpdateEmailConfigurationCommand updateEmailConfigura
@Transactional
@EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.DELETE_EMAIL_CONFIGURATION)
public String process(final DeleteEmailConfigurationCommand deleteEmailConfigurationCommand) {
this.emailGatewayConfigurationRepository.deleteEmailGatewayConfigurationEntityBy(deleteEmailConfigurationCommand.getIdentifier());
this.emailGatewayConfigurationRepository.deleteEmailGatewayConfigurationEntityByIdentifier(deleteEmailConfigurationCommand.getIdentifier());
return deleteEmailConfigurationCommand.getIdentifier();
}
}
@@ -60,7 +60,7 @@ public String process(final CreateSMSConfigurationCommand createSMSConfiguration
@Transactional
public String process(final UpdateSMSConfigurationCommand updateSMSConfigurationCommand) {
final SMSGatewayConfigurationEntity newEntity = SMSConfigurationMapper.map(updateSMSConfigurationCommand.getSMSConfiguration());
this.smsGatewayConfigurationRepository.deleteSMSGatewayConfigurationEntityBy(newEntity.getIdentifier());
this.smsGatewayConfigurationRepository.deleteSMSGatewayConfigurationEntityByIdentifier(newEntity.getIdentifier());
this.smsGatewayConfigurationRepository.save(newEntity);

return newEntity.getIdentifier();
@@ -70,7 +70,7 @@ public String process(final UpdateSMSConfigurationCommand updateSMSConfiguration
@Transactional
@EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.DELETE_SMS_CONFIGURATION)
public String process(final DeleteSMSConfigurationCommand deleteSMSConfigurationCommand) {
smsGatewayConfigurationRepository.deleteSMSGatewayConfigurationEntityBy(deleteSMSConfigurationCommand.getIdentifier());
smsGatewayConfigurationRepository.deleteSMSGatewayConfigurationEntityByIdentifier(deleteSMSConfigurationCommand.getIdentifier());
return deleteSMSConfigurationCommand.getIdentifier();
}
}
@@ -18,13 +18,16 @@
*/
package org.apache.fineract.cn.notification.service.internal.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.apache.fineract.cn.anubis.config.EnableAnubis;
import org.apache.fineract.cn.async.config.EnableAsync;
import org.apache.fineract.cn.cassandra.config.EnableCassandra;
import org.apache.fineract.cn.command.config.EnableCommandProcessing;
import org.apache.fineract.cn.customer.api.v1.client.CustomerManager;
import org.apache.fineract.cn.identity.api.v1.client.IdentityManager;
import org.apache.fineract.cn.lang.ApplicationName;
import org.apache.fineract.cn.lang.config.EnableServiceException;
import org.apache.fineract.cn.lang.config.EnableTenantContext;
import org.apache.fineract.cn.mariadb.config.EnableMariaDB;
@@ -99,19 +102,37 @@ public void configurePathMatch(final PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(Boolean.FALSE);
}

@Bean
public PooledConnectionFactory jmsFactory() {
PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(this.environment.getProperty("activemq.brokerUrl", "vm://localhost?broker.persistent=false"));
pooledConnectionFactory.setConnectionFactory(activeMQConnectionFactory);
return pooledConnectionFactory;
}

@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(PooledConnectionFactory jmsFactory) {
final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(jmsFactory);
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
factory.setConnectionFactory(jmsFactory);
factory.setErrorHandler(ex -> {
loggerBean().warn(ex.getCause().toString());
loggerBean().error(ex.getCause().toString());
});
factory.setConcurrency(this.environment.getProperty("activemq.concurrency","1-1"));
factory.setConcurrency(this.environment.getProperty("activemq.concurrency", "1"));
return factory;
}

@Bean
public JmsTemplate jmsTemplate(ApplicationName applicationName, PooledConnectionFactory jmsFactory) {
ActiveMQTopic activeMQTopic = new ActiveMQTopic(applicationName.toString());
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setConnectionFactory(jmsFactory);
jmsTemplate.setDefaultDestination(activeMQTopic);
return jmsTemplate;
}

@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
@@ -120,7 +141,7 @@ public SpringTemplateEngine springTemplateEngine() {
}

@Bean
public SpringResourceTemplateResolver htmlTemplateResolver(){
public SpringResourceTemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
emailTemplateResolver.setPrefix("classpath:/templates/");
emailTemplateResolver.setSuffix(".html");
@@ -35,5 +35,6 @@ public interface EmailGatewayConfigurationRepository extends JpaRepository<Email
@Query("SELECT entity FROM EmailGatewayConfigurationEntity entity WHERE entity.identifier='DEFAULT'")
Optional<EmailGatewayConfigurationEntity> defaultGateway();

void deleteEmailGatewayConfigurationEntityBy(String identifier);
void deleteEmailGatewayConfigurationEntityByIdentifier(String identifier);

}
@@ -35,5 +35,5 @@ public interface SMSGatewayConfigurationRepository extends JpaRepository<SMSGate
@Query("SELECT entity FROM SMSGatewayConfigurationEntity entity WHERE entity.identifier='DEFAULT'")
Optional<SMSGatewayConfigurationEntity> defaultGateway();

void deleteSMSGatewayConfigurationEntityBy(String identifier);
void deleteSMSGatewayConfigurationEntityByIdentifier(String identifier);
}
@@ -18,7 +18,6 @@
*/
package org.apache.fineract.cn.notification.service.internal.service;

import org.apache.fineract.cn.command.annotation.Aggregate;
import org.apache.fineract.cn.command.annotation.CommandHandler;
import org.apache.fineract.cn.command.annotation.CommandLogLevel;
import org.apache.fineract.cn.command.annotation.EventEmitter;
@@ -46,7 +45,6 @@
import java.util.Properties;

@Component
@Aggregate
public class EmailService {

private final EmailGatewayConfigurationRepository emailGatewayConfigurationRepository;
@@ -113,27 +111,21 @@ private boolean setNewConfiguration(EmailConfiguration configuration) {
return false;
}

@CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO)
@Transactional
@EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.SEND_EMAIL_NOTIFICATION)
public String sendPlainEmail(String to, String subject, String message) {
SimpleMailMessage mail = new SimpleMailMessage();

try {
mail.setTo(to);
mail.setSubject(subject);
mail.setText(message);
//this.mailSender.send(mail);
this.mailSender.send(mail);
return to;
} catch (MailException exception) {
logger.debug("Caused by:" + exception.getCause().toString());
}
return to;
}

@CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO)
@Transactional
@EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.SEND_EMAIL_NOTIFICATION)
public String sendFormattedEmail(String to,
String subject,
Map<String, Object> message,
@@ -146,7 +138,7 @@ public String sendFormattedEmail(String to,
messageHelper.setText(content, true);
};
try {
//this.mailSender.send(messagePreparator);
this.mailSender.send(messagePreparator);
return to;
} catch (MailException e) {
logger.error("Failed to send Formatted email{}", e.getMessage());
@@ -60,7 +60,7 @@ public Optional<Customer> findCustomer(final String customerIdentifier, String t
return customerService.findCustomer(customerIdentifier);
}

public String sendSMS(String receiver, String template) {
public int sendSMS(String receiver, String template) {
if (!this.smsService.isConfigured) this.smsService.configureServiceWithDefaultGateway();
return this.smsService.sendSMS(receiver, template);
}

0 comments on commit 6b0cd7a

Please sign in to comment.