Skip to content

Commit

Permalink
Merge pull request #13 from anavilohia/al3750/moreUnitTests
Browse files Browse the repository at this point in the history
Add ResourceType unit tests, Update Resource unit tests
  • Loading branch information
anavilohia authored Oct 17, 2024
2 parents 9c2e19f + e5427fe commit eb7ff95
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ public ResourceType(String typeName, int totalUnits, double latitude, double lon

// Create initial resources
for (int resourceNumber = 1; resourceNumber <= totalUnits; resourceNumber++) {
String resourceId = typeName + " " + resourceNumber;
Resource newResource = new Resource(resourceId);
resources.put(resourceId, newResource);
addResource();
}
}

/**
* Adds a new resource within this resource type.
*/
public void addResource() {
public final void addResource() {
int resourceNumber = getTotalUnits() + 1;
String resourceId = typeName + " " + resourceNumber;
Resource newResource = new Resource(resourceId);
Expand Down Expand Up @@ -75,7 +73,7 @@ public Resource findAvailableResource(LocalDateTime startTime) {
return null;
}

public int getTotalUnits() {
public final int getTotalUnits() {
return resources.size();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package dev.coms4156.project.livesched;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

/**
* Unit tests to be used for ResourceType class.
*/
@SpringBootTest
@ContextConfiguration
class ResourceTypeUnitTests {

/**
* Set up to be run before all tests.
*/
@BeforeEach
void setupResourceTypeForTesting() {

testResourceType = new ResourceType(testTypeName, testTotalUnits, testLatitude, testLongitude);
testStartTime = LocalDateTime.now();
}

/**
* Test for ResourceType class addResource method.
*/
@Test
void addResourceTest() {
assertEquals(testResourceType.getTotalUnits(), testTotalUnits,
"Total number of resources should be " + testTotalUnits + " before add");

testResourceType.addResource();
int expectedNewTotal = testTotalUnits + 1;
assertEquals(testResourceType.getTotalUnits(), expectedNewTotal,
"Total number of resources should be " + expectedNewTotal + " after adding one resource");

testResourceType.addResource();
testResourceType.addResource();
testResourceType.addResource();
expectedNewTotal = expectedNewTotal + 3;
assertEquals(testResourceType.getTotalUnits(), expectedNewTotal,
"Total number of resources should be "
+ expectedNewTotal
+ " after adding three resources");
}

/**
* Test for ResourceType class findAvailableResource method.
*/
@Test
void findAvailableResourceTest() {

// Create mock resources
mockResource1 = mock(Resource.class);
mockResource2 = mock(Resource.class);

try {
// Inject mock resources into the private 'resources' field
Field resourcesField = ResourceType.class.getDeclaredField("resources");
resourcesField.setAccessible(true); // Make the private field accessible
Map<String, Resource> resources =
(Map<String, Resource>) resourcesField.get(testResourceType);
resources.put("Hospital 1", mockResource1);
resources.put("Hospital 2", mockResource2);

} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
fail("Reflection failed to access or modify the 'resources' field: " + e.getMessage());
}

when(mockResource1.isAvailableAt(testStartTime)).thenReturn(false);
when(mockResource2.isAvailableAt(testStartTime)).thenReturn(true);
Resource availableResource = testResourceType.findAvailableResource(testStartTime);
assertEquals(mockResource2, availableResource,
"The available resource should be 'Hospital 2'");

when(mockResource1.isAvailableAt(testStartTime)).thenReturn(false);
when(mockResource2.isAvailableAt(testStartTime)).thenReturn(false);
availableResource = testResourceType.findAvailableResource(testStartTime);
assertNull(availableResource,
"No resources should be available at the given time");

Exception exception = assertThrows(IllegalArgumentException.class, () ->
testResourceType.findAvailableResource(null));
assertEquals("Start time cannot be null.", exception.getMessage(),
"The exception message should indicate that start time cannot be null.");
}

/**
* Test for ResourceType class countAvailableUnits method.
*/
@Test
void countAvailableUnitsTest() {

// Create mock resources
mockResource1 = mock(Resource.class);
mockResource2 = mock(Resource.class);
mockResource3 = mock(Resource.class);

try {
// Inject mock resources into the private 'resources' field
Field resourcesField = ResourceType.class.getDeclaredField("resources");
resourcesField.setAccessible(true); // Make the private field accessible
Map<String, Resource> resources =
(Map<String, Resource>) resourcesField.get(testResourceType);
resources.put("Hospital 1", mockResource1);
resources.put("Hospital 2", mockResource2);
resources.put("Hospital 3", mockResource2);

} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
fail("Reflection failed to access or modify the 'resources' field: " + e.getMessage());
}

when(mockResource1.isAvailableAt(testStartTime)).thenReturn(false);
when(mockResource2.isAvailableAt(testStartTime)).thenReturn(false);
when(mockResource2.isAvailableAt(testStartTime)).thenReturn(false);
int availableResourceCount = testResourceType.countAvailableUnits(testStartTime);
assertEquals(0, availableResourceCount,
"The available resource should be " + 0);

when(mockResource1.isAvailableAt(testStartTime)).thenReturn(true);
availableResourceCount = testResourceType.countAvailableUnits(testStartTime);
assertEquals(1, availableResourceCount,
"The available resource should be " + 1);

when(mockResource2.isAvailableAt(testStartTime)).thenReturn(true);
when(mockResource3.isAvailableAt(testStartTime)).thenReturn(true);
availableResourceCount = testResourceType.countAvailableUnits(testStartTime);
assertEquals(3, availableResourceCount,
"The available resource should be " + 3);
}

/**
* Test for ResourceType class getTotalUnits method.
*/
@Test
void getTotalUnitsTest() {
assertEquals(testResourceType.getTotalUnits(), testTotalUnits,
"Total number of resources should be " + testTotalUnits + " before add");

testResourceType.addResource();
testResourceType.addResource();
testResourceType.addResource();
int expectedNewTotal = testTotalUnits + 3;
assertEquals(testResourceType.getTotalUnits(), expectedNewTotal,
"Total number of resources should be "
+ expectedNewTotal
+ " after adding three resources");
}

/**
* Test for ResourceType class getLocation method.
*/
@Test
void getLocationTest() {
String expectedResult = testLatitude + ", " + testLongitude;
assertEquals(testResourceType.getLocation(), expectedResult,
"Resource location should be " + expectedResult);

expectedResult = testLongitude + ", " + testLatitude;
assertNotEquals(testResourceType.getLocation(), expectedResult,
"Resource location should not be " + expectedResult);

assertNotEquals(testResourceType.getLocation(), "",
"Resource location should not be empty string");
}

/**
* Test for ResourceType class updateLocation method.
*/
@Test
void updateLocationTest() {
String expectedResult = testLatitude + ", " + testLongitude;
assertEquals(testResourceType.getLocation(), expectedResult,
"Resource location should be " + expectedResult + " before update");

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
testResourceType.updateLocation(-91.0, testLongitude);
}, "Resource cannot be assigned invalid latitude");
assertEquals("Latitude must be between -90 and 90.", exception.getMessage(),
"Exception message should match for latitude out of bounds");

exception = assertThrows(IllegalArgumentException.class, () -> {
testResourceType.updateLocation(testLatitude, 300.2);
}, "Resource cannot be assigned invalid longitude");
assertEquals("Longitude must be between -180 and 180.", exception.getMessage(),
"Exception message should match for longitude out of bounds");

testResourceType.updateLocation(-90.0, 145.34);
assertEquals(testResourceType.getLocation(), "-90.0, 145.34",
"Resource location should be -90.0, 145.34 after update");
}




/**
* These instances are used for testing.
*/
public static ResourceType testResourceType;
private Resource mockResource1;
private Resource mockResource2;
private Resource mockResource3;
final String testTypeName = "Hospital";
final int testTotalUnits = 0;
final double testLatitude = 80.0;
final double testLongitude = 55.65;
LocalDateTime testStartTime;
}

Loading

0 comments on commit eb7ff95

Please sign in to comment.