1 | /* |
---|
2 | * rapidandroid - SMS gateway for the android platform |
---|
3 | * Copyright (C) 2009 Dimagi Inc., UNICEF |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation, either version 3 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | * |
---|
18 | */ |
---|
19 | package org.rapidandroid.tests; |
---|
20 | |
---|
21 | import java.io.BufferedReader; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | import java.io.StringReader; |
---|
25 | import java.text.ParseException; |
---|
26 | import java.util.Date; |
---|
27 | import java.util.StringTokenizer; |
---|
28 | import java.util.Vector; |
---|
29 | |
---|
30 | import org.rapidandroid.ModelBootstrap; |
---|
31 | import org.rapidandroid.content.translation.MessageTranslator; |
---|
32 | import org.rapidandroid.content.translation.ModelTranslator; |
---|
33 | import org.rapidandroid.content.translation.ParsedDataTranslator; |
---|
34 | import org.rapidandroid.data.RapidSmsDBConstants; |
---|
35 | import org.rapidandroid.data.SmsDbHelper; |
---|
36 | import org.rapidsms.java.core.model.Form; |
---|
37 | import org.rapidsms.java.core.model.Message; |
---|
38 | import org.rapidsms.java.core.model.Monitor; |
---|
39 | import org.rapidsms.java.core.parser.IParseResult; |
---|
40 | import org.rapidsms.java.core.parser.service.ParsingService; |
---|
41 | |
---|
42 | import android.content.ContentValues; |
---|
43 | import android.content.Context; |
---|
44 | import android.content.Intent; |
---|
45 | import android.database.Cursor; |
---|
46 | import android.net.Uri; |
---|
47 | import android.telephony.gsm.SmsManager; |
---|
48 | import android.test.AndroidTestCase; |
---|
49 | |
---|
50 | /** |
---|
51 | * @author Daniel Myung [email protected] |
---|
52 | * @created Feb 6, 2009 Summary: |
---|
53 | */ |
---|
54 | public class MessageCorpusTests extends AndroidTestCase { |
---|
55 | |
---|
56 | private String[] prefixes = null; |
---|
57 | private Form[] forms = null; |
---|
58 | private Context mContext = null; |
---|
59 | |
---|
60 | private SmsDbHelper mHelper; |
---|
61 | |
---|
62 | private void initLists() { |
---|
63 | forms = ModelTranslator.getAllForms(); |
---|
64 | prefixes = new String[forms.length]; |
---|
65 | for (int i = 0; i < forms.length; i++) { |
---|
66 | prefixes[i] = forms[i].getPrefix(); |
---|
67 | getContext().getContentResolver().delete(Uri.parse(RapidSmsDBConstants.FormData.CONTENT_URI_PREFIX |
---|
68 | + forms[i].getFormId()), null, null); |
---|
69 | |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | /* |
---|
74 | * (non-Javadoc) |
---|
75 | * |
---|
76 | * @see android.test.AndroidTestCase#setUp() |
---|
77 | */ |
---|
78 | @Override |
---|
79 | protected void setUp() throws Exception { |
---|
80 | // TODO Auto-generated method stub |
---|
81 | super.setUp(); |
---|
82 | ModelBootstrap.InitApplicationDatabase(getContext()); |
---|
83 | mHelper = new SmsDbHelper(getContext()); |
---|
84 | ModelTranslator.setDbHelper(mHelper, getContext()); |
---|
85 | |
---|
86 | initLists(); |
---|
87 | getContext().getContentResolver().delete(RapidSmsDBConstants.Message.CONTENT_URI, null, null); |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * (non-Javadoc) |
---|
92 | * |
---|
93 | * @see android.test.AndroidTestCase#tearDown() |
---|
94 | */ |
---|
95 | @Override |
---|
96 | protected void tearDown() throws Exception { |
---|
97 | // TODO Auto-generated method stub |
---|
98 | super.tearDown(); |
---|
99 | } |
---|
100 | |
---|
101 | @SuppressWarnings("finally") |
---|
102 | private Vector<String[]> readRawMessages() { |
---|
103 | String rawMessageText = loadAssetFile("testdata/rawdata.csv"); |
---|
104 | return readCsv(rawMessageText); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * @param rawMessageText |
---|
109 | * @return |
---|
110 | */ |
---|
111 | private Vector<String[]> readCsv(String rawMessageText) { |
---|
112 | StringReader sr = new StringReader(rawMessageText); |
---|
113 | BufferedReader bufRdr = new BufferedReader(sr); |
---|
114 | |
---|
115 | String line = null; |
---|
116 | int row = 0; |
---|
117 | int col = 0; |
---|
118 | Vector<String[]> lines = new Vector<String[]>(); |
---|
119 | // read each line of text file |
---|
120 | try { |
---|
121 | while ((line = bufRdr.readLine()) != null) { |
---|
122 | StringTokenizer st = new StringTokenizer(line, ","); |
---|
123 | int tokCount = st.countTokens(); |
---|
124 | |
---|
125 | String[] tokenizedLine = new String[tokCount]; |
---|
126 | int toki = 0; |
---|
127 | while (st.hasMoreTokens()) { |
---|
128 | tokenizedLine[toki] = st.nextToken(); |
---|
129 | toki++; |
---|
130 | } |
---|
131 | lines.add(tokenizedLine); |
---|
132 | } |
---|
133 | } catch (IOException e) { |
---|
134 | // TODO Auto-generated catch block |
---|
135 | e.printStackTrace(); |
---|
136 | } finally { |
---|
137 | |
---|
138 | try { |
---|
139 | sr.close(); |
---|
140 | bufRdr.close(); |
---|
141 | } catch (IOException e) { |
---|
142 | // TODO Auto-generated catch block |
---|
143 | e.printStackTrace(); |
---|
144 | } |
---|
145 | } |
---|
146 | return lines; |
---|
147 | } |
---|
148 | |
---|
149 | private Vector<String[]> readRawAnswers() { |
---|
150 | String rawMessageText = loadAssetFile("testdata/answers.csv"); |
---|
151 | return readCsv(rawMessageText); |
---|
152 | } |
---|
153 | |
---|
154 | private Form determineForm(String message) { |
---|
155 | int len = prefixes.length; |
---|
156 | for (int i = 0; i < len; i++) { |
---|
157 | String prefix = prefixes[i]; |
---|
158 | if (message.toLowerCase().trim().startsWith(prefix + " ")) { |
---|
159 | return forms[i]; |
---|
160 | } |
---|
161 | } |
---|
162 | return null; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Retains the dates in a given message |
---|
167 | * |
---|
168 | * @param phone |
---|
169 | * @param date |
---|
170 | * @param text |
---|
171 | */ |
---|
172 | private void injectMessageDirect(String phone, Date date, String text) { |
---|
173 | Uri writeMessageUri = RapidSmsDBConstants.Message.CONTENT_URI; |
---|
174 | Monitor monitor = MessageTranslator.GetMonitorAndInsertIfNew(getContext(), phone); |
---|
175 | |
---|
176 | ContentValues messageValues = new ContentValues(); |
---|
177 | messageValues.put(RapidSmsDBConstants.Message.MESSAGE, text); |
---|
178 | messageValues.put(RapidSmsDBConstants.Message.MONITOR, monitor.getID()); |
---|
179 | |
---|
180 | messageValues.put(RapidSmsDBConstants.Message.TIME, Message.SQLDateFormatter.format(date)); |
---|
181 | messageValues.put(RapidSmsDBConstants.Message.RECEIVE_TIME, Message.SQLDateFormatter.format(date)); |
---|
182 | messageValues.put(RapidSmsDBConstants.Message.IS_OUTGOING, false); |
---|
183 | |
---|
184 | Uri msgUri = null; |
---|
185 | Form mForm = determineForm(text); |
---|
186 | msgUri = getContext().getContentResolver().insert(writeMessageUri, messageValues); |
---|
187 | Vector<IParseResult> results = ParsingService.ParseMessage(mForm, text); |
---|
188 | ParsedDataTranslator.InsertFormData(getContext(), mForm, Integer.valueOf(msgUri.getPathSegments().get(1)) |
---|
189 | .intValue(), results); |
---|
190 | } |
---|
191 | |
---|
192 | private String loadAssetFile(String filename) { |
---|
193 | try { |
---|
194 | InputStream is = getContext().getAssets().open(filename); |
---|
195 | |
---|
196 | int size = is.available(); |
---|
197 | |
---|
198 | // Read the entire asset into a local byte buffer. |
---|
199 | byte[] buffer = new byte[size]; |
---|
200 | is.read(buffer); |
---|
201 | is.close(); |
---|
202 | |
---|
203 | // Convert the buffer into a Java string. |
---|
204 | String text = new String(buffer); |
---|
205 | |
---|
206 | return text; |
---|
207 | |
---|
208 | } catch (IOException e) { |
---|
209 | // Should never happen! |
---|
210 | throw new RuntimeException(e); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | private void sendMessageViaSMS(Context context, String mesg) { |
---|
215 | SmsManager smgr = SmsManager.getDefault(); |
---|
216 | Intent intent = new Intent(); |
---|
217 | smgr.sendTextMessage("5554", null, mesg, null, null); |
---|
218 | } |
---|
219 | |
---|
220 | public void testInsertDirect() { |
---|
221 | Vector<String[]> rawMessages = readRawMessages(); |
---|
222 | int len = rawMessages.size(); |
---|
223 | |
---|
224 | for (int i = 0; i < len; i++) { |
---|
225 | String[] line = rawMessages.get(i); |
---|
226 | assertEquals(3, line.length); |
---|
227 | |
---|
228 | String datestr = line[0]; |
---|
229 | |
---|
230 | Date dateval = new Date(); |
---|
231 | try { |
---|
232 | dateval = Message.SQLDateFormatter.parse(datestr); |
---|
233 | } catch (ParseException e) { |
---|
234 | // TODO Auto-generated catch block |
---|
235 | e.printStackTrace(); |
---|
236 | assertTrue(false); |
---|
237 | } |
---|
238 | String sender = line[1]; |
---|
239 | String text = line[2]; |
---|
240 | |
---|
241 | injectMessageDirect(sender, dateval, text); |
---|
242 | } |
---|
243 | |
---|
244 | Cursor cr = getContext().getContentResolver().query(RapidSmsDBConstants.Message.CONTENT_URI, null, null, null, |
---|
245 | null); |
---|
246 | assertEquals(cr.getCount(), rawMessages.size()); |
---|
247 | } |
---|
248 | |
---|
249 | // public void testInsertViaSendSMS() { |
---|
250 | // Vector<String[]> rawMessages = readRawMessages(); |
---|
251 | // int len = rawMessages.size(); |
---|
252 | // |
---|
253 | // for(int i = 0; i < len; i++) { |
---|
254 | // String[] line = rawMessages.get(i); |
---|
255 | // assertEquals(3,line.length); |
---|
256 | // String text = line[2]; |
---|
257 | // |
---|
258 | // sendMessageViaSMS(getContext(),text); |
---|
259 | // } |
---|
260 | // |
---|
261 | // Cursor cr = |
---|
262 | // getContext().getContentResolver().query(RapidSmsDBConstants.Message.CONTENT_URI, |
---|
263 | // null,null,null,null); |
---|
264 | // assertEquals(cr.getCount(),rawMessages.size()); |
---|
265 | // } |
---|
266 | |
---|
267 | public void testInsertVerifyCounts() { |
---|
268 | testInsertDirect(); |
---|
269 | Vector<String[]> rawMessages = readRawMessages(); |
---|
270 | Vector<String[]> rawAnswers = readRawAnswers(); |
---|
271 | Form mForm = null; |
---|
272 | assertEquals(rawMessages.size(), rawAnswers.size()); |
---|
273 | int len = rawMessages.size(); |
---|
274 | for (int i = 0; i < len; i++) { |
---|
275 | String[] line = rawMessages.get(i); |
---|
276 | // String[] answers = rawAnswers.get(i); |
---|
277 | String text = line[2]; |
---|
278 | mForm = determineForm(text); |
---|
279 | if (i == 0) { |
---|
280 | Cursor cr = getContext().getContentResolver() |
---|
281 | .query( |
---|
282 | Uri.parse(RapidSmsDBConstants.FormData.CONTENT_URI_PREFIX |
---|
283 | + mForm.getFormId()), null, null, null, null); |
---|
284 | assertEquals(cr.getCount(), len); |
---|
285 | cr.close(); |
---|
286 | // sorry, ridiculous, i know |
---|
287 | break; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | // now, let's parse the data itself |
---|
292 | Cursor answercursor = getContext().getContentResolver() |
---|
293 | .query( |
---|
294 | Uri.parse(RapidSmsDBConstants.FormData.CONTENT_URI_PREFIX |
---|
295 | + mForm.getFormId()), null, null, null, null); |
---|
296 | |
---|
297 | answercursor.moveToFirst(); |
---|
298 | |
---|
299 | for (int i = 0; i < len; i++) { |
---|
300 | String[] answers = rawAnswers.get((len - 1) - i); |
---|
301 | // assertEquals(answercursor.getColumnCount(), 6); |
---|
302 | |
---|
303 | // this is cuz the answer data has a leading column |
---|
304 | for (int j = 1; j < answers.length; j++) { |
---|
305 | assertEquals(answers[j].trim().toLowerCase(), answercursor.getString(j + 1)); |
---|
306 | } |
---|
307 | answercursor.moveToNext(); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | } |
---|