Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -424,6 +425,13 @@ public ResponseEntity<List<GenomicDataCountItem>> fetchGenomicDataCounts(
throws StudyNotFoundException {
List<GenomicDataFilter> genomicDataFilters = genomicDataCountFilter.getGenomicDataFilters();
StudyViewFilter studyViewFilter = genomicDataCountFilter.getStudyViewFilter();

// If genomicDataFilters is null or empty, return an empty result list
// This prevents IndexOutOfBoundsException in the MyBatis mapper
if (genomicDataFilters == null || genomicDataFilters.isEmpty()) {
return ResponseEntity.ok(Collections.emptyList());
}

// when there is only one filter, it means study view is doing a single chart filter operation
// remove filter from studyViewFilter to return all data counts
// the reason we do this is to make sure after chart get filtered, user can still see unselected
Expand Down Expand Up @@ -499,6 +507,13 @@ public ResponseEntity<List<GenomicDataCountItem>> fetchMutationDataCounts(
GenomicDataCountFilter genomicDataCountFilter) {
List<GenomicDataFilter> genomicDataFilters = genomicDataCountFilter.getGenomicDataFilters();
StudyViewFilter studyViewFilter = genomicDataCountFilter.getStudyViewFilter();

// If genomicDataFilters is null or empty, return an empty result list
// This prevents IndexOutOfBoundsException in the MyBatis mapper
if (genomicDataFilters == null || genomicDataFilters.isEmpty()) {
return ResponseEntity.ok(Collections.emptyList());
}

// when there is only one filter, it means study view is doing a single chart filter operation
// remove filter from studyViewFilter to return all data counts
// the reason we do this is to make sure after chart get filtered, user can still see unselected
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.cbioportal.application.rest.vcolumnstore;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.Collections;
import org.cbioportal.domain.studyview.StudyViewService;
import org.cbioportal.legacy.web.parameter.GenomicDataCountFilter;
import org.cbioportal.legacy.web.parameter.StudyViewFilter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

/**
* Unit tests for ColumnarStoreStudyViewController.
*
* When genomicDataFilters is empty or null, the endpoints should return
* an empty list instead of throwing an IndexOutOfBoundsException.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ColumnarStoreStudyViewControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockBean
private StudyViewService studyViewService;

private static final String TEST_STUDY_ID = "test_study";

@Test
@WithMockUser
public void fetchGenomicDataCountsWithEmptyFiltersReturnsEmptyList() throws Exception {
GenomicDataCountFilter filter = new GenomicDataCountFilter();
filter.setGenomicDataFilters(Collections.emptyList());

StudyViewFilter studyViewFilter = new StudyViewFilter();
studyViewFilter.setStudyIds(Arrays.asList(TEST_STUDY_ID));
filter.setStudyViewFilter(studyViewFilter);

mockMvc.perform(post("/api/column-store/genomic-data-counts/fetch")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
.andExpect(status().isOk())
.andExpect(content().json("[]"));

// Verify that the service was never called (early return due to empty filters)
verify(studyViewService, never()).getCNACountsByGeneSpecific(
org.mockito.ArgumentMatchers.any(),
org.mockito.ArgumentMatchers.anyList()
);
}

@Test
@WithMockUser
public void fetchGenomicDataCountsWithNullFiltersReturnsEmptyList() throws Exception {
GenomicDataCountFilter filter = new GenomicDataCountFilter();
filter.setGenomicDataFilters(null);

StudyViewFilter studyViewFilter = new StudyViewFilter();
studyViewFilter.setStudyIds(Arrays.asList(TEST_STUDY_ID));
filter.setStudyViewFilter(studyViewFilter);

mockMvc.perform(post("/api/column-store/genomic-data-counts/fetch")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
.andExpect(status().isOk())
.andExpect(content().json("[]"));

// Verify that the service was never called (early return due to null filters)
verify(studyViewService, never()).getCNACountsByGeneSpecific(
org.mockito.ArgumentMatchers.any(),
org.mockito.ArgumentMatchers.anyList()
);
}

@Test
@WithMockUser
public void fetchMutationDataCountsWithEmptyFiltersReturnsEmptyList() throws Exception {
GenomicDataCountFilter filter = new GenomicDataCountFilter();
filter.setGenomicDataFilters(Collections.emptyList());

StudyViewFilter studyViewFilter = new StudyViewFilter();
studyViewFilter.setStudyIds(Arrays.asList(TEST_STUDY_ID));
filter.setStudyViewFilter(studyViewFilter);

mockMvc.perform(post("/api/column-store/mutation-data-counts/fetch")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
.andExpect(status().isOk())
.andExpect(content().json("[]"));

// Verify that the service was never called (early return due to empty filters)
verify(studyViewService, never()).getMutationCountsByGeneSpecific(
org.mockito.ArgumentMatchers.any(),
org.mockito.ArgumentMatchers.anyList()
);
}

@Test
@WithMockUser
public void fetchMutationDataCountsWithNullFiltersReturnsEmptyList() throws Exception {
GenomicDataCountFilter filter = new GenomicDataCountFilter();
filter.setGenomicDataFilters(null);

StudyViewFilter studyViewFilter = new StudyViewFilter();
studyViewFilter.setStudyIds(Arrays.asList(TEST_STUDY_ID));
filter.setStudyViewFilter(studyViewFilter);

mockMvc.perform(post("/api/column-store/mutation-data-counts/fetch")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(filter)))
.andExpect(status().isOk())
.andExpect(content().json("[]"));

// Verify that the service was never called (early return due to null filters)
verify(studyViewService, never()).getMutationCountsByGeneSpecific(
org.mockito.ArgumentMatchers.any(),
org.mockito.ArgumentMatchers.anyList()
);
}
}
Loading