Skip to content

Commit d312705

Browse files
committed
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
1 parent 02b4fbb commit d312705

File tree

5 files changed

+172
-3
lines changed

5 files changed

+172
-3
lines changed

src/main/resources/application.properties

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:5432/${POSTGRES_DB}?cur
33
spring.datasource.username=${POSTGRES_USER}
44
spring.datasource.password=${POSTGRES_PASSWORD}
55
spring.datasource.driver-class-name=org.postgresql.Driver
6+
67
server.port=80
78

89
dashboard.columns={abbreviation:'Abbreviation',name:'Name',clinvars:'Clinical Variables'}
910
dashboard.column-order=abbreviation,name,clinvars
1011
dashboard.nonmeta-columns=abbreviation,name
1112
dashboard.enable.extra_details=true
12-
dashboard.enable.bdc_hack=true
13-
dashboard.layout.type=
13+
dashboard.enable.bdc_hack=false
14+
dashboard.layout.type=default
1415

1516
filtering.unfilterable_concepts=stigmatized
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.junit.jupiter.api.Tag;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
7+
import org.springframework.boot.test.mock.mockito.MockBean;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import static org.mockito.Mockito.when;
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
17+
18+
@Tag("unit")
19+
@WebMvcTest(DashboardDrawerController.class)
20+
class DashboardDrawerControllerTest {
21+
22+
@Autowired
23+
private MockMvc mockMvc;
24+
25+
@MockBean
26+
private DashboardDrawerService dashboardDrawerService;
27+
28+
@Test
29+
void testFindAll() throws Exception {
30+
DashboardDrawerList mockList = new DashboardDrawerList(new ArrayList<>()); // Populate mock data if needed
31+
when(dashboardDrawerService.findAll()).thenReturn(mockList);
32+
33+
mockMvc.perform(get("/dashboard-drawer").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
34+
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
35+
}
36+
37+
@Test
38+
void testFindByDatasetId() throws Exception {
39+
DashboardDrawer mockDrawer =
40+
new DashboardDrawer(1, "Full Name", "Abbreviation", List.of("Consent 1"), "Summary", List.of("Focus 1"), "Design", "Sponsor");
41+
42+
when(dashboardDrawerService.findByDatasetId(1)).thenReturn(mockDrawer);
43+
44+
mockMvc.perform(get("/dashboard-drawer/1").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
45+
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.junit.jupiter.api.Tag;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.InjectMocks;
7+
import org.mockito.Mock;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
10+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
11+
12+
import java.util.Collections;
13+
import java.util.List;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.mockito.ArgumentMatchers.*;
17+
import static org.mockito.Mockito.*;
18+
19+
@Tag("unit")
20+
@ExtendWith(MockitoExtension.class)
21+
class DashboardDrawerRepositoryTest {
22+
23+
@Mock
24+
private NamedParameterJdbcTemplate template;
25+
26+
@InjectMocks
27+
private DashboardDrawerRepository repository;
28+
29+
@Test
30+
void testGetDashboardDrawerRowsWithoutDatasetId_Success() {
31+
DashboardDrawer mockDashboardDrawer = new DashboardDrawer(
32+
1, // datasetId
33+
"Study Full Name", // studyFullname
34+
"Study Abbreviation", // studyAbbreviation
35+
List.of("Consent Group 1"), // consentGroups
36+
"Study Summary", // studySummary
37+
List.of("Study Focus 1"), // studyFocus
38+
"Study Design", // studyDesign
39+
"Sponsor" // sponsor
40+
);
41+
42+
List<DashboardDrawer> expectedRows = Collections.singletonList(mockDashboardDrawer);
43+
when(template.query(anyString(), any(DashboardDrawerRowMapper.class))).thenReturn(expectedRows);
44+
45+
List<DashboardDrawer> result = repository.getDashboardDrawerRows();
46+
47+
assertEquals(expectedRows, result);
48+
verify(template, times(1)).query(anyString(), any(DashboardDrawerRowMapper.class));
49+
}
50+
51+
@Test
52+
void testGetDashboardDrawerRowsWithDatasetId_Success() {
53+
DashboardDrawer mockDashboardDrawer = new DashboardDrawer(
54+
1, // datasetId
55+
"Study Full Name", // studyFullname
56+
"Study Abbreviation", // studyAbbreviation
57+
List.of("Consent Group 1"), // consentGroups
58+
"Study Summary", // studySummary
59+
List.of("Study Focus 1"), // studyFocus
60+
"Study Design", // studyDesign
61+
"Sponsor" // sponsor
62+
);
63+
64+
List<DashboardDrawer> expectedRows = Collections.singletonList(mockDashboardDrawer);
65+
when(template.query(anyString(), any(MapSqlParameterSource.class), any(DashboardDrawerRowMapper.class))).thenReturn(expectedRows);
66+
67+
List<DashboardDrawer> result = repository.getDashboardDrawerRows(1);
68+
69+
assertEquals(expectedRows, result);
70+
verify(template, times(1)).query(anyString(), any(MapSqlParameterSource.class), any(DashboardDrawerRowMapper.class));
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.junit.jupiter.api.Tag;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.Mock;
7+
import org.mockito.junit.jupiter.MockitoExtension;
8+
9+
import java.util.List;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.mockito.ArgumentMatchers.anyInt;
13+
import static org.mockito.Mockito.*;
14+
15+
@Tag("unit")
16+
@ExtendWith(MockitoExtension.class)
17+
class DashboardDrawerServiceTest {
18+
19+
@Mock
20+
private DashboardDrawerRepository repository;
21+
22+
private static final String DASHBOARD_LAYOUT_BDC = "bdc";
23+
private static final String DASHBOARD_LAYOUT_OTHER = "other";
24+
25+
@Test
26+
void testFindAll_WithBDCLayout() {
27+
List<DashboardDrawer> mockRecords = List.of(
28+
new DashboardDrawer(1, "Full Name", "Abbreviation", List.of("Consent 1"), "Summary", List.of("Focus 1"), "Design", "Sponsor")
29+
);
30+
when(repository.getDashboardDrawerRows()).thenReturn(mockRecords);
31+
32+
DashboardDrawerService serviceWithBDCLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_BDC);
33+
34+
DashboardDrawerList result = serviceWithBDCLayout.findAll();
35+
36+
assertEquals(mockRecords, result.dashboardDrawerList());
37+
verify(repository, times(1)).getDashboardDrawerRows();
38+
}
39+
40+
@Test
41+
void testFindAll_WithNonBDCLayout() {
42+
DashboardDrawerService serviceWithOtherLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_OTHER);
43+
44+
DashboardDrawerList result = serviceWithOtherLayout.findAll();
45+
46+
assertEquals(0, result.dashboardDrawerList().size());
47+
verifyNoInteractions(repository);
48+
}
49+
}

src/test/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ spring.datasource.driver-class-name=org.postgresql.Driver
77
dashboard.columns={abbreviation:'Abbreviation',melast:'This one goes last',name:'Name',clinvars:'Clinical Variables',participants:'Participants'}
88
dashboard.column-order=abbreviation,name,clinvars
99
dashboard.nonmeta-columns=abbreviation,name
10-
1110
dashboard.enable.extra_details=true
1211
dashboard.enable.bdc_hack=false
12+
dashboard.layout.type=default
1313

1414
filtering.unfilterable_concepts=stigmatized

0 commit comments

Comments
 (0)