From 6d3cc0c42db3ba2d78160aca3646d55ed90c0a76 Mon Sep 17 00:00:00 2001 From: Tommy Lau <8563294+tommylau523@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:24:07 -0800 Subject: [PATCH] SW-6306 Added event listener to sync deal name and application internal name (#2677) This is currently designed to be one way only. --- ...ater.kt => ApplicationVariablesUpdater.kt} | 14 ++- .../ApplicationVariableValuesService.kt | 21 +++++ ....kt => ApplicationVariablesUpdaterTest.kt} | 94 +++++++++++-------- .../ApplicationVariableValuesServiceTest.kt | 30 ++++++ 4 files changed, 121 insertions(+), 38 deletions(-) rename src/main/kotlin/com/terraformation/backend/accelerator/{ApplicationCountryUpdater.kt => ApplicationVariablesUpdater.kt} (77%) rename src/test/kotlin/com/terraformation/backend/accelerator/{ApplicationCountryUpdaterTest.kt => ApplicationVariablesUpdaterTest.kt} (60%) diff --git a/src/main/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdater.kt b/src/main/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdater.kt similarity index 77% rename from src/main/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdater.kt rename to src/main/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdater.kt index 327bc9475371..5a538d01825c 100644 --- a/src/main/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdater.kt +++ b/src/main/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdater.kt @@ -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 @@ -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, @@ -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, + ) + } + } } diff --git a/src/main/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesService.kt b/src/main/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesService.kt index 89e620d3abb3..5fef216dc63a 100644 --- a/src/main/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesService.kt +++ b/src/main/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesService.kt @@ -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 @@ -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) } + } + } } diff --git a/src/test/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdaterTest.kt b/src/test/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdaterTest.kt similarity index 60% rename from src/test/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdaterTest.kt rename to src/test/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdaterTest.kt index 5fc9666f4137..f8c5345d05ab 100644 --- a/src/test/kotlin/com/terraformation/backend/accelerator/ApplicationCountryUpdaterTest.kt +++ b/src/test/kotlin/com/terraformation/backend/accelerator/ApplicationVariablesUpdaterTest.kt @@ -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 @@ -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() private val variableStore = mockk() private val applicationVariableValuesService = mockk() - private val updater: ApplicationCountryUpdater by lazy { - ApplicationCountryUpdater( + private val updater: ApplicationVariablesUpdater by lazy { + ApplicationVariablesUpdater( SystemUser(usersDao), applicationStore, variableStore, @@ -76,8 +78,10 @@ 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", @@ -85,43 +89,59 @@ class ApplicationCountryUpdaterTest : DatabaseTest(), RunsAsUser { 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) + } + } } } diff --git a/src/test/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesServiceTest.kt b/src/test/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesServiceTest.kt index 425b883a3cab..5576adf0bca0 100644 --- a/src/test/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesServiceTest.kt +++ b/src/test/kotlin/com/terraformation/backend/accelerator/variables/ApplicationVariableValuesServiceTest.kt @@ -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") + } + } }