Skip to content

Commit

Permalink
Add unit test for new added interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyuan0124 committed Nov 18, 2024
1 parent b1679a5 commit 58c8a12
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 LEIDOS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.eclipse.mosaic.interactions.msger;
import org.junit.Test;
import static org.junit.Assert.*;

import org.eclipse.mosaic.interactions.application.MsgerRequestTrafficEvent;

public class MsgerRequestTrafficEventTest {

@Test
public void testConstructorAndGetters() {
long time = 100L;
String vehicleId = "vehicle123";
String parameterName = "speed";

MsgerRequestTrafficEvent event = new MsgerRequestTrafficEvent(time, vehicleId, parameterName);

// Validate constructor and getters
assertEquals(time, event.getTime());
assertEquals(vehicleId, event.vehicleId());
assertEquals(parameterName, event.getParameterName());
}

@Test
public void testEqualsAndHashCode() {
MsgerRequestTrafficEvent event1 = new MsgerRequestTrafficEvent(100L, "vehicle123", "speed");
MsgerRequestTrafficEvent event2 = new MsgerRequestTrafficEvent(100L, "vehicle123", "speed");
MsgerRequestTrafficEvent event3 = new MsgerRequestTrafficEvent(200L, "vehicle456", "position");

// Test equality
assertEquals(event1, event2);
assertNotEquals(event1, event3);

// Test hash codes
assertEquals(event1.hashCode(), event2.hashCode());
assertNotEquals(event1.hashCode(), event3.hashCode());
}

@Test
public void testToString() {
MsgerRequestTrafficEvent event = new MsgerRequestTrafficEvent(100L, "vehicle123", "speed");

// Verify toString output
String expectedString = "MsgerRequestTrafficEvent[vehicleId=vehicle123,Parameter=speed]";
assertEquals(expectedString, event.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.eclipse.mosaic.interactions.msger;

import org.eclipse.mosaic.interactions.application.MsgerResponseTrafficEvent;
import org.eclipse.mosaic.lib.objects.trafficevent.MsgerTrafficEvent;
import org.junit.Test;
import static org.junit.Assert.*;

public class MsgerResponseTrafficEventTest {

@Test
public void testConstructorAndGetters() {
long time = 100L;
MsgerTrafficEvent trafficEvent = new MsgerTrafficEvent("vehicle123", 100.5f, 200.5f, 5.0f, 45.0f);

MsgerResponseTrafficEvent response = new MsgerResponseTrafficEvent(time, trafficEvent);

// Validate constructor and getters
assertEquals(time, response.getTime());
assertEquals(trafficEvent, response.getTrafficEvent());
}

@Test
public void testToString() {
MsgerTrafficEvent trafficEvent = new MsgerTrafficEvent("vehicle123", 100.5f, 200.5f, 5.0f, 45.0f);
MsgerResponseTrafficEvent response = new MsgerResponseTrafficEvent(100L, trafficEvent);

// Verify toString output
String toStringOutput = response.toString();
assertTrue(toStringOutput.contains("upTrack=100.5"));
assertTrue(toStringOutput.contains("downTrack=200.5"));
assertTrue(toStringOutput.contains("minimumGap=5.0"));
assertTrue(toStringOutput.contains("advisorySpeed=45.0"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.eclipse.mosaic.lib.trafficevent;

import org.junit.Test;
import static org.junit.Assert.*;

import org.eclipse.mosaic.lib.objects.trafficevent.MsgerTrafficEvent;

public class MsgerTrafficEventTest {

@Test
public void testConstructorAndGetters() {
String vehicleId = "vehicle123";
float upTrack = 100.5f;
float downTrack = 200.5f;
float minimumGap = 5.0f;
float advisorySpeed = 45.0f;

MsgerTrafficEvent event = new MsgerTrafficEvent(vehicleId, upTrack, downTrack, minimumGap, advisorySpeed);

// Validate constructor and getters
assertEquals(vehicleId, event.getVehicleId());
assertEquals(upTrack, event.getUpTrack(), 0.001);
assertEquals(downTrack, event.getDownTrack(), 0.001);
assertEquals(minimumGap, event.getMinimumGap(), 0.001);
assertEquals(advisorySpeed, event.getAdvisorySpeed(), 0.001);
}

@Test
public void testEqualsAndHashCode() {
MsgerTrafficEvent event1 = new MsgerTrafficEvent("vehicle123", 100.5f, 200.5f, 5.0f, 45.0f);
MsgerTrafficEvent event2 = new MsgerTrafficEvent("vehicle123", 100.5f, 200.5f, 5.0f, 45.0f);
MsgerTrafficEvent event3 = new MsgerTrafficEvent("vehicle456", 50.0f, 150.0f, 3.0f, 30.0f);

// Test equality
assertEquals(event1, event2);
assertNotEquals(event1, event3);

// Test hash codes
assertEquals(event1.hashCode(), event2.hashCode());
assertNotEquals(event1.hashCode(), event3.hashCode());
}

@Test
public void testToString() {
MsgerTrafficEvent event = new MsgerTrafficEvent("vehicle123", 100.5f, 200.5f, 5.0f, 45.0f);

// Verify toString output
String expectedString = "MsgerTrafficEvent[vehicleId=vehicle123,upTrack=100.5,downTrack=200.5,minimumGap=5.0,advisorySpeed=45.0]";
assertTrue(event.toString().contains("vehicleId=vehicle123"));
assertTrue(event.toString().contains("upTrack=100.5"));
assertTrue(event.toString().contains("downTrack=200.5"));
assertTrue(event.toString().contains("minimumGap=5.0"));
assertTrue(event.toString().contains("advisorySpeed=45.0"));
}
}

0 comments on commit 58c8a12

Please sign in to comment.