Skip to content
Closed
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 @@ -4,8 +4,10 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.opendevstack.component_provisioner.client.component_catalog.v1.model.CatalogItem;
import org.opendevstack.component_provisioner.client.component_catalog.v1.model.ProjectComponentInfo;
import org.opendevstack.component_provisioner.server.controllers.exceptions.InvalidRestEntityException;
import org.opendevstack.component_provisioner.server.controllers.exceptions.ProjectConfigurationException;
import org.opendevstack.component_provisioner.server.controllers.exceptions.RestEntityNotFoundException;
import org.opendevstack.component_provisioner.server.controllers.exceptions.SlugNotFoundException;
import org.opendevstack.component_provisioner.server.controllers.model.ProjectComponentStatus;
import org.opendevstack.component_provisioner.server.controllers.model.awx.AwxResponse;
Expand All @@ -22,6 +24,7 @@
import org.springframework.web.client.RestClientException;

import java.util.Arrays;
import java.util.Objects;

@Service
@Slf4j
Expand Down Expand Up @@ -161,16 +164,17 @@ public void validate(String projectKey, String componentId, CreateIncidentAction
var isDeployed = getParameterString(createIncidentAction, "is_deployed");
var changeNumber = getParameterString(createIncidentAction, "change_number");
var reason = getParameterString(createIncidentAction, "reason");
var hasAutomatedDeletionWorkflow = getAutomatedDeletionWorkflowFlag(projectKey, componentId);

var mainParamsAreEmpty = StringUtils.isBlank(projectKey) || StringUtils.isBlank(componentId);
var extraParamsAreEmtpy = StringUtils.isBlank(isDeployed)
var extraParamsAreEmpty = StringUtils.isBlank(isDeployed)
|| StringUtils.isBlank(changeNumber) || StringUtils.isBlank(reason);

if (mainParamsAreEmpty) {
if (!hasAutomatedDeletionWorkflow && mainParamsAreEmpty) {
throw new InvalidRestEntityException("project_key, component_id are required.");
}

if (extraParamsAreEmtpy) {
if (extraParamsAreEmpty) {
throw new InvalidRestEntityException("is_deployed, change_number and reason are required.");
}
}
Expand Down Expand Up @@ -239,4 +243,13 @@ private AwxWorkflowJobLaunch buildAwxWorkflowJobLaunch(String projectKey, String

return entitiesMapper.asAwxWorkflowJobLaunch(createIncidentAction);
}

public boolean getAutomatedDeletionWorkflowFlag(String projectKey, String componentId) {
var accessToken = authenticationProvider.getAccessToken();
return componentCatalogService.getProjectComponents(projectKey, accessToken).stream()
.filter(projectComponentInfo -> componentId.equals(projectComponentInfo.getComponentId()))
.findFirst()
.map(ProjectComponentInfo::getHasAutomatedDeletionWorkflow)
.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static ProjectComponentInfo of(ProjectComponentStatus status) {
.componentId("componentId")
.componentUrl("http://www.example.com")
.canBeDeleted(false)
.hasAutomatedDeletionWorkflow(false)
.status(status.name())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,88 @@ void givenAProjectWithNoClusters_whenAddSystemParametersToActionIsCalled_thenThr
var ex = assertThrows(ProjectConfigurationException.class, () -> facade.addSystemParametersToAction(projectKey, action));
assertThat(ex.getMessage()).contains("PRJ");
}

@Test
void givenComponentWithAutomatedDeletionWorkflowTrue_whenGetAutomatedDeletionWorkflowFlagIsCalled_thenReturnsTrue() {
// given
var projectKey = "PRJ";
var componentId = "CID";
var accessToken = "token";

var component = ProjectComponentInfoMother.of();
component.setComponentId(componentId);
component.setHasAutomatedDeletionWorkflow(true);

when(authenticationProvider.getAccessToken()).thenReturn(accessToken);
when(componentCatalogService.getProjectComponents(projectKey, accessToken))
.thenReturn(List.of(component));

// when
var result = facade.getAutomatedDeletionWorkflowFlag(projectKey, componentId);

// then
assertThat(result).isTrue();
}

@Test
void givenComponentWithAutomatedDeletionWorkflowFalse_whenGetAutomatedDeletionWorkflowFlagIsCalled_thenReturnsFalse() {
// given
var projectKey = "PRJ";
var componentId = "CID";
var accessToken = "token";

var component = ProjectComponentInfoMother.of();
component.setComponentId(componentId);
component.setHasAutomatedDeletionWorkflow(false);

when(authenticationProvider.getAccessToken()).thenReturn(accessToken);
when(componentCatalogService.getProjectComponents(projectKey, accessToken))
.thenReturn(List.of(component));

// when
var result = facade.getAutomatedDeletionWorkflowFlag(projectKey, componentId);

// then
assertThat(result).isFalse();
}

@Test
void givenComponentNotFound_whenGetAutomatedDeletionWorkflowFlagIsCalled_thenReturnsFalse() {
// given
var projectKey = "PRJ";
var componentId = "CID";
var accessToken = "token";

var otherComponent = ProjectComponentInfoMother.of();
otherComponent.setComponentId("OTHER");
otherComponent.setHasAutomatedDeletionWorkflow(true);

when(authenticationProvider.getAccessToken()).thenReturn(accessToken);
when(componentCatalogService.getProjectComponents(projectKey, accessToken))
.thenReturn(List.of(otherComponent));

// when
var result = facade.getAutomatedDeletionWorkflowFlag(projectKey, componentId);

// then
assertThat(result).isFalse();
}

@Test
void givenNoComponents_whenGetAutomatedDeletionWorkflowFlagIsCalled_thenReturnsFalse() {
// given
var projectKey = "PRJ";
var componentId = "CID";
var accessToken = "token";

when(authenticationProvider.getAccessToken()).thenReturn(accessToken);
when(componentCatalogService.getProjectComponents(projectKey, accessToken))
.thenReturn(Collections.emptyList());

// when
var result = facade.getAutomatedDeletionWorkflowFlag(projectKey, componentId);

// then
assertThat(result).isFalse();
}
}
Loading