Skip to content

Commit

Permalink
MAT-6482: add tests for deleteStratification
Browse files Browse the repository at this point in the history
  • Loading branch information
chubert-sb committed Dec 12, 2023
1 parent 264936b commit c32324c
Showing 1 changed file with 122 additions and 17 deletions.
139 changes: 122 additions & 17 deletions src/test/java/cms/gov/madie/measure/services/GroupServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import static org.mockito.Mockito.when;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -1592,19 +1589,6 @@ public void testUpdateStratification() {
assertTrue(capturedStratification.getAssociations().contains(PopulationType.DENOMINATOR));
}

@Test
void testDeleteStratification() {
when(measureService.findMeasureById(anyString())).thenReturn(measure);

doReturn(measure).when(measureRepository).save(any(Measure.class));

Measure output =
groupService.deleteStratification(
measure.getId(), group2.getId(), strata1.getId(), "test.user");

assertEquals(0, output.getGroups().get(0).getStratifications().size());
}

@Test
void testUpdateStratificationReturnsExceptionForResourceNotFound() {
assertThrows(
Expand Down Expand Up @@ -1639,4 +1623,125 @@ void testCreateStratificationReturnsInvalidDraftStatusException() {
groupService.createOrUpdateStratification(
group2.getId(), "testid", strata1, "test.user"));
}

@Test
void testDeleteStratification() {
when(measureService.findMeasureById(anyString())).thenReturn(measure);

doReturn(measure).when(measureRepository).save(any(Measure.class));

Measure output =
groupService.deleteStratification(
measure.getId(), group2.getId(), strata1.getId(), "test.user");

assertEquals(0, output.getGroups().get(0).getStratifications().size());
}

@Test
void testDeleteStratificationReturnsExceptionForNullMeasureId() {
assertThrows(
InvalidIdException.class,
() -> groupService.deleteStratification("", "grouptestid", "stratificationId", "OtherUser"));
}

@Test
void testDeleteStratificationThrowsAccessException() {
String groupId = "testgroupid";
final Measure measure =
Measure.builder()
.id("measure-id")
.createdBy("OtherUser")
.measureMetaData(MeasureMetaData.builder().draft(true).build())
.measureSet(MeasureSet.builder().owner("OtherUser").build())
.build();
when(measureService.findMeasureById(anyString())).thenReturn(measure);
doThrow(new UnauthorizedException("Measure", measure.getId(), "user2"))
.when(measureService)
.verifyAuthorization(anyString(), any(Measure.class));
assertThrows(
UnauthorizedException.class,
() -> groupService.deleteStratification("measure-id", groupId, "stratificationId", "user2"));
}

@Test
void testDeleteStratificationReturnsInvalidDraftStatusException() {
String groupId = "testgroupid";
final Measure measure =
Measure.builder()
.id("measure-id")
.createdBy("OtherUser")
.measureMetaData(MeasureMetaData.builder().draft(false).build())
.measureSet(MeasureSet.builder().owner("OtherUser").build())
.build();
when(measureService.findMeasureById(anyString())).thenReturn(measure);
assertThrows(
InvalidDraftStatusException.class,
() -> groupService.deleteStratification("measure-id", groupId, "stratificationId", "user2"));
}

@Test
void testDeleteStratificationReturnsExceptionForResourceNotFound() {
assertThrows(
ResourceNotFoundException.class,
() -> groupService.deleteStratification("testid", "testgroupid", "stratificationId", "user2"));
}

@Test
void testDeleteStratificationReturnsExceptionForNullId() {
final Measure measure =
Measure.builder()
.id("measure-id")
.createdBy("OtherUser")
.measureMetaData(MeasureMetaData.builder().draft(true).build())
.measureSet(MeasureSet.builder().owner("OtherUser").build())
.build();
when(measureService.findMeasureById(anyString())).thenReturn(measure);

assertThrows(
InvalidIdException.class,
() -> groupService.deleteStratification("measure-id", "", "stratificationId", "OtherUser"));
}

@Test
void testDeleteStratificationReturnsExceptionForGroupNotFoundInMeasure() {
Group group =
Group.builder()
.id("testgroupid")
.scoring("Cohort")
.populations(
List.of(
new Population(
"id-1",
PopulationType.INITIAL_POPULATION,
"Initial Population",
null,
null)))
.build();

Measure existingMeasure =
Measure.builder()
.id("measure-id")
.createdBy("test.user")
.groups(List.of(group))
.measureMetaData(MeasureMetaData.builder().draft(true).build())
.measureSet(MeasureSet.builder().owner("test.user").build())
.build();
when(measureService.findMeasureById(anyString())).thenReturn(existingMeasure);

assertThrows(
ResourceNotFoundException.class,
() -> groupService.deleteStratification("measure-id", "grouptestid1", "stratificationId", "test.user"));
}

@Test
void testDeleteStratificationReturnsExceptionForStratificationNotFoundInMeasure() {
measure.getGroups().get(0).setStratifications(Collections.emptyList());

when(measureService.findMeasureById(anyString())).thenReturn(measure);

assertThrows(
ResourceNotFoundException.class,
() -> groupService.deleteStratification(
measure.getId(), group2.getId(), strata1.getId(), "test.user"));
}
}

0 comments on commit c32324c

Please sign in to comment.