|
| 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 | +} |
0 commit comments