Skip to content

Commit

Permalink
Fises issue RestComm#58 - Extra update to fit code design
Browse files Browse the repository at this point in the history
  • Loading branch information
vetss committed Apr 19, 2016
1 parent eacd974 commit 8a0ef9d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,21 @@ public interface GenericDigits extends ISUPParameter {

void setEncodedDigits(byte[] digits);

/**
* Return decoded digits string as for following rules: BCD_EVEN, BCD_ODD and IA5 are supported.
* In BCD case '*' is treated as 11 value, '#' is treated as 12 value
*
* @return
* @throws UnsupportedEncodingException
*/
String getDecodedDigits() throws UnsupportedEncodingException;

/**
* Encode a digits string as for following rules: BCD_EVEN, BCD_ODD and IA5 are supported.
* In BCD case '*' is treated as 11 value, '#' is treated as 12 value
* @param encodingScheme
* @param digits
* @throws UnsupportedEncodingException
*/
void setDecodedDigits(int encodingScheme, String digits) throws UnsupportedEncodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@

import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;

import org.mobicents.protocols.ss7.isup.ParameterException;
import org.mobicents.protocols.ss7.isup.message.parameter.GenericDigits;
import org.mobicents.protocols.ss7.isup.util.BcdHelper;

import javax.xml.bind.DatatypeConverter;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
* Start time:12:24:47 2009-03-31<br>
Expand All @@ -43,6 +46,7 @@ public class GenericDigitsImpl extends AbstractISUPParameter implements GenericD
private static final String ENCODING_SCHEME = "encodingScheme";
private static final String TYPE_OF_DIGITS = "typeOfDigits";
private static final String DIGITS = "digits";
private static final Charset asciiCharset = Charset.forName("ASCII");

private static final int DEFAULT_VALUE = 0;

Expand All @@ -62,11 +66,10 @@ public GenericDigitsImpl(int encodingScheme, int typeOfDigits, byte[] digits) {
this.setEncodedDigits(digits);
}

public GenericDigitsImpl(int encodingScheme, int typeOfDigits, String digits) {
public GenericDigitsImpl(int encodingScheme, int typeOfDigits, String digits) throws UnsupportedEncodingException {
super();
this.encodingScheme = encodingScheme;
this.typeOfDigits = typeOfDigits;
this.setEncodedDigits(BcdHelper.encodeHexStringToBCD(digits));
setDecodedDigits(encodingScheme, digits);
}

public GenericDigitsImpl() {
Expand All @@ -75,24 +78,44 @@ public GenericDigitsImpl() {
}

public String getDecodedDigits() throws UnsupportedEncodingException {
return BcdHelper.bcdDecodeToHexString(encodingScheme, digits);
switch (encodingScheme) {
case GenericDigits._ENCODING_SCHEME_BCD_EVEN:
case GenericDigits._ENCODING_SCHEME_BCD_ODD:
return BcdHelper.bcdDecodeToHexString(encodingScheme, digits);
case GenericDigits._ENCODING_SCHEME_IA5:
return new String(digits, asciiCharset);
default:
//TODO: add other encoding schemas support
throw new UnsupportedEncodingException("Specified GenericDigits encoding: " + encodingScheme + " is unsupported");
}

}

public void setDecodedDigits(int encodingScheme, String digits) throws UnsupportedEncodingException {
if (digits == null || digits.length() < 1) {
throw new IllegalArgumentException("Digits must not be null");
throw new IllegalArgumentException("Digits must not be null or zero length");
}
//TODO: analyse if encoding scheme is correctly set (ODD/EVEN) vs number of digits
switch (encodingScheme) {
case GenericDigits._ENCODING_SCHEME_BCD_EVEN:
case GenericDigits._ENCODING_SCHEME_BCD_ODD:
if ((digits.length() % 2) == 0) {
if (encodingScheme == GenericDigits._ENCODING_SCHEME_BCD_ODD)
throw new UnsupportedEncodingException("SCHEME_BCD_ODD is possible only for odd digits count");
} else {
if (encodingScheme == GenericDigits._ENCODING_SCHEME_BCD_EVEN)
throw new UnsupportedEncodingException("SCHEME_BCD_EVEN is possible only for odd digits count");
}
this.encodingScheme = encodingScheme;
this.setEncodedDigits(BcdHelper.encodeHexStringToBCD(digits));
break;
case GenericDigits._ENCODING_SCHEME_IA5:
this.encodingScheme = encodingScheme;
this.setEncodedDigits(digits.getBytes(asciiCharset));
break;
default:
//TODO: add other encoding schemas support
throw new UnsupportedEncodingException("Specified GenericDigits encoding: " + encodingScheme + " is unsupported");
}
this.setEncodedDigits(BcdHelper.encodeHexStringToBCD(digits));
}

public int decode(byte[] b) throws ParameterException {
Expand Down Expand Up @@ -165,6 +188,14 @@ public String toString() {
sb.append(", encodedDigits=[");
sb.append(DatatypeConverter.printHexBinary(digits));
sb.append("]");

try {
String s = getDecodedDigits();
sb.append(", decodedDigits=[");
sb.append(s);
sb.append("]");
} catch (Exception e) {
}
}
sb.append("]");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ private byte[] getEncodedOddData() {
return new byte[] { 0x21, 0x43, 0x65, 0x07 };
}

private byte[] getIA5Data() {
return new byte[] { 67, 65, 66, 97, 98, 49, 50 }; // "ABab12"
}

private String digitsEvenString = "123456";

private String digitsOddString = "1234567";

private String digitsIA5String = "ABab12";

@Test(groups = { "functional.decode", "parameter" })
public void testDecodeEven() throws Exception {

Expand All @@ -90,6 +96,7 @@ public void testDecodeEven() throws Exception {
assertEquals(prim.getEncodingScheme(), GenericDigits._ENCODING_SCHEME_BCD_EVEN);
assertEquals(prim.getTypeOfDigits(), GenericDigits._TOD_BGCI);
assertEquals(prim.getEncodedDigits(), getEncodedEvenData());
assertEquals(prim.getDecodedDigits(), "123456");
}

@Test(groups = { "functional.decode", "parameter" })
Expand All @@ -101,6 +108,7 @@ public void testDecodeOdd() throws Exception {
assertEquals(prim.getEncodingScheme(), GenericDigits._ENCODING_SCHEME_BCD_ODD);
assertEquals(prim.getTypeOfDigits(), GenericDigits._TOD_BGCI);
assertEquals(prim.getEncodedDigits(), getEncodedOddData());
assertEquals(prim.getDecodedDigits(), "1234567");
}

@Test(groups = { "functional.encode", "parameter" })
Expand All @@ -115,6 +123,11 @@ public void testEncodeEven() throws Exception {

assertTrue(Arrays.equals(data, encodedData));


prim = new GenericDigitsImpl(GenericDigits._ENCODING_SCHEME_BCD_EVEN, GenericDigits._TOD_BGCI, "123456");
encodedData = prim.encode();
assertTrue(Arrays.equals(data, encodedData));

}

@Test(groups = { "functional.encode", "parameter" })
Expand All @@ -129,6 +142,10 @@ public void testEncodeOdd() throws Exception {

assertTrue(Arrays.equals(data, encodedData));


prim = new GenericDigitsImpl(GenericDigits._ENCODING_SCHEME_BCD_ODD, GenericDigits._TOD_BGCI, "1234567");
encodedData = prim.encode();
assertTrue(Arrays.equals(data, encodedData));
}

@Test(groups = { "functional.encode", "parameter" })
Expand Down Expand Up @@ -164,6 +181,24 @@ public void testSetDecodedHexDigits() throws Exception {
assertTrue(BcdHelper.convertTelcoCharsToHexDigits(hexString).equals(convertedDigits));
}

@Test(groups = { "functional.encode", "parameter" })
public void testEncodingIA5() throws Exception {
GenericDigitsImpl prim = new GenericDigitsImpl();
prim.setDecodedDigits(GenericDigits._ENCODING_SCHEME_IA5, digitsIA5String);
prim.setTypeOfDigits(GenericDigits._TOD_BGCI);

assertEquals(getIA5Data(), prim.encode());
}

@Test(groups = { "functional.decode", "parameter" })
public void testDecodingIA5() throws Exception {
GenericDigitsImpl prim = new GenericDigitsImpl();
prim.decode(getIA5Data());

assertEquals(prim.getEncodingScheme(), GenericDigits._ENCODING_SCHEME_IA5);
assertEquals(prim.getTypeOfDigits(), GenericDigits._TOD_BGCI);
assertEquals(prim.getDecodedDigits(), digitsIA5String);
}


@Test(groups = { "functional.xml.serialize", "parameter" })
Expand Down Expand Up @@ -192,5 +227,6 @@ public void testXMLSerialize() throws Exception {
assertEquals(copy.getEncodingScheme(), original.getEncodingScheme());
assertEquals(copy.getTypeOfDigits(), original.getTypeOfDigits());
assertEquals(copy.getEncodedDigits(), original.getEncodedDigits());
assertEquals(copy.getDecodedDigits(), original.getDecodedDigits());
}
}

0 comments on commit 8a0ef9d

Please sign in to comment.