Skip to content

Commit

Permalink
SW-6306 Added event listener to sync deal name and application intern…
Browse files Browse the repository at this point in the history
…al name (#2677)

This is currently designed to be one way only.
  • Loading branch information
tommylau523 authored Dec 9, 2024
1 parent 4e008cd commit 6d3cc0c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.terraformation.backend.accelerator

import com.terraformation.backend.accelerator.db.ApplicationStore
import com.terraformation.backend.accelerator.event.ApplicationInternalNameUpdatedEvent
import com.terraformation.backend.accelerator.event.VariableValueUpdatedEvent
import com.terraformation.backend.accelerator.variables.ApplicationVariableValuesService
import com.terraformation.backend.accelerator.variables.StableId
Expand All @@ -11,7 +12,7 @@ import jakarta.inject.Named
import org.springframework.context.event.EventListener

@Named
class ApplicationCountryUpdater(
class ApplicationVariablesUpdater(
private val systemUser: SystemUser,
private val applicationStore: ApplicationStore,
private val variableStore: VariableStore,
Expand All @@ -33,4 +34,15 @@ class ApplicationCountryUpdater(
}
}
}

@EventListener
fun on(event: ApplicationInternalNameUpdatedEvent) {
systemUser.run {
val application = applicationStore.fetchOneById(event.applicationId)
applicationVariableValuesService.updateDealName(
application.projectId,
application.internalName,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.terraformation.backend.documentproducer.db.VariableValueStore
import com.terraformation.backend.documentproducer.model.ExistingValue
import com.terraformation.backend.documentproducer.model.SelectValue
import com.terraformation.backend.documentproducer.model.SelectVariable
import com.terraformation.backend.documentproducer.model.TextVariable
import com.terraformation.backend.documentproducer.model.Variable
import com.terraformation.backend.log.perClassLogger
import jakarta.inject.Named
Expand Down Expand Up @@ -148,4 +149,24 @@ class ApplicationVariableValuesService(
operation?.let { variableValueStore.updateValues(listOf(it), false) }
}
}

/** Update deal name variable for a project. */
fun updateDealName(projectId: ProjectId, dealName: String) {
val dealNameVariable =
variablesByStableId[StableIds.dealName] as? TextVariable
?: throw IllegalStateException("Deal name variable stable ID not configured correctly")

val operation =
updateTextValueOperation(
projectId,
dealNameVariable,
null,
dealName,
)

systemUser.run {
// Uses elevated permission to update variables without trigger workflow
operation?.let { variableValueStore.updateValues(listOf(it), false) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.terraformation.backend.accelerator

import com.terraformation.backend.RunsAsUser
import com.terraformation.backend.accelerator.db.ApplicationStore
import com.terraformation.backend.accelerator.event.ApplicationInternalNameUpdatedEvent
import com.terraformation.backend.accelerator.event.VariableValueUpdatedEvent
import com.terraformation.backend.accelerator.model.ApplicationVariableValues
import com.terraformation.backend.accelerator.model.ExistingApplicationModel
Expand All @@ -26,17 +27,18 @@ import io.mockk.verify
import java.math.BigDecimal
import java.time.Instant
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class ApplicationCountryUpdaterTest : DatabaseTest(), RunsAsUser {
class ApplicationVariablesUpdaterTest : DatabaseTest(), RunsAsUser {
override val user: TerrawareUser = mockUser()

private val applicationStore = mockk<ApplicationStore>()
private val variableStore = mockk<VariableStore>()
private val applicationVariableValuesService = mockk<ApplicationVariableValuesService>()

private val updater: ApplicationCountryUpdater by lazy {
ApplicationCountryUpdater(
private val updater: ApplicationVariablesUpdater by lazy {
ApplicationVariablesUpdater(
SystemUser(usersDao),
applicationStore,
variableStore,
Expand Down Expand Up @@ -76,52 +78,70 @@ class ApplicationCountryUpdaterTest : DatabaseTest(), RunsAsUser {
@BeforeEach
fun setup() {
every { applicationStore.fetchByProjectId(projectId) } returns listOf(applicationModel)
every { applicationStore.fetchOneById(applicationId) } returns applicationModel
every { applicationStore.updateCountryCode(applicationId, any()) } returns Unit
every { variableStore.fetchOneVariable(countryVariableId) } returns countryVariable

every { applicationVariableValuesService.fetchValues(projectId) } returns
ApplicationVariableValues(
countryCode = "US",
landUseModelHectares = emptyMap(),
numSpeciesToBePlanted = 10,
projectType = PreScreenProjectType.Terrestrial,
totalExpansionPotential = BigDecimal(10))
every { applicationVariableValuesService.updateDealName(projectId, any()) } returns Unit
}

@Test
fun `updates the country code and internal name of an application if country variable updated`() {
updater.on(VariableValueUpdatedEvent(projectId, countryVariableId))

verify(exactly = 1) { applicationStore.updateCountryCode(applicationId, "US") }
}

@Test
fun `does not update if project has no application`() {
every { applicationStore.fetchByProjectId(projectId) } returns emptyList()

updater.on(VariableValueUpdatedEvent(projectId, countryVariableId))

verify(exactly = 0) { applicationVariableValuesService.fetchValues(any()) }
verify(exactly = 0) { applicationStore.updateCountryCode(any(), any()) }
@Nested
inner class VariableValueUpdatedEventListener {

@Test
fun `updates the country code and internal name of an application if country variable updated`() {
updater.on(VariableValueUpdatedEvent(projectId, countryVariableId))

verify(exactly = 1) { applicationStore.updateCountryCode(applicationId, "US") }
}

@Test
fun `does not update if project has no application`() {
every { applicationStore.fetchByProjectId(projectId) } returns emptyList()

updater.on(VariableValueUpdatedEvent(projectId, countryVariableId))

verify(exactly = 0) { applicationVariableValuesService.fetchValues(any()) }
verify(exactly = 0) { applicationStore.updateCountryCode(any(), any()) }
}

@Test
fun `does not update for a non-country variable`() {
val variableId = VariableId(2)
every { variableStore.fetchOneVariable(variableId) } returns
SelectVariable(
BaseVariableProperties(
id = variableId,
manifestId = null,
name = "not country",
position = 1,
stableId = ""),
false,
emptyList(),
)

updater.on(VariableValueUpdatedEvent(projectId, variableId))

verify(exactly = 0) { applicationVariableValuesService.fetchValues(any()) }
verify(exactly = 0) { applicationStore.updateCountryCode(any(), any()) }
}
}

@Test
fun `does not update for a non-country variable`() {
val variableId = VariableId(2)
every { variableStore.fetchOneVariable(variableId) } returns
SelectVariable(
BaseVariableProperties(
id = variableId,
manifestId = null,
name = "not country",
position = 1,
stableId = ""),
false,
emptyList(),
)

updater.on(VariableValueUpdatedEvent(projectId, variableId))

verify(exactly = 0) { applicationVariableValuesService.fetchValues(any()) }
verify(exactly = 0) { applicationStore.updateCountryCode(any(), any()) }
@Nested
inner class ApplicationInternalNameUpdatedEvent {
@Test
fun `updates deal name variable value to application internal name`() {
updater.on(ApplicationInternalNameUpdatedEvent(applicationId))
verify(exactly = 1) {
applicationVariableValuesService.updateDealName(projectId, applicationModel.internalName)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,34 @@ class ApplicationVariableValuesServiceTest : DatabaseTest(), RunsAsUser {
assertEquals(listOf(brazilOptionId), selections, "Project country variable value selections")
}
}

@Nested
inner class UpdateDealNameVariable {
@Test
fun `adds a new deal name variable value`() {
service.updateDealName(inserted.projectId, "New deal name")

val lastValueRow =
variableValuesDao
.fetchByVariableId(variableIdsByStableId[StableIds.dealName]!!)
.filter { it.projectId!! == inserted.projectId }
.maxBy { it.id!!.value }

assertEquals(lastValueRow.textValue, "New deal name")
}

@Test
fun `replaces existing deal name variable value`() {
insertValue(variableIdsByStableId[StableIds.dealName]!!, textValue = "Old deal name")
service.updateDealName(inserted.projectId, "New deal name")

val lastValueRow =
variableValuesDao
.fetchByVariableId(variableIdsByStableId[StableIds.dealName]!!)
.filter { it.projectId!! == inserted.projectId }
.maxBy { it.id!!.value }

assertEquals(lastValueRow.textValue, "New deal name")
}
}
}

0 comments on commit 6d3cc0c

Please sign in to comment.