Management API: Rollout collection to correctly expose totalTargetsPerStatus property (#1332)

* Initial commit
* Add unit test
This commit is contained in:
Stefan Behl
2023-03-14 15:22:19 +01:00
committed by GitHub
parent 090db6fd7b
commit e80bef6156
2 changed files with 229 additions and 209 deletions

View File

@@ -39,12 +39,13 @@ import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.RolloutGroup; import org.eclipse.hawkbit.repository.model.RolloutGroup;
import org.eclipse.hawkbit.repository.model.RolloutGroupConditions; import org.eclipse.hawkbit.repository.model.RolloutGroupConditions;
import org.eclipse.hawkbit.repository.model.Target; import org.eclipse.hawkbit.repository.model.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.hawkbit.security.SystemSecurityContext; import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.utils.TenantConfigHelper; import org.eclipse.hawkbit.utils.TenantConfigHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -98,24 +99,39 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
final int sanitizedLimitParam = PagingUtility.sanitizePageLimitParam(pagingLimitParam); final int sanitizedLimitParam = PagingUtility.sanitizePageLimitParam(pagingLimitParam);
final Sort sorting = PagingUtility.sanitizeRolloutSortParam(sortParam); final Sort sorting = PagingUtility.sanitizeRolloutSortParam(sortParam);
final boolean isFullMode = MgmtRepresentationMode.fromValue(representationModeParam).orElseGet(() -> {
// no need for a 400, just apply a safe fallback
LOG.warn("Received an invalid representation mode: {}", representationModeParam);
return MgmtRepresentationMode.COMPACT;
}) == MgmtRepresentationMode.FULL;
final Pageable pageable = new OffsetBasedPageRequest(sanitizedOffsetParam, sanitizedLimitParam, sorting); final Pageable pageable = new OffsetBasedPageRequest(sanitizedOffsetParam, sanitizedLimitParam, sorting);
final Page<Rollout> findRolloutsAll; final Slice<Rollout> rollouts;
final long totalElements;
if (rsqlParam != null) { if (rsqlParam != null) {
findRolloutsAll = this.rolloutManagement.findByRsql(pageable, rsqlParam, false); if (isFullMode) {
rollouts = this.rolloutManagement.findByFiltersWithDetailedStatus(pageable, rsqlParam, false);
totalElements = this.rolloutManagement.countByFilters(rsqlParam);
} else {
final Page<Rollout> findRolloutsAll = this.rolloutManagement.findByRsql(pageable, rsqlParam, false);
totalElements = findRolloutsAll.getTotalElements();
rollouts = findRolloutsAll;
}
} else { } else {
findRolloutsAll = this.rolloutManagement.findAll(pageable, false); if (isFullMode) {
rollouts = this.rolloutManagement.findAllWithDetailedStatus(pageable, false);
totalElements = this.rolloutManagement.count();
} else {
final Page<Rollout> findRolloutsAll = this.rolloutManagement.findAll(pageable, false);
totalElements = findRolloutsAll.getTotalElements();
rollouts = findRolloutsAll;
}
} }
final MgmtRepresentationMode repMode = MgmtRepresentationMode.fromValue(representationModeParam) final List<MgmtRolloutResponseBody> rest = MgmtRolloutMapper.toResponseRollout(rollouts.getContent(),
.orElseGet(() -> { isFullMode);
// no need for a 400, just apply a safe fallback
LOG.warn("Received an invalid representation mode: {}", representationModeParam);
return MgmtRepresentationMode.COMPACT;
});
final List<MgmtRolloutResponseBody> rest = MgmtRolloutMapper.toResponseRollout(findRolloutsAll.getContent(), return ResponseEntity.ok(new PagedList<>(rest, totalElements));
repMode == MgmtRepresentationMode.FULL);
return ResponseEntity.ok(new PagedList<>(rest, findRolloutsAll.getTotalElements()));
} }
@Override @Override
@@ -235,8 +251,8 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
findRolloutGroupsAll = this.rolloutGroupManagement.findByRollout(pageable, rolloutId); findRolloutGroupsAll = this.rolloutGroupManagement.findByRollout(pageable, rolloutId);
} }
final List<MgmtRolloutGroupResponseBody> rest = MgmtRolloutMapper final List<MgmtRolloutGroupResponseBody> rest = MgmtRolloutMapper.toResponseRolloutGroup(
.toResponseRolloutGroup(findRolloutGroupsAll.getContent(), tenantConfigHelper.isConfirmationFlowEnabled()); findRolloutGroupsAll.getContent(), tenantConfigHelper.isConfirmationFlowEnabled());
return ResponseEntity.ok(new PagedList<>(rest, findRolloutGroupsAll.getTotalElements())); return ResponseEntity.ok(new PagedList<>(rest, findRolloutGroupsAll.getTotalElements()));
} }

