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-3321 Complete return values of kaivuilmoitus taydennys #919

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

umeetiusbaar
Copy link
Contributor

Description

  1. Use checkChangeInYhteystieto for KaivuilmoitusData contacts
  2. New implementation of checkChangesInAreas - it will check changes inside areas, not just do equals comparison
  3. Implementations of Hakemusalue.listChanges: equals check for other than KaivuilmoitusAlue and field-by-field comparison for KaivuilmoitusAlue
  4. Implementation of Tyoalue.listChanges
  5. Implementation of checking changes in haittojenhallintasuunnitelma
  6. Tests for all

Jira Issue: https://helsinkisolutionoffice.atlassian.net/browse/HAI-3321

Type of change

  • Bug fix
  • New feature
  • Other

Instructions for testing

  1. Create a kaivuilmoitus and a täydennyspyyntö for it
  2. Create täydennys, do changes especially in work areas, haittojenhallintasuunnitelma and contacts
  3. Check the returned muutokset value when the form is saved

Checklist:

  • I have written new tests (if applicable)
  • I have ran the tests myself (if applicable)
  • I have made necessary changes to the documentation, link to confluence
    or other location:

Other relevant info

Please describe here if there is e.g. some requirements for this change or
other info that the tester/user needs to know.

second: Tyoalue,
): String? =
if (property.get(this) != property.get(second)) {
"areas[$alueIndex].tyoalueet[$tyoalueIndex].${property.name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should just return the name of the property. The caller should handle placing the change in the proper context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored to use a single fun <T : Any> T.checkChange(property: KProperty1<T, Any?>, second: T): String? (in Utils.kt)

"areas[$index].${property.name}"
} else null

private fun Tyoalue.checkChange(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same checkChange method that's used in Hakemus could be used for all of these checkChange methods, if the constraints on the D type were relaxed. The method would also need to be moved to a shared location.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored to use a single fun <T : Any> T.checkChange(property: KProperty1<T, Any?>, second: T): String? (in Utils.kt)

val changes = mutableListOf<String>()
checkChange(alueIndex, tyoalueIndex, Tyoalue::geometry, other)?.let { changes.add(it) }
return if (changes.isNotEmpty())
listOf("areas[$alueIndex].tyoalueet[$tyoalueIndex]") + changes
Copy link
Contributor

@corvidian corvidian Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of taking the previous indexes as parameters, it would be cleaner to take the previous path. So this method doesn't need to assume the context in which it's run. Similar to how the path parameters are used in validations.

This would then reduce to changes + path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this as suggested.

): List<String> {
val changedElementsInFirst =
firstAreas.withIndex().mapNotNull { (i, tyoalue) ->
tyoalue.listChanges(index, i, secondAreas.getOrNull(i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's only one field that can be different in a tyoalue, it could be checked right here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the check for Tyoalue.geometry here.

Comment on lines 159 to 163
Haittojenhallintatyyppi.entries.mapNotNull { tyyppi ->
if (first[tyyppi] != second[tyyppi]) {
"areas[$index].haittojenhallintasuunnitelma[${tyyppi.name}]"
} else null
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be clearer if you first filtered out the changed ones and then changed the name.

Suggested change
Haittojenhallintatyyppi.entries.mapNotNull { tyyppi ->
if (first[tyyppi] != second[tyyppi]) {
"areas[$index].haittojenhallintasuunnitelma[${tyyppi.name}]"
} else null
}
Haittojenhallintatyyppi.entries
.filter { tyyppi -> first[tyyppi] != second[tyyppi] }
.map { "areas[$index].haittojenhallintasuunnitelma[${tyyppi.name}]" }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested.

other.haittojenhallintasuunnitelma,
)
)
return if (changes.isNotEmpty()) changes else null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't an empty list a good way to say there were no changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. Changed this.

.map { it.index }
val elementsInSecondButNotFirst = secondAreas.indices.drop(firstAreas.size)
return (changedElementsInFirst + elementsInSecondButNotFirst).map { "areas[$it]" }
firstAreas.withIndex().mapNotNull { (i, area) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If area.listChanges() would return an empty list when there are no changes this could use flatMap with no reason to flatten separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it as suggested.

assertThat(result).containsExactly("name", "startTime")
}

private fun commonFieldCases(): List<Arguments> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to repeat these tests? Both the code and the data interfaces are shared between the two types of applications.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed repeated tests that test the same data

fun `returns several changes when an area is added to the middle`() {
val updated =
@Nested
inner class AreaChanges {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the code checking the area changes are in a different class now, maybe the test code should be for a different class as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created HakemusalueTest and placed tests that test a single Hakemusalue there. Kept those tests that test the area collection (changed the inner test class name to AreasChanges).

Comment on lines 695 to 717
val updated =
base.copy(
areas =
listOf(
ApplicationFactory.createExcavationNotificationArea(
tyoalueet =
listOf(
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.polygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.secondPolygon()
),
)
)
)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deep nesting makes these really hard to read. Some intermediary values would make these a lot more readable.

Suggested change
val updated =
base.copy(
areas =
listOf(
ApplicationFactory.createExcavationNotificationArea(
tyoalueet =
listOf(
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.polygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.secondPolygon()
),
)
)
)
)
val tyoalueet =
listOf(
ApplicationFactory.createTyoalue(geometry = GeometriaFactory.polygon()),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.thirdPolygon()
),
ApplicationFactory.createTyoalue(
geometry = GeometriaFactory.secondPolygon()
),
)
val area =
ApplicationFactory.createExcavationNotificationArea(tyoalueet = tyoalueet)
val updated = base.copy(areas = listOf(area))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested.

…hanges in contacts and in areas and work areas.
…ludes changes in contacts and in areas and work areas.
…his includes changes in contacts and in areas and work areas.
@umeetiusbaar umeetiusbaar force-pushed the HAI-3321/complete-return-values-of-kaivuilmoitus-taydennys branch from c02dd84 to f4c4a9d Compare January 16, 2025 15:50
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
9.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants