forked from Luke-Sikina/picsure-search-refinement
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit and integration tests for LegacySearch
Introduces unit tests for `LegacySearchQueryMapper` to validate JSON parsing and string replacement functionality. Adds integration tests for `LegacySearchController` to verify search response correctness using a PostgreSQL container.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
.../harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.legacysearch; | ||
|
||
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.LegacyResponse; | ||
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.Results; | ||
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.SearchResult; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@SpringBootTest | ||
@Testcontainers | ||
class LegacySearchControllerIntegrationTest { | ||
|
||
@Autowired | ||
LegacySearchController legacySearchController; | ||
|
||
@Container | ||
static final PostgreSQLContainer<?> databaseContainer = new PostgreSQLContainer<>("postgres:16").withReuse(true) | ||
.withCopyFileToContainer(MountableFile.forClasspathResource("seed.sql"), "/docker-entrypoint-initdb.d/seed.sql"); | ||
|
||
@DynamicPropertySource | ||
static void mySQLProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", databaseContainer::getJdbcUrl); | ||
registry.add("spring.datasource.username", databaseContainer::getUsername); | ||
registry.add("spring.datasource.password", databaseContainer::getPassword); | ||
registry.add("spring.datasource.db", databaseContainer::getDatabaseName); | ||
} | ||
|
||
@Test | ||
void shouldGetLegacyResponseByStudyID() throws IOException { | ||
String jsonString = """ | ||
{"query":{"searchTerm":"phs000007","includedTags":[],"excludedTags":[],"returnTags":"true","offset":0,"limit":100}} | ||
"""; | ||
|
||
ResponseEntity<LegacyResponse> legacyResponseResponseEntity = legacySearchController.legacySearch(jsonString); | ||
System.out.println(legacyResponseResponseEntity); | ||
Assertions.assertEquals(HttpStatus.OK, legacyResponseResponseEntity.getStatusCode()); | ||
LegacyResponse legacyResponseBody = legacyResponseResponseEntity.getBody(); | ||
Assertions.assertNotNull(legacyResponseBody); | ||
Results results = legacyResponseBody.results(); | ||
List<SearchResult> searchResults = results.searchResults(); | ||
searchResults.forEach(searchResult -> Assertions.assertEquals("phs000007", searchResult.result().studyId())); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...t/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchQueryMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.legacysearch; | ||
|
||
import edu.harvard.dbmi.avillach.dictionary.filter.Filter; | ||
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.LegacySearchQuery; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.io.IOException; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
class LegacySearchQueryMapperTest { | ||
|
||
@Autowired | ||
LegacySearchQueryMapper legacySearchQueryMapper; | ||
|
||
@Test | ||
void shouldParseSearchRequest() throws IOException { | ||
String jsonString = """ | ||
{"query":{"searchTerm":"age","includedTags":[],"excludedTags":[],"returnTags":"true","offset":0,"limit":100}} | ||
"""; | ||
|
||
LegacySearchQuery legacySearchQuery = legacySearchQueryMapper.mapFromJson(jsonString); | ||
Filter filter = legacySearchQuery.filter(); | ||
Pageable pageable = legacySearchQuery.pageable(); | ||
|
||
Assertions.assertEquals("age", filter.search()); | ||
Assertions.assertEquals(100, pageable.getPageSize()); | ||
} | ||
|
||
@Test | ||
void shouldReplaceUnderscore() throws IOException { | ||
String jsonString = | ||
""" | ||
{"query":{"searchTerm":"tutorial-biolincc_digitalis","includedTags":[],"excludedTags":[],"returnTags":"true","offset":0,"limit":100}} | ||
"""; | ||
|
||
LegacySearchQuery legacySearchQuery = legacySearchQueryMapper.mapFromJson(jsonString); | ||
Filter filter = legacySearchQuery.filter(); | ||
Pageable pageable = legacySearchQuery.pageable(); | ||
|
||
Assertions.assertEquals("tutorial-biolincc/digitalis", filter.search()); | ||
Assertions.assertEquals(100, pageable.getPageSize()); | ||
} | ||
|
||
} |