Skip to content

Commit

Permalink
Add DashboardDrawer feature with controller, service, and repository
Browse files Browse the repository at this point in the history
Introduced DashboardDrawerController, DashboardDrawerService, and DashboardDrawerRepository classes to support queries and data retrieval for the dashboard drawer functionality. The service includes methods to fetch drawer data for all datasets or a specific dataset, utilizing materialized views with a fallback query mechanism.
  • Loading branch information
TDeSain committed Nov 15, 2024
1 parent d2e6751 commit 96ec3cc
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 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 {
// Mock the service response
DashboardDrawerList mockList = new DashboardDrawerList(new ArrayList<>()); // Populate mock data if needed
when(dashboardDrawerService.findAll()).thenReturn(mockList);

// Perform GET request and verify response
mockMvc.perform(get("/dashboard-drawer")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
// Add further checks for content as needed
}

@Test
void testFindByDatasetId() throws Exception {
// Mock the service response
DashboardDrawerList mockList = new DashboardDrawerList(new ArrayList<>()); // Populate mock data if needed
when(dashboardDrawerService.findByDatasetId(1)).thenReturn(mockList);

// Perform GET request with path variable and verify response
mockMvc.perform(get("/dashboard-drawer/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
// Add further checks for content as needed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@Tag("integration")
@SpringBootTest
class DashboardDrawerRepositoryIntegrationTest {

@Autowired
private DashboardDrawerRepository repository;

@Autowired
private NamedParameterJdbcTemplate template;

@Test
void testGetDashboardDrawerRows() {
List<DashboardDrawer> rows = repository.getDashboardDrawerRows();
assertNotNull(rows, "Rows should not be null");
rows.forEach(row -> System.out.println("Repository Row: " + row));
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testGetDashboardDrawerRows(Integer datasetId) {
List<DashboardDrawer> rows = repository.getDashboardDrawerRows(datasetId);
assertNotNull(rows, "Rows should not be null");
rows.forEach(row -> System.out.println("Repository Row: " + row));
}

@Test
void testJdbcTemplateQuery() {
// Use the template directly to test the query
String sql = """
SELECT d.dataset_id,
MAX(d.full_name) study_fullname,
MAX(d.abbreviation) study_abbreviation,
ARRAY_AGG(DISTINCT c.description) consent_groups,
MAX(d.description) study_summary,
ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor
FROM dataset d
JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id
JOIN consent c ON d.dataset_id = c.dataset_id
GROUP BY d.dataset_id
""";

List<DashboardDrawer> rows = template.query(sql, new DashboardDrawerRowMapper());
assertNotNull(rows, "Rows should not be null");
rows.forEach(row -> System.out.println("Template Row: " + row));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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,74 @@
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);
}

@Test
void testFindByDatasetId_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(anyInt())).thenReturn(mockRecords);

DashboardDrawerService serviceWithBDCLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_BDC);

DashboardDrawerList result = serviceWithBDCLayout.findByDatasetId(1);

assertEquals(mockRecords, result.dashboardDrawerList());
verify(repository, times(1)).getDashboardDrawerRows(1);
}

@Test
void testFindByDatasetId_WithNonBDCLayout() {
DashboardDrawerService serviceWithOtherLayout = new DashboardDrawerService(repository, DASHBOARD_LAYOUT_OTHER);

DashboardDrawerList result = serviceWithOtherLayout.findByDatasetId(1);

assertEquals(0, result.dashboardDrawerList().size());
verifyNoInteractions(repository);
}
}

0 comments on commit 96ec3cc

Please sign in to comment.