Skip to content

Commit

Permalink
Added unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Jul 31, 2023
1 parent 8d0c6ea commit f2c5463
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void bind() throws IOException {
* @throws IOException If there is an issue with the underlying socket object or
* methods
*/
public void sendMsgs(byte[] data) throws IOException {
public void sendV2xMsg(byte[] data) throws IOException {
sendPacket(data, rxMessagePort);
}

Expand All @@ -220,7 +220,7 @@ public void sendMsgs(byte[] data) throws IOException {
* @param data The binary data to transmit
* @throws IOException If there is an issue with the underlying socket object or methods
*/
public void sendTimeSyncMsgs(byte[] data) throws IOException {
public void sendTimeSyncMsg(byte[] data) throws IOException {
sendPacket(data, timeSyncPort);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void onV2XMessageRx(byte[] rxMsg, String rxRsuId) {

InfrastructureInstance rsu = managedInstances.get(rxRsuId);
try {
rsu.sendMsgs(rxMsg);
rsu.sendV2xMsg(rxMsg);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public void onTimeStepUpdate(InfrastructureTimeMessage message) throws IOExcepti
}

for (InfrastructureInstance currentInstance : managedInstances.values()) {
currentInstance.sendTimeSyncMsgs(encodeTimeMessage(message));
currentInstance.sendTimeSyncMsg(encodeTimeMessage(message));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.eclipse.mosaic.fed.infrastructure.ambassador;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -28,10 +27,8 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;

import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction;
import org.eclipse.mosaic.lib.geo.CartesianPoint;
import org.eclipse.mosaic.lib.math.Vector3d;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
Expand All @@ -42,9 +39,6 @@
import org.eclipse.mosaic.lib.objects.detector.Size;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.matchers.Any;
import org.mockito.internal.verification.Times;

import com.google.gson.Gson;

Expand Down Expand Up @@ -123,9 +117,9 @@ public void testOnTimeStepUpdate() throws IOException {
Gson gson = new Gson();
byte[] datagram = gson.toJson(message).getBytes();
// Verify that all instances sendTimeSyncMsgs was called.
verify(instance1).sendTimeSyncMsgs(datagram);
verify(instance2).sendTimeSyncMsgs(datagram);
verify(instance2).sendTimeSyncMsgs(datagram);
verify(instance1).sendTimeSyncMsg(datagram);
verify(instance2).sendTimeSyncMsg(datagram);
verify(instance2).sendTimeSyncMsg(datagram);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
*/
package org.eclipse.mosaic.fed.infrastructure.ambassador;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
Expand All @@ -30,6 +36,7 @@
import org.eclipse.mosaic.lib.objects.detector.Orientation;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.internal.util.reflection.FieldSetter;

public class InfrastructureInstanceTest {
Expand Down Expand Up @@ -112,4 +119,45 @@ public void testGetterSetterConstructor() {


}

@Test
public void testSendV2xMsg() throws IOException {
String test_msg = "test message";
instance.sendV2xMsg(test_msg.getBytes());
ArgumentCaptor<DatagramPacket> packet = ArgumentCaptor.forClass(DatagramPacket.class);

verify(socket, times(1)).send(packet.capture());

assertArrayEquals(test_msg.getBytes(), packet.getValue().getData());
assertEquals(instance.getRxMessagePort(), packet.getValue().getPort());
assertEquals(address, packet.getValue().getAddress());
}

@Test
public void testSendTimeSyncMsg() throws IOException {
String test_msg = "test message";
instance.sendTimeSyncMsg(test_msg.getBytes());
ArgumentCaptor<DatagramPacket> packet = ArgumentCaptor.forClass(DatagramPacket.class);

verify(socket, times(1)).send(packet.capture());

assertArrayEquals(test_msg.getBytes(), packet.getValue().getData());
assertEquals(instance.getTimeSyncPort(), packet.getValue().getPort());
assertEquals(address, packet.getValue().getAddress());
}

@Test
public void testSendInteraction() throws IOException {
String test_msg = "test message";
instance.sendInteraction(test_msg.getBytes());
ArgumentCaptor<DatagramPacket> packet = ArgumentCaptor.forClass(DatagramPacket.class);

verify(socket, times(1)).send(packet.capture());

assertArrayEquals(test_msg.getBytes(), packet.getValue().getData());
assertEquals(instance.getSimulatedInteractionPort(), packet.getValue().getPort());
assertEquals(address, packet.getValue().getAddress());
}


}

0 comments on commit f2c5463

Please sign in to comment.