PageRenderTime 60ms CodeModel.GetById 17ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

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