KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > sms > test > TestSmsServiceCase


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.sms.test;
6
7 import org.apache.commons.logging.Log;
8 import org.exoplatform.container.PortalContainer;
9 import org.exoplatform.services.communication.sms.SmsMonitorService;
10 import org.exoplatform.services.communication.sms.SmsService;
11 import org.exoplatform.services.communication.sms.common.*;
12 import org.exoplatform.services.communication.sms.encoder.MessageFormat;
13 import org.exoplatform.services.communication.sms.mock.MockSmsService;
14 import org.exoplatform.services.communication.sms.model.*;
15 import org.exoplatform.services.communication.sms.model.RecipientImpl;
16 import org.exoplatform.services.communication.sms.provider.Provider;
17 import org.exoplatform.services.communication.sms.provider.Sender;
18 import org.exoplatform.services.communication.sms.util.SmsUtil;
19 import org.exoplatform.services.log.LogService;
20 import org.exoplatform.test.BasicTestCase;
21
22
23 /**
24  * @author: Ove Ranheim
25  * @email: oranheim@users.sourceforge.net
26  */

27 public class TestSmsServiceCase extends BasicTestCase {
28
29     static private String JavaDoc USERNAME = "uid";
30     static private String JavaDoc PASSWORD = "pwd";
31     
32     static protected Log log_;
33     static protected SmsService service_;
34     static protected SmsMonitorService monitor_;
35     static protected Messages messages_;
36
37     public TestSmsServiceCase(String JavaDoc name) {
38         super(name);
39     }
40
41     public void setUp() throws Exception JavaDoc {
42         setTestNumber(1);
43         if (service_ == null) {
44             PortalContainer manager = PortalContainer.getInstance();
45             service_ = (SmsService) manager.getComponentInstanceOfType(MockSmsService.class);
46             monitor_ = (SmsMonitorService) manager.getComponentInstanceOfType(SmsMonitorService.class);
47             LogService logservice = (LogService) manager.getComponentInstanceOfType(LogService.class);
48             log_ = logservice.getLog("org.exoplatform.services.communication.sms.test");
49             messages_ = createTestMessage();
50         }
51     }
52     
53     protected void tearDown() throws Exception JavaDoc {
54         log_ = null;
55         service_ = null;
56         monitor_ = null;
57         messages_ = null;
58     }
59     
60     private Messages createTestMessage() {
61         RecipientImpl._identity = new Integer JavaDoc(0);
62         messages_ = service_.createMessages();
63
64         Message message = messages_.addMessage();
65         message.setFrom("123456789");
66         message.addRecipient(service_.createRecipient("123456789"));
67         message.addRecipient("987654321");
68         message.setFormat(MessageFormat.PLAIN_TEXT);
69         message.setContent("Plain text message");
70
71         message = messages_.addMessage();
72         message.setFrom("123456789");
73         message.addRecipient("2002");
74         message.setFormat(MessageFormat.UNICODE_TEXT);
75         message.setContent("Soci�t� � Responsabilit� Limit�e");
76         return messages_;
77     }
78
79     public void testNumberFormat() {
80         String JavaDoc token1 = "+33-11-44-55-66";
81         String JavaDoc token2 = "0047-227.79.933";
82         String JavaDoc token3 = "0047-227.79a933";
83         
84         token1 = SmsUtil.prepareCellularNumber(token1);
85         assertEquals(token1, "3311445566");
86         
87         token2 = SmsUtil.prepareCellularNumber(token2);
88         assertEquals(token2, "4722779933");
89         
90         boolean isAlpha = SmsUtil.isAlpha(token3);
91         assertEquals(isAlpha, true);
92     }
93     
94     public void testInitialDraft() {
95         Provider provider = service_.createProdatProvider(USERNAME, PASSWORD);
96         Sender sender = service_.createSender(provider);
97         assertEquals( messages_.getLogonStatus(), LogonStatus.NONE );
98         
99         Message m1 = messages_.getMessage(0);
100         Message m2 = messages_.getMessage(1);
101         
102         assertEquals( m1.getFormat(), MessageFormat.PLAIN_TEXT );
103         assertEquals( m2.getFormat(), MessageFormat.UNICODE_TEXT );
104         
105         Recipient r1 = (Recipient) m1.getRecipients().get(0);
106         Recipient r2 = (Recipient) m1.getRecipients().get(1);
107         Recipient r3 = (Recipient) m2.getRecipients().get(0);
108         
109         log_.info(r1.getStatus());
110         assertEquals( r1.getStatus(), MessageStatus.DRAFT );
111         assertEquals( r2.getStatus(), MessageStatus.DRAFT );
112         assertEquals( r3.getStatus(), MessageStatus.DRAFT );
113     }
114
115
116     public void testPreparePending() {
117         try {
118             Provider provider = service_.createProdatProvider(USERNAME, PASSWORD);
119             Sender sender = service_.createSender(provider);
120             sender.prepare(messages_);
121             assertEquals( messages_.getLogonStatus(), LogonStatus.NONE );
122             
123             Message m1 = messages_.getMessage(0);
124             Message m2 = messages_.getMessage(1);
125             
126             assertEquals( m1.getFormat(), MessageFormat.PLAIN_TEXT );
127             assertEquals( m2.getFormat(), MessageFormat.UNICODE_TEXT );
128             
129             Recipient r1 = (Recipient) m1.getRecipients().get(0);
130             Recipient r2 = (Recipient) m1.getRecipients().get(1);
131             Recipient r3 = (Recipient) m2.getRecipients().get(0);
132             
133             log_.info(r1.getStatus());
134             assertEquals( r1.getStatus(), MessageStatus.PENDING );
135             assertEquals( r2.getStatus(), MessageStatus.PENDING );
136             assertEquals( r3.getStatus(), MessageStatus.PENDING );
137             
138         } catch (RequestException re) {
139             re.printStackTrace();
140         } catch (ConvertException e) {
141             e.printStackTrace();
142         }
143     }
144
145
146     public void testPerformSending() {
147         try {
148             Provider provider = service_.createProdatProvider(USERNAME, PASSWORD);
149             Sender sender = service_.createSender(provider);
150             sender.prepare(messages_);
151             sender.send();
152             assertEquals( messages_.getLogonStatus(), LogonStatus.SUCCESS );
153             
154             Message m1 = messages_.getMessage(0);
155             Message m2 = messages_.getMessage(1);
156             
157             assertEquals( m1.getFormat(), MessageFormat.PLAIN_TEXT );
158             assertEquals( m2.getFormat(), MessageFormat.UNICODE_TEXT );
159             
160             Recipient r1 = (Recipient) m1.getRecipients().get(0);
161             Recipient r2 = (Recipient) m1.getRecipients().get(1);
162             Recipient r3 = (Recipient) m2.getRecipients().get(0);
163             
164             log_.info(r1.getStatus());
165             assertEquals( r1.getStatus(), MessageStatus.COMPLETE );
166             assertEquals( r2.getStatus(), MessageStatus.COMPLETE );
167             assertEquals( r3.getStatus(), MessageStatus.FAILURE );
168             
169             if (messages_.hasErrorOccured())
170                 log_.info("Recipient 1 in Message 2 failed due to: " + r3.getError());
171             
172         } catch (RequestException re) {
173             re.printStackTrace();
174         } catch (ConvertException e) {
175             e.printStackTrace();
176         } catch (CommunicationError e) {
177             e.printStackTrace();
178         } catch (ResponseException e) {
179             e.printStackTrace();
180         }
181     }
182     
183     public void testMonitorService() {
184         if (this.getTestNumber() == 1) {
185             assertEquals( monitor_.getCountMessages(), new Integer JavaDoc(3) );
186             assertEquals( monitor_.getSuccessfullMessages(), new Integer JavaDoc(2) );
187             assertEquals( monitor_.getFailedMessages(), new Integer JavaDoc(1) );
188         }
189     }
190
191 }
Popular Tags