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

[Tech] Ajout de logs et de tests pour tous les cas d'usage #1965

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ dependencies {
testImplementation("org.testcontainers:junit-jupiter:1.20.4")
testImplementation("net.java.dev.jna:jna:5.16.0")
testImplementation("net.ttddyy:datasource-proxy:1.10.1")
testImplementation("io.mockk:mockk:1.13.14")
}

group = "fr.gouv.cacem"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ data class MissionEntity(
var startDateTimeUtc: ZonedDateTime,
@Patchable
var endDateTimeUtc: ZonedDateTime? = null,
val createdAtUtc: ZonedDateTime? = null,
val updatedAtUtc: ZonedDateTime? = null,
val createdAtUtc: ZonedDateTime?,
val updatedAtUtc: ZonedDateTime?,
val envActions: List<EnvActionEntity>? = listOf(),
val isDeleted: Boolean,
val isGeometryComputedFromControls: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.mappers.PatchEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IEnvActionRepository
import java.util.*
import org.slf4j.LoggerFactory
import java.util.UUID

@UseCase
class PatchEnvAction(
private val envActionRepository: IEnvActionRepository,
private val patchEnvAction: PatchEntity<EnvActionEntity, PatchableEnvActionEntity>,
) {
private val logger = LoggerFactory.getLogger(PatchEnvAction::class.java)

fun execute(
id: UUID,
patchableEnvActionEntity: PatchableEnvActionEntity,
): EnvActionEntity {
logger.info("Attempt to PATCH envaction $id")
envActionRepository.findById(id)?.let {
patchEnvAction.execute(it, patchableEnvActionEntity)
return envActionRepository.save(it)
val patchedEnvAction = envActionRepository.save(it)
logger.info("envaction $id patched")
return patchedEnvAction
}
throw BackendUsageException(BackendUsageErrorCode.ENTITY_NOT_FOUND, "envAction $id not found")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import org.slf4j.LoggerFactory

@UseCase
class ArchiveAdministration(
private val administrationRepository: IAdministrationRepository,
private val canArchiveAdministration: CanArchiveAdministration,
) {
private val logger = LoggerFactory.getLogger(ArchiveAdministration::class.java)

fun execute(administrationId: Int) {
logger.info("Attempt to ARCHIVE administration $administrationId")
if (!canArchiveAdministration.execute(administrationId)) {
throw BackendUsageException(
BackendUsageErrorCode.UNARCHIVED_CHILD,
"Cannot archive administration (ID=$administrationId) due to some of its control units not being archived.",
)
}

administrationRepository.archiveById(administrationId)
logger.info("Administration $administrationId archived")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import org.slf4j.LoggerFactory

@UseCase
class CanArchiveAdministration(private val administrationRepository: IAdministrationRepository) {
private val logger = LoggerFactory.getLogger(CanArchiveAdministration::class.java)

fun execute(administrationId: Int): Boolean {
logger.info("Can administration $administrationId be archived")
val fullAdministration = administrationRepository.findById(administrationId)

return fullAdministration.controlUnits.all { it.isArchived }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import org.slf4j.LoggerFactory

@UseCase
class CanDeleteAdministration(private val administrationRepository: IAdministrationRepository) {
private val logger = LoggerFactory.getLogger(CanDeleteAdministration::class.java)

fun execute(administrationId: Int): Boolean {
logger.info("Can administration $administrationId be deleted")
val fullAdministration = administrationRepository.findById(administrationId)

return fullAdministration.controlUnits.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.administration
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import org.slf4j.LoggerFactory

@UseCase
class CreateOrUpdateAdministration(private val administrationRepository: IAdministrationRepository) {
private val logger = LoggerFactory.getLogger(CreateOrUpdateAdministration::class.java)

fun execute(administration: AdministrationEntity): AdministrationEntity {
return administrationRepository.save(administration)
logger.info("Attempt to CREATE or UPDATE administration ${administration.id}")
val administrationEntity = administrationRepository.save(administration)
logger.info("Created or updated administration ${administrationEntity.id}")

return administrationEntity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.administration
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.exceptions.CouldNotDeleteException
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import org.slf4j.LoggerFactory

@UseCase
class DeleteAdministration(
private val administrationRepository: IAdministrationRepository,
private val canDeleteAdministration: CanDeleteAdministration,
) {
private val logger = LoggerFactory.getLogger(DeleteAdministration::class.java)

fun execute(administrationId: Int) {
logger.info("Attempt to DELETE administration $administrationId")

if (!canDeleteAdministration.execute(administrationId)) {
throw CouldNotDeleteException(
"Cannot delete administration (ID=$administrationId) due to existing relationships.",
)
}

administrationRepository.deleteById(administrationId)
logger.info("Administration $administrationId deleted")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.administration
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
import org.slf4j.LoggerFactory

@UseCase
class GetAdministrationById(private val administrationRepository: IAdministrationRepository) {
private val logger = LoggerFactory.getLogger(GetAdministrationById::class.java)

fun execute(administrationId: Int): FullAdministrationDTO {
logger.info("GET administration $administrationId")
return administrationRepository.findById(administrationId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GetAdministrations(private val administrationRepository: IAdministrationRe
private val logger = LoggerFactory.getLogger(GetAdministrations::class.java)

fun execute(): List<FullAdministrationDTO> {
logger.info("Attempt to GET all administrations")
val fullAdministrations = administrationRepository.findAll()

logger.info("Found ${fullAdministrations.size} administrations.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class GetAllAMPs(private val ampRepository: IAMPRepository) {
private val logger = LoggerFactory.getLogger(GetAllAMPs::class.java)

fun execute(): List<AMPEntity> {
logger.info("Attempt to GET all AMPs")
val amps = ampRepository.findAll()
logger.info("Found ${amps.size} amps")
logger.info("Found ${amps.size} AMPs")

return amps
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.authorization
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.hash
import fr.gouv.cacem.monitorenv.domain.repositories.IUserAuthorizationRepository
import org.slf4j.LoggerFactory

@UseCase
class DeleteUser(
private val userAuthorizationRepository: IUserAuthorizationRepository,
) {
private val logger = LoggerFactory.getLogger(DeleteUser::class.java)

fun execute(email: String) {
logger.info("Attempt to DELETE user $email")
val hashedEmail = hash(email)

userAuthorizationRepository.delete(hashedEmail)
logger.info("User $email deleted")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class GetAuthorizedUser(

fun execute(email: String): UserAuthorization {
val hashedEmail = hash(email)
logger.info("Attempt to GET user $hashedEmail")

val userAuthorization = userAuthorizationRepository.findByHashedEmail(hashedEmail)

if (userAuthorization != null) {
logger.info("Found user $hashedEmail")
return userAuthorization
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GetIsAuthorizedUser(
* protected, reject
*/
val hashedEmail = hash(email)
logger.info("Is user $hashedEmail AUTHORIZED")
return userAuthorizationRepository.findByHashedEmail(hashedEmail)?.isSuperUser ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.authorization
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.authorization.UserAuthorization
import fr.gouv.cacem.monitorenv.domain.repositories.IUserAuthorizationRepository
import org.slf4j.LoggerFactory

@UseCase
class SaveUser(
private val userAuthorizationRepository: IUserAuthorizationRepository,
) {
private val logger = LoggerFactory.getLogger(SaveUser::class.java)

fun execute(userAuthorization: UserAuthorization) {
logger.info("Attempt to CREATE or UPDATE user ${userAuthorization.hashedEmail}")
userAuthorizationRepository.save(userAuthorization)
logger.info("User ${userAuthorization.hashedEmail} created or updated")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class GetControlPlans(
private val logger = LoggerFactory.getLogger(GetControlPlans::class.java)

fun execute(): ControlPlanThemes {
logger.info("Attempt to GET all control plans")
val controlPlanThemes = controlPlanThemeRepository.findAll()
val controlPlanSubThemes = controlPlanSubThemeRepository.findAll()
val controlPlanTags = controlPlanTagRepository.findAll()
logger.info(
"Found ${controlPlanThemes.size} control plan themes, ${controlPlanSubThemes.size} subthemes, and ${controlPlanTags.size} tags ",
"Found ${controlPlanThemes.size} control plan themes, ${controlPlanSubThemes.size} control plan subthemes and ${controlPlanTags.size} control plan tags",
)

return Triple(controlPlanThemes, controlPlanSubThemes, controlPlanTags)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class GetControlPlansByYear(
private val logger = LoggerFactory.getLogger(GetControlPlansByYear::class.java)

fun execute(year: Int): ControlPlanByYear {
logger.info("Attempt to GET all control plans for year $year")
val controlPlanThemes = controlPlanThemeRepository.findByYear(year)
val controlPlanSubThemes = controlPlanSubThemeRepository.findByYear(year)
val controlPlanTags = controlPlanTagRepository.findByYear(year)
logger.info("Found ${controlPlanSubThemes.size} control plan subthemes for year $year")
logger.info(
"Found ${controlPlanThemes.size} control plan themes, ${controlPlanSubThemes.size} control plan subthemes and ${controlPlanTags.size} control plan tags for year $year",
)

return Triple(controlPlanThemes, controlPlanSubThemes, controlPlanTags)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitRepository
import org.slf4j.LoggerFactory

@UseCase
class ArchiveControlUnit(private val controlUnitRepository: IControlUnitRepository) {
private val logger = LoggerFactory.getLogger(ArchiveControlUnit::class.java)

fun execute(controlUnitId: Int) {
return controlUnitRepository.archiveById(controlUnitId)
logger.info("Attempt to ARCHIVE control unit $controlUnitId")
controlUnitRepository.archiveById(controlUnitId)
logger.info("Control unit $controlUnitId archived")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitResourceRepository
import org.slf4j.LoggerFactory

@UseCase
class ArchiveControlUnitResource(private val controlUnitResourceRepository: IControlUnitResourceRepository) {
private val logger = LoggerFactory.getLogger(ArchiveControlUnitResource::class.java)

fun execute(controlUnitResourceId: Int) {
return controlUnitResourceRepository.archiveById(controlUnitResourceId)
logger.info("Attempt to ARCHIVE control unit resource $controlUnitResourceId")
controlUnitResourceRepository.archiveById(controlUnitResourceId)
logger.info("Control unit resource $controlUnitResourceId archived")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import org.slf4j.LoggerFactory

@UseCase
class CanDeleteControlUnit(
private val missionRepository: IMissionRepository,
private val reportingRepository: IReportingRepository,
) {
private val logger = LoggerFactory.getLogger(CanDeleteControlUnit::class.java)

fun execute(controlUnitId: Int): Boolean {
logger.info("Can control unit $controlUnitId be deleted")
val nonDeletedMissions = missionRepository.findByControlUnitId(controlUnitId).filter { !it.isDeleted }
val nonDeletedReportings = reportingRepository.findByControlUnitId(controlUnitId).filter { !it.isDeleted }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import org.slf4j.LoggerFactory

@UseCase
class CanDeleteControlUnitResource(
private val missionRepository: IMissionRepository,
) {
private val logger = LoggerFactory.getLogger(CanDeleteControlUnitResource::class.java)

fun execute(controlUnitResourceId: Int): Boolean {
logger.info("Can control unit resource $controlUnitResourceId be deleted")
val missions = missionRepository.findByControlUnitResourceId(controlUnitResourceId)

return missions.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitRepository
import org.slf4j.LoggerFactory

@UseCase
class CreateOrUpdateControlUnit(private val controlUnitRepository: IControlUnitRepository) {
private val logger = LoggerFactory.getLogger(CreateOrUpdateControlUnit::class.java)

fun execute(controlUnit: ControlUnitEntity): ControlUnitEntity {
return controlUnitRepository.save(controlUnit)
logger.info("Attempt to CREATE or UPDATE control unit ${controlUnit.id}")
val controlUnitEntity = controlUnitRepository.save(controlUnit)
logger.info("Control unit ${controlUnitEntity.id} created or updated")

return controlUnitEntity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitContactRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitRepository
import org.slf4j.LoggerFactory

@UseCase
class CreateOrUpdateControlUnitContact(
private val controlUnitRepository: IControlUnitRepository,
private val controlUnitContactRepository: IControlUnitContactRepository,
) {
private val logger = LoggerFactory.getLogger(CreateOrUpdateControlUnitContact::class.java)

fun execute(controlUnitContact: ControlUnitContactEntity): ControlUnitContactEntity {
logger.info("Attempt to CREATE or UPDATE control unit contact ${controlUnitContact.id}")
val validControlUnitContact = validateSubscriptions(controlUnitContact)
validatePhone(validControlUnitContact)

Expand All @@ -28,6 +32,8 @@ class CreateOrUpdateControlUnitContact(
createdOrUpdatedControlUnitContact.id,
)
}
logger.info("Control unit contact ${createdOrUpdatedControlUnitContact.id} created or updated")

return createdOrUpdatedControlUnitContact
}

Expand Down
Loading
Loading