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