PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/testsuite/tests/src/test/java/org/mobicents/slee/resources/diameter/tests/factories/RoFactoriesTest.java

http://mobicents.googlecode.com/
Java | 431 lines | 295 code | 75 blank | 61 comment | 21 complexity | 12a489d25e85009f5bfd26c502ab0d15 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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.slee.resources.diameter.tests.factories;
  23. import static org.jdiameter.client.impl.helpers.Parameters.AcctApplId;
  24. import static org.jdiameter.client.impl.helpers.Parameters.ApplicationId;
  25. import static org.jdiameter.client.impl.helpers.Parameters.Assembler;
  26. import static org.jdiameter.client.impl.helpers.Parameters.AuthApplId;
  27. import static org.jdiameter.client.impl.helpers.Parameters.OwnDiameterURI;
  28. import static org.jdiameter.client.impl.helpers.Parameters.OwnRealm;
  29. import static org.jdiameter.client.impl.helpers.Parameters.OwnVendorID;
  30. import static org.jdiameter.client.impl.helpers.Parameters.PeerName;
  31. import static org.jdiameter.client.impl.helpers.Parameters.PeerRating;
  32. import static org.jdiameter.client.impl.helpers.Parameters.PeerTable;
  33. import static org.jdiameter.client.impl.helpers.Parameters.RealmEntry;
  34. import static org.jdiameter.client.impl.helpers.Parameters.RealmTable;
  35. import static org.jdiameter.client.impl.helpers.Parameters.VendorId;
  36. import static org.jdiameter.server.impl.helpers.Parameters.RealmEntryExpTime;
  37. import static org.jdiameter.server.impl.helpers.Parameters.RealmEntryIsDynamic;
  38. import static org.jdiameter.server.impl.helpers.Parameters.RealmHosts;
  39. import static org.jdiameter.server.impl.helpers.Parameters.RealmLocalAction;
  40. import static org.jdiameter.server.impl.helpers.Parameters.RealmName;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertNull;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.mobicents.slee.resources.diameter.tests.factories.BaseFactoriesTest.*;
  46. import net.java.slee.resource.diameter.base.events.avp.DiameterIdentity;
  47. import net.java.slee.resource.diameter.cca.events.avp.CcRequestType;
  48. import net.java.slee.resource.diameter.ro.RoAvpFactory;
  49. import net.java.slee.resource.diameter.ro.RoClientSessionActivity;
  50. import net.java.slee.resource.diameter.ro.RoMessageFactory;
  51. import net.java.slee.resource.diameter.ro.RoServerSessionActivity;
  52. import net.java.slee.resource.diameter.ro.events.RoCreditControlAnswer;
  53. import net.java.slee.resource.diameter.ro.events.RoCreditControlRequest;
  54. import org.jdiameter.api.Answer;
  55. import org.jdiameter.api.ApplicationId;
  56. import org.jdiameter.api.IllegalDiameterStateException;
  57. import org.jdiameter.api.InternalException;
  58. import org.jdiameter.api.OverloadException;
  59. import org.jdiameter.api.Request;
  60. import org.jdiameter.api.RouteException;
  61. import org.jdiameter.api.Stack;
  62. import org.jdiameter.api.app.AppAnswerEvent;
  63. import org.jdiameter.api.app.AppRequestEvent;
  64. import org.jdiameter.api.app.AppSession;
  65. import org.jdiameter.api.auth.events.ReAuthAnswer;
  66. import org.jdiameter.api.auth.events.ReAuthRequest;
  67. import org.jdiameter.api.ro.ClientRoSession;
  68. import org.jdiameter.api.ro.ClientRoSessionListener;
  69. import org.jdiameter.api.ro.ServerRoSession;
  70. import org.jdiameter.api.ro.ServerRoSessionListener;
  71. import org.jdiameter.client.api.ISessionFactory;
  72. import org.jdiameter.client.impl.app.ro.ClientRoSessionDataLocalImpl;
  73. import org.jdiameter.client.impl.app.ro.ClientRoSessionImpl;
  74. import org.jdiameter.client.impl.helpers.EmptyConfiguration;
  75. import org.jdiameter.common.api.app.ro.IRoMessageFactory;
  76. import org.jdiameter.server.impl.app.ro.ServerRoSessionDataLocalImpl;
  77. import org.jdiameter.server.impl.app.ro.ServerRoSessionImpl;
  78. import org.junit.Test;
  79. import org.mobicents.diameter.dictionary.AvpDictionary;
  80. import org.mobicents.slee.resource.diameter.base.DiameterAvpFactoryImpl;
  81. import org.mobicents.slee.resource.diameter.base.DiameterMessageFactoryImpl;
  82. import org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl;
  83. import org.mobicents.slee.resource.diameter.ro.RoAvpFactoryImpl;
  84. import org.mobicents.slee.resource.diameter.ro.RoClientSessionActivityImpl;
  85. import org.mobicents.slee.resource.diameter.ro.RoMessageFactoryImpl;
  86. import org.mobicents.slee.resource.diameter.ro.RoServerSessionActivityImpl;
  87. /**
  88. * Test class for JAIN SLEE Diameter Ro (Online Charging) RA Message and AVP Factories
  89. *
  90. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  91. */
  92. public class RoFactoriesTest implements IRoMessageFactory, ServerRoSessionListener, ClientRoSessionListener {
  93. private static String clientHost = "127.0.0.1";
  94. private static String clientPort = "13868";
  95. private static String clientURI = "aaa://" + clientHost + ":" + clientPort;
  96. private static String serverHost = "localhost";
  97. private static String serverPort = "3868";
  98. private static String serverURI = "aaa://" + serverHost + ":" + serverPort;
  99. private static String realmName = "mobicents.org";
  100. private static RoMessageFactory roMessageFactory;
  101. private static RoAvpFactory roAvpFactory;
  102. private static Stack stack;
  103. private static ServerRoSession serverSession;
  104. private static ClientRoSession clientSession;
  105. static {
  106. stack = new org.jdiameter.client.impl.StackImpl();
  107. try {
  108. stack.init(new MyConfiguration());
  109. }
  110. catch (Exception e) {
  111. throw new RuntimeException("Failed to initialize the stack.");
  112. }
  113. DiameterMessageFactoryImpl baseFactory = new DiameterMessageFactoryImpl(stack);
  114. DiameterAvpFactoryImpl baseAvpFactory = new DiameterAvpFactoryImpl();
  115. roAvpFactory = new RoAvpFactoryImpl(baseAvpFactory);
  116. try {
  117. roMessageFactory = new RoMessageFactoryImpl(baseFactory, stack.getSessionFactory().getNewSession().getSessionId(), stack);
  118. }
  119. catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. try {
  123. AvpDictionary.INSTANCE.parseDictionary(RoFactoriesTest.class.getClassLoader().getResourceAsStream("dictionary.xml"));
  124. }
  125. catch (Exception e) {
  126. throw new RuntimeException("Failed to parse dictionary file.");
  127. }
  128. }
  129. private RoServerSessionActivity roServerSession = null;
  130. private RoClientSessionActivity roClientSession = null;
  131. public RoFactoriesTest() {
  132. try {
  133. serverSession = new ServerRoSessionImpl(new ServerRoSessionDataLocalImpl(), this, (ISessionFactory) stack.getSessionFactory(), this, null, null);
  134. clientSession = new ClientRoSessionImpl(new ClientRoSessionDataLocalImpl(), this, (ISessionFactory) stack.getSessionFactory(), this, null, null);
  135. roServerSession = new RoServerSessionActivityImpl(roMessageFactory, roAvpFactory, serverSession, new DiameterIdentity("127.0.0.2"), new DiameterIdentity("mobicents.org"), stack);
  136. roClientSession = new RoClientSessionActivityImpl(roMessageFactory, roAvpFactory, clientSession, new DiameterIdentity("127.0.0.2"), new DiameterIdentity("mobicents.org"), stack);
  137. ((RoServerSessionActivityImpl) roServerSession).fetchCurrentState(roMessageFactory.createRoCreditControlRequest());
  138. }
  139. catch (IllegalDiameterStateException e) {
  140. throw new RuntimeException("Failed to parse dictionary file.");
  141. }
  142. }
  143. @Test
  144. public void isRequestCCR() throws Exception {
  145. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  146. assertTrue("Request Flag in Credit-Control-Request is not set.", ccr.getHeader().isRequest());
  147. }
  148. @Test
  149. public void isProxiableCCR() throws Exception {
  150. RoCreditControlRequest acr = roMessageFactory.createRoCreditControlRequest();
  151. assertTrue("The 'P' bit is not set by default in Ro Credit-Control-Request, it should.", acr.getHeader().isProxiable());
  152. }
  153. @Test
  154. public void testGettersAndSettersCCR() throws Exception {
  155. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  156. int nFailures = AvpAssistant.testMethods(ccr, RoCreditControlRequest.class);
  157. assertEquals("Some methods have failed. See logs for more details.", 0, nFailures);
  158. }
  159. @Test
  160. public void hasRoApplicationIdCCR() throws Exception {
  161. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  162. assertTrue("Auth-Application-Id AVP in Ro CCR must be 4, it is " + ccr.getAuthApplicationId(), ccr.getAuthApplicationId() == 4);
  163. }
  164. @Test
  165. public void isAnswerCCA() throws Exception {
  166. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  167. assertFalse("Request Flag in Credit-Control-Answer is set.", cca.getHeader().isRequest());
  168. }
  169. @Test
  170. public void isProxiableCopiedCCA() throws Exception {
  171. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  172. ((RoServerSessionActivityImpl) roServerSession).fetchCurrentState(ccr);
  173. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  174. assertEquals("The 'P' bit is not copied from request in Ro Credit-Control-Answer, it should. [RFC3588/6.2]", ccr.getHeader().isProxiable(), cca.getHeader().isProxiable());
  175. // Reverse 'P' bit ...
  176. ((DiameterMessageImpl) ccr).getGenericData().setProxiable(!ccr.getHeader().isProxiable());
  177. assertTrue("The 'P' bit was not modified in Ro Credit-Control-Request, it should.", ccr.getHeader().isProxiable() != cca.getHeader().isProxiable());
  178. ((RoServerSessionActivityImpl) roServerSession).fetchCurrentState(ccr);
  179. cca = roServerSession.createRoCreditControlAnswer();
  180. assertEquals("The 'P' bit is not copied from request in Ro Credit-Control-Answer, it should. [RFC3588/6.2]", ccr.getHeader().isProxiable(), cca.getHeader().isProxiable());
  181. }
  182. @Test
  183. public void hasTFlagSetCCA() throws Exception {
  184. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  185. ((DiameterMessageImpl) ccr).getGenericData().setReTransmitted(true);
  186. assertTrue("The 'T' flag should be set in Credit-Control-Request", ccr.getHeader().isPotentiallyRetransmitted());
  187. ((RoServerSessionActivityImpl) roServerSession).fetchCurrentState(ccr);
  188. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  189. assertFalse("The 'T' flag should not be set in Credit-Control-Answer", cca.getHeader().isPotentiallyRetransmitted());
  190. }
  191. @Test
  192. public void testGettersAndSettersCCA() throws Exception {
  193. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  194. int nFailures = AvpAssistant.testMethods(cca, RoCreditControlAnswer.class);
  195. assertEquals("Some methods have failed. See logs for more details.", 0, nFailures);
  196. }
  197. @Test
  198. public void hasRoApplicationIdCCA() throws Exception {
  199. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  200. assertTrue("Auth-Application-Id AVP in Ro CCA must be 4, it is " + cca.getAuthApplicationId(), cca.getAuthApplicationId() == 4);
  201. }
  202. @Test
  203. public void hasDestinationHostCCA() throws Exception {
  204. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  205. assertNull("The Destination-Host and Destination-Realm AVPs MUST NOT be present in the answer message. [RFC3588/6.2]", cca.getDestinationHost());
  206. }
  207. @Test
  208. public void hasDestinationRealmCCA() throws Exception {
  209. RoCreditControlAnswer cca = roServerSession.createRoCreditControlAnswer();
  210. assertNull("The Destination-Host and Destination-Realm AVPs MUST NOT be present in the answer message. [RFC3588/6.2]", cca.getDestinationRealm());
  211. }
  212. @Test
  213. public void testMessageFactoryApplicationIdChangeCCR() throws Exception {
  214. long vendor = 10415L;
  215. ApplicationId originalAppId = ((RoMessageFactoryImpl)roMessageFactory).getApplicationId();
  216. boolean isAuth = originalAppId.getAuthAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  217. boolean isAcct = originalAppId.getAcctAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  218. boolean isVendor = originalAppId.getVendorId() != 0L;
  219. assertTrue("Invalid Application-Id (" + originalAppId + "). Should only, and at least, contain either Auth or Acct value.", (isAuth && !isAcct) || (!isAuth && isAcct));
  220. System.out.println("Default VENDOR-ID for Ro is " + originalAppId.getVendorId());
  221. // let's create a message and see how it comes...
  222. RoCreditControlRequest originalCCR = roMessageFactory.createRoCreditControlRequest();
  223. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, originalCCR);
  224. // now we switch..
  225. originalCCR = null;
  226. isVendor = !isVendor;
  227. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(isVendor ? vendor : 0L, isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  228. // create a new message and see how it comes...
  229. RoCreditControlRequest changedCCR = roMessageFactory.createRoCreditControlRequest();
  230. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, changedCCR);
  231. // revert back to default
  232. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(originalAppId.getVendorId(), isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  233. }
  234. @Test
  235. public void testClientSessionApplicationIdChangeCCR() throws Exception {
  236. long vendor = 10415L;
  237. ApplicationId originalAppId = ((RoMessageFactoryImpl)roMessageFactory).getApplicationId();
  238. boolean isAuth = originalAppId.getAuthAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  239. boolean isAcct = originalAppId.getAcctAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  240. boolean isVendor = originalAppId.getVendorId() != 0L;
  241. assertTrue("Invalid Application-Id (" + originalAppId + "). Should only, and at least, contain either Auth or Acct value.", (isAuth && !isAcct) || (!isAuth && isAcct));
  242. System.out.println("Default VENDOR-ID for Ro is " + originalAppId.getVendorId());
  243. // let's create a message and see how it comes...
  244. RoCreditControlRequest originalCCR = roClientSession.createRoCreditControlRequest(CcRequestType.EVENT_REQUEST);
  245. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, originalCCR);
  246. // now we switch..
  247. originalCCR = null;
  248. isVendor = !isVendor;
  249. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(isVendor ? vendor : 0L, isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  250. // create a new message and see how it comes...
  251. RoCreditControlRequest changedCCR = roClientSession.createRoCreditControlRequest(CcRequestType.EVENT_REQUEST);
  252. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, changedCCR);
  253. // revert back to default
  254. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(originalAppId.getVendorId(), isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  255. }
  256. @Test
  257. public void testServerSessionApplicationIdChangeCCA() throws Exception {
  258. long vendor = 10415L;
  259. ApplicationId originalAppId = ((RoMessageFactoryImpl)roMessageFactory).getApplicationId();
  260. boolean isAuth = originalAppId.getAuthAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  261. boolean isAcct = originalAppId.getAcctAppId() != org.jdiameter.api.ApplicationId.UNDEFINED_VALUE;
  262. boolean isVendor = originalAppId.getVendorId() != 0L;
  263. assertTrue("Invalid Application-Id (" + originalAppId + "). Should only, and at least, contain either Auth or Acct value.", (isAuth && !isAcct) || (!isAuth && isAcct));
  264. System.out.println("Default VENDOR-ID for Ro is " + originalAppId.getVendorId());
  265. // let's create a message and see how it comes...
  266. RoCreditControlRequest ccr = roMessageFactory.createRoCreditControlRequest();
  267. ((RoServerSessionActivityImpl)roServerSession).fetchCurrentState(ccr);
  268. RoCreditControlAnswer originalCCA = roServerSession.createRoCreditControlAnswer();
  269. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, originalCCA);
  270. // now we switch..
  271. originalCCA = null;
  272. isVendor = !isVendor;
  273. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(isVendor ? vendor : 0L, isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  274. // create a new message and see how it comes...
  275. RoCreditControlAnswer changedCCA = roServerSession.createRoCreditControlAnswer();
  276. checkCorrectApplicationIdAVPs(isVendor, isAuth, isAcct, changedCCA);
  277. // revert back to default
  278. ((RoMessageFactoryImpl)roMessageFactory).setApplicationId(originalAppId.getVendorId(), isAuth ? originalAppId.getAuthAppId() : originalAppId.getAcctAppId());
  279. }
  280. /**
  281. * Class representing the Diameter Configuration
  282. */
  283. public static class MyConfiguration extends EmptyConfiguration {
  284. public MyConfiguration() {
  285. super();
  286. add(Assembler, Assembler.defValue());
  287. add(OwnDiameterURI, clientURI);
  288. add(OwnRealm, realmName);
  289. add(OwnVendorID, 193L);
  290. // Set Ericsson SDK feature
  291. // add(UseUriAsFqdn, true);
  292. // Set Common Applications
  293. add(ApplicationId,
  294. // AppId 1
  295. getInstance().add(VendorId, 193L).add(AuthApplId, 0L).add(AcctApplId, 19302L));
  296. // Set peer table
  297. add(PeerTable,
  298. // Peer 1
  299. getInstance().add(PeerRating, 1).add(PeerName, serverURI));
  300. // Set realm table
  301. add(RealmTable,
  302. // Realm 1
  303. getInstance().add(RealmEntry, getInstance().
  304. add(RealmName, realmName).
  305. add(ApplicationId, getInstance().add(VendorId, 193L).add(AuthApplId, 0L).add(AcctApplId, 19302L)).
  306. add(RealmHosts, clientHost + ", " + serverHost).
  307. add(RealmLocalAction, "LOCAL").
  308. add(RealmEntryIsDynamic, false).
  309. add(RealmEntryExpTime, 1000L)));
  310. }
  311. }
  312. public ReAuthAnswer createReAuthAnswer(Answer answer) {
  313. // TODO Auto-generated method stub
  314. return null;
  315. }
  316. public ReAuthRequest createReAuthRequest(Request req) {
  317. // TODO Auto-generated method stub
  318. return null;
  319. }
  320. public long[] getApplicationIds() {
  321. return new long[] { RoMessageFactory._RO_AUTH_APP_ID };
  322. }
  323. public void doOtherEvent(AppSession session, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  324. // NO-OP
  325. }
  326. public void doReAuthAnswer(ServerRoSession session, ReAuthRequest request, ReAuthAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  327. // NO-OP
  328. }
  329. public void doCreditControlRequest(ServerRoSession session, org.jdiameter.api.ro.events.RoCreditControlRequest request) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  330. // NO-OP
  331. }
  332. public org.jdiameter.api.ro.events.RoCreditControlRequest createCreditControlRequest(Request request) {
  333. // NO-OP
  334. return null;
  335. }
  336. public org.jdiameter.api.ro.events.RoCreditControlAnswer createCreditControlAnswer(Answer answer) {
  337. // NO-OP
  338. return null;
  339. }
  340. public void doCreditControlAnswer(ClientRoSession session, org.jdiameter.api.ro.events.RoCreditControlRequest request,
  341. org.jdiameter.api.ro.events.RoCreditControlAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  342. // NO-OP
  343. }
  344. public void doReAuthRequest(ClientRoSession session, ReAuthRequest request) throws InternalException, IllegalDiameterStateException, RouteException,
  345. OverloadException {
  346. // NO-OP
  347. }
  348. public int getDefaultDDFHValue() {
  349. // NO-OP
  350. return 0;
  351. }
  352. public int getDefaultCCFHValue() {
  353. // NO-OP
  354. return 0;
  355. }
  356. }