Junit_tests.RFMConfigurationMessageUnitTest 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
The newest version!
/*
* 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.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.sql.Timestamp;
import org.junit.Test;
import org.bitbucket.rfnetwork.rfppf.common.BitConverter;
import org.bitbucket.rfnetwork.rfppf.messages.MessageFactory;
import org.bitbucket.rfnetwork.rfppf.messages.rfm.*;
import org.bitbucket.rfnetwork.rfppf.messages.rfm.configuration.*;
public class RFMConfigurationMessageUnitTest {
@Test
public void TestEmptyRequestMessage() {
byte version = 1;
byte message_num = 2;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x00, (byte)RFMMessage.MessageTypes.ConfigTimestampRequest.getValue(), message_num, 0x4E, 0x72 };
RFMMessageBuilder builder = new RFMMessageBuilder();
builder.Version = version;
builder.MessageNumber = message_num;
builder.MessageType = RFMMessage.MessageTypes.ConfigTimestampRequest;
GetTimeRequestMessage msg = (GetTimeRequestMessage)MessageFactory.CreateMessage(GetTimeRequestMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), message_num);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigTimestampRequest);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetTimeRequestMessage)MessageFactory.CreateMessage(GetTimeRequestMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestResponseWithTimestampMessage() {
// GMT - Tuesday, January 1, 2019 12:00:00 PM, Epoch - 1546344000
byte version = 1;
byte messageNum = 2;
Timestamp testTime = new Timestamp(1546344000000L);
byte[] testTimeBytes = BitConverter.getBytes((int)((testTime.getTime() / 1000) & 0xFFFFFFFF));
byte[] test_msg = new byte[] { (byte)0x96, version, 0x04, (byte)RFMMessage.MessageTypes.ConfigTimestampResponse.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x40, (byte)0xEE };
System.arraycopy(testTimeBytes, 0, test_msg, 5, testTimeBytes.length);
GetTimeResponseMessageBuilder builder = new GetTimeResponseMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
builder.Time = testTime;
GetTimeResponseMessage msg = (GetTimeResponseMessage)MessageFactory.CreateMessage(GetTimeResponseMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigTimestampResponse);
assertEquals(msg.getTime(), testTime);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetTimeResponseMessage)MessageFactory.CreateMessage(GetTimeResponseMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestEmptyGetRegisterRequestMessage() {
// GMT - Tuesday, January 1, 2019 12:00:00 PM, Epoch - 1546344000
byte version = 1;
byte messageNum = 2;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x06, (byte)RFMMessage.MessageTypes.ConfigRegisterGetRequest.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xF9, 0x45 };
GetRegisterRequestMessageBuilder builder = new GetRegisterRequestMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
GetRegisterRequestMessage msg = (GetRegisterRequestMessage)MessageFactory.CreateMessage(GetRegisterRequestMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterGetRequest);
assertEquals(msg.getDeviceLongID(), 0L);
assertEquals(msg.getRegisterNumber(), 0);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetRegisterRequestMessage)MessageFactory.CreateMessage(GetRegisterRequestMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestCompleteGetRegisterRequestMessage() {
byte version = 1;
byte messageNum = 2;
long deviceID = 0xFFEEDDCCL; // ID - 4293844428
int reg = 255;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x06, (byte)RFMMessage.MessageTypes.ConfigRegisterGetRequest.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x9E, 0x0C };
System.arraycopy(BitConverter.getBytes((int)(deviceID & 0xFFFFFFFFL)), 0, test_msg, 5, 4);
System.arraycopy(BitConverter.getBytes((short)(reg & 0xFFFF)), 0, test_msg, 9, 2);
GetRegisterRequestMessageBuilder builder = new GetRegisterRequestMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
builder.DeviceID = deviceID;
builder.RegisterNumber = reg;
GetRegisterRequestMessage msg = (GetRegisterRequestMessage)MessageFactory.CreateMessage(GetRegisterRequestMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterGetRequest);
assertEquals(msg.getDeviceLongID(), deviceID);
assertEquals(msg.getRegisterNumber(), reg);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetRegisterRequestMessage)MessageFactory.CreateMessage(GetRegisterRequestMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestEmptyGetRegisterResponseMessage() {
byte version = 1;
byte messageNum = 2;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x0B, (byte)RFMMessage.MessageTypes.ConfigRegisterGetResponse.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xC9, 0x76 };
GetRegisterResponseMessageBuilder builder = new GetRegisterResponseMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
GetRegisterResponseMessage msg = (GetRegisterResponseMessage)MessageFactory.CreateMessage(GetRegisterResponseMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterGetResponse);
assertEquals(msg.getDeviceLongID(), 0L);
assertEquals(msg.getRegisterNumber(), 0);
assertEquals(msg.getRegisterValue(), 0L);
assertEquals(msg.getStatus(), ConfigureMessage.MessageStatus.OK);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetRegisterResponseMessage)MessageFactory.CreateMessage(GetRegisterResponseMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestCompleteGetRegisterResponseMessage() {
byte version = 1;
byte messageNum = 2;
long deviceID = 0xFFEEDDCCL; // ID - 4293844428
int register = 255;
long value = 0xFFEEDDCCL; // Value - 4293844428
ConfigureMessage.MessageStatus status = ConfigureMessage.MessageStatus.RegisterNumberError;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x0B, (byte)RFMMessage.MessageTypes.ConfigRegisterGetResponse.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)(status.getValue() & 0xFF), (byte)0xDA, 0x46 };
System.arraycopy(BitConverter.getBytes((int)(deviceID & 0xFFFFFFFFL)), 0, test_msg, 5, 4);
System.arraycopy(BitConverter.getBytes((short)(register & 0xFFFF)), 0, test_msg, 9, 2);
System.arraycopy(BitConverter.getBytes((int)(value & 0xFFFFFFFFL)), 0, test_msg, 11, 4);
GetRegisterResponseMessageBuilder builder = new GetRegisterResponseMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
builder.DeviceID = deviceID;
builder.RegisterNumber = register;
builder.RegisterValue = value;
builder.Status = status;
GetRegisterResponseMessage msg = (GetRegisterResponseMessage)MessageFactory.CreateMessage(GetRegisterResponseMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterGetResponse);
assertEquals(msg.getDeviceLongID(), deviceID);
assertEquals(msg.getRegisterNumber(), register);
assertEquals(msg.getRegisterValue(), value);
assertEquals(msg.getStatus(), ConfigureMessage.MessageStatus.RegisterNumberError);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (GetRegisterResponseMessage)MessageFactory.CreateMessage(GetRegisterResponseMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestEmptySetRegisterRequestMessage() {
byte version = 1;
byte messageNum = 2;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x0A, (byte)RFMMessage.MessageTypes.ConfigRegisterSetRequest.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, (byte)0xA7 };
SetRegisterRequestMessageBuilder builder = new SetRegisterRequestMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
SetRegisterRequestMessage msg = (SetRegisterRequestMessage)MessageFactory.CreateMessage(SetRegisterRequestMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterSetRequest);
assertEquals(msg.getDeviceLongID(), 0L);
assertEquals(msg.getRegisterNumber(), 0);
assertEquals(msg.getRegisterValue(), 0L);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (SetRegisterRequestMessage)MessageFactory.CreateMessage(SetRegisterRequestMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestCompleteSetRegisterRequestMessage() {
byte version = 1;
byte messageNum = 2;
long deviceID = 0xFFEEDDCCL; // ID - 4293844428
int reg = 255;
long value = 0xFFEEDDCCL;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x0A, (byte)RFMMessage.MessageTypes.ConfigRegisterSetRequest.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, (byte)0xD2 };
System.arraycopy(BitConverter.getBytes((int)(deviceID & 0xFFFFFFFFL)), 0, test_msg, 5, 4);
System.arraycopy(BitConverter.getBytes((short)(reg & 0xFFFF)), 0, test_msg, 9, 2);
System.arraycopy(BitConverter.getBytes((int)(value & 0xFFFFFFFFL)), 0, test_msg, 11, 4);
SetRegisterRequestMessageBuilder builder = new SetRegisterRequestMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
builder.DeviceID = deviceID;
builder.RegisterNumber = reg;
builder.RegisterValue = value;
SetRegisterRequestMessage msg = (SetRegisterRequestMessage)MessageFactory.CreateMessage(SetRegisterRequestMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterSetRequest);
assertEquals(msg.getDeviceLongID(), deviceID);
assertEquals(msg.getRegisterNumber(), reg);
assertEquals(msg.getRegisterValue(), value);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (SetRegisterRequestMessage)MessageFactory.CreateMessage(SetRegisterRequestMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestEmptySetRegisterResponseMessage() {
byte version = 1;
byte messageNum = 2;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x07, (byte)RFMMessage.MessageTypes.ConfigRegisterSetResponse.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, (byte)0xA3 };
SetRegisterResponseMessageBuilder builder = new SetRegisterResponseMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
SetRegisterResponseMessage msg = (SetRegisterResponseMessage)MessageFactory.CreateMessage(SetRegisterResponseMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterSetResponse);
assertEquals(msg.getDeviceLongID(), 0L);
assertEquals(msg.getRegisterNumber(), 0);
assertEquals(msg.getStatus(), ConfigureMessage.MessageStatus.OK);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (SetRegisterResponseMessage)MessageFactory.CreateMessage(SetRegisterResponseMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
@Test
public void TestCompleteSetRegisterResponseMessage() {
byte version = 1;
byte messageNum = 2;
long deviceID = 0xFFEEDDCCL; // ID - 4293844428
int register = 255;
ConfigureMessage.MessageStatus status = ConfigureMessage.MessageStatus.RegisterNumberError;
byte[] test_msg = new byte[] { (byte)0x96, version, 0x07, (byte)RFMMessage.MessageTypes.ConfigRegisterSetResponse.getValue(), messageNum, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)(status.getValue() & 0xFF), (byte)0xAC, 0x0D };
System.arraycopy(BitConverter.getBytes((int)(deviceID & 0xFFFFFFFFL)), 0, test_msg, 5, 4);
System.arraycopy(BitConverter.getBytes((short)(register & 0xFFFF)), 0, test_msg, 9, 2);
SetRegisterResponseMessageBuilder builder = new SetRegisterResponseMessageBuilder();
builder.Version = version;
builder.MessageNumber = messageNum;
builder.DeviceID = deviceID;
builder.RegisterNumber = register;
builder.Status = status;
SetRegisterResponseMessage msg = (SetRegisterResponseMessage)MessageFactory.CreateMessage(SetRegisterResponseMessage.class, builder);
assertEquals(msg.getVersion(), version);
assertEquals(msg.getMessageNumber(), messageNum);
assertEquals(msg.getMessageType(), RFMMessage.MessageTypes.ConfigRegisterSetResponse);
assertEquals(msg.getDeviceLongID(), deviceID);
assertEquals(msg.getRegisterNumber(), register);
assertEquals(msg.getStatus(), ConfigureMessage.MessageStatus.RegisterNumberError);
assertArrayEquals(msg.GetBytes(), test_msg);
// Test message parsing
msg = (SetRegisterResponseMessage)MessageFactory.CreateMessage(SetRegisterResponseMessage.class);
msg.Parse(test_msg);
assertArrayEquals(msg.GetBytes(), test_msg);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy