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 tests for DashboardDrawer components
- Added `DashboardDrawerServiceTest` to test service logic - Created `DashboardDrawerControllerTest` for controller layer tests - Implemented `DashboardDrawerRepositoryTest` for repository queries - Updated `application.properties` to set default layout type and disabled BDC hack logs in test and main resources
- Loading branch information
Showing
5 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
...a/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerControllerTest.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,47 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer; | ||
|
||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@Tag("unit") | ||
@WebMvcTest(DashboardDrawerController.class) | ||
class DashboardDrawerControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private DashboardDrawerService dashboardDrawerService; | ||
|
||
@Test | ||
void testFindAll() throws Exception { | ||
DashboardDrawerList mockList = new DashboardDrawerList(new ArrayList<>()); // Populate mock data if needed | ||
when(dashboardDrawerService.findAll()).thenReturn(mockList); | ||
|
||
mockMvc.perform(get("/dashboard-drawer").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); | ||
} | ||
|
||
@Test | ||
void testFindByDatasetId() throws Exception { | ||
DashboardDrawer mockDrawer = | ||
new DashboardDrawer(1, "Full Name", "Abbreviation", List.of("Consent 1"), "Summary", List.of("Focus 1"), "Design", "Sponsor"); | ||
|
||
when(dashboardDrawerService.findByDatasetId(1)).thenReturn(mockDrawer); | ||
|
||
mockMvc.perform(get("/dashboard-drawer/1").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...a/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerRepositoryTest.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,72 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer; | ||
|
||
import org.junit.jupiter.api.Tag; | ||
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; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@Tag("unit") | ||
@ExtendWith(MockitoExtension.class) | ||
class DashboardDrawerRepositoryTest { | ||
|
||
@Mock | ||
private NamedParameterJdbcTemplate template; | ||
|
||
@InjectMocks | ||
private DashboardDrawerRepository repository; | ||
|
||
@Test | ||
void testGetDashboardDrawerRowsWithoutDatasetId_Success() { | ||
DashboardDrawer mockDashboardDrawer = new DashboardDrawer( | ||
1, // datasetId | ||
"Study Full Name", // studyFullname | ||
"Study Abbreviation", // studyAbbreviation | ||
List.of("Consent Group 1"), // consentGroups | ||
"Study Summary", // studySummary | ||
List.of("Study Focus 1"), // studyFocus | ||
"Study Design", // studyDesign | ||
"Sponsor" // sponsor | ||
); | ||
|
||
List<DashboardDrawer> expectedRows = Collections.singletonList(mockDashboardDrawer); | ||
when(template.query(anyString(), any(DashboardDrawerRowMapper.class))).thenReturn(expectedRows); | ||
|
||
List<DashboardDrawer> result = repository.getDashboardDrawerRows(); | ||
|
||
assertEquals(expectedRows, result); | ||
verify(template, times(1)).query(anyString(), any(DashboardDrawerRowMapper.class)); | ||
} | ||
|
||
@Test | ||
void testGetDashboardDrawerRowsWithDatasetId_Success() { | ||
DashboardDrawer mockDashboardDrawer = new DashboardDrawer( | ||
1, // datasetId | ||
"Study Full Name", // studyFullname | ||
"Study Abbreviation", // studyAbbreviation | ||
List.of("Consent Group 1"), // consentGroups | ||
"Study Summary", // studySummary | ||
List.of("Study Focus 1"), // studyFocus | ||
"Study Design", // studyDesign | ||
"Sponsor" // sponsor | ||
); | ||
|
||
List<DashboardDrawer> expectedRows = Collections.singletonList(mockDashboardDrawer); | ||
when(template.query(anyString(), any(MapSqlParameterSource.class), any(DashboardDrawerRowMapper.class))).thenReturn(expectedRows); | ||
|
||
List<DashboardDrawer> result = repository.getDashboardDrawerRows(1); | ||
|
||
assertEquals(expectedRows, result); | ||
verify(template, times(1)).query(anyString(), any(MapSqlParameterSource.class), any(DashboardDrawerRowMapper.class)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerServiceTest.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,49 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer; | ||
|
||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.Mockito.*; | ||
|
||
@Tag("unit") | ||
@ExtendWith(MockitoExtension.class) | ||
class DashboardDrawerServiceTest { | ||
|
||
@Mock | ||
private DashboardDrawerRepository repository; | ||
|
||
private static final String DASHBOARD_LAYOUT_BDC = "bdc"; | ||
private static final String DASHBOARD_LAYOUT_OTHER = "other"; | ||
|
||
@Test | ||
void testFindAll_WithBDCLayout() { | ||
List<DashboardDrawer> mockRecords = List.of( | ||
new DashboardDrawer(1, "Full Name", "Abbreviation", List.of("Consent 1"), "Summary", List.of("Focus 1"), "Design", "Sponsor") | ||
); | ||
when(repository.getDashboardDrawerRows()).thenReturn(mockRecords); | ||
|
||
DashboardDrawerService serviceWithBDCLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_BDC); | ||
|
||
DashboardDrawerList result = serviceWithBDCLayout.findAll(); | ||
|
||
assertEquals(mockRecords, result.dashboardDrawerList()); | ||
verify(repository, times(1)).getDashboardDrawerRows(); | ||
} | ||
|
||
@Test | ||
void testFindAll_WithNonBDCLayout() { | ||
DashboardDrawerService serviceWithOtherLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_OTHER); | ||
|
||
DashboardDrawerList result = serviceWithOtherLayout.findAll(); | ||
|
||
assertEquals(0, result.dashboardDrawerList().size()); | ||
verifyNoInteractions(repository); | ||
} | ||
} |
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