Skip to content

Commit

Permalink
modify internal integration tests (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
anavilohia authored Dec 6, 2024
1 parent 543cc48 commit 84949f3
Showing 1 changed file with 116 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,8 +26,97 @@
*/
@SpringBootTest
@ContextConfiguration
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class InternalIntegrationTests {

private RouteController routeController;

@BeforeEach
void setUp() {
routeController = new RouteController();
}

/**
* This test ensures that a task can be added and retrieved successfully.
*/
@Test
@Order(1)
void integrationTestAddAndRetrieveTask() {
// Add tasks for TestClient1 and TestClient2
String startTime = LocalDateTime.now().plusHours(2)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
String endTime = LocalDateTime.now().plusHours(4)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));

routeController.addTask("Task1", 1, startTime, endTime, 40.71, -74.0, "TestClient1");
routeController.addTask("Task2", 3, startTime, endTime, 20.71, -80.3, "TestClient2");

// Retrieve tasks for TestClient1
ResponseEntity<?> response = routeController.retrieveTasks("TestClient1");
List<Task> responseBody = (List<Task>) response.getBody();

// Assert that response body size is accurate to clientId
assertEquals(1, responseBody.size(), "TestClient1 database should contain 1 task");

// Assert that details of the retrieved Task are accurate
Task task1 = responseBody.get(0);
assertEquals("Task1", task1.getTaskName(), "Task name should match");
assertTrue(task1.getResources().isEmpty(), "Task resources should be empty");
}

/**
* This test ensures that resource types can be added and retrieved successfully.
*/
@Test
@Order(2)
void integrationTestAddAndRetrieveResourceTypes() {
// Add resource types
routeController.addResourceType("ResourceType1", 5, 40.71, -74.0, "TestClient1");
routeController.addResourceType("ResourceType2", 10, 43.65, -84.0, "TestClient1");

// Retrieve resource types using the RouteController
ResponseEntity<?> response = routeController.retrieveResourceTypes("TestClient1");
List<ResourceType> responseBody = (List<ResourceType>) response.getBody();

// Assert that the response contains the added resource types
assertNotNull(responseBody, "Response body should not be null");
assertEquals(2, responseBody.size(), "There should be 2 resource types");

// Assert the details of the retrieved resourceType are accurate
ResourceType resourceType2 = responseBody.get(1);
assertEquals("ResourceType2", resourceType2.getTypeName(),
"Second resource type name should match");
assertEquals(10, resourceType2.getTotalUnits(),
"Second resource type units should match");
}

/**
* This test ensures that resource types for a specific task can be modified successfully.
*/
@Test
@Order(3)
void integrationTestModifyResourceType() {
// Find taskId
ResponseEntity<?> response = routeController.retrieveTasks("TestClient1");
List<Task> responseBody = (List<Task>) response.getBody();
String task1Id = responseBody.get(0).getTaskId();

// Modify the resource type for Task1
routeController.modifyResourceType(task1Id, "ResourceType1", 3, "TestClient1");

// Retrieve modified task
response = routeController.retrieveTask(task1Id, "TestClient1");
Task modifiedTask = (Task) response.getBody();

// Assert that the resourceType was modified correctly for task
Map<ResourceType, Integer> resources = modifiedTask.getResources();
assertEquals(1, resources.size(), "Task should have exactly 1 resource type assigned");
ResourceType assignedResource = resources.keySet().iterator().next();
assertEquals("ResourceType1", assignedResource.getTypeName(),
"Resource type name should match");
assertEquals(3, resources.get(assignedResource), "Resource quantity should be updated to 3");
}

/**
* This test ensures that:
* 1. The RouteController correctly interacts with LiveSchedApplication
Expand All @@ -30,32 +125,10 @@ public class InternalIntegrationTests {
* 3. The resources are correctly assigned to tasks in the schedule.
*/
@Test
void integrationTestUpdateSchedule() {
// Setup test database and controller
MyFileDatabase testDatabase = new MyFileDatabase(1,
"tasks.dat", "resources.dat", "schedules.dat",
"tasksObject", "resourcesObject", "schedulesObject");

// Use overrideDatabase to set up the test database in the application
LiveSchedApplication.overrideDatabase(testDatabase, "testClient");

// Add ResourceTypes
ResourceType resourceType1 = new ResourceType("Type1", 3, 40.71, -74.0);
ResourceType resourceType2 = new ResourceType("Type2", 5, 40.71, -74.0);
testDatabase.addResourceType(resourceType1);
testDatabase.addResourceType(resourceType2);

// Add a task that needs the above ResourceTypes
Map<ResourceType, Integer> resources = new HashMap<>();
resources.put(resourceType1, 1);
resources.put(resourceType2, 2);
Task task = new Task("1", "Test Task", resources, 1,
LocalDateTime.now(), LocalDateTime.now().plusHours(1), 40.71, -74.0);
testDatabase.addTask(task);

@Order(4)
void integrationTestUpdateandRetrieveSchedule() {
// Call the updateSchedule endpoint
RouteController routeController = new RouteController();
ResponseEntity<?> response = routeController.updateSchedule(10.0, "testClient");
ResponseEntity<?> response = routeController.updateSchedule(10.0, "TestClient1");

// Assert that the response contains the updated schedule
assertEquals(HttpStatus.OK, response.getStatusCode(), "Response should have status 200 OK");
Expand All @@ -64,19 +137,28 @@ void integrationTestUpdateSchedule() {
assertFalse(schedule.isEmpty(), "Schedule should not be empty");

// Assert that the database contains the updated schedule
MyFileDatabase testDatabase = LiveSchedApplication.getClientFileDatabase("TestClient1");
Schedule masterSchedule = testDatabase.getMasterSchedule();
assertNotNull(masterSchedule, "Database's master schedule should not be null");
assertEquals(1, masterSchedule.getTaskSchedule().size(),
"Master schedule should contain 1 task");

// Verify that the resources were correctly assigned in the schedule
Map.Entry<Task, List<Resource>> scheduleEntry =
masterSchedule.getTaskSchedule().entrySet().iterator().next();
assertEquals(task.getTaskId(), scheduleEntry.getKey().getTaskId(), "Task ID should match");
assertEquals(3, scheduleEntry.getValue().size(),
"Total three resources should be assigned");
// Retrieve the updated schedule again using retrieveSchedule
response = routeController.retrieveSchedule("TestClient1");
schedule = (List<Map<String, Object>>) response.getBody();

LiveSchedApplication.restoreDatabase("testClient");
}
// Assert that the schedule contains Task1 with the correct assigned resourceType
Map<String, Object> scheduleEntry = schedule.get(0);

Task scheduledTask = (Task) scheduleEntry.get("task");
assertEquals("Task1", scheduledTask.getTaskName(), "Task name should match");

}
List<Resource> scheduledResources = (List<Resource>) scheduleEntry.get("assignedResources");
Resource resource1 = scheduledResources.get(0);
assertEquals(3, scheduledResources.size(), "Task should have 3 assigned resources");
assertTrue(resource1.getResourceId().contains("ResourceType1"),
"First resource should be ResourceType1");

LiveSchedApplication.restoreDatabase("TestClient1");
}
}

0 comments on commit 84949f3

Please sign in to comment.