View File

@@ -27,8 +27,8 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.awaitility.Duration; import org.awaitility.Duration;
@@ -77,17 +77,17 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
private static final String HREF_ROLLOUT_PREFIX = "http://localhost/rest/v1/rollouts/"; private static final String HREF_ROLLOUT_PREFIX = "http://localhost/rest/v1/rollouts/";
@Autowired private RolloutManagement rolloutManagement; @Autowired
private RolloutManagement rolloutManagement;
@Autowired private RolloutGroupManagement rolloutGroupManagement; @Autowired
private RolloutGroupManagement rolloutGroupManagement;
@Test @Test
@Description("Testing that creating rollout with wrong body returns bad request") @Description("Testing that creating rollout with wrong body returns bad request")
void createRolloutWithInvalidBodyReturnsBadRequest() throws Exception { void createRolloutWithInvalidBodyReturnsBadRequest() throws Exception {
mvc.perform(post("/rest/v1/rollouts").content("invalid body") mvc.perform(post("/rest/v1/rollouts").content("invalid body").contentType(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print())
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isBadRequest()) .andExpect(status().isBadRequest())
.andExpect(jsonPath("errorCode", equalTo("hawkbit.server.error.rest.body.notReadable"))); .andExpect(jsonPath("errorCode", equalTo("hawkbit.server.error.rest.body.notReadable")));
} }
@@ -97,36 +97,28 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
@WithUser(allSpPermissions = true, removeFromAllPermission = "CREATE_ROLLOUT") @WithUser(allSpPermissions = true, removeFromAllPermission = "CREATE_ROLLOUT")
void createRolloutWithInsufficientPermissionReturnsForbidden() throws Exception { void createRolloutWithInsufficientPermissionReturnsForbidden() throws Exception {
final DistributionSet dsA = testdataFactory.createDistributionSet(""); final DistributionSet dsA = testdataFactory.createDistributionSet("");
mvc.perform(post("/rest/v1/rollouts").content( mvc.perform(post("/rest/v1/rollouts")
JsonBuilder.rollout("name", "desc", 10, dsA.getId(), "name==test", null)) .content(JsonBuilder.rollout("name", "desc", 10, dsA.getId(), "name==test", null))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()).andExpect(status().is(403)).andReturn();
.andDo(MockMvcResultPrinter.print())
.andExpect(status().is(403))
.andReturn();
} }
@Test @Test
@Description("Testing that creating rollout with not existing distribution set returns not found") @Description("Testing that creating rollout with not existing distribution set returns not found")
void createRolloutWithNotExistingDistributionSetReturnsNotFound() throws Exception { void createRolloutWithNotExistingDistributionSetReturnsNotFound() throws Exception {
mvc.perform(post("/rest/v1/rollouts").content(JsonBuilder.rollout("name", "desc", 10, 1234, "name==test", null)) mvc.perform(post("/rest/v1/rollouts").content(JsonBuilder.rollout("name", "desc", 10, 1234, "name==test", null))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isNotFound()).andReturn();
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isNotFound())
.andReturn();
} }
@Test @Test
@Description("Testing that creating rollout with not valid formed target filter query returns bad request") @Description("Testing that creating rollout with not valid formed target filter query returns bad request")
void createRolloutWithNotWellFormedFilterReturnsBadRequest() throws Exception { void createRolloutWithNotWellFormedFilterReturnsBadRequest() throws Exception {
final DistributionSet dsA = testdataFactory.createDistributionSet(""); final DistributionSet dsA = testdataFactory.createDistributionSet("");
mvc.perform(post("/rest/v1/rollouts").content( mvc.perform(post("/rest/v1/rollouts")
JsonBuilder.rollout("name", "desc", 5, dsA.getId(), "name=test", null)) .content(JsonBuilder.rollout("name", "desc", 5, dsA.getId(), "name=test", null))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isBadRequest())
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isBadRequest())
.andExpect(jsonPath("errorCode", equalTo("hawkbit.server.error.rest.param.rsqlParamSyntax"))) .andExpect(jsonPath("errorCode", equalTo("hawkbit.server.error.rest.param.rsqlParamSyntax")))
.andReturn(); .andReturn();
} }
@@ -163,13 +155,12 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final int maxGroups = quotaManagement.getMaxRolloutGroupsPerRollout(); final int maxGroups = quotaManagement.getMaxRolloutGroupsPerRollout();
testdataFactory.createTargets(20, "target", "rollout"); testdataFactory.createTargets(20, "target", "rollout");
mvc.perform(post("/rest/v1/rollouts").content(JsonBuilder.rollout("rollout1", "rollout1Desc", maxGroups + 1, mvc.perform(post("/rest/v1/rollouts")
testdataFactory.createDistributionSet("ds").getId(), "id==target*", .content(JsonBuilder.rollout("rollout1", "rollout1Desc", maxGroups + 1,
new RolloutGroupConditionBuilder().withDefaults().build())) testdataFactory.createDistributionSet("ds").getId(), "id==target*",
.contentType(MediaType.APPLICATION_JSON) new RolloutGroupConditionBuilder().withDefaults().build()))
.accept(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()) .andDo(MockMvcResultPrinter.print()).andExpect(status().isForbidden())
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.exceptionClass", equalTo(AssignmentQuotaExceededException.class.getName()))) .andExpect(jsonPath("$.exceptionClass", equalTo(AssignmentQuotaExceededException.class.getName())))
.andExpect(jsonPath("$.errorCode", equalTo(SpServerError.SP_QUOTA_EXCEEDED.getKey()))); .andExpect(jsonPath("$.errorCode", equalTo(SpServerError.SP_QUOTA_EXCEEDED.getKey())));
@@ -182,13 +173,12 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final int maxTargets = quotaManagement.getMaxTargetsPerRolloutGroup(); final int maxTargets = quotaManagement.getMaxTargetsPerRolloutGroup();
testdataFactory.createTargets(maxTargets + 1, "target", "rollout"); testdataFactory.createTargets(maxTargets + 1, "target", "rollout");
mvc.perform(post("/rest/v1/rollouts").content( mvc.perform(post("/rest/v1/rollouts")
JsonBuilder.rollout("rollout1", "rollout1Desc", 1, testdataFactory.createDistributionSet("ds").getId(), .content(JsonBuilder.rollout("rollout1", "rollout1Desc", 1,
"id==target*", new RolloutGroupConditionBuilder().withDefaults().build())) testdataFactory.createDistributionSet("ds").getId(), "id==target*",
.contentType(MediaType.APPLICATION_JSON) new RolloutGroupConditionBuilder().withDefaults().build()))
.accept(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()) .andDo(MockMvcResultPrinter.print()).andExpect(status().isForbidden())
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.exceptionClass", equalTo(AssignmentQuotaExceededException.class.getName()))) .andExpect(jsonPath("$.exceptionClass", equalTo(AssignmentQuotaExceededException.class.getName())))
.andExpect(jsonPath("$.errorCode", equalTo(SpServerError.SP_QUOTA_EXCEEDED.getKey()))); .andExpect(jsonPath("$.errorCode", equalTo(SpServerError.SP_QUOTA_EXCEEDED.getKey())));
@@ -229,17 +219,11 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final int amountTargets = 10; final int amountTargets = 10;
testdataFactory.createTargets(amountTargets, "ro-target", "rollout"); testdataFactory.createTargets(amountTargets, "ro-target", "rollout");
final List<RolloutGroup> rolloutGroups = Arrays.asList(entityFactory.rolloutGroup() final List<RolloutGroup> rolloutGroups = Arrays.asList(
.create() entityFactory.rolloutGroup().create().name("Group1").description("Group1desc").targetPercentage(0F)
.name("Group1") .build(),
.description("Group1desc") entityFactory.rolloutGroup().create().name("Group2").description("Group2desc").targetPercentage(100F)
.targetPercentage(0F) .build());
.build(), entityFactory.rolloutGroup()
.create()
.name("Group2")
.description("Group2desc")
.targetPercentage(100F)
.build());
final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build(); final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build();
@@ -260,17 +244,11 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final int amountTargets = 10; final int amountTargets = 10;
testdataFactory.createTargets(amountTargets, "ro-target", "rollout"); testdataFactory.createTargets(amountTargets, "ro-target", "rollout");
final List<RolloutGroup> rolloutGroups = Arrays.asList(entityFactory.rolloutGroup() final List<RolloutGroup> rolloutGroups = Arrays.asList(
.create() entityFactory.rolloutGroup().create().name("Group1").description("Group1desc").targetPercentage(1F)
.name("Group1") .build(),
.description("Group1desc") entityFactory.rolloutGroup().create().name("Group2").description("Group2desc").targetPercentage(101F)
.targetPercentage(1F) .build());
.build(), entityFactory.rolloutGroup()
.create()
.name("Group2")
.description("Group2desc")
.targetPercentage(101F)
.build());
final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build(); final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build();
@@ -286,12 +264,9 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
@Test @Test
@Description("Testing the empty list is returned if no rollout exists") @Description("Testing the empty list is returned if no rollout exists")
void noRolloutReturnsEmptyList() throws Exception { void noRolloutReturnsEmptyList() throws Exception {
mvc.perform(get("/rest/v1/rollouts").accept(MediaType.APPLICATION_JSON)) mvc.perform(get("/rest/v1/rollouts").accept(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print())
.andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk()) .andExpect(jsonPath("$.content", hasSize(0))).andExpect(jsonPath("$.total", equalTo(0)));
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.content", hasSize(0)))
.andExpect(jsonPath("$.total", equalTo(0)));
} }
@Test @Test
@@ -302,10 +277,10 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
// create rollout including the created targets with prefix 'rollout' // create rollout including the created targets with prefix 'rollout'
final Rollout rollout = rolloutManagement.create( final Rollout rollout = rolloutManagement.create(
entityFactory.rollout().create().name("rollout1").set(dsA.getId()) entityFactory.rollout().create().name("rollout1").set(dsA.getId())
.targetFilterQuery("controllerId==rollout*"), .targetFilterQuery("controllerId==rollout*"),
4, false, new RolloutGroupConditionBuilder().withDefaults() 4, false, new RolloutGroupConditionBuilder().withDefaults()
.successCondition(RolloutGroupSuccessCondition.THRESHOLD, "100").build()); .successCondition(RolloutGroupSuccessCondition.THRESHOLD, "100").build());
retrieveAndVerifyRolloutInCreating(dsA, rollout); retrieveAndVerifyRolloutInCreating(dsA, rollout);
retrieveAndVerifyRolloutInReady(rollout); retrieveAndVerifyRolloutInReady(rollout);
@@ -313,6 +288,46 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
retrieveAndVerifyRolloutInRunning(rollout); retrieveAndVerifyRolloutInRunning(rollout);
} }
@Test
@Description("Retrieves the list of rollouts with representation mode 'full'.")
void retrieveRolloutListFullRepresentation() throws Exception {
testdataFactory.createTargets(20, "rollout", "rollout");
final DistributionSet dsA = testdataFactory.createDistributionSet("");
// create a running rollout for the created targets
final Rollout rollout = rolloutManagement.create(
entityFactory.rollout().create().name("rollout1").set(dsA.getId())
.targetFilterQuery("controllerId==rollout*"),
4, false, new RolloutGroupConditionBuilder().withDefaults()
.successCondition(RolloutGroupSuccessCondition.THRESHOLD, "100").build());
rolloutManagement.handleRollouts();
rolloutManagement.start(rollout.getId());
rolloutManagement.handleRollouts();
// request the list of rollouts with full representation
mvc.perform(get("/rest/v1/rollouts?representation=full").accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()).andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.content", hasSize(1))).andExpect(jsonPath("$.total", equalTo(1)))
.andExpect(jsonPath("content[0].id", equalTo(rollout.getId().intValue())))
.andExpect(jsonPath("content[0].name", equalTo("rollout1")))
.andExpect(jsonPath("content[0].status", equalTo("running")))
.andExpect(jsonPath("content[0].targetFilterQuery", equalTo("controllerId==rollout*")))
.andExpect(jsonPath("content[0].distributionSetId", equalTo(dsA.getId().intValue())))
.andExpect(jsonPath("content[0].totalTargets", equalTo(20)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus").exists())
.andExpect(jsonPath("content[0].totalTargetsPerStatus.running", equalTo(5)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus.notstarted", equalTo(0)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus.scheduled", equalTo(15)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus.cancelled", equalTo(0)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus.finished", equalTo(0)))
.andExpect(jsonPath("content[0].totalTargetsPerStatus.error", equalTo(0)))
.andExpect(jsonPath("content[0].deleted", equalTo(false)))
.andExpect(jsonPath("content[0].totalGroups", equalTo(4)))
.andExpect(jsonPath("content[0]._links.self.href", startsWith(HREF_ROLLOUT_PREFIX)));
}
@ParameterizedTest @ParameterizedTest
@ValueSource(booleans = { true, false }) @ValueSource(booleans = { true, false })
@Description("Verify the confirmation required flag is not part of the rollout parent entity") @Description("Verify the confirmation required flag is not part of the rollout parent entity")
@@ -354,8 +369,8 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final List<Rollout> content = rolloutManagement.findAll(PAGE, false).getContent(); final List<Rollout> content = rolloutManagement.findAll(PAGE, false).getContent();
assertThat(content).hasSizeGreaterThan(0).allSatisfy(rollout -> { assertThat(content).hasSizeGreaterThan(0).allSatisfy(rollout -> {
assertThat(rolloutGroupManagement.findByRollout(PAGE, rollout.getId())) assertThat(rolloutGroupManagement.findByRollout(PAGE, rollout.getId()))
.describedAs("Confirmation required flag depends on feature active.") .describedAs("Confirmation required flag depends on feature active.")
.allMatch(group -> group.isConfirmationRequired() == confirmationFlowActive); .allMatch(group -> group.isConfirmationRequired() == confirmationFlowActive);
}); });
} }
@@ -416,16 +431,16 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build(); final RolloutGroupConditions rolloutGroupConditions = new RolloutGroupConditionBuilder().withDefaults().build();
final List<String> rolloutGroups = Arrays.asList( final List<String> rolloutGroups = Arrays.asList(
JsonBuilder.rolloutGroup("Group1", "Group1desc", null, percentTargetsInGroup1, false, JsonBuilder.rolloutGroup("Group1", "Group1desc", null, percentTargetsInGroup1, false,
rolloutGroupConditions), rolloutGroupConditions),
JsonBuilder.rolloutGroup("Group2", "Group1desc", null, percentTargetsInGroup2, null, JsonBuilder.rolloutGroup("Group2", "Group1desc", null, percentTargetsInGroup2, null,
rolloutGroupConditions)); rolloutGroupConditions));
mvc.perform(post("/rest/v1/rollouts") mvc.perform(post("/rest/v1/rollouts")
.content(JsonBuilder.rollout("rollout2", "desc", null, dsA.getId(), "id==ro-target*", .content(JsonBuilder.rollout("rollout2", "desc", null, dsA.getId(), "id==ro-target*",
rolloutGroupConditions, rolloutGroups, null, null, null)) rolloutGroupConditions, rolloutGroups, null, null, null))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated()).andReturn(); .andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated()).andReturn();
final List<Rollout> content = rolloutManagement.findAll(PAGE, false).getContent(); final List<Rollout> content = rolloutManagement.findAll(PAGE, false).getContent();
assertThat(content).hasSize(1).allSatisfy(rollout -> { assertThat(content).hasSize(1).allSatisfy(rollout -> {
@@ -456,8 +471,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
.andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0)))
.andExpect(jsonPath("$.deleted", equalTo(false))) .andExpect(jsonPath("$.deleted", equalTo(false))).andExpect(jsonPath("$.totalGroups", equalTo(4)));
.andExpect(jsonPath("$.totalGroups", equalTo(4)));
} }
@Step @Step
@@ -475,8 +489,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
.andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0)))
.andExpect(jsonPath("$.deleted", equalTo(false))) .andExpect(jsonPath("$.deleted", equalTo(false))).andExpect(jsonPath("$.totalGroups", equalTo(4)));
.andExpect(jsonPath("$.totalGroups", equalTo(4)));
} }
@Step @Step
@@ -496,8 +509,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
.andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.cancelled", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.finished", equalTo(0)))
.andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0))) .andExpect(jsonPath("$.totalTargetsPerStatus.error", equalTo(0)))
.andExpect(jsonPath("$.deleted", equalTo(false))) .andExpect(jsonPath("$.deleted", equalTo(false))).andExpect(jsonPath("$.totalGroups", equalTo(4)));
.andExpect(jsonPath("$.totalGroups", equalTo(4)));
} }
@Step @Step
@@ -529,8 +541,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
allOf(startsWith(HREF_ROLLOUT_PREFIX), endsWith("/triggerNextGroup")))) allOf(startsWith(HREF_ROLLOUT_PREFIX), endsWith("/triggerNextGroup"))))
.andExpect(jsonPath("$._links.groups.href", .andExpect(jsonPath("$._links.groups.href",
allOf(startsWith(HREF_ROLLOUT_PREFIX), containsString("/deploygroups")))) allOf(startsWith(HREF_ROLLOUT_PREFIX), containsString("/deploygroups"))))
.andExpect(jsonPath("$.deleted", equalTo(false))) .andExpect(jsonPath("$.deleted", equalTo(false))).andExpect(jsonPath("$.totalGroups", equalTo(4)));
.andExpect(jsonPath("$.totalGroups", equalTo(4)));
} }
@Test @Test
@@ -615,7 +626,8 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("confirmationOptions") @MethodSource("confirmationOptions")
@Description("Testing that rollout paged list is limited by the query param limit") @Description("Testing that rollout paged list is limited by the query param limit")
void retrieveRolloutGroupsForSpecificRollout(final boolean confirmationFlowEnabled, final boolean confirmationRequired) throws Exception { void retrieveRolloutGroupsForSpecificRollout(final boolean confirmationFlowEnabled,
final boolean confirmationRequired) throws Exception {
// setup // setup
final int amountTargets = 20; final int amountTargets = 20;
testdataFactory.createTargets(amountTargets, "rollout", "rollout"); testdataFactory.createTargets(amountTargets, "rollout", "rollout");
@@ -951,8 +963,8 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
// create rollout including the created targets with prefix 'rollout' // create rollout including the created targets with prefix 'rollout'
final Rollout rollout = createRollout("rollout1", 2, dsA.getId(), "controllerId==rollout*"); final Rollout rollout = createRollout("rollout1", 2, dsA.getId(), "controllerId==rollout*");
final RolloutGroup firstGroup = rolloutGroupManagement.findByRollout(PageRequest.of(0, 1, Direction.ASC, "id"), final RolloutGroup firstGroup = rolloutGroupManagement
rollout.getId()).getContent().get(0); .findByRollout(PageRequest.of(0, 1, Direction.ASC, "id"), rollout.getId()).getContent().get(0);
// retrieve targets from the first rollout group with known ID // retrieve targets from the first rollout group with known ID
mvc.perform( mvc.perform(
@@ -974,8 +986,8 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
// create rollout including the created targets with prefix 'rollout' // create rollout including the created targets with prefix 'rollout'
final Rollout rollout = createRollout("rollout1", 2, dsA.getId(), "controllerId==rollout*"); final Rollout rollout = createRollout("rollout1", 2, dsA.getId(), "controllerId==rollout*");
final RolloutGroup firstGroup = rolloutGroupManagement.findByRollout(PageRequest.of(0, 1, Direction.ASC, "id"), final RolloutGroup firstGroup = rolloutGroupManagement
rollout.getId()).getContent().get(0); .findByRollout(PageRequest.of(0, 1, Direction.ASC, "id"), rollout.getId()).getContent().get(0);
final String targetInGroup = rolloutGroupManagement.findTargetsOfRolloutGroup(PAGE, firstGroup.getId()) final String targetInGroup = rolloutGroupManagement.findTargetsOfRolloutGroup(PAGE, firstGroup.getId())
.getContent().get(0).getControllerId(); .getContent().get(0).getControllerId();
@@ -1029,8 +1041,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
final Rollout rollout = createRollout("rollout1", 4, dsA.getId(), "controllerId==rollout*"); final Rollout rollout = createRollout("rollout1", 4, dsA.getId(), "controllerId==rollout*");
// starting rollout // starting rollout
mvc.perform(post("/rest/v1/rollouts/{rolloutId}/start", rollout.getId())) mvc.perform(post("/rest/v1/rollouts/{rolloutId}/start", rollout.getId())).andDo(MockMvcResultPrinter.print())
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk()); .andExpect(status().isOk());
// Run here, because scheduler is disabled during tests // Run here, because scheduler is disabled during tests
@@ -1041,14 +1052,11 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
} }
private void awaitRunningState(final Long rolloutId) { private void awaitRunningState(final Long rolloutId) {
Awaitility.await() Awaitility.await().atMost(Duration.ONE_MINUTE).pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).with()
.atMost(Duration.ONE_MINUTE) .until(() -> WithSpringAuthorityRule
.pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) .runAsPrivileged(
.with()
.until(() -> WithSpringAuthorityRule.runAsPrivileged(
() -> rolloutManagement.get(rolloutId).orElseThrow(NoSuchElementException::new)) () -> rolloutManagement.get(rolloutId).orElseThrow(NoSuchElementException::new))
.getStatus() .getStatus().equals(RolloutStatus.RUNNING));
.equals(RolloutStatus.RUNNING));
} }
@Test @Test
@@ -1061,8 +1069,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
// create rollout including the created targets with prefix 'rollout' // create rollout including the created targets with prefix 'rollout'
final Rollout rollout = createRollout("rolloutDelete", 4, dsA.getId(), "controllerId==rolloutDelete*"); final Rollout rollout = createRollout("rolloutDelete", 4, dsA.getId(), "controllerId==rolloutDelete*");
mvc.perform(delete("/rest/v1/rollouts/{rolloutid}", rollout.getId())) mvc.perform(delete("/rest/v1/rollouts/{rolloutid}", rollout.getId())).andDo(MockMvcResultPrinter.print())
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk()); .andExpect(status().isOk());
assertStatusIs(rollout, RolloutStatus.DELETING); assertStatusIs(rollout, RolloutStatus.DELETING);
@@ -1073,15 +1080,13 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
void deleteRunningRollout() throws Exception { void deleteRunningRollout() throws Exception {
final Rollout rollout = testdataFactory.createSoftDeletedRollout("softDeletedRollout"); final Rollout rollout = testdataFactory.createSoftDeletedRollout("softDeletedRollout");
mvc.perform(get("/rest/v1/rollouts/{rolloutid}", rollout.getId())) mvc.perform(get("/rest/v1/rollouts/{rolloutid}", rollout.getId())).andDo(MockMvcResultPrinter.print())
.andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()).andExpect(jsonPath("$.deleted", equalTo(true)));
.andExpect(status().isOk())
.andExpect(jsonPath("$.deleted", equalTo(true)));
assertStatusIs(rollout, RolloutStatus.DELETED); assertStatusIs(rollout, RolloutStatus.DELETED);
} }
private void assertStatusIs(final Rollout rollout, RolloutStatus expected) { private void assertStatusIs(final Rollout rollout, final RolloutStatus expected) {
final Optional<Rollout> updatedRollout = rolloutManagement.get(rollout.getId()); final Optional<Rollout> updatedRollout = rolloutManagement.get(rollout.getId());
assertThat(updatedRollout).get().extracting(Rollout::getStatus).isEqualTo(expected); assertThat(updatedRollout).get().extracting(Rollout::getStatus).isEqualTo(expected);
} }
@@ -1323,90 +1328,89 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
} }
} }
private void retrieveAndCompareRolloutsContent(final DistributionSet dsA, final String urlTemplate, boolean isFullRepresentation) throws Exception { private void retrieveAndCompareRolloutsContent(final DistributionSet dsA, final String urlTemplate,
final boolean isFullRepresentation) throws Exception {
mvc.perform(get(urlTemplate).accept(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()) mvc.perform(get(urlTemplate).accept(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.content", hasSize(2))).andExpect(jsonPath("$.total", equalTo(2))) .andExpect(jsonPath("$.content", hasSize(2))).andExpect(jsonPath("$.total", equalTo(2)))
.andExpect(jsonPath("content[0].name", equalTo("rollout1"))) .andExpect(jsonPath("content[0].name", equalTo("rollout1")))
.andExpect(jsonPath("content[0].status", equalTo("ready"))) .andExpect(jsonPath("content[0].status", equalTo("ready")))
.andExpect(jsonPath("content[0].targetFilterQuery", equalTo("id==target*"))) .andExpect(jsonPath("content[0].targetFilterQuery", equalTo("id==target*")))
.andExpect(jsonPath("content[0].distributionSetId", equalTo(dsA.getId().intValue()))) .andExpect(jsonPath("content[0].distributionSetId", equalTo(dsA.getId().intValue())))
.andExpect(jsonPath("content[0].createdBy", equalTo("bumlux"))) .andExpect(jsonPath("content[0].createdBy", equalTo("bumlux")))
.andExpect(jsonPath("content[0].createdAt", not(equalTo(0)))) .andExpect(jsonPath("content[0].createdAt", not(equalTo(0))))
.andExpect(jsonPath("content[0].lastModifiedBy", equalTo("bumlux"))) .andExpect(jsonPath("content[0].lastModifiedBy", equalTo("bumlux")))
.andExpect(jsonPath("content[0].lastModifiedAt", not(equalTo(0)))) .andExpect(jsonPath("content[0].lastModifiedAt", not(equalTo(0))))
.andExpect(jsonPath("content[0].totalTargets", equalTo(20))) .andExpect(jsonPath("content[0].totalTargets", equalTo(20)))
.andExpect(isFullRepresentation .andExpect(isFullRepresentation ? jsonPath("$.content[0].totalTargetsPerStatus").exists()
? jsonPath("$.content[0].totalTargetsPerStatus").exists() : jsonPath("content[0].totalTargetsPerStatus").doesNotExist())
: jsonPath("content[0].totalTargetsPerStatus").doesNotExist()) .andExpect(isFullRepresentation ? jsonPath("$.content[0].totalGroups", equalTo(5))
.andExpect(isFullRepresentation : jsonPath("content[0].totalGroups").doesNotExist())
? jsonPath("$.content[0].totalGroups", equalTo(5)) .andExpect(isFullRepresentation
: jsonPath("content[0].totalGroups").doesNotExist()) ? jsonPath("$.content[0]._links.start.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.start.href").doesNotExist())
? jsonPath("$.content[0]._links.start.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.start.href").doesNotExist()) ? jsonPath("$.content[0]._links.pause.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.pause.href").doesNotExist())
? jsonPath("$.content[0]._links.pause.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.pause.href").doesNotExist()) ? jsonPath("$.content[0]._links.resume.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.resume.href").doesNotExist())
? jsonPath("$.content[0]._links.resume.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.resume.href").doesNotExist()) ? jsonPath("$.content[0]._links.triggerNextGroup.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.triggerNextGroup.href").doesNotExist())
? jsonPath("$.content[0]._links.triggerNextGroup.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.triggerNextGroup.href").doesNotExist()) ? jsonPath("$.content[0]._links.approve.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.approve.href").doesNotExist())
? jsonPath("$.content[0]._links.approve.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.approve.href").doesNotExist()) ? jsonPath("$.content[0]._links.deny.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.deny.href").doesNotExist())
? jsonPath("$.content[0]._links.deny.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.deny.href").doesNotExist()) ? jsonPath("$.content[0]._links.groups.href", startsWith(HREF_ROLLOUT_PREFIX))
.andExpect(isFullRepresentation : jsonPath("content[0]._links.groups.href").doesNotExist())
? jsonPath("$.content[0]._links.groups.href", startsWith(HREF_ROLLOUT_PREFIX)) .andExpect(isFullRepresentation
: jsonPath("content[0]._links.groups.href").doesNotExist()) ? jsonPath("$.content[0]._links.distributionset.href",
.andExpect(isFullRepresentation startsWith("http://localhost/rest/v1/distributionsets/"))
? jsonPath("$.content[0]._links.distributionset.href", startsWith("http://localhost/rest/v1/distributionsets/")) : jsonPath("content[0]._links.distributionset.href").doesNotExist())
: jsonPath("content[0]._links.distributionset.href").doesNotExist()) .andExpect(jsonPath("content[0]._links.self.href", startsWith(HREF_ROLLOUT_PREFIX)))
.andExpect(jsonPath("content[0]._links.self.href", startsWith(HREF_ROLLOUT_PREFIX))) .andExpect(jsonPath("content[1].name", equalTo("rollout2")))
.andExpect(jsonPath("content[1].name", equalTo("rollout2"))) .andExpect(jsonPath("content[1].status", equalTo("ready")))
.andExpect(jsonPath("content[1].status", equalTo("ready"))) .andExpect(jsonPath("content[1].targetFilterQuery", equalTo("id==target-0001*")))
.andExpect(jsonPath("content[1].targetFilterQuery", equalTo("id==target-0001*"))) .andExpect(jsonPath("content[1].distributionSetId", equalTo(dsA.getId().intValue())))
.andExpect(jsonPath("content[1].distributionSetId", equalTo(dsA.getId().intValue()))) .andExpect(jsonPath("content[1].createdBy", equalTo("bumlux")))
.andExpect(jsonPath("content[1].createdBy", equalTo("bumlux"))) .andExpect(jsonPath("content[1].createdAt", not(equalTo(0))))
.andExpect(jsonPath("content[1].createdAt", not(equalTo(0)))) .andExpect(jsonPath("content[1].lastModifiedBy", equalTo("bumlux")))
.andExpect(jsonPath("content[1].lastModifiedBy", equalTo("bumlux"))) .andExpect(jsonPath("content[1].lastModifiedAt", not(equalTo(0))))
.andExpect(jsonPath("content[1].lastModifiedAt", not(equalTo(0)))) .andExpect(jsonPath("content[1].totalTargets", equalTo(10)))
.andExpect(jsonPath("content[1].totalTargets", equalTo(10))) .andExpect(isFullRepresentation ? jsonPath("$.content[1].totalTargetsPerStatus").exists()
.andExpect(isFullRepresentation : jsonPath("content[1].totalTargetsPerStatus").doesNotExist())
? jsonPath("$.content[1].totalTargetsPerStatus").exists() .andExpect(isFullRepresentation ? jsonPath("$.content[0].totalGroups", equalTo(5))
: jsonPath("content[1].totalTargetsPerStatus").doesNotExist()) : jsonPath("content[0].totalGroups").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[0].totalGroups", equalTo(5)) ? jsonPath("$.content[1]._links.start.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[0].totalGroups").doesNotExist()) : jsonPath("content[1]._links.start.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.start.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.pause.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.start.href").doesNotExist()) : jsonPath("content[1]._links.pause.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.pause.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.resume.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.pause.href").doesNotExist()) : jsonPath("content[1]._links.resume.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.resume.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.triggerNextGroup.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.resume.href").doesNotExist()) : jsonPath("content[1]._links.triggerNextGroup.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.triggerNextGroup.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.approve.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.triggerNextGroup.href").doesNotExist()) : jsonPath("content[1]._links.approve.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.approve.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.deny.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.approve.href").doesNotExist()) : jsonPath("content[1]._links.deny.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.deny.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.groups.href", startsWith(HREF_ROLLOUT_PREFIX))
: jsonPath("content[1]._links.deny.href").doesNotExist()) : jsonPath("content[1]._links.groups.href").doesNotExist())
.andExpect(isFullRepresentation .andExpect(isFullRepresentation
? jsonPath("$.content[1]._links.groups.href", startsWith(HREF_ROLLOUT_PREFIX)) ? jsonPath("$.content[1]._links.distributionset.href",
: jsonPath("content[1]._links.groups.href").doesNotExist()) startsWith("http://localhost/rest/v1/distributionsets/"))
.andExpect(isFullRepresentation : jsonPath("content[1]._links.distributionset.href").doesNotExist())
? jsonPath("$.content[1]._links.distributionset.href", startsWith("http://localhost/rest/v1/distributionsets/")) .andExpect(jsonPath("content[1]._links.self.href", startsWith(HREF_ROLLOUT_PREFIX)));
: jsonPath("content[1]._links.distributionset.href").doesNotExist())
.andExpect(jsonPath("content[1]._links.self.href", startsWith(HREF_ROLLOUT_PREFIX)));
} }
} }