Junit_tests.GPRSParserUnitTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rfppf Show documentation
Show all versions of rfppf Show documentation
RF-Networks Protocol Parser Framework
/*
* Copyright (c) 2019, RF-Networks Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code are not permitted.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of RF Networks Ltd. nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package Junit_tests;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.UUID;
import org.bitbucket.rfnetwork.rfppf.common.*;
import org.bitbucket.rfnetwork.rfppf.messages.*;
import org.bitbucket.rfnetwork.rfppf.messages.gprs.GPRSMessage;
import org.bitbucket.rfnetwork.rfppf.messages.gprs.gateway.*;
import org.bitbucket.rfnetwork.rfppf.messages.oldprotocol.*;
import org.bitbucket.rfnetwork.rfppf.parsers.GPRSParser;
import org.junit.Test;
public class GPRSParserUnitTest implements IParser {
GPRSMessage gprsMessage = null;
@Override
public void MessageReceived(ProtocolParser parser, ProtocolMessage msg) {
gprsMessage = (GPRSMessage)msg;
}
@Override
public void MessageError(ProtocolParser parser, ProtocolMessage msg) {
}
@Test
public void TestParseGPRSMessageWithData() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x11, (byte)0x96, 0x04, (byte)0xD7, 0x66, 0x1C, 0x5E, 0x54, 0x4D, 0x5A, 0x12, 0x00, 0x42, 0x01, 0x00, (byte)0xFF, (byte)0xFF, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x0C, (byte)0xC1, 0x2B, 0x1A, 0x04, 0x0B, (byte)0xDB, 0x04, (byte)0x8C, 0x5E, 0x64, 0x25, 0x7C, (byte)0xF0 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGPRSOldProtocolReceiverModemVoltageMessage() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x08, 0x00, 0x00, (byte)0xCA, 0x29, (byte)0xCD, 0x5E, (byte)0xE9, 0x10, 0x6B, (byte)0xE0, (byte)0xCF, 0x41, 0x01, 0x00, (byte)0xFF, (byte)0xFF, 0x00, 0x00, (byte)0xFF, 0x09, 0x00, 0x00, 0x00, (byte)0xA8, 0x52, (byte)0xE5, 0x07, 0x0A, 0x4A, 0x48, (byte)0xE1, 0x7A, 0x40, 0x37 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
ReceiverMessage rcvMessage = (ReceiverMessage)MessageFactory.ParseReceiverMessage(gprsMessage.getBody());
assertFalse(rcvMessage instanceof ReceiverConfigurationTemperatureMessage);
ReceiverModemVoltageMessage mv = (ReceiverModemVoltageMessage)rcvMessage;
assertNotNull(mv);
assertEquals(mv.getModemVoltage(), 3.92F, 0.001F);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGPRSOldProtocolReceiverVoltageMessage() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x0C, 0x00, 0x00, (byte)0xCA, 0x29, (byte)0xCD, 0x5E, (byte)0xE9, 0x10, 0x6B, (byte)0xE0, (byte)0xCF, 0x41, 0x01, 0x00, (byte)0xFF, (byte)0xFF, 0x00, 0x00, (byte)0xFF, 0x09, 0x00, 0x00, 0x00, (byte)0xA8, 0x52, (byte)0xE5, 0x0B, (byte)0xAA, 0x4A, 0x08, 0x00, 0x00, 0x0D, 0x0A, 0x02, 0x43, 0x0F, 0x67 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
ReceiverMessage rcvMessage = (ReceiverMessage)MessageFactory.ParseReceiverMessage(gprsMessage.getBody());
assertFalse(rcvMessage instanceof ReceiverConfigurationTemperatureMessage);
assertTrue(rcvMessage instanceof ReceiverConfigurationVoltageMessage);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGPRSOldProtocolReceiverTemperatureWithPayloadTwoBytesMessage()
{
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x0C, 0x5F, (byte)0x99, 0x6C, (byte)0x84, 0x16, 0x5F, 0x54, 0x4D, 0x5A, 0x12, 0x00, 0x42, 0x01, 0x00, (byte)0xF0, (byte)0xA3, 0x00, 0x00, (byte)0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B, (byte)0xAA, (byte)0xAA, 0x08, (byte)0xFF, 0x00, 0x0D, 0x0B, 0x02, 0x2C, 0x00, (byte)0xA1 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
ReceiverMessage rcvMessage = (ReceiverMessage)MessageFactory.ParseReceiverMessage(gprsMessage.getBody());
assertTrue(rcvMessage instanceof ReceiverConfigurationTemperatureMessage);
assertFalse(rcvMessage instanceof ReceiverConfigurationVoltageMessage);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGPRSOldProtocolReceiverGPSMessage()
{
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x1D, (byte)0xEF, 0x06, 0x43, 0x4D, 0x09, 0x65, 0x66, (byte)0xA4, (byte)0xDE, (byte)0xBC, (byte)0xA2, 0x0E, 0x03, 0x00, (byte)0xF2, (byte)0x9F, 0x00, 0x00, (byte)0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x01, 0x5B, 0x18, 0x1F, (byte)0xD1, (byte)0x9B, (byte)0xE0, 0x27, 0x00, 0x01, 0x00, 0x00, (byte)0xC0, 0x7F, 0x00, 0x00, (byte)0xC0, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x94, 0x19 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
ReceiverMessage rcvMessage = (ReceiverMessage)MessageFactory.ParseReceiverMessage(gprsMessage.getBody());
assertNotNull(rcvMessage);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestGPRSOldProtocolTagMagnitudeMessage()
{
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x11, (byte)0xE4, 0x01, (byte)0xBA, 0x08, (byte)0xCA, 0x65, 0x66, (byte)0xA4, (byte)0xDE, (byte)0xBC, (byte)0xA2, 0x0E, 0x03, 0x00, (byte)0xF2, (byte)0x9F, 0x00, 0x00, (byte)0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, (byte)0xE4, 0x0C, 0x19, 0x0C, (byte)0x9A, 0x44, 0x1E, 0x39, 0x55, 0x06, 0x00, 0x01, 0x00, 0x60, 0x07, (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x14, (byte)0xE5, 0x01, (byte)0xBA, 0x08, (byte)0xCA, 0x65, 0x66, (byte)0xA4, (byte)0xDE, (byte)0xBC, (byte)0xA2, 0x0E, 0x03, 0x00, (byte)0xF2, (byte)0x9F, 0x00, 0x00, (byte)0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x01, (byte)0xE5, 0x0F, 0x1F, 0x0C, (byte)0x9A, 0x44, 0x1E, 0x3A, 0x05, (byte)0xA6, (byte)0xFB, (byte)0xBC, 0x07, (byte)0xC9, (byte)0xF7, (byte)0xD0, 0x60, (byte)0xAF };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
ReceiverMessage rcvMessage = (ReceiverMessage)MessageFactory.ParseReceiverMessage(gprsMessage.getBody());
assertNotNull(rcvMessage);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewayUnknownMessage() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x00, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNull(gprsMessage);
byte[] data1 = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x01, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
gprsMessage = null;
gprsParser.ClearBuffer();
gprsParser.AppendBytes(data1);
Thread.sleep(5);
assertNotNull(gprsMessage);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewayAcknowledgeMessage() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x04, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0x00, 0x03 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
GatewayAcknowledgeMessage ackMsg = (GatewayAcknowledgeMessage)gprsMessage.getGatewayMessage();
assertEquals(ackMsg.getAcknowledgeMessageNumber(), 2);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewayWhoIAmMessage() {
try {
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x15, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, (byte)0xF1, (byte)0xF2, (byte)0xF3, (byte)0xF4, (byte)0xF5, (byte)0xF6, 0x2A };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
GatewayWhoIAmMessage whoIAmMsg = (GatewayWhoIAmMessage)gprsMessage.getGatewayMessage();
assertNotNull(whoIAmMsg);
assertEquals(whoIAmMsg.getRFVersionMajor(), 3);
assertEquals(whoIAmMsg.getRFVersionMinor(), 2);
assertEquals(whoIAmMsg.getRFVersionBuild(), 1);
assertEquals(whoIAmMsg.getHWVersionMajor(), 6);
assertEquals(whoIAmMsg.getHWVersionMinor(), 5);
assertEquals(whoIAmMsg.getHWVersionBuild(), 4);
assertEquals(whoIAmMsg.getSWVersionMajor(), 9);
assertEquals(whoIAmMsg.getSWVersionMinor(), 8);
assertEquals(whoIAmMsg.getSWVersionBuild(), 7);
assertEquals(whoIAmMsg.getSerial(), 0x13121110);
assertEquals(whoIAmMsg.getMAC(), "F6:F5:F4:F3:F2:F1");
GPRSMessage gprsMsg = (GPRSMessage)MessageFactory.CreateMessage(GPRSMessage.class);
gprsMsg.setPayloadType((byte)1);
gprsMsg.setVersion((byte)1);
gprsMsg.setMessageNumber(0x1A);
gprsMsg.setReceiverID(0xFFFFFFFF);
gprsMsg.setMainReceiverID(0x00031452a10e5e72L);
gprsMsg.setRSSI((byte)0xFF);
gprsMsg.setTimestamp(new Timestamp(1735474650000L));
gprsMsg.setGatewayMessage(whoIAmMsg);
assertArrayEquals(gprsMsg.GetBytes(), data);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewaySendSMSMessage() {
try {
String phone_number = "+972544662861";
String message = "TEST";
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x18, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0x01, 0x0E, 0x2B, 0x39, 0x37, 0x32, 0x35, 0x34, 0x34, 0x36, 0x36, 0x32, 0x38, 0x36, 0x31, 0x00, 0x05, 0x54, 0x45, 0x53, 0x54, 0x00, 0x00 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
GatewaySendSMSMessage sendSMSMsg = (GatewaySendSMSMessage)gprsMessage.getGatewayMessage();
assertNotNull(sendSMSMsg);
assertEquals(sendSMSMsg.getEncoding(), GatewaySendSMSMessage.SMSEncoding.GSM7bit);
assertEquals(sendSMSMsg.getPhoneNumber(), phone_number);
assertEquals(sendSMSMsg.getMessage(), message);
GPRSMessage gprsMsg = (GPRSMessage)MessageFactory.CreateMessage(GPRSMessage.class);
gprsMsg.setPayloadType((byte)1);
gprsMsg.setVersion((byte)1);
gprsMsg.setMessageNumber(0x1A);
gprsMsg.setReceiverID(0xFFFFFFFF);
gprsMsg.setMainReceiverID(0x00031452a10e5e72L);
gprsMsg.setRSSI((byte)0xFF);
gprsMsg.setTimestamp(new Timestamp(1735474650000L));
gprsMsg.setGatewayMessage(sendSMSMsg);
assertArrayEquals(gprsMsg.GetBytes(), data);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewayRPCRequestMessage() {
try {
UUID uuid = UUID.fromString("4865e243-fd18-4d65-ad7a-18903c2f9c4d");
BigInteger guidint = new BigInteger(1, RFPPFHelper.UUIDtoBytesArray(uuid));
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x13, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x43, (byte)0xE2, 0x65, 0x48, 0x18, (byte)0xFD, 0x65, 0x4D, (byte)0xAD, 0x7A, 0x18, (byte)0x90, 0x3C, 0x2F, (byte)0x9C, 0x4D, 0x01, (byte)0xC0 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
GatewayRPCRequestMessage rpcRequestMsg = (GatewayRPCRequestMessage)gprsMessage.getGatewayMessage();
assertNotNull(rpcRequestMsg);
assertEquals(rpcRequestMsg.getRPCID(), guidint);
assertArrayEquals(rpcRequestMsg.getPayload(), new byte[] { 0x01 });
GPRSMessage gprsMsg = (GPRSMessage)MessageFactory.CreateMessage(GPRSMessage.class);
gprsMsg.setPayloadType((byte)1);
gprsMsg.setVersion((byte)1);
gprsMsg.setMessageNumber(0x1A);
gprsMsg.setReceiverID(0xFFFFFFFF);
gprsMsg.setMainReceiverID(0x00031452a10e5e72L);
gprsMsg.setRSSI((byte)0xFF);
gprsMsg.setTimestamp(new Timestamp(1735474650000L));
gprsMsg.setGatewayMessage(rpcRequestMsg);
assertArrayEquals(gprsMsg.GetBytes(), data);
} catch(Exception ex) {
fail();
}
}
@Test
public void TestParseGatewayRPCResponseMessage() {
try {
UUID uuid = UUID.fromString("4865e243-fd18-4d65-ad7a-18903c2f9c4d");
BigInteger guidint = new BigInteger(1, RFPPFHelper.UUIDtoBytesArray(uuid));
byte[] data = { (byte)0xAA, 0x55, (byte)0xAA, 0x55, 0x13, 0x1A, 0x00, (byte)0xDA, 0x3D, 0x71, 0x67, 0x72, 0x5E, 0x0E, (byte)0xA1, 0x52, 0x14, 0x03, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x43, (byte)0xE2, 0x65, 0x48, 0x18, (byte)0xFD, 0x65, 0x4D, (byte)0xAD, 0x7A, 0x18, (byte)0x90, 0x3C, 0x2F, (byte)0x9C, 0x4D, 0x01, (byte)0xC1 };
GPRSParser gprsParser = new GPRSParser();
gprsParser.addListener(this);
gprsMessage = null;
gprsParser.AppendBytes(data);
Thread.sleep(5);
assertNotNull(gprsMessage);
GatewayRPCResponseMessage rpcResponseMsg = (GatewayRPCResponseMessage)gprsMessage.getGatewayMessage();
assertNotNull(rpcResponseMsg);
assertEquals(rpcResponseMsg.getRPCID(), guidint);
assertArrayEquals(rpcResponseMsg.getPayload(), new byte[] { 0x01 });
GPRSMessage gprsMsg = (GPRSMessage)MessageFactory.CreateMessage(GPRSMessage.class);
gprsMsg.setPayloadType((byte)1);
gprsMsg.setVersion((byte)1);
gprsMsg.setMessageNumber(0x1A);
gprsMsg.setReceiverID(0xFFFFFFFF);
gprsMsg.setMainReceiverID(0x00031452a10e5e72L);
gprsMsg.setRSSI((byte)0xFF);
gprsMsg.setTimestamp(new Timestamp(1735474650000L));
gprsMsg.setGatewayMessage(rpcResponseMsg);
assertArrayEquals(gprsMsg.GetBytes(), data);
} catch(Exception ex) {
fail();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy