Skip to content

Commit

Permalink
HAI-1844: Create hankekayttaja instance for Hanke perustaja
Browse files Browse the repository at this point in the history
Add functionality to include Hanke perustaja in hanke kayttaja listing. Hanke perustaja is now mandatory information in Hanke creation.

A hanke generated from a cable report is a special case. In these cases perustaja is generated from the ordered person in application contacts.
  • Loading branch information
pitkni committed Aug 21, 2023
1 parent ceec889 commit 5ccb892
Show file tree
Hide file tree
Showing 25 changed files with 498 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import fi.hel.haitaton.hanke.geometria.Geometriat
import fi.hel.haitaton.hanke.logging.DisclosureLogService
import fi.hel.haitaton.hanke.permissions.PermissionCode
import fi.hel.haitaton.hanke.permissions.PermissionService
import fi.hel.haitaton.hanke.permissions.Role
import io.mockk.checkUnnecessaryStub
import io.mockk.clearAllMocks
import io.mockk.confirmVerified
Expand Down Expand Up @@ -322,7 +321,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
createdAt = getCurrentTimeUTC(),
)
every { hankeService.createHanke(any()) }.returns(createdHanke)
justRun { permissionService.setPermission(hankeId, USERNAME, Role.KAIKKI_OIKEUDET) }

post(url, hankeToBeCreated)
.andExpect(status().isOk)
Expand All @@ -332,7 +330,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll

verifySequence {
hankeService.createHanke(any())
permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET)
disclosureLogService.saveDisclosureLogsForHanke(createdHanke, USERNAME)
}
}
Expand All @@ -341,7 +338,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
fun `When perustaja is provided return provided information`() {
val hanke = HankeFactory.create().withPerustaja()
every { hankeService.createHanke(any()) } returns hanke
justRun { permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET) }

post(url, hanke)
.andExpect(status().isOk)
Expand All @@ -350,7 +346,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll

verifySequence {
hankeService.createHanke(any())
permissionService.setPermission(any(), any(), any())
disclosureLogService.saveDisclosureLogsForHanke(any(), any())
}
}
Expand All @@ -360,13 +355,11 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
val hanke = HankeFactory.create().apply { generated = true }
every { hankeService.createHanke(hanke.copy(id = null, generated = false)) } returns
hanke.copy(generated = false)
justRun { permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET) }

post(url, hanke).andExpect(status().isOk)

verifySequence {
hankeService.createHanke(any())
permissionService.setPermission(any(), any(), any())
disclosureLogService.saveDisclosureLogsForHanke(any(), any())
}
}
Expand All @@ -393,7 +386,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
id = 12
omistajat[0].id = 3
}
justRun { permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET) }
every { hankeService.createHanke(any()) }.returns(expectedHanke)
val expectedContent = expectedHanke.toJsonString()

Expand All @@ -405,7 +397,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll

verifySequence {
hankeService.createHanke(any())
permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET)
disclosureLogService.saveDisclosureLogsForHanke(expectedHanke, USERNAME)
}
}
Expand Down Expand Up @@ -457,7 +448,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
expectedDateLoppu.toJsonString().substringAfter('"').substringBeforeLast('"')
// faking the service call
every { hankeService.createHanke(any()) }.returns(expectedHanke)
justRun { permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET) }

// Call it and check results
post(url, hankeToBeMocked)
Expand All @@ -472,7 +462,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll

