Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAI-1844: Create hankekayttaja instance for Hanke perustaja #390

Merged
merged 13 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,14 +1,12 @@
package fi.hel.haitaton.hanke

import com.fasterxml.jackson.databind.node.ObjectNode
import fi.hel.haitaton.hanke.application.ApplicationsResponse
import fi.hel.haitaton.hanke.domain.HankeWithApplications
import fi.hel.haitaton.hanke.domain.Hankealue
import fi.hel.haitaton.hanke.factory.AlluDataFactory
import fi.hel.haitaton.hanke.factory.DateFactory
import fi.hel.haitaton.hanke.factory.HankeFactory
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistaja
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withPerustaja
import fi.hel.haitaton.hanke.geometria.Geometriat
import fi.hel.haitaton.hanke.logging.DisclosureLogService
import fi.hel.haitaton.hanke.permissions.PermissionCode
Expand Down Expand Up @@ -334,22 +332,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
}
}

@Test
fun `When perustaja is provided return provided information`() {
val hanke = HankeFactory.create().withPerustaja()
every { hankeService.createHanke(any()) } returns hanke

post(url, hanke)
.andExpect(status().isOk)
.andExpect(jsonPath("$.perustaja.nimi").value("Pertti Perustaja"))
.andExpect(jsonPath("$.perustaja.email").value("[email protected]"))

verifySequence {
hankeService.createHanke(any())
disclosureLogService.saveDisclosureLogsForHanke(any(), any())
}
}

@Test
fun `Sanitize hanke input and return 200`() {
val hanke = HankeFactory.create().apply { generated = true }
Expand All @@ -364,17 +346,6 @@ class HankeControllerITests(@Autowired override val mockMvc: MockMvc) : Controll
}
}

@Test
fun `With perustaja without sahkoposti returns 400`() {
val hakemus = HankeFactory.create().withPerustaja()
val content: ObjectNode = OBJECT_MAPPER.valueToTree(hakemus)
(content.get("perustaja") as ObjectNode).remove("email")

postRaw(url, content.toJsonString())
.andExpect(status().isBadRequest)
.andExpect(hankeError(HankeError.HAI0003))
}

