Skip to content

Commit

Permalink
Add unit tests for DashboardDrawer components
Browse files Browse the repository at this point in the history
- 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
TDeSain committed Nov 19, 2024
1 parent 02b4fbb commit d312705
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:5432/${POSTGRES_DB}?cur
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
spring.datasource.driver-class-name=org.postgresql.Driver

server.port=80

dashboard.columns={abbreviation:'Abbreviation',name:'Name',clinvars:'Clinical Variables'}
dashboard.column-order=abbreviation,name,clinvars
dashboard.nonmeta-columns=abbreviation,name
dashboard.enable.extra_details=true
dashboard.enable.bdc_hack=true
dashboard.layout.type=
dashboard.enable.bdc_hack=false
dashboard.layout.type=default

filtering.unfilterable_concepts=stigmatized
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));
}
}
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));
}
}
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);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ spring.datasource.driver-class-name=org.postgresql.Driver
dashboard.columns={abbreviation:'Abbreviation',melast:'This one goes last',name:'Name',clinvars:'Clinical Variables',participants:'Participants'}
dashboard.column-order=abbreviation,name,clinvars
dashboard.nonmeta-columns=abbreviation,name

dashboard.enable.extra_details=true
dashboard.enable.bdc_hack=false
dashboard.layout.type=default

filtering.unfilterable_concepts=stigmatized

0 comments on commit d312705

Please sign in to comment.