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

http://mobicents.googlecode.com/ · Java · 137 lines · 83 code · 23 blank · 31 comment · 0 complexity · 9b0819bfe8368f923ac307427d658cf7 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
  4. * contributors as indicated by the @authors tag. All rights reserved.
  5. * See the copyright.txt in the distribution for a full listing
  6. * of individual contributors.
  7. *
  8. * This copyrighted material is made available to anyone wishing to use,
  9. * modify, copy, or redistribute it subject to the terms and conditions
  10. * of the GNU General Public License, v. 2.0.
  11. *
  12. * This program 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. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License,
  18. * v. 2.0 along with this distribution; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301, USA.
  21. */
  22. package org.mobicents.protocols.ss7.map.primitives;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertNotNull;
  25. import static org.testng.Assert.assertNull;
  26. import static org.testng.Assert.assertTrue;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.ByteArrayOutputStream;
  29. import java.io.InputStream;
  30. import java.io.ObjectInputStream;
  31. import java.io.ObjectOutputStream;
  32. import java.util.Arrays;
  33. import org.mobicents.protocols.asn.AsnInputStream;
  34. import org.mobicents.protocols.asn.AsnOutputStream;
  35. import org.mobicents.protocols.ss7.map.api.primitives.AlertingCategory;
  36. import org.mobicents.protocols.ss7.map.api.primitives.AlertingLevel;
  37. import org.testng.annotations.AfterClass;
  38. import org.testng.annotations.AfterTest;
  39. import org.testng.annotations.BeforeClass;
  40. import org.testng.annotations.BeforeTest;
  41. import org.testng.annotations.Test;
  42. /**
  43. * @author amit bhayani
  44. *
  45. */
  46. public class AlertingPatternTest {
  47. @BeforeClass
  48. public static void setUpClass() throws Exception {
  49. }
  50. @AfterClass
  51. public static void tearDownClass() throws Exception {
  52. }
  53. @BeforeTest
  54. public void setUp() {
  55. }
  56. @AfterTest
  57. public void tearDown() {
  58. }
  59. @Test(groups = { "functional.decode","primitives"})
  60. public void testDecode() throws Exception {
  61. byte[] data = new byte[] { (byte) 0x04, 0x01, 0x07 };
  62. AsnInputStream asn = new AsnInputStream(data);
  63. int tag = asn.readTag();
  64. AlertingPatternImpl addNum = new AlertingPatternImpl();
  65. addNum.decodeAll(asn);
  66. assertNull(addNum.getAlertingLevel());
  67. assertNotNull(addNum.getAlertingCategory());
  68. assertEquals( addNum.getAlertingCategory(),AlertingCategory.Category4);
  69. }
  70. @Test(groups = { "functional.encode","primitives"})
  71. public void testEncode() throws Exception {
  72. byte[] data = new byte[] { (byte) 0x04, 0x01, 0x07 };
  73. AlertingPatternImpl addNum = new AlertingPatternImpl(AlertingCategory.Category4);
  74. AsnOutputStream asnOS = new AsnOutputStream();
  75. addNum.encodeAll(asnOS);
  76. byte[] encodedData = asnOS.toByteArray();
  77. assertTrue(Arrays.equals(data, encodedData));
  78. }
  79. @Test(groups = { "functional.serialize", "primitives" })
  80. public void testSerialization() throws Exception {
  81. AlertingPatternImpl original = new AlertingPatternImpl(AlertingCategory.Category4);
  82. // serialize
  83. ByteArrayOutputStream out = new ByteArrayOutputStream();
  84. ObjectOutputStream oos = new ObjectOutputStream(out);
  85. oos.writeObject(original);
  86. oos.close();
  87. // deserialize
  88. byte[] pickled = out.toByteArray();
  89. InputStream in = new ByteArrayInputStream(pickled);
  90. ObjectInputStream ois = new ObjectInputStream(in);
  91. Object o = ois.readObject();
  92. AlertingPatternImpl copy = (AlertingPatternImpl) o;
  93. //test result
  94. assertEquals(copy.getAlertingCategory(), original.getAlertingCategory());
  95. assertEquals(copy, original);
  96. assertNull(copy.getAlertingLevel());
  97. original = new AlertingPatternImpl(AlertingLevel.Level1);
  98. // serialize
  99. out = new ByteArrayOutputStream();
  100. oos = new ObjectOutputStream(out);
  101. oos.writeObject(original);
  102. oos.close();
  103. // deserialize
  104. pickled = out.toByteArray();
  105. in = new ByteArrayInputStream(pickled);
  106. ois = new ObjectInputStream(in);
  107. o = ois.readObject();
  108. copy = (AlertingPatternImpl) o;
  109. //test result
  110. assertEquals(copy.getAlertingLevel(), original.getAlertingLevel());
  111. assertEquals(copy, original);
  112. assertNull(copy.getAlertingCategory());
  113. }
  114. }