@Test
fun `Add Hanke and HankeYhteystiedot and return it with newly created hankeTunnus (POST)`() {
val hankeToBeMocked =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -169,7 +168,6 @@ 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 @@ -29,7 +29,6 @@ import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistaj
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistajat
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedRakennuttaja
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withHankealue
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withPerustaja
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withYhteystiedot
import fi.hel.haitaton.hanke.factory.HankealueFactory
import fi.hel.haitaton.hanke.geometria.Geometriat
Expand Down Expand Up @@ -96,13 +95,13 @@ class HankeServiceITests : DatabaseTest() {

@MockkBean private lateinit var cableReportService: CableReportService
@Autowired private lateinit var hankeService: HankeService
@Autowired private lateinit var permissionService: PermissionService
@Autowired private lateinit var auditLogRepository: AuditLogRepository
@Autowired private lateinit var applicationRepository: ApplicationRepository
@Autowired private lateinit var hankeRepository: HankeRepository
@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 @@ -117,8 +116,7 @@ class HankeServiceITests : DatabaseTest() {

@Test
fun `create Hanke with full data set succeeds and returns a new domain object with the correct values`() {
val hanke: Hanke =
getATestHanke().withYhteystiedot { it.id = null }.withPerustaja().withHankealue()
val hanke: Hanke = getATestHanke().withYhteystiedot { it.id = null }.withHankealue()
corvidian marked this conversation as resolved.
Show resolved Hide resolved

val datetimeAlku = hanke.alueet[0].haittaAlkuPvm // nextyear.2.20 23:45:56Z
val datetimeLoppu = hanke.alueet[0].haittaLoppuPvm // nextyear.2.21 0:12:34Z
Expand Down Expand Up @@ -149,7 +147,7 @@ class HankeServiceITests : DatabaseTest() {
assertThat(returnedHanke.loppuPvm).isEqualTo(expectedDateLoppu)
assertThat(returnedHanke.vaihe).isEqualTo(Vaihe.SUUNNITTELU)
assertThat(returnedHanke.suunnitteluVaihe).isEqualTo(SuunnitteluVaihe.RAKENNUS_TAI_TOTEUTUS)
assertThat(returnedHanke.perustaja).isEqualTo(Perustaja("Pertti Perustaja", "[email protected]"))
assertThat(returnedHanke.perustaja).isNull()
assertThat(returnedHanke.tyomaaKatuosoite).isEqualTo("Testikatu 1")
assertThat(returnedHanke.tyomaaTyyppi).contains(TyomaaTyyppi.VESI, TyomaaTyyppi.MUU)
assertThat(returnedHanke.alueet[0].kaistaHaitta)
Expand Down Expand Up @@ -210,8 +208,8 @@ class HankeServiceITests : DatabaseTest() {
assertThat(rakennuttaja.id).isNotEqualTo(firstId)
assertThat(toteuttaja.id).isNotEqualTo(firstId)
assertThat(toteuttaja.id).isNotEqualTo(rakennuttaja.id)
assertThat(hankeKayttajaRepository.findAll()).hasSize(5)
assertThat(kayttajaTunnisteRepository.findAll()).hasSize(4) // Hanke perustaja not included
assertThat(hankeKayttajaRepository.findAll()).hasSize(4)
assertThat(kayttajaTunnisteRepository.findAll()).hasSize(4)
}

@Test
Expand Down Expand Up @@ -942,9 +940,10 @@ class HankeServiceITests : DatabaseTest() {

with(result) {
val application = applications.first()

assertThat(hanke.hankeTunnus).isEqualTo(application.hankeTunnus)
assertThat(hanke.nimi).isEqualTo(application.applicationData.name)
assertThat(hanke.perustaja)
.isEqualTo(Perustaja("Teppo Testihenkilö", "[email protected]"))
assertThat(application.applicationData.name)
.isEqualTo(inputApplication.applicationData.name)
}
Expand Down Expand Up @@ -1383,10 +1382,11 @@ class HankeServiceITests : DatabaseTest() {
)
val templateData =
TemplateData(
hankeId = updatedHanke.id!!,
hankeTunnus = updatedHanke.hankeTunnus!!,
alueId = updatedHanke.alueet[0].id,
geometriaId = updatedHanke.alueet[0].geometriat?.id,
updatedHanke.id!!,
updatedHanke.hankeTunnus!!,
updatedHanke.perustaja != null,
updatedHanke.alueet[0].id,
updatedHanke.alueet[0].geometriat?.id,
hankeVersion = 1,
geometriaVersion = 1,
tormaystarkasteluTulos = true,
Expand Down Expand Up @@ -1451,8 +1451,7 @@ class HankeServiceITests : DatabaseTest() {
}

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

private fun HankeEntity.toDomainObject(): Hanke =
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
)
with(this) {
Hanke(
id,
hankeTunnus,
onYKTHanke,
nimi,
kuvaus,
vaihe,
suunnitteluVaihe,
version,
createdByUserId ?: "",
createdAt?.atZone(TZ_UTC),
modifiedByUserId,
modifiedAt?.atZone(TZ_UTC),
this.status
)
}

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

private fun assertFeaturePropertiesIsReset(hanke: Hanke, propertiesWanted: Map<String, Any?>) {
assertThat(hanke.alueet).isNotEmpty
Expand All @@ -1508,6 +1510,7 @@ 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 @@ -1535,17 +1538,18 @@ class HankeServiceITests : DatabaseTest() {
): String {
val templateData =
TemplateData(
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,
hanke.id!!,
hanke.hankeTunnus!!,
hanke.perustaja != null,
alue?.id,
alue?.geometriat?.id,
geometriaVersion,
hankeVersion,
nextYear(),
tormaystarkasteluTulos,
alue?.nimi,
alkuPvm,
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,10 +121,13 @@ class ApplicationServiceITest : DatabaseTest() {
confirmVerified(cableReportServiceAllu)
}

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

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

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

Expand Down Expand Up @@ -370,9 +373,9 @@ class ApplicationServiceITest : DatabaseTest() {
@Test
fun `getAllApplicationsForUser returns applications for user hankkeet`() {
assertThat(applicationRepository.findAll()).isEmpty()
val hanke = createHankeEntity(hankeTunnus = "HAI-1234")
val hanke2 = createHankeEntity(hankeTunnus = "HAI-1235")
val hanke3 = createHankeEntity(hankeTunnus = "HAI-1236")
val hanke = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1234"))
val hanke2 = hankeRepository.save(HankeEntity(hankeTunnus = "HAI-1235"))
val hanke3 = hankeRepository.save(HankeEntity(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 @@ -397,7 +400,7 @@ class ApplicationServiceITest : DatabaseTest() {

@Test
fun `getApplicationById returns correct application`() {
val hanke = createHankeEntity(hankeTunnus = "HAI-1234")
val hanke = hankeRepository.save(HankeEntity(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 @@ -507,7 +510,7 @@ class ApplicationServiceITest : DatabaseTest() {

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

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

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