PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/primitives/MAPExtensionContainerTest.java

http://mobicents.googlecode.com/
Java | 195 lines | 133 code | 34 blank | 28 comment | 21 complexity | 8a128093a6367a4ca12bbf1d8552fddf 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.protocols.ss7.map.primitives;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertTrue;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.InputStream;
  28. import java.io.ObjectInputStream;
  29. import java.io.ObjectOutputStream;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import org.mobicents.protocols.asn.AsnInputStream;
  33. import org.mobicents.protocols.asn.AsnOutputStream;
  34. import org.mobicents.protocols.asn.Tag;
  35. import org.mobicents.protocols.ss7.map.MAPParameterFactoryImpl;
  36. import org.mobicents.protocols.ss7.map.api.MAPParameterFactory;
  37. import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
  38. import org.mobicents.protocols.ss7.map.api.primitives.MAPPrivateExtension;
  39. import org.testng.annotations.AfterClass;
  40. import org.testng.annotations.AfterTest;
  41. import org.testng.annotations.BeforeClass;
  42. import org.testng.annotations.BeforeTest;
  43. import org.testng.annotations.Test;
  44. /**
  45. * @author sergey vetyutnev
  46. *
  47. */
  48. public class MAPExtensionContainerTest {
  49. MAPParameterFactory mapServiceFactory = new MAPParameterFactoryImpl();
  50. public static MAPExtensionContainer GetTestExtensionContainer() {
  51. MAPParameterFactory mapServiceFactory = new MAPParameterFactoryImpl();
  52. ArrayList<MAPPrivateExtension> al = new ArrayList<MAPPrivateExtension>();
  53. al.add(mapServiceFactory
  54. .createMAPPrivateExtension(new long[] { 1, 2, 3, 4 }, new byte[] { 11, 12, 13, 14, 15 }));
  55. al.add(mapServiceFactory.createMAPPrivateExtension(new long[] { 1, 2, 3, 6 }, null));
  56. al.add(mapServiceFactory.createMAPPrivateExtension(new long[] { 1, 2, 3, 5 }, new byte[] { 21, 22, 23, 24, 25,
  57. 26 }));
  58. MAPExtensionContainer cnt = mapServiceFactory.createMAPExtensionContainer(al, new byte[] { 31, 32, 33 });
  59. return cnt;
  60. }
  61. public static Boolean CheckTestExtensionContainer(MAPExtensionContainer extContainer) {
  62. if (extContainer == null || extContainer.getPrivateExtensionList().size() != 3)
  63. return false;
  64. for (int i = 0; i < 3; i++) {
  65. MAPPrivateExtension pe = extContainer.getPrivateExtensionList().get(i);
  66. long[] lx = null;
  67. byte[] bx = null;
  68. switch (i) {
  69. case 0:
  70. lx = new long[] { 1, 2, 3, 4 };
  71. bx = new byte[] { 11, 12, 13, 14, 15 };
  72. break;
  73. case 1:
  74. lx = new long[] { 1, 2, 3, 6 };
  75. bx = null;
  76. break;
  77. case 2:
  78. lx = new long[] { 1, 2, 3, 5 };
  79. bx = new byte[] { 21, 22, 23, 24, 25, 26 };
  80. break;
  81. }
  82. if (pe.getOId() == null || !Arrays.equals(pe.getOId(), lx))
  83. return false;
  84. if (bx == null) {
  85. if (pe.getData() != null)
  86. return false;
  87. } else {
  88. if (pe.getData() == null || !Arrays.equals(pe.getData(), bx))
  89. return false;
  90. }
  91. }
  92. byte[] by = new byte[] { 31, 32, 33 };
  93. if (extContainer.getPcsExtensions() == null || !Arrays.equals(extContainer.getPcsExtensions(), by))
  94. return false;
  95. return true;
  96. }
  97. @BeforeClass
  98. public static void setUpClass() throws Exception {
  99. }
  100. @AfterClass
  101. public static void tearDownClass() throws Exception {
  102. }
  103. @BeforeTest
  104. public void setUp() {
  105. }
  106. @AfterTest
  107. public void tearDown() {
  108. }
  109. @Test(groups = { "functional.decode","primitives"})
  110. public void testDecode() throws Exception {
  111. byte[] data = this.getEncodedData();
  112. AsnInputStream ais = new AsnInputStream(data);
  113. int tag = ais.readTag();
  114. MAPExtensionContainerImpl extCont = new MAPExtensionContainerImpl();
  115. extCont.decodeAll(ais);
  116. assertEquals( tag,Tag.SEQUENCE);
  117. assertEquals( CheckTestExtensionContainer(extCont),Boolean.TRUE);
  118. }
  119. @Test(groups = { "functional.encode","primitives"})
  120. public void testEncode() throws Exception {
  121. byte[] data = this.getEncodedData();
  122. MAPExtensionContainerImpl extCont = (MAPExtensionContainerImpl)GetTestExtensionContainer();
  123. AsnOutputStream asnOS = new AsnOutputStream();
  124. extCont.encodeAll(asnOS);
  125. byte[] res = asnOS.toByteArray();
  126. assertTrue( Arrays.equals(data,res));
  127. }
  128. @Test(groups = { "functional.equality", "primitives" })
  129. public void testEquality() throws Exception {
  130. MAPExtensionContainerImpl original = (MAPExtensionContainerImpl)GetTestExtensionContainer();
  131. MAPExtensionContainerImpl copy = (MAPExtensionContainerImpl)GetTestExtensionContainer();
  132. assertEquals(copy, original);
  133. }
  134. @Test(groups = { "functional.serialize", "primitives" })
  135. public void testSerialization() throws Exception {
  136. MAPExtensionContainerImpl original = (MAPExtensionContainerImpl)GetTestExtensionContainer();
  137. // serialize
  138. ByteArrayOutputStream out = new ByteArrayOutputStream();
  139. ObjectOutputStream oos = new ObjectOutputStream(out);
  140. oos.writeObject(original);
  141. oos.close();
  142. // deserialize
  143. byte[] pickled = out.toByteArray();
  144. InputStream in = new ByteArrayInputStream(pickled);
  145. ObjectInputStream ois = new ObjectInputStream(in);
  146. Object o = ois.readObject();
  147. MAPExtensionContainerImpl copy = (MAPExtensionContainerImpl) o;
  148. //test result
  149. assertTrue(Arrays.equals(copy.getPcsExtensions(), original.getPcsExtensions()));
  150. assertEquals(copy.getPrivateExtensionList().size(), original.getPrivateExtensionList().size());
  151. ArrayList<MAPPrivateExtension> copyPriExt = copy.getPrivateExtensionList();
  152. ArrayList<MAPPrivateExtension> originalPriExt = original.getPrivateExtensionList();
  153. for(int i=0;i<copyPriExt.size();i++){
  154. assertEquals(copyPriExt.get(i), originalPriExt.get(i));
  155. }
  156. }
  157. private byte[] getEncodedData() {
  158. return new byte[] { 48, 39, (byte) 160, 32, 48, 10, 6, 3, 42, 3, 4, 11, 12, 13, 14, 15, 48, 5, 6, 3, 42, 3, 6, 48, 11, 6, 3, 42, 3, 5, 21, 22, 23, 24,
  159. 25, 26, (byte) 161, 3, 31, 32, 33 };
  160. }
  161. }