Skip to content

Commit

Permalink
handle npe
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Nov 30, 2023
1 parent 6b43756 commit ab1a0ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.broadinstitute.consent.http.enumeration.UserRoles;
Expand Down Expand Up @@ -481,12 +482,15 @@ public Response getDatasets(@QueryParam("ids") List<Integer> datasetIds) {
.collect(Collectors.toSet());
if (!foundIds.containsAll(datasetIds)) {
// find the differences
List<Integer> differences = new ArrayList<>(datasetIds);
differences.removeAll(foundIds);
List<Integer> differences = new ArrayList<>(datasetIds)
.stream()
.filter(Objects::nonNull)
.filter(Predicate.not(foundIds::contains))
.toList();
throw new NotFoundException(
"Could not find datasets with ids: "
+ String.join(",",
differences.stream().map((i) -> i.toString()).collect(Collectors.toSet())));
differences.stream().map(Object::toString).collect(Collectors.toSet())));

}
return Response.ok(datasets).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,29 @@ void testGetDatasetsNotFound() {
assertFalse(((Error) response.getEntity()).message().contains("1"));
}

@Test
void testGetDatasetsNotFoundNullValues() {
Dataset ds1 = new Dataset();
ds1.setDataSetId(1);
Dataset ds3 = new Dataset();
ds3.setDataSetId(3);

when(datasetService.findDatasetsByIds(any())).thenReturn(List.of(
ds1,
ds3
));

initResource();
List<Integer> input = new ArrayList<>(List.of(1, 2, 3, 4));
input.add(null);
Response response = resource.getDatasets(input);
assertEquals(HttpStatusCodes.STATUS_CODE_NOT_FOUND, response.getStatus());
assertTrue(((Error) response.getEntity()).message().contains("4"));
assertFalse(((Error) response.getEntity()).message().contains("3"));
assertTrue(((Error) response.getEntity()).message().contains("2"));
assertFalse(((Error) response.getEntity()).message().contains("1"));
}


@Test
void testUpdateDatasetDataUse_OK() {
Expand Down

0 comments on commit ab1a0ab

Please sign in to comment.