Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1256 from ZupIT/hotfix/deployment-configuration-d…
Browse files Browse the repository at this point in the history
…elete-and-patch

HotFix: Fixing deployment configuration validation on workspace update
  • Loading branch information
Felipe Brunelli de Andrade committed May 21, 2021
2 parents a9dc2d2 + 3d03496 commit 81a40fd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open class PatchWorkspaceInteractorImpl(

val workspace = workspaceService.find(workspaceId)

checkIfDeploymentConfigurationCanBeUpdated(workspaceId, request.patches)
checkIfDeploymentConfigurationCanBeUpdated(request.patches, workspace)

val updatedWorkspace = request.applyPatch(workspace)

Expand Down Expand Up @@ -134,19 +134,30 @@ open class PatchWorkspaceInteractorImpl(
return configuration != updatedInformation && updatedInformation != null
}

private fun checkIfDeploymentConfigurationCanBeUpdated(workspaceId: String, patches: List<PatchOperation>) {
private fun checkIfDeploymentConfigurationCanBeUpdated(patches: List<PatchOperation>, workspace: Workspace) {
patches.forEach { patch ->
if (isDeploymentConfigurationDelete(patch) && hasActiveDeploymentInWorkspace(workspaceId)) {
if (isDeploymentConfigurationDeleteOrUpdate(patch, workspace) && hasActiveDeploymentInWorkspace(workspace.id)) {
throw BusinessException.of(MooveErrorCode.ACTIVE_DEPLOYMENT_NAMESPACE_ERROR)
}
}
}

private fun isDeploymentConfigurationDelete(patch: PatchOperation): Boolean {
return (patch.op == OpCodeEnum.REMOVE || patch.op == OpCodeEnum.REPLACE) &&
private fun isDeploymentConfigurationDeleteOrUpdate(patch: PatchOperation, workspace: Workspace): Boolean {
return isDeploymentDeleteConfiguration(patch) ||
isDeploymentUpdateConfiguration(patch, workspace)
}

private fun isDeploymentDeleteConfiguration(patch: PatchOperation): Boolean {
return patch.op == OpCodeEnum.REMOVE &&
patch.path == DEPLOYMENT_CONFIGURATION_PATH
}

private fun isDeploymentUpdateConfiguration(patch: PatchOperation, workspace: Workspace): Boolean {
return patch.op == OpCodeEnum.REPLACE &&
patch.path == DEPLOYMENT_CONFIGURATION_PATH &&
!workspace.deploymentConfigurationId.isNullOrBlank()
}

private fun hasActiveDeploymentInWorkspace(workspaceId: String): Boolean {
return deploymentService.existsActiveListByWorkspace(workspaceId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,67 +154,6 @@ class PatchWorkspaceInteractorImplTest extends Specification {
}
}

def 'when replacing deployment configuration id, should patch information successfully'() {
def author = getDummyUser()
given:
def previousDeploymentConfigId = "ba7006e0-a653-46dd-90be-71a0987f548a"
def newDeploymentConfigId = TestUtils.deploymentConfigId
def newDeploymentConfig = TestUtils.deploymentConfig
def workspace = new Workspace("309d992e-9d3c-4a32-aa78-e19471affd56", "Workspace Name", author, LocalDateTime.now(), [],
WorkspaceStatusEnum.INCOMPLETE, null, null, null, null, previousDeploymentConfigId)
def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REPLACE, "/deploymentConfigurationId", newDeploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
0 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
0 * gitConfigurationRepository.exists(_, _)
0 * villagerService.checkIfRegistryConfigurationExists(_, _)
1 * deploymentConfigurationRepository.exists(workspace.id, newDeploymentConfigId) >> newDeploymentConfig
0 * metricConfigurationRepository.exists(_, _)
1 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
1 * workspaceRepository.update(_) >> { arguments ->
def workspaceUpdated = arguments[0]
workspaceUpdated instanceof Workspace

assert workspaceUpdated.id == workspace.id
assert workspaceUpdated.name == workspace.name
assert workspaceUpdated.author == workspace.author
assert workspaceUpdated.status == workspace.status
assert workspaceUpdated.gitConfigurationId == workspace.gitConfigurationId
assert workspaceUpdated.registryConfigurationId == workspace.gitConfigurationId
assert workspaceUpdated.deploymentConfigurationId == newDeploymentConfigId
}
}

def 'when deployment configuration id does not exist, should throw exception'() {
given:
def deploymentConfigId = TestUtils.deploymentConfigId
def newDeploymentConfigId = "9014531b-372f-4b11-9259-dc9a5779f69a"
def author = getDummyUser()
def workspace = new Workspace("309d992e-9d3c-4a32-aa78-e19471affd56", "Workspace Name", author, LocalDateTime.now(), [],
WorkspaceStatusEnum.INCOMPLETE, null, null, null, null, deploymentConfigId)

def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REPLACE, "/deploymentConfigurationId", newDeploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
0 * gitConfigurationRepository.exists(_, _)
0 * villagerService.checkIfRegistryConfigurationExists(_, _)
1 * deploymentConfigurationRepository.exists(workspace.id, newDeploymentConfigId)
1 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
0 * workspaceRepository.update(_)

def ex = thrown(NotFoundException)
ex.resourceName == "deploymentConfigurationId"
ex.id == newDeploymentConfigId
}

def 'when registry configuration id does not exist, should throw exception'() {
given:
def registryConfigurationId = "e6128936-3fb2-4d10-9264-4ac63b689e56"
Expand Down Expand Up @@ -528,23 +467,69 @@ class PatchWorkspaceInteractorImplTest extends Specification {
}
}

def 'when removing deployment configuration and dont have active deployment, should patch information successfully'() {
def 'when deployment configuration id does not exist, should throw exception'() {
given:
def workspace = TestUtils.workspace
def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REMOVE, "/deploymentConfigurationId", TestUtils.deploymentConfigId)])
def deploymentConfigId = TestUtils.deploymentConfigId
def newDeploymentConfigId = "9014531b-372f-4b11-9259-dc9a5779f69a"
def author = getDummyUser()
def workspace = new Workspace("309d992e-9d3c-4a32-aa78-e19471affd56", "Workspace Name", author, LocalDateTime.now(), [],
WorkspaceStatusEnum.INCOMPLETE, null, null, null, null, deploymentConfigId)

def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REPLACE, "/deploymentConfigurationId", newDeploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
0 * gitConfigurationRepository.exists(_, _)
0 * villagerService.checkIfRegistryConfigurationExists(_, _)
1 * deploymentConfigurationRepository.exists(workspace.id, newDeploymentConfigId)
1 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
1 * workspaceRepository.update(_)
0 * workspaceRepository.update(_)

def ex = thrown(NotFoundException)
ex.resourceName == "deploymentConfigurationId"
ex.id == newDeploymentConfigId
}

def 'when replacing deployment configuration id and not have active deployment, should patch information successfully'() {

def 'when removing deployment configuration and have active deployment, should throw exception'() {
given:
def previousDeploymentConfigId = "ba7006e0-a653-46dd-90be-71a0987f548a"

def newDeploymentConfigId = TestUtils.deploymentConfigId

def workspace = new Workspace("309d992e-9d3c-4a32-aa78-e19471affd56", "Workspace Name", TestUtils.user, LocalDateTime.now(), [],
WorkspaceStatusEnum.INCOMPLETE, null, null, null, null, previousDeploymentConfigId)

def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REPLACE, "/deploymentConfigurationId", newDeploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
1 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
0 * gitConfigurationRepository.exists(_, _)
0 * villagerService.checkIfRegistryConfigurationExists(_, _)
1 * deploymentConfigurationRepository.exists(workspace.id, newDeploymentConfigId) >> true
0 * metricConfigurationRepository.exists(_, _)
1 * workspaceRepository.update(_) >> { arguments ->
def workspaceUpdated = arguments[0]
workspaceUpdated instanceof Workspace

assert workspaceUpdated.id == workspace.id
assert workspaceUpdated.name == workspace.name
assert workspaceUpdated.author == workspace.author
assert workspaceUpdated.status == workspace.status
assert workspaceUpdated.gitConfigurationId == workspace.gitConfigurationId
assert workspaceUpdated.registryConfigurationId == workspace.gitConfigurationId
assert workspaceUpdated.deploymentConfigurationId == newDeploymentConfigId
}
}

def 'when replacing deployment configuration id and have active deployment, should throw exception'() {
given:
def workspace = TestUtils.workspace
def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REMOVE, "/deploymentConfigurationId", TestUtils.deploymentConfigId)])
Expand All @@ -560,22 +545,44 @@ class PatchWorkspaceInteractorImplTest extends Specification {
exception.errorCode == MooveErrorCode.ACTIVE_DEPLOYMENT_NAMESPACE_ERROR
}

def 'when replacing deployment configuration and dont have active deployment, should patch information successfully'() {
def 'when replacing deployment configuration and is a new configuration, should patch information successfully'() {
given:
def workspace = TestUtils.workspace

def workspace = new Workspace("309d992e-9d3c-4a32-aa78-e19471affd56", "Workspace Name", TestUtils.user, LocalDateTime.now(), [],
WorkspaceStatusEnum.INCOMPLETE, null, null, null, null, null)

def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REPLACE, "/deploymentConfigurationId", TestUtils.deploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
0 * deploymentRepository.existActiveListByWorkspaceId(workspace.id)
0 * gitConfigurationRepository.exists(_, _)
0 * villagerService.checkIfRegistryConfigurationExists(_, _)
1 * deploymentConfigurationRepository.exists(workspace.id, TestUtils.deploymentConfigId) >> true
0 * metricConfigurationRepository.exists(_, _)
1 * workspaceRepository.update(_)

}

def 'when removing deployment configuration and not have active deployment, should patch information successfully'() {
given:
def workspace = TestUtils.workspace
def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REMOVE, "/deploymentConfigurationId", TestUtils.deploymentConfigId)])

when:
interactor.execute(workspace.id, request)

then:
1 * workspaceRepository.find(workspace.id) >> Optional.of(workspace)
1 * deploymentRepository.existActiveListByWorkspaceId(workspace.id) >> false
1 * workspaceRepository.update(_)

}

def 'when replacing deployment configuration and have active deployment, should throw exception'() {
def 'when removing deployment configuration and have active deployment, should throw exception'() {
given:
def workspace = TestUtils.workspace
def request = new PatchWorkspaceRequest([new PatchOperation(OpCodeEnum.REMOVE, "/deploymentConfigurationId", TestUtils.deploymentConfigId)])
Expand All @@ -591,6 +598,7 @@ class PatchWorkspaceInteractorImplTest extends Specification {
exception.errorCode == MooveErrorCode.ACTIVE_DEPLOYMENT_NAMESPACE_ERROR
}


private User getDummyUser() {
new User('4e806b2a-557b-45c5-91be-1e1db909bef6', 'User name', 'user@email.com', 'user.photo.png',
new ArrayList<WorkspacePermissions>(), false, LocalDateTime.now())
Expand Down

0 comments on commit 81a40fd

Please sign in to comment.