Skip to content

Commit 780c964

Browse files
Merge pull request #869 from motech-implementations/sms-payload
sms-payload changes
2 parents ebf1d2b + 6d95316 commit 780c964

File tree

6 files changed

+401
-5
lines changed

6 files changed

+401
-5
lines changed

imi/src/main/java/org/motechproject/nms/imi/service/impl/SmsNotificationServiceImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class SmsNotificationServiceImpl implements SmsNotificationService {
5050

5151
private static final String ALERT_NAME = "Sms notification failed";
5252

53+
private static final String SMS_MESSAGE_TYPE = "imi.sms.messageType";
54+
5355
private static final Logger LOGGER = LoggerFactory.getLogger(SmsNotificationServiceImpl.class);
5456

5557
private AlertService alertService;
@@ -84,7 +86,7 @@ public boolean sendSms(Long callingNumber, Map<String, String> smsParams) {
8486
return sender.sendNotificationRequest(httpPost, HttpStatus.SC_CREATED, ALERT_ID, ALERT_NAME);
8587
}
8688

87-
private HttpPost prepareSmsRequest(Long callingNumber, Map<String, String> smsParams) {
89+
public HttpPost prepareSmsRequest(Long callingNumber, Map<String, String> smsParams) {
8890

8991
String senderId = settingsFacade.getProperty(SMS_SENDER_ID);
9092
String endpoint = settingsFacade.getProperty(SMS_NOTIFICATION_URL);
@@ -93,9 +95,14 @@ private HttpPost prepareSmsRequest(Long callingNumber, Map<String, String> smsPa
9395
String smsEntityId = smsParams.get("smsEntityId");
9496
String smsTelemarketerId = smsParams.get("smsTelemarketerId");
9597
String smsTemplateId = smsParams.get("smsTemplateId");
98+
String messageType = smsParams.get("smsMessageType");
99+
100+
LOGGER.info("senderId:{}, endpoint:{}, callbackEndpoint:{}, smsContent:{}, smsEntityId:{}, " +
101+
"smsTelemarketerId:{}, smsTemplateId:{}, messageType:{}", senderId, endpoint, callbackEndpoint,
102+
smsContent, smsEntityId, smsTelemarketerId, smsTemplateId, messageType);
96103

97104

98-
if (senderId == null || endpoint == null || smsContent == null || smsEntityId == null || smsTelemarketerId == null || smsTemplateId == null || callbackEndpoint == null) {
105+
if (senderId == null || endpoint == null || smsContent == null || smsEntityId == null || smsTelemarketerId == null || smsTemplateId == null || callbackEndpoint == null || messageType == null) {
99106

100107
Map<String, String> alertData = new HashMap<>();
101108
alertData.put(SMS_SENDER_ID, senderId);
@@ -105,6 +112,7 @@ private HttpPost prepareSmsRequest(Long callingNumber, Map<String, String> smsPa
105112
alertData.put(SMS_TELEMARKETER_ID, smsTelemarketerId);
106113
alertData.put(SMS_TEMPLATE_ID, smsTemplateId);
107114
alertData.put(CALLBACK_URL, callbackEndpoint);
115+
alertData.put(SMS_MESSAGE_TYPE,messageType);
108116

109117
LOGGER.error("Unable to find sms settings. Check IMI sms gateway settings");
110118
alertService.create("settingsFacade", "properties", "Could not get sms settings",
@@ -138,6 +146,7 @@ private HttpPost prepareSmsRequest(Long callingNumber, Map<String, String> smsPa
138146
template = template.replace("<smsTelemarketerId>", smsTelemarketerId);
139147
template = template.replace("<notificationUrl>", callbackEndpoint);
140148
template = template.replace("<correlationId>", DateTime.now().toString());
149+
template = template.replace("<messageType>", messageType);
141150

142151
LOGGER.info("Sms Template"+ template);
143152

imi/src/main/resources/smsTemplate.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"message": "<messageContent>"
99
},
1010
"clientCorrelator": "<correlationId>",
11+
"messageType": "<messageType>",
1112
"receiptRequest": {
1213
"notifyURL": "<notificationUrl>",
1314
"callbackData": ""
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package org.motechproject.nms.imi.ut;
2+
3+
import static org.mockito.Mockito.*;
4+
import static org.junit.Assert.*;
5+
6+
import org.apache.http.client.methods.HttpPost;
7+
import org.apache.http.entity.StringEntity;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.mockito.Mock;
11+
import org.mockito.MockitoAnnotations;
12+
import org.motechproject.alerts.contract.AlertService;
13+
import org.motechproject.alerts.domain.AlertStatus;
14+
import org.motechproject.alerts.domain.AlertType;
15+
import org.motechproject.nms.imi.service.SmsNotificationService;
16+
import org.motechproject.nms.imi.service.impl.SmsNotificationServiceImpl;
17+
import org.motechproject.server.config.SettingsFacade;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class SmsNotificationServiceUnitTest {
24+
@Mock
25+
private SettingsFacade settingsFacade;
26+
27+
@Mock
28+
private AlertService alertService;
29+
30+
private SmsNotificationServiceImpl smsService;
31+
32+
@Before
33+
public void setUp() {
34+
MockitoAnnotations.initMocks(this);
35+
smsService = new SmsNotificationServiceImpl(alertService, settingsFacade);
36+
}
37+
38+
@Test
39+
public void testPrepareSmsRequest_Success() throws Exception {
40+
// Arrange
41+
Long callingNumber = 1234567890L;
42+
Map<String, String> smsParams = new HashMap<>();
43+
smsParams.put("smsContent", "Test SMS Content");
44+
smsParams.put("smsEntityId", "entity123");
45+
smsParams.put("smsTelemarketerId", "telemarketer123");
46+
smsParams.put("smsTemplateId", "template123");
47+
smsParams.put("smsMessageType", "Transactional");
48+
49+
when(settingsFacade.getProperty("imi.sms.sender.id")).thenReturn("Sender123");
50+
when(settingsFacade.getProperty("imi.sms.notification.url")).thenReturn("http://example.com/endpoint/senderId");
51+
when(settingsFacade.getProperty("imi.sms.status.callback.url")).thenReturn("http://callback.url");
52+
when(settingsFacade.getProperty("imi.sms.authentication.key")).thenReturn("auth-key-123");
53+
when(settingsFacade.getRawConfig("smsTemplate.json")).thenReturn(new ByteArrayInputStream("{\n \"phoneNumber\": \"<phoneNumber>\",\n \"senderId\": \"<senderId>\",\n \"messageContent\": \"<messageContent>\",\n \"smsTemplateId\": \"<smsTemplateId>\",\n \"smsEntityId\": \"<smsEntityId>\",\n \"smsTelemarketerId\": \"<smsTelemarketerId>\",\n \"notificationUrl\": \"<notificationUrl>\",\n \"correlationId\": \"<correlationId>\",\n \"messageType\": \"<messageType>\"\n}".getBytes()));
54+
55+
// Act
56+
HttpPost request = smsService.prepareSmsRequest(callingNumber, smsParams);
57+
58+
// Assert
59+
assertNotNull(request);
60+
assertEquals("http://example.com/endpoint/Sender123", request.getURI().toString());
61+
assertEquals("application/json", request.getFirstHeader("Content-type").getValue());
62+
assertEquals("auth-key-123", request.getFirstHeader("Key").getValue());
63+
StringEntity entity = (StringEntity) request.getEntity();
64+
String entityContent = entityToString(entity);
65+
assertTrue(entityContent.contains("1234567890"));
66+
assertTrue(entityContent.contains("Sender123"));
67+
assertTrue(entityContent.contains("Test SMS Content"));
68+
assertTrue(entityContent.contains("entity123"));
69+
assertTrue(entityContent.contains("template123"));
70+
assertTrue(entityContent.contains("telemarketer123"));
71+
assertTrue(entityContent.contains("Transactional"));
72+
}
73+
74+
@Test
75+
public void testPrepareSmsRequest_MissingSettings() {
76+
// Arrange
77+
Long callingNumber = 1234567890L;
78+
Map<String, String> smsParams = new HashMap<>();
79+
smsParams.put("smsContent", "Test SMS Content");
80+
smsParams.put("smsEntityId", "entity123");
81+
smsParams.put("smsTelemarketerId", "telemarketer123");
82+
smsParams.put("smsTemplateId", "template123");
83+
smsParams.put("smsMessageType", "Transactional");
84+
85+
when(settingsFacade.getProperty("SMS_SENDER_ID")).thenReturn(null);
86+
87+
// Act
88+
HttpPost request = smsService.prepareSmsRequest(callingNumber, smsParams);
89+
90+
// Assert
91+
assertNull(request);
92+
verify(alertService).create(
93+
eq("settingsFacade"),
94+
eq("properties"),
95+
eq("Could not get sms settings"),
96+
eq(AlertType.CRITICAL),
97+
eq(AlertStatus.NEW),
98+
eq(0),
99+
anyMap()
100+
);
101+
}
102+
103+
// Utility method to convert StringEntity to String
104+
private String entityToString(StringEntity entity) throws Exception {
105+
java.io.InputStream inputStream = entity.getContent();
106+
java.util.Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
107+
return s.hasNext() ? s.next() : "";
108+
}
109+
}

mobile-academy/src/main/java/org/motechproject/nms/mobileacademy/service/impl/CourseNotificationServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class CourseNotificationServiceImpl implements CourseNotificationService
4848
private static final String ADDRESS = "address";
4949
private static final String SMS_CONTENT_PREFIX = "sms.content.";
5050
private static final String SMS_TEMPLATE_ID_PREFIX = "sms.templateId.";
51+
private static final String SMS_MESSAGE_TYPE_PREFIX = "sms.messageType.";
5152
private static final String SMS_DEFAULT_LANGUAGE_PROPERTY = "default";
5253
private static final Logger LOGGER = LoggerFactory.getLogger(CourseNotificationServiceImpl.class);
5354

@@ -265,12 +266,20 @@ private Map<String, String> buildSmsParams(Long flwId, CourseCompletionRecord cc
265266
String smsEntityId = settingsFacade.getProperty("sms.entityId.default");
266267
String smsTelemarketerId = settingsFacade.getProperty("sms.telemarketerId.default");
267268
String smsTemplateId = settingsFacade.getProperty(SMS_TEMPLATE_ID_PREFIX + smsLanguageProperty);
269+
if(smsTemplateId == null){
270+
smsTemplateId = settingsFacade.getProperty("sms.templateId.default");
271+
}
272+
String messageType = settingsFacade.getProperty(SMS_MESSAGE_TYPE_PREFIX + smsLanguageProperty);
273+
if(messageType == null){
274+
messageType = settingsFacade.getProperty("sms.messageType.default");
275+
}
268276

269277
Map<String, String> smsParams = new HashMap<String, String>();
270278
smsParams.put("smsContent", smsContent);
271279
smsParams.put("smsEntityId", smsEntityId);
272280
smsParams.put("smsTelemarketerId", smsTelemarketerId);
273281
smsParams.put("smsTemplateId", smsTemplateId);
282+
smsParams.put("smsMessageType", messageType);
274283

275284
return smsParams;
276285
}

mobile-academy/src/main/resources/sms-content.properties

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ sms.content.50 = Badhai ho didi, apne Mobile Academy course pura kar liya hai. C
99
# NATIONAL DEFAULT
1010
sms.content.default = Badhai ho didi, apne Mobile Academy course pura kar liya hai. Certificate paane ke liye is SMS ko apne block meeting mein dikhayen.\\n\\nMoHFW
1111

12-
sms.templateId.default = localhost
13-
sms.entityId.default = localhost
14-
sms.telemarketerId.default = localhost
12+
sms.templateId.default = 1007163065348946395
13+
sms.entityId.default = 1301159100860122510
14+
sms.telemarketerId.default = 1001096933494158
15+
sms.messageType.default =
16+
sms.messageType.50 = 4

0 commit comments

Comments
 (0)