verifySequence {
hankeService.createHanke(any())
permissionService.setPermission(any(), any(), Role.KAIKKI_OIKEUDET)
disclosureLogService.saveDisclosureLogsForHanke(expectedHanke, USERNAME)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.hel.haitaton.hanke

import fi.hel.haitaton.hanke.domain.YhteystietoTyyppi.YRITYS
import fi.hel.haitaton.hanke.factory.HankeFactory
import java.time.LocalDateTime
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -168,6 +169,7 @@ internal class HankeRepositoryITests : DatabaseTest() {
createdAt = null,
modifiedByUserId = null,
modifiedAt = null,
perustaja = HankeFactory.defaultPerustaja.toEntity(),
)

/* Keeping just seconds so that database truncation does not affect testing. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import fi.hel.haitaton.hanke.logging.Status
import fi.hel.haitaton.hanke.logging.UserRole
import fi.hel.haitaton.hanke.permissions.HankeKayttajaRepository
import fi.hel.haitaton.hanke.permissions.KayttajaTunnisteRepository
import fi.hel.haitaton.hanke.permissions.PermissionCode
import fi.hel.haitaton.hanke.permissions.PermissionService
import fi.hel.haitaton.hanke.test.Asserts.isRecent
import fi.hel.haitaton.hanke.test.TestUtils
import fi.hel.haitaton.hanke.test.TestUtils.nextYear
Expand Down Expand Up @@ -100,6 +102,7 @@ class HankeServiceITests : DatabaseTest() {
@Autowired private lateinit var hankeKayttajaRepository: HankeKayttajaRepository
@Autowired private lateinit var kayttajaTunnisteRepository: KayttajaTunnisteRepository
@Autowired private lateinit var jdbcTemplate: JdbcTemplate
@Autowired private lateinit var permissionService: PermissionService

@BeforeEach
fun clearMocks() {
Expand All @@ -125,6 +128,10 @@ class HankeServiceITests : DatabaseTest() {
// Call create and get the return object:
val returnedHanke = hankeService.createHanke(hanke)

// Verify privileges
PermissionCode.values().forEach {
assertThat(permissionService.hasPermission(returnedHanke.id!!, USER_NAME, it)).isTrue()
}
// Check the return object in general:
assertThat(returnedHanke).isNotNull
assertThat(returnedHanke).isNotSameAs(hanke)
Expand Down Expand Up @@ -203,8 +210,8 @@ class HankeServiceITests : DatabaseTest() {
assertThat(rakennuttaja.id).isNotEqualTo(firstId)
assertThat(toteuttaja.id).isNotEqualTo(firstId)
assertThat(toteuttaja.id).isNotEqualTo(rakennuttaja.id)
assertThat(hankeKayttajaRepository.findAll()).hasSize(4)
assertThat(kayttajaTunnisteRepository.findAll()).hasSize(4)
assertThat(hankeKayttajaRepository.findAll()).hasSize(5)
assertThat(kayttajaTunnisteRepository.findAll()).hasSize(5)
}

@Test
Expand Down Expand Up @@ -1376,11 +1383,10 @@ class HankeServiceITests : DatabaseTest() {
)
val templateData =
TemplateData(
updatedHanke.id!!,
updatedHanke.hankeTunnus!!,
updatedHanke.perustaja != null,
updatedHanke.alueet[0].id,
updatedHanke.alueet[0].geometriat?.id,
hankeId = updatedHanke.id!!,
hankeTunnus = updatedHanke.hankeTunnus!!,
alueId = updatedHanke.alueet[0].id,
geometriaId = updatedHanke.alueet[0].geometriat?.id,
hankeVersion = 1,
geometriaVersion = 1,
tormaystarkasteluTulos = true,
Expand Down Expand Up @@ -1445,7 +1451,8 @@ class HankeServiceITests : DatabaseTest() {
}

private fun initHankeWithHakemus(alluId: Int): HankeEntity {
val hanke = hankeRepository.save(HankeEntity(hankeTunnus = "HAI23-1"))
val hanke =
hankeRepository.save(HankeFactory.createNewEntity(id = null, hankeTunnus = "HAI23-1"))
val application =
applicationRepository.save(
AlluDataFactory.createApplicationEntity(
Expand All @@ -1459,36 +1466,33 @@ class HankeServiceITests : DatabaseTest() {
}

private fun HankeEntity.toDomainObject(): Hanke =
with(this) {
Hanke(
id,
hankeTunnus,
onYKTHanke,
nimi,
kuvaus,
vaihe,
suunnitteluVaihe,
version,
createdByUserId ?: "",
createdAt?.atZone(TZ_UTC),
modifiedByUserId,
modifiedAt?.atZone(TZ_UTC),
this.status
)
}
Hanke(
id = id,
hankeTunnus = hankeTunnus,
onYKTHanke = onYKTHanke,
nimi = nimi,
kuvaus = kuvaus,
vaihe = vaihe,
suunnitteluVaihe = suunnitteluVaihe,
version = version,
createdBy = createdByUserId ?: "",
createdAt = createdAt?.atZone(TZ_UTC),
modifiedBy = modifiedByUserId,
modifiedAt = modifiedAt?.atZone(TZ_UTC),
status = status,
perustaja = HankeFactory.defaultPerustaja
)

private fun ApplicationEntity.toDomainObject(): Application =
with(this) {
Application(
id,
alluid,
alluStatus,
applicationIdentifier,
applicationType,
applicationData,
hanke.hankeTunnus ?: ""
)
}
Application(
id = id,
alluid = alluid,
alluStatus = alluStatus,
applicationIdentifier = applicationIdentifier,
applicationType = applicationType,
applicationData = applicationData,
hankeTunnus = hanke.hankeTunnus ?: ""
)

private fun assertFeaturePropertiesIsReset(hanke: Hanke, propertiesWanted: Map<String, Any?>) {
assertThat(hanke.alueet).isNotEmpty
Expand All @@ -1504,7 +1508,6 @@ class HankeServiceITests : DatabaseTest() {
data class TemplateData(
val hankeId: Int,
val hankeTunnus: String,
val hankePerustaja: Boolean = false,
val alueId: Int? = null,
val geometriaId: Int? = null,
val geometriaVersion: Int = 0,
Expand Down Expand Up @@ -1532,18 +1535,17 @@ class HankeServiceITests : DatabaseTest() {
): String {
val templateData =
TemplateData(
hanke.id!!,
hanke.hankeTunnus!!,
hanke.perustaja != null,
alue?.id,
alue?.geometriat?.id,
geometriaVersion,
hankeVersion,
nextYear(),
tormaystarkasteluTulos,
alue?.nimi,
alkuPvm,
loppuPvm,
hankeId = hanke.id!!,
hankeTunnus = hanke.hankeTunnus!!,
alueId = alue?.id,
geometriaId = alue?.geometriat?.id,
geometriaVersion = geometriaVersion,
hankeVersion = hankeVersion,
nextYear = nextYear(),
tormaystarkasteluTulos = tormaystarkasteluTulos,
alueNimi = alue?.nimi,
alkuPvm = alkuPvm,
loppuPvm = loppuPvm,
)
return Template.parse(
"/fi/hel/haitaton/hanke/logging/expectedHankeWithPoints.json.mustache".getResourceAsText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,10 @@ class ApplicationServiceITest : DatabaseTest() {
confirmVerified(cableReportServiceAllu)
}

fun createHanke(): Hanke {
return hankeService.createHanke(HankeFactory.create())
}
fun createHanke(hanke: Hanke = HankeFactory.create()): Hanke = hankeService.createHanke(hanke)

fun createHankeEntity(): HankeEntity {
return hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1234"))
}
fun createHankeEntity(id: Int? = null, hankeTunnus: String? = "HAI-1234"): HankeEntity =
hankeRepository.save(HankeFactory.createNewEntity(id = null, hankeTunnus = hankeTunnus))

@Test
fun `create creates an audit log entry for created application`() {
Expand Down Expand Up @@ -343,8 +340,8 @@ class ApplicationServiceITest : DatabaseTest() {
fun `getAllApplicationsForUser returns applications for the correct user`() {
assertThat(applicationRepository.findAll()).isEmpty()
val otherUser = "otherUser"
val hanke = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1234"))
val hanke2 = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1235"))
val hanke = createHankeEntity(hankeTunnus = "HAI-1234")
val hanke2 = createHankeEntity(hankeTunnus = "HAI-1235")
permissionService.setPermission(hanke.id!!, USERNAME, Role.HAKEMUSASIOINTI)
permissionService.setPermission(hanke2.id!!, "otherUser", Role.HAKEMUSASIOINTI)

Expand Down Expand Up @@ -373,9 +370,9 @@ class ApplicationServiceITest : DatabaseTest() {
@Test
fun `getAllApplicationsForUser returns applications for user hankkeet`() {
assertThat(applicationRepository.findAll()).isEmpty()
val hanke = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1234"))
val hanke2 = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1235"))
val hanke3 = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1236"))
val hanke = createHankeEntity(hankeTunnus = "HAI-1234")
val hanke2 = createHankeEntity(hankeTunnus = "HAI-1235")
val hanke3 = createHankeEntity(hankeTunnus = "HAI-1236")
permissionService.setPermission(hanke.id!!, USERNAME, Role.HAKEMUSASIOINTI)
permissionService.setPermission(hanke2.id!!, USERNAME, Role.HAKEMUSASIOINTI)
val application1 = alluDataFactory.saveApplicationEntity(username = USERNAME, hanke = hanke)
Expand All @@ -400,7 +397,7 @@ class ApplicationServiceITest : DatabaseTest() {

@Test
fun `getApplicationById returns correct application`() {
val hanke = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1234"))
val hanke = createHankeEntity(hankeTunnus = "HAI-1234")
val applications = alluDataFactory.saveApplicationEntities(3, USERNAME, hanke = hanke)
val selectedId = applications[1].id!!
assertThat(applicationRepository.findAll()).hasSize(3)
Expand Down Expand Up @@ -510,7 +507,7 @@ class ApplicationServiceITest : DatabaseTest() {

@Test
fun `create throws exception when application area is outside hankealue`() {
val hanke = hankeService.createHanke(HankeFactory.create().withHankealue())
val hanke = createHanke(HankeFactory.create().withHankealue())
val cableReportApplicationData =
AlluDataFactory.createCableReportApplicationData(areas = listOf(havisAmanda))
val newApplication =
Expand Down Expand Up @@ -881,7 +878,7 @@ class ApplicationServiceITest : DatabaseTest() {

@Test
fun `updateApplicationData throws exception when application area is outside hankealue`() {
val hanke = hankeService.createHanke(HankeFactory.create().withHankealue())
val hanke = createHanke(HankeFactory.create().withHankealue())
val hankeEntity = hankeRepository.getReferenceById(hanke.id!!)
val application =
alluDataFactory.saveApplicationEntity(USERNAME, hanke = hankeEntity) { it.alluid = 21 }
Expand Down Expand Up @@ -1142,7 +1139,7 @@ class ApplicationServiceITest : DatabaseTest() {

@Test
fun `Throws an exception when application area is outside hankealue`() {
val hanke = hankeService.createHanke(HankeFactory.create().withHankealue())
val hanke = createHanke(HankeFactory.create().withHankealue())
val hankeEntity = hankeRepository.getReferenceById(hanke.id!!)
val application =
alluDataFactory.saveApplicationEntity(USERNAME, hanke = hankeEntity) {
Expand Down
Loading

0 comments on commit 5ccb892

Please sign in to comment.