/protocols/ss7/isup/isup-impl/src/test/java/org/mobicents/protocols/ss7/isup/impl/stack/timers/EventTestHarness.java

http://mobicents.googlecode.com/ · Java · 449 lines · 207 code · 62 blank · 180 comment · 46 complexity · 4f9eae9bf8a3073db255c50b120bc163 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.ss7.isup.impl.stack.timers;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.fail;
  25. import java.io.ByteArrayOutputStream;
  26. import java.io.IOException;
  27. import java.nio.ByteBuffer;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import java.util.Properties;
  31. import org.mobicents.protocols.ss7.isup.ISUPEvent;
  32. import org.mobicents.protocols.ss7.isup.ISUPListener;
  33. import org.mobicents.protocols.ss7.isup.ISUPProvider;
  34. import org.mobicents.protocols.ss7.isup.ISUPStack;
  35. import org.mobicents.protocols.ss7.isup.ISUPTimeoutEvent;
  36. import org.mobicents.protocols.ss7.isup.ParameterException;
  37. import org.mobicents.protocols.ss7.isup.impl.CircuitManagerImpl;
  38. import org.mobicents.protocols.ss7.isup.impl.ISUPStackImpl;
  39. import org.mobicents.protocols.ss7.isup.impl.message.AbstractISUPMessage;
  40. import org.mobicents.protocols.ss7.isup.message.ISUPMessage;
  41. import org.mobicents.protocols.ss7.mtp.Mtp3;
  42. import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
  43. import org.mobicents.protocols.ss7.mtp.Mtp3UserPart;
  44. import org.mobicents.protocols.ss7.mtp.Mtp3UserPartBaseImpl;
  45. /**
  46. * @author baranowb
  47. *
  48. */
  49. public abstract class EventTestHarness /*extends TestCase*/ implements ISUPListener {
  50. protected ISUPStack stack;
  51. protected ISUPProvider provider;
  52. protected TimerTestMtp3UserPart userPart;
  53. // events received by by this listener
  54. protected List<EventReceived> localEventsReceived;
  55. // events sent to remote ISUP peer.
  56. protected List<EventReceived> remoteEventsReceived;
  57. public void setUp() throws Exception {
  58. this.userPart = new TimerTestMtp3UserPart();
  59. this.userPart.start();
  60. this.stack = new ISUPStackImpl();
  61. this.stack.configure(getSpecificConfig());
  62. this.provider = this.stack.getIsupProvider();
  63. this.provider.addListener(this);
  64. this.stack.setMtp3UserPart(this.userPart);
  65. CircuitManagerImpl cm = new CircuitManagerImpl();
  66. cm.addCircuit(1, 1);
  67. this.stack.setCircuitManager(cm);
  68. this.stack.start();
  69. localEventsReceived = new ArrayList<EventTestHarness.EventReceived>();
  70. remoteEventsReceived = new ArrayList<EventTestHarness.EventReceived>();
  71. }
  72. public void tearDown() throws Exception {
  73. //this is done in tests
  74. //this.stack.stop();
  75. }
  76. protected void compareEvents(List<EventReceived> expectedLocalEvents, List<EventReceived> expectedRemoteEventsReceived) {
  77. if (expectedLocalEvents.size() != this.localEventsReceived.size()) {
  78. fail("Size of local events: " + this.localEventsReceived.size() + ", does not equal expected events: " + expectedLocalEvents.size()+"\n"+
  79. doStringCompare(localEventsReceived, expectedLocalEvents));
  80. }
  81. if (expectedRemoteEventsReceived.size() != this.remoteEventsReceived.size()) {
  82. fail("Size of remote events: " + this.remoteEventsReceived.size() + ", does not equal expected events: " + expectedRemoteEventsReceived.size()+"\n"+
  83. doStringCompare(remoteEventsReceived, expectedRemoteEventsReceived));
  84. }
  85. for (int index = 0; index < expectedLocalEvents.size(); index++) {
  86. assertEquals(localEventsReceived.get(index),expectedLocalEvents.get(index), "Local received event does not match, index[" + index + "]");
  87. }
  88. for (int index = 0; index < expectedLocalEvents.size(); index++) {
  89. assertEquals( remoteEventsReceived.get(index),expectedRemoteEventsReceived.get(index), "Remote received event does not match, index[" + index + "]");
  90. }
  91. }
  92. protected String doStringCompare(List lst1,List lst2)
  93. {
  94. StringBuilder sb = new StringBuilder();
  95. int size1 = lst1.size();
  96. int size2 = lst2.size();
  97. int count = size1;
  98. if(count<size2)
  99. {
  100. count = size2;
  101. }
  102. for(int index = 0;count>index;index++)
  103. {
  104. String s1 = size1>index ? lst1.get(index).toString() : "NOP";
  105. String s2 = size2>index ? lst2.get(index).toString() : "NOP";
  106. sb.append(s1).append(" - ").append(s2).append("\n");
  107. }
  108. return sb.toString();
  109. }
  110. public void onEvent(ISUPEvent event) {
  111. this.localEventsReceived.add(new MessageEventReceived(System.currentTimeMillis(), event));
  112. }
  113. public void onTimeout(ISUPTimeoutEvent event) {
  114. this.localEventsReceived.add(new TimeoutEventReceived(System.currentTimeMillis(), event));
  115. }
  116. // method implemented by test, to answer stack.
  117. protected void doAnswer() {
  118. ISUPMessage answer = getAnswer();
  119. int opc = 1;
  120. int dpc = 2;
  121. int si = Mtp3._SI_SERVICE_ISUP;
  122. int ni = 2;
  123. int sls = 3;
  124. // int ssi = ni << 2;
  125. // ByteArrayOutputStream bout = new ByteArrayOutputStream();
  126. // // encoding routing label
  127. // bout.write((byte) (((ssi & 0x0F) << 4) | (si & 0x0F)));
  128. // bout.write((byte) dpc);
  129. // bout.write((byte) (((dpc >> 8) & 0x3F) | ((opc & 0x03) << 6)));
  130. // bout.write((byte) (opc >> 2));
  131. // bout.write((byte) (((opc >> 10) & 0x0F) | ((sls & 0x0F) << 4)));
  132. try {
  133. byte[] message = ((AbstractISUPMessage) answer).encode();
  134. // bout.write(message);
  135. // byte[] msg = bout.toByteArray();
  136. // this.userPart.toRead.add(msg);
  137. Mtp3TransferPrimitive mtpMsg = new Mtp3TransferPrimitive(si, ni, 0, opc, dpc, sls, message);
  138. this.userPart.sendTransferMessageToLocalUser(mtpMsg, mtpMsg.getSls());
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. fail("Failed on receive message: " + e);
  142. }
  143. }
  144. protected void doWait(long t) throws InterruptedException {
  145. Thread.currentThread().sleep(t);
  146. }
  147. /**
  148. * return orignial request
  149. * @return
  150. */
  151. protected abstract ISUPMessage getRequest();
  152. /**
  153. * return answer to be sent.
  154. * @return
  155. */
  156. protected abstract ISUPMessage getAnswer();
  157. /**
  158. * callback method, it returns specific configuration properties for stack
  159. *
  160. * @return
  161. */
  162. protected abstract Properties getSpecificConfig();
  163. protected class EventReceived {
  164. private long tstamp;
  165. /**
  166. * @param tstamp
  167. */
  168. public EventReceived(long tstamp) {
  169. super();
  170. this.tstamp = tstamp;
  171. }
  172. public boolean equals(Object obj) {
  173. if (this == obj)
  174. return true;
  175. if (obj == null)
  176. return false;
  177. if (getClass() != obj.getClass())
  178. return false;
  179. EventReceived other = (EventReceived) obj;
  180. if (tstamp != other.tstamp) {
  181. // we have some tolerance
  182. if (other.tstamp - 100 < tstamp || other.tstamp + 100 > tstamp) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. }
  191. protected class MessageEventReceived extends EventReceived {
  192. private ISUPEvent event;
  193. /**
  194. * @param tstamp
  195. */
  196. public MessageEventReceived(long tstamp, ISUPEvent event) {
  197. super(tstamp);
  198. this.event = event;
  199. }
  200. public boolean equals(Object obj) {
  201. if (this == obj)
  202. return true;
  203. if (!super.equals(obj))
  204. return false;
  205. if (getClass() != obj.getClass())
  206. return false;
  207. MessageEventReceived other = (MessageEventReceived) obj;
  208. if (event == null) {
  209. if (other.event != null)
  210. return false;
  211. // FIXME: use event equal?
  212. } else if (event.getMessage().getMessageType().getCode() != other.event.getMessage().getMessageType().getCode())
  213. return false;
  214. return true;
  215. }
  216. public String toString() {
  217. return "MessageEventReceived [event=" + event + ", tstamp= " + super.tstamp + "]";
  218. }
  219. }
  220. protected class TimeoutEventReceived extends EventReceived {
  221. private ISUPTimeoutEvent event;
  222. public TimeoutEventReceived(long tstamp, ISUPTimeoutEvent event) {
  223. super(tstamp);
  224. this.event = event;
  225. }
  226. public boolean equals(Object obj) {
  227. if (this == obj)
  228. return true;
  229. if (!super.equals(obj))
  230. return false;
  231. if (getClass() != obj.getClass())
  232. return false;
  233. TimeoutEventReceived other = (TimeoutEventReceived) obj;
  234. if (event == null) {
  235. if (other.event != null)
  236. return false;
  237. // FIXME: use event equal?
  238. } else if (event.getMessage().getMessageType().getCode() != other.event.getMessage().getMessageType().getCode()) {
  239. return false;
  240. } else if (event.getTimerId() != other.event.getTimerId()) {
  241. return false;
  242. }
  243. return true;
  244. }
  245. public String toString() {
  246. return "TimeoutEventReceived [event=" + event + ", tstamp= " + super.tstamp + "]";
  247. }
  248. }
  249. private class TimerTestMtp3UserPart extends Mtp3UserPartBaseImpl
  250. {
  251. public void sendTransferMessageToLocalUser(Mtp3TransferPrimitive msg, int seqControl) {
  252. super.sendTransferMessageToLocalUser(msg, seqControl);
  253. }
  254. @Override
  255. public void sendMessage(Mtp3TransferPrimitive mtpMsg) throws IOException {
  256. // here we have to parse ISUPMsg and store in receivedRemote
  257. long tstamp = System.currentTimeMillis();
  258. byte[] payload = mtpMsg.getData();
  259. int commandCode = payload[2];
  260. AbstractISUPMessage msg = (AbstractISUPMessage) provider.getMessageFactory().createCommand(commandCode);
  261. try {
  262. msg.decode(payload, provider.getParameterFactory());
  263. MessageEventReceived event = new MessageEventReceived(tstamp, new ISUPEvent(provider, msg));
  264. remoteEventsReceived.add(event);
  265. } catch (ParameterException e) {
  266. e.printStackTrace();
  267. fail("Failed on message write: " + e);
  268. }
  269. }
  270. // private ArrayList<byte[]> toRead = new ArrayList();
  271. //// /* (non-Javadoc)
  272. //// * @see org.mobicents.protocols.ss7.mtp.Mtp3UserPart#execute()
  273. //// */
  274. //// @Override
  275. //// public void execute() throws IOException {
  276. ////
  277. ////
  278. //// }
  279. //
  280. // /* (non-Javadoc)
  281. // * @see org.mobicents.protocols.ss7.mtp.Mtp3UserPart#read(java.nio.ByteBuffer)
  282. // */
  283. // @Override
  284. // public int read(ByteBuffer arg0) throws IOException {
  285. // if(toRead.size()>0)
  286. // {
  287. // byte[] data = toRead.remove(0);
  288. // arg0.put(data);
  289. // return data.length;
  290. // }
  291. // return 0;
  292. // }
  293. //
  294. // /* (non-Javadoc)
  295. // * @see org.mobicents.protocols.ss7.mtp.Mtp3UserPart#write(java.nio.ByteBuffer)
  296. // */
  297. // @Override
  298. // public int write(ByteBuffer arg0) throws IOException {
  299. // // here we have to parse ISUPMsg and store in receivedRemote
  300. // long tstamp = System.currentTimeMillis();
  301. // byte[] msu = new byte[arg0.limit()];
  302. // arg0.get(msu);
  303. // //arg0.s
  304. // // FIXME: change this, dont copy over and over.
  305. // int commandCode = msu[7];// 3(RL) + 1(SI)+2(CIC) -
  306. // // http://pt.com/page/tutorials/ss7-tutorial/mtp
  307. // byte[] payload = new byte[msu.length - 5];
  308. // System.arraycopy(msu, 5, payload, 0, payload.length);
  309. // // for post processing
  310. // AbstractISUPMessage msg = (AbstractISUPMessage) provider.getMessageFactory().createCommand(commandCode);
  311. // try {
  312. // msg.decode(payload, provider.getParameterFactory());
  313. // MessageEventReceived event = new MessageEventReceived(tstamp, new ISUPEvent(provider, msg));
  314. // remoteEventsReceived.add(event);
  315. // return msu.length;
  316. // } catch (ParameterException e) {
  317. // e.printStackTrace();
  318. // fail("Failed on message write: " + e);
  319. // }
  320. // return 0;
  321. // }
  322. //
  323. // public void print(StringBuffer sb, int leftPad, int descPad) {
  324. // // left pad
  325. // FormatterHelp.createPad(sb, leftPad);
  326. //
  327. // // Add name
  328. // sb.append(this.linksetName);
  329. //
  330. // // check if length is less than Link.NAME_SIZE, add padding
  331. // if (this.linksetName.length() < Linkset.NAME_SIZE) {
  332. // FormatterHelp.createPad(sb, Linkset.NAME_SIZE - this.linksetName.length());
  333. // }
  334. //
  335. // // add desc padding
  336. // FormatterHelp.createPad(sb, descPad);
  337. //
  338. // // type is dahdi
  339. // sb.append("dahdi");
  340. //
  341. // // add desc padding
  342. // FormatterHelp.createPad(sb, descPad);
  343. //
  344. // // add opc
  345. // sb.append(LINKSET_OPC).append(FormatterHelp.EQUAL_SIGN).append(this.opc);
  346. //
  347. // // opc can be max 8 (ANSI is max 24bits) digits. Add padding if its not
  348. // int length = (Integer.toString(this.opc).length());
  349. // if (length < 8) {
  350. // FormatterHelp.createPad(sb, 8 - length);
  351. // }
  352. //
  353. // // add desc padding
  354. // FormatterHelp.createPad(sb, descPad);
  355. //
  356. // // add apc
  357. // sb.append(LINKSET_APC).append(FormatterHelp.EQUAL_SIGN).append(this.apc);
  358. //
  359. // // opc can be max 8 (ANSI is max 24bits) digits. Add padding if its not
  360. // length = (Integer.toString(this.apc).length());
  361. // if (length < 8) {
  362. // FormatterHelp.createPad(sb, 8 - length);
  363. // }
  364. //
  365. // // add desc padding
  366. // FormatterHelp.createPad(sb, descPad);
  367. //
  368. // // add NI
  369. // sb.append(LINKSET_NI).append(FormatterHelp.EQUAL_SIGN).append(this.ni);
  370. //
  371. // // add desc padding
  372. // FormatterHelp.createPad(sb, descPad);
  373. //
  374. // // add state
  375. // sb.append(LINKSET_STATE).append(FormatterHelp.EQUAL_SIGN).append(FormatterHelp.getLinksetState(this.state));
  376. //
  377. // sb.append(FormatterHelp.NEW_LINE);
  378. //
  379. // for (FastMap.Entry<String, Link> e = this.links.head(), end = this.links.tail(); (e = e.getNext()) != end;) {
  380. // Link link = e.getValue();
  381. // link.print(sb, 4, 2);
  382. // sb.append(FormatterHelp.NEW_LINE);
  383. // }
  384. //
  385. // }
  386. }
  387. }