HARDCODED_REQUEST_TYPES =
+ Arrays.asList(
+ "CS Department BS/MS program",
+ "Scholarship or Fellowship",
+ "MS program (other than CS Dept BS/MS)",
+ "PhD program",
+ "Other");
+
+ /** Result class for loadRequestTypes method. */
+ @Data
+ @AllArgsConstructor
+ public static class LoadResult {
+ private int loaded;
+ private int skipped;
+ }
+
+ /**
+ * Load hardcoded request types into the database if they don't already exist.
+ *
+ * This method checks for each hardcoded request type and only creates it if it's not already
+ * in the database.
+ *
+ * @return LoadResult containing the number of types loaded and skipped
+ */
+ public LoadResult loadRequestTypes() {
+ log.info("Loading hardcoded request types...");
+ int loadedCount = 0;
+ int skippedCount = 0;
+
+ for (String type : HARDCODED_REQUEST_TYPES) {
+ if (requestTypeRepository.findByRequestType(type).isEmpty()) {
+ RequestType requestType = RequestType.builder().requestType(type).build();
+ requestTypeRepository.save(requestType);
+ log.info("Loaded request type: {}", type);
+ loadedCount++;
+ } else {
+ log.debug("Request type already exists, skipping: {}", type);
+ skippedCount++;
+ }
+ }
+
+ log.info("Request type loading completed. Loaded: {}, Skipped: {}", loadedCount, skippedCount);
+ return new LoadResult(loadedCount, skippedCount);
+ }
+}
diff --git a/src/main/resources/application-development.properties b/src/main/resources/application-development.properties
index 3e3da24c..e9de84ee 100644
--- a/src/main/resources/application-development.properties
+++ b/src/main/resources/application-development.properties
@@ -1,6 +1,6 @@
logging.level.sql=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
-spring.datasource.url=jdbc:h2:file:./target/db-development
+spring.datasource.url=jdbc:h2:file:./target/db-development;AUTO_SERVER=TRUE
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.settings.web-allow-others=true
diff --git a/src/test/java/edu/ucsb/cs156/rec/services/RequestTypeServiceTest.java b/src/test/java/edu/ucsb/cs156/rec/services/RequestTypeServiceTest.java
new file mode 100644
index 00000000..ec268ea7
--- /dev/null
+++ b/src/test/java/edu/ucsb/cs156/rec/services/RequestTypeServiceTest.java
@@ -0,0 +1,86 @@
+package edu.ucsb.cs156.rec.services;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import edu.ucsb.cs156.rec.entities.RequestType;
+import edu.ucsb.cs156.rec.repositories.RequestTypeRepository;
+import java.util.Optional;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class RequestTypeServiceTest {
+
+ @Mock private RequestTypeRepository requestTypeRepository;
+
+ @InjectMocks private RequestTypeService requestTypeService;
+
+ @Test
+ public void test_loadRequestTypes_loadsAllTypesWhenNoneExist() {
+ // Arrange
+ when(requestTypeRepository.findByRequestType(any(String.class))).thenReturn(Optional.empty());
+
+ // Act
+ RequestTypeService.LoadResult result = requestTypeService.loadRequestTypes();
+
+ // Assert
+ // Should save 5 request types: CS Department BS/MS program, Scholarship or Fellowship,
+ // MS program (other than CS Dept BS/MS), PhD program, Other
+ verify(requestTypeRepository, times(5)).save(any(RequestType.class));
+ verify(requestTypeRepository, times(5)).findByRequestType(any(String.class));
+ assertEquals(5, result.getLoaded());
+ assertEquals(0, result.getSkipped());
+ }
+
+ @Test
+ public void test_loadRequestTypes_skipsExistingTypes() {
+ // Arrange
+ RequestType existingType = RequestType.builder().id(1L).requestType("Other").build();
+
+ // Mock that some types exist and some don't
+ when(requestTypeRepository.findByRequestType("CS Department BS/MS program"))
+ .thenReturn(Optional.empty());
+ when(requestTypeRepository.findByRequestType("Scholarship or Fellowship"))
+ .thenReturn(Optional.of(existingType));
+ when(requestTypeRepository.findByRequestType("MS program (other than CS Dept BS/MS)"))
+ .thenReturn(Optional.empty());
+ when(requestTypeRepository.findByRequestType("PhD program")).thenReturn(Optional.empty());
+ when(requestTypeRepository.findByRequestType("Other")).thenReturn(Optional.of(existingType));
+
+ // Act
+ RequestTypeService.LoadResult result = requestTypeService.loadRequestTypes();
+
+ // Assert
+ // Should only save 3 new types (skipping "Scholarship or Fellowship" and "Other")
+ verify(requestTypeRepository, times(3)).save(any(RequestType.class));
+ verify(requestTypeRepository, times(5)).findByRequestType(any(String.class));
+ assertEquals(3, result.getLoaded());
+ assertEquals(2, result.getSkipped());
+ }
+
+ @Test
+ public void test_loadRequestTypes_skipsAllWhenAllExist() {
+ // Arrange
+ RequestType existingType = RequestType.builder().id(1L).requestType("Some Type").build();
+
+ when(requestTypeRepository.findByRequestType(any(String.class)))
+ .thenReturn(Optional.of(existingType));
+
+ // Act
+ RequestTypeService.LoadResult result = requestTypeService.loadRequestTypes();
+
+ // Assert
+ // Should not save any types since all already exist
+ verify(requestTypeRepository, times(0)).save(any(RequestType.class));
+ verify(requestTypeRepository, times(5)).findByRequestType(any(String.class));
+ assertEquals(0, result.getLoaded());
+ assertEquals(5, result.getSkipped());
+ }
+}