summaryrefslogtreecommitdiff
path: root/orktrack/src/net/sf/oreka/orktrack/test/PortTest.java
blob: 2c16784953476444a10b22ef9b430046a761358f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

package net.sf.oreka.orktrack.test;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.oreka.Direction;
import net.sf.oreka.HibernateManager;
import net.sf.oreka.orktrack.OrkTrack;
import net.sf.oreka.orktrack.PortManager;
import net.sf.oreka.orktrack.ProgramManager;
import net.sf.oreka.orktrack.ServiceManager;
import net.sf.oreka.orktrack.messages.MetadataMessage;
import net.sf.oreka.orktrack.messages.TapeMessage;
import net.sf.oreka.persistent.LoginString;
import net.sf.oreka.persistent.RecProgram;
import net.sf.oreka.persistent.RecSegment;
import net.sf.oreka.persistent.RecTape;
import net.sf.oreka.persistent.Service;
import net.sf.oreka.persistent.User;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class PortTest extends TestCase {
	
	public static void main (String[] args) {
		junit.textui.TestRunner.run (suite());
	}
	protected void setUp() throws Exception {
		OrkTrack.initialize("C:/oreka/log4j.properties", "C:/oreka/hsqldb.hbm.xml", "C:/oreka/orktrack.config.xml");
	}
	public static Test suite() {
		return new TestSuite(PortTest.class);
	}

//	=== commented out because touches private elements	
//	public void testAreTogether() {
//		
//		Port port = new Port();
//		
//		long now = new Date().getTime();
//		RecTape tape = new RecTape();
//		tape.setTimestamp(new Date(now));
//		tape.setDuration(10000);
//		
//		RecSegment seg = new RecSegment();
//		seg.setTimestamp(new Date(now));
//		seg.setDuration(10000);
//		
//		assertTrue(port.areTogether(tape, seg));
//		
//		tape.setTimestamp(new Date(now-2000));
//		assertTrue(port.areTogether(tape, seg));
//		
//		tape.setTimestamp(new Date(now-4000));
//		assertFalse(port.areTogether(tape, seg));
//		
//		tape.setTimestamp(new Date(now));
//		seg.setDuration(12000);
//		assertTrue(port.areTogether(tape, seg));
//		
//		seg.setDuration(14000);
//		assertFalse(port.areTogether(tape, seg));
//		
//		tape.setTimestamp(new Date(now));
//		tape.setDuration(10000);
//		seg.setTimestamp(new Date(now+5000));
//		seg.setDuration(5000);
//		assertTrue(port.areTogether(tape, seg));
//		
//		seg.setDuration(8000);
//		assertFalse(port.areTogether(tape, seg));
//		
//	}
	
//=== commented out because touches private elements	
//	public void testFindAndAssociateTape()  throws Exception {
//		
//		Session hbnSession = HibernateManager.getSession();
//		Transaction tx = hbnSession.beginTransaction();
//		
//		// case 1, a segment arrives after a tape
//		Port port = new Port();
//		RecTape tape1 = new RecTape();		// older tape
//		RecTape tape2 = new RecTape();		// newer tape
//		RecTape fooTape = new RecTape();	// this is the one
//		long now = new Date().getTime();
//		fooTape.setTimestamp(new Date(now));
//		tape1.setTimestamp(new Date(now-10000));
//		tape2.setTimestamp(new Date(now+10000));
//		fooTape.setDuration(10000);
//		tape1.setDuration(10000);
//		tape2.setDuration(10000);
//		
//		port.recTapes.addFirst(tape1);
//		port.recTapes.addFirst(fooTape);
//		port.recTapes.addFirst(tape2);
//
//		RecSegment seg = new RecSegment();
//		seg.setTimestamp(new Date(now));
//		seg.setDuration(5000);
//		port.findAndAssociateTape(seg, hbnSession);
//		
//		assertTrue(seg.getRecTape() == fooTape);
//		assertTrue(seg.getRecTapeOffset() == 0);
//		
//		// case 2, a second segment arrives and is part of the same tape
//		RecSegment seg2 = new RecSegment();
//		seg2.setTimestamp(new Date(now+4000));
//		seg2.setDuration(5000);
//		port.findAndAssociateTape(seg2, hbnSession);
//		
//		assertTrue(seg2.getRecTape() == fooTape);
//		assertTrue(seg2.getRecTapeOffset() == 4000);
//		
//		hbnSession.close();
//	}
	
//	public void testTapeMessage() throws Exception {	
//		
//		// Generate start and stop messages
//		TapeMessage startMsg = new TapeMessage();
//		startMsg.setCapturePort("port1");
//		startMsg.setService("service1");
//		startMsg.setStage(TapeMessage.CaptureStage.start);
//		long startTime = new Date().getTime();
//		int startTimestamp = (int)(startTime/1000);
//		startMsg.setTimestamp(startTimestamp);
//		
//		startMsg.process();
//		
//		TapeMessage stopMsg = new TapeMessage();
//		stopMsg.setCapturePort("port1");
//		stopMsg.setDirection(Direction.in);
//		stopMsg.setFilename("test.wav");
//		stopMsg.setLocalEntryPoint("dnis1");
//		stopMsg.setLocalParty("9833");
//		stopMsg.setRemoteParty("514-425-5678");
//		stopMsg.setService("service1");
//		stopMsg.setStage(TapeMessage.CaptureStage.stop);
//		stopMsg.setTimestamp(startTimestamp + 10);
//		
//		stopMsg.process();
//		
//		// verify database entities.
//		Session hbnSession = HibernateManager.getSession();
//		Transaction tx = hbnSession.beginTransaction();
//		long time = ((long)startTimestamp)*1000;
//		GregorianCalendar cal = new GregorianCalendar();
//		cal.setTimeInMillis(time);
//			
//		RecSegment seg = null;
//		Iterator segments = hbnSession.createQuery(
//	    	"from RecSegment as seg where seg.timestamp=:date")
//	    	.setCalendar("date", cal)
//	    	.list()
//	    	.iterator();
//		if(segments.hasNext()) {
//			seg = (RecSegment)segments.next();
//			assertTrue(seg.getLocalParty().equals("9833"));
//			
//			RecTape tape = seg.getRecTape();
//			assertTrue(tape.getFilename().equals("test.wav"));
//		}
//		else {
//			fail();
//		}
//		tx.commit();
//		hbnSession.close();
//	}
	
	public void testTapeAndMetadataMessage() throws Exception {
		
		Session hbnSession = HibernateManager.getSession();
		Transaction tx = hbnSession.beginTransaction();
		Service recService = ServiceManager.retrieveOrCreate("recservice", hbnSession);
		Service ctiService = ServiceManager.retrieveOrCreate("ctiservice", hbnSession);		
		ctiService.setRecordMaster(true);
		hbnSession.save(recService);
		
		User user = new User();
		user.setFirstname("salisse");
		LoginString ls = new LoginString();
		ls.setUser(user);
		ls.setLoginString("1973");
		hbnSession.save(user);
		hbnSession.save(ls);
		
		PortManager.instance().addPort("recport", "ctiport", hbnSession);
		
		// create program that does not filter anything
		RecProgram prog1 = new RecProgram();
		hbnSession.save(prog1);
		ProgramManager.instance().addProgram(prog1);
		
		tx.commit();
		hbnSession.close();
		

		
		// Generate tape start and stop messages
		TapeMessage startMsg = new TapeMessage();
		startMsg.setCapturePort("recport");
		startMsg.setService("recservice");
		startMsg.setStage(TapeMessage.CaptureStage.start);
		long startTime = new Date().getTime();
		int startTimestamp = (int)(startTime/1000);
		startMsg.setTimestamp(startTimestamp);
		
		startMsg.process();
		
		TapeMessage stopMsg = new TapeMessage();
		stopMsg.setCapturePort("recport");
		stopMsg.setService("recservice");
		stopMsg.setDirection(Direction.IN);
		stopMsg.setFilename("test.wav");
		stopMsg.setLocalEntryPoint("dnis1");
		stopMsg.setLocalParty("9833");
		stopMsg.setRemoteParty("514-425-5678");
		stopMsg.setStage(TapeMessage.CaptureStage.stop);
		stopMsg.setTimestamp(startTimestamp + 10);
		
		stopMsg.process();
		
		// Generate metadata start and stop messages
		MetadataMessage mdStartMsg = new MetadataMessage();
		mdStartMsg.setStage(TapeMessage.CaptureStage.start);
		mdStartMsg.setTimestamp(startTimestamp + 3);
		mdStartMsg.setCapturePort("ctiport");
		mdStartMsg.setService("ctiservice");
		
		mdStartMsg.process();
		
		MetadataMessage mdStopMsg = new MetadataMessage();
		mdStopMsg.setStage(TapeMessage.CaptureStage.stop);
		mdStopMsg.setLocalParty("1973");
		mdStopMsg.setTimestamp(startTimestamp + 5);
		mdStopMsg.setCapturePort("ctiport");
		mdStopMsg.setService("ctiservice");
		
		mdStopMsg.process();
		
		// verify database entities.
		hbnSession = HibernateManager.getSession();
		tx = hbnSession.beginTransaction();
		long time = ((long)(startTimestamp+3))*1000;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTimeInMillis(time);
			
		RecSegment seg = null;
		Iterator segments = hbnSession.createQuery(
	    	"from RecSegment as seg where seg.timestamp=:date")
	    	.setCalendar("date", cal)
	    	.list()
	    	.iterator();
		if(segments.hasNext()) {
			seg = (RecSegment)segments.next();
			assertTrue(seg.getLocalParty().equals("1973"));
			assertTrue(seg.getRecTapeOffset() == 3000);
			
			RecTape tape = seg.getRecTape();
			assertTrue(tape.getFilename().equals("test.wav"));
			
			User user2 = seg.getUser();
			assertTrue(user.getId() == user2.getId());
		}
		else {
			fail();
		}
		tx.commit();
		hbnSession.close();
	}
	
//	public void testUserManager() throws Exception {
//		
//		Session hbnSession = HibernateManager.getSession();
//		Transaction tx = hbnSession.beginTransaction();
//		User user = new User();
//		user.setFirstname("salisse");
//		LoginString ls = new LoginString();
//		ls.setUser(user);
//		ls.setLoginString("4568");
//		hbnSession.save(user);
//		hbnSession.save(ls);
//		User user2 = UserManager.getByLoginString("4568", hbnSession);
//		assertTrue(user.getFirstname().equals(user2.getFirstname()));
//		tx.commit();
//		hbnSession.close();
//	}
}