diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMCombined.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMCombined.kt index cb4e1d6c..d039b731 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMCombined.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMCombined.kt @@ -48,7 +48,7 @@ class ExportMissionAEMCombined( return MissionExportEntity( - fileName = "Rapport de patrouille combiné - ${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt", + fileName = "tableaux-AEM-combinés.ods", fileContent = output?.fileContent.orEmpty() ) diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMMultipleZipped.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMMultipleZipped.kt index 9214a647..4414c041 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMMultipleZipped.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMMultipleZipped.kt @@ -44,7 +44,7 @@ class ExportMissionAEMMultipleZipped( val output = zipFiles.execute(filesToZip) return MissionExportEntity( - fileName = "Rapports de patrouille.odt", + fileName = "tableaux-AEM.zip", fileContent = output ) diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingle.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingle.kt index 77859413..59b9258f 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingle.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingle.kt @@ -62,7 +62,7 @@ class ExportMissionAEMSingle( logger.info("ODS file created and converted to Base64") return MissionExportEntity( - fileName = "Rapport_AEM.ods", + fileName = "tableaux-AEM.ods", fileContent = base64Content ) } else { diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombined.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombined.kt index 4fda658a..31174598 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombined.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombined.kt @@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory class ExportMissionPatrolCombined( private val formatDateTime: FormatDateTime, private val exportMissionPatrolSingle: ExportMissionPatrolSingle, - private val getMissionById: GetMission, + private val getMission: GetMission, ) { private val logger = LoggerFactory.getLogger(ExportMissionPatrolCombined::class.java) @@ -25,14 +25,14 @@ class ExportMissionPatrolCombined( * @return a MissionExportEntity with file name and content */ fun execute(missionIds: List): MissionExportEntity? { - try { + if (missionIds.isEmpty()) return null // retrieve missions var missions = mutableListOf() for (missionId in missionIds) { - val mission = getMissionById.execute(missionId) + val mission = getMission.execute(missionId) if (mission != null) { missions.add(mission) } @@ -46,9 +46,8 @@ class ExportMissionPatrolCombined( // create file val output = exportMissionPatrolSingle.createFile(mission = mission) - return MissionExportEntity( - fileName = "Rapport de patrouille combiné - ${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt", + fileName = "rapports-patrouille-combinés_${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt", fileContent = output?.fileContent.orEmpty() ) diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZipped.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZipped.kt index 28b9dab8..99fb8877 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZipped.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZipped.kt @@ -16,7 +16,6 @@ class ExportMissionPatrolMultipleZipped( private val logger = LoggerFactory.getLogger(ExportMissionPatrolMultipleZipped::class.java) - /** * Returns a zip with several Rapports de Patrouille * There will be one file per complete mission @@ -26,6 +25,8 @@ class ExportMissionPatrolMultipleZipped( */ fun execute(missionIds: List): MissionExportEntity? { try { + if (missionIds.isEmpty()) return null + val filesToZip = mutableListOf(); // retrieve missions @@ -44,7 +45,7 @@ class ExportMissionPatrolMultipleZipped( val output = zipFiles.execute(filesToZip) return MissionExportEntity( - fileName = "Rapports de patrouille.odt", + fileName = "rapports-patrouille.zip", fileContent = output ) diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingle.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingle.kt index fd5f87c9..2fae67ba 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingle.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingle.kt @@ -406,7 +406,7 @@ class ExportMissionPatrolSingle( return MissionExportEntity( - fileName = "Rapport de patrouille ${service?.name ?: ""} ${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt", + fileName = "rapport-patrouille_${service?.name ?: ""}_${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt", fileContent = base64Content ) diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReports.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReports.kt index 16025832..5ff8b6eb 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReports.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReports.kt @@ -24,9 +24,9 @@ class ExportMissionReports( reportType: ExportReportTypeEnum ): MissionExportEntity? { try { - require(!(missionIds.isNullOrEmpty())) { "ExportMissionPatrol - mission list must not be empty" } + if (missionIds.isEmpty()) return null - var output: MissionExportEntity? = when (reportType) { + val output: MissionExportEntity? = when (reportType) { ExportReportTypeEnum.AEM -> { when (exportMode) { ExportModeEnum.INDIVIDUAL_MISSION -> { diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportMissionAEMTests.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportMissionAEMTests.kt index 9a81afdd..5935a219 100644 --- a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportMissionAEMTests.kt +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportMissionAEMTests.kt @@ -1,7 +1,7 @@ package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionActionEntity -import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionAEMExportEntity +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.ExportMissionAEM import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FillAEMExcelRow @@ -38,19 +38,19 @@ class ExportMissionAEMTests { } - @Test - fun `execute AEM export return a MissionAEMExportEntity when mission and action exist`() { - val action = NavActionControlMock.create().toNavActionEntity() - val missionAction = MissionActionEntity.NavAction(action) + @Test + fun `execute AEM export return a MissionAEMExportEntity when mission and action exist`() { + val action = NavActionControlMock.create().toNavActionEntity() + val missionAction = MissionActionEntity.NavAction(action) - val missionId = 1 - val mission = MissionEntityMock.create(actions = listOf(missionAction)) - Mockito.`when`(getMissionById.execute(missionId)).thenReturn(mission) + val missionId = 1 + val mission = MissionEntityMock.create(actions = listOf(missionAction)) + Mockito.`when`(getMissionById.execute(missionId)).thenReturn(mission) - val result = exportMissionAEM.execute(missionId) + val result = exportMissionAEM.execute(missionId) - Assertions.assertThat(result).isNotNull() - Assertions.assertThat(result).isInstanceOf(MissionAEMExportEntity::class.java) + Assertions.assertThat(result).isNotNull() + Assertions.assertThat(result).isInstanceOf(MissionExportEntity::class.java) - } + } } diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportZipMissionsAEMTests.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportZipMissionsAEMTests.kt deleted file mode 100644 index a24ff301..00000000 --- a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/ExportZipMissionsAEMTests.kt +++ /dev/null @@ -1,64 +0,0 @@ -package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export - -import fr.gouv.dgampa.rapportnav.domain.entities.mission.CompletenessForStatsEntity -import fr.gouv.dgampa.rapportnav.domain.entities.mission.CompletenessForStatsStatusEnum -import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionActionEntity -import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionAEMExportEntity -import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission -import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.ExportZipMissionsAEM -import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FillAEMExcelRow -import fr.gouv.gmampa.rapportnav.mocks.mission.MissionEntityMock -import fr.gouv.gmampa.rapportnav.mocks.mission.action.NavActionControlMock -import org.assertj.core.api.Assertions -import org.junit.jupiter.api.Test -import org.mockito.Mockito -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.mock.mockito.MockBean - -@SpringBootTest(classes = [ExportZipMissionsAEM::class]) -class ExportZipMissionsAEMTests { - - @MockBean - private lateinit var getMissionById: GetMission - - @MockBean - private lateinit var fillAEMExcelRow: FillAEMExcelRow - - @Autowired - private lateinit var exportZipMissionsAEM: ExportZipMissionsAEM - - - @Test - fun `should not return a MissionExportAEMEntity because missions are not complete`() { - val action = NavActionControlMock.create().toNavActionEntity() - val missionAction = MissionActionEntity.NavAction(action) - val mission = MissionEntityMock.create(actions = listOf(missionAction)) - val mission2 = MissionEntityMock.create(actions = listOf(missionAction), id = 2) - - Mockito.`when`(getMissionById.execute(1)).thenReturn(mission) - Mockito.`when`(getMissionById.execute(2)).thenReturn(mission2) - - val missionAEMExport = exportZipMissionsAEM.execute(listOf(1, 2)) - - Assertions.assertThat(missionAEMExport).isNotInstanceOf(MissionAEMExportEntity::class.java) - } - - @Test - fun `should return a MissionExportAEMEntity`() { - val action = NavActionControlMock.create().toNavActionEntity() - val missionAction = MissionActionEntity.NavAction(action) - val mission = MissionEntityMock.create( - actions = listOf(missionAction), - completenessForStats = CompletenessForStatsEntity(status = CompletenessForStatsStatusEnum.COMPLETE) - ) - val mission2 = MissionEntityMock.create(actions = listOf(missionAction), id = 2) - - Mockito.`when`(getMissionById.execute(1)).thenReturn(mission) - Mockito.`when`(getMissionById.execute(2)).thenReturn(mission2) - - val missionAEMExport = exportZipMissionsAEM.execute(listOf(1, 2)) - - Assertions.assertThat(missionAEMExport).isInstanceOf(MissionAEMExportEntity::class.java) - } -} diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionsAEMTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingleTest.kt similarity index 53% rename from backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionsAEMTest.kt rename to backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingleTest.kt index 4541dcbe..c5f4bf33 100644 --- a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionsAEMTest.kt +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionAEMSingleTest.kt @@ -1,9 +1,9 @@ package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionActionEntity -import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionAEMExportEntity +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission -import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionsAEM +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionAEMSingle import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FillAEMExcelRow import fr.gouv.gmampa.rapportnav.mocks.mission.MissionEntityMock import fr.gouv.gmampa.rapportnav.mocks.mission.action.NavActionControlMock @@ -14,11 +14,11 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.mock.mockito.MockBean -@SpringBootTest(classes = [ExportMissionsAEM::class]) -class ExportMissionsAEMTest { +@SpringBootTest(classes = [ExportMissionAEMSingle::class]) +class ExportMissionAEMSingleTest { @Autowired - private lateinit var exportMissionListAEM: ExportMissionsAEM + private lateinit var exportMissionListAEM: ExportMissionAEMSingle @MockBean private lateinit var fillAEMExcelRow: FillAEMExcelRow @@ -27,18 +27,31 @@ class ExportMissionsAEMTest { private lateinit var getMissionById: GetMission @Test - fun `execute AEM mission list export return a MissionAEMExportEntity when mission list has actions`() { + fun `execute AEM export return null when mission not exist`() { + val missionId = 123 + val mission = MissionEntityMock.create(id = missionId) + Mockito.`when`(getMissionById.execute(missionId)).thenReturn(null) + + val result = exportMissionListAEM.createFile(mission) + + Assertions.assertThat(result).isNull() + + } + + @Test + fun `execute AEM mission list export return a MissionExportEntity when mission list has actions`() { + val missionId = 1 val action = NavActionControlMock.create().toNavActionEntity() val missionAction = MissionActionEntity.NavAction(action) - val mission = MissionEntityMock.create(actions = listOf(missionAction)) + val mission = MissionEntityMock.create(id = missionId, actions = listOf(missionAction)) - Mockito.`when`(getMissionById.execute(1)).thenReturn(mission) + Mockito.`when`(getMissionById.execute(missionId)).thenReturn(mission) - val result = exportMissionListAEM.execute(listOf(1)) + val result = exportMissionListAEM.createFile(mission) Assertions.assertThat(result).isNotNull() - Assertions.assertThat(result).isInstanceOf(MissionAEMExportEntity::class.java) + Assertions.assertThat(result).isInstanceOf(MissionExportEntity::class.java) } } diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombinedTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombinedTest.kt new file mode 100644 index 00000000..9b4802aa --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolCombinedTest.kt @@ -0,0 +1,70 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 + +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionPatrolCombined +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionPatrolSingle +import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FormatDateTime +import fr.gouv.gmampa.rapportnav.mocks.mission.MissionEntityMock +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.mockito.Mockito +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean + +@SpringBootTest(classes = [ExportMissionPatrolCombined::class, FormatDateTime::class]) +class ExportMissionPatrolCombinedTest { + + @Autowired + private lateinit var exportMissionPatrolCombined: ExportMissionPatrolCombined + + @MockBean + private lateinit var exportMissionPatrolSingle: ExportMissionPatrolSingle + + @MockBean + private lateinit var getMission: GetMission + + @Test + fun `should return null for empty mission list`() { + val result = exportMissionPatrolCombined.execute(emptyList()) + assertEquals(null, result) + } + + @Test + fun `should export a file`() { + val missionIds = listOf(1) + val mission = MissionEntityMock.create(id = missionIds.first()) + Mockito.`when`(exportMissionPatrolSingle.createFile(Mockito.any())).thenReturn( + MissionExportEntity( + fileName = "exportMissionPatrolSingle.odt", + fileContent = "MockContent" + ) + ) + Mockito.`when`(getMission.execute(Mockito.anyInt(), Mockito.any())).thenReturn( + mission + ) + + val result = exportMissionPatrolCombined.execute(missionIds) + + assertNotNull(result) + assertEquals("rapports-patrouille-combinés_2022-01-02.odt", result?.fileName) + } + + @Test + fun `should handle exception and return null`() { + // Arrange: Force an exception when getMission.execute is called + val missionIds = listOf(1) + Mockito.`when`(getMission.execute(Mockito.anyInt(), Mockito.any())) + .thenThrow(RuntimeException("Mock exception")) + + // Act: Call the method + val result = exportMissionPatrolCombined.execute(missionIds) + + // Assert: Verify the result is null and no further interactions happen + assertEquals(null, result) + Mockito.verifyNoInteractions(exportMissionPatrolSingle) + } + +} diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZippedTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZippedTest.kt new file mode 100644 index 00000000..32c400e0 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolMultipleZippedTest.kt @@ -0,0 +1,70 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 + +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionPatrolMultipleZipped +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionPatrolSingle +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ZipFiles +import fr.gouv.gmampa.rapportnav.mocks.mission.MissionEntityMock +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.mockito.Mockito +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean + +@SpringBootTest(classes = [ExportMissionPatrolMultipleZipped::class, ZipFiles::class]) +class ExportMissionPatrolMultipleZippedTest { + + @Autowired + private lateinit var exportMissionPatrolMultipleZipped: ExportMissionPatrolMultipleZipped + + @MockBean + private lateinit var exportMissionPatrolSingle: ExportMissionPatrolSingle + + @MockBean + private lateinit var getMission: GetMission + + @Test + fun `should return null for empty mission list`() { + val result = exportMissionPatrolMultipleZipped.execute(emptyList()) + assertEquals(null, result) + } + + @Test + fun `should export a file`() { + val missionIds = listOf(1) + val mission = MissionEntityMock.create(id = missionIds.first()) + Mockito.`when`(exportMissionPatrolSingle.createFile(Mockito.any())).thenReturn( + MissionExportEntity( + fileName = "exportMissionPatrolSingle.odt", + fileContent = "MockContent" + ) + ) + Mockito.`when`(getMission.execute(Mockito.anyInt(), Mockito.any())).thenReturn( + mission + ) + + val result = exportMissionPatrolMultipleZipped.execute(missionIds) + + assertNotNull(result) + assertEquals("rapports-patrouille.zip", result?.fileName) + } + + @Test + fun `should handle exception and return null`() { + // Arrange: Force an exception when getMission.execute is called + val missionIds = listOf(1) + Mockito.`when`(getMission.execute(Mockito.anyInt(), Mockito.any())) + .thenThrow(RuntimeException("Mock exception")) + + // Act: Call the method + val result = exportMissionPatrolMultipleZipped.execute(missionIds) + + // Assert: Verify the result is null and no further interactions happen + assertEquals(null, result) + Mockito.verifyNoInteractions(exportMissionPatrolSingle) + } + +} diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingleTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingleTest.kt new file mode 100644 index 00000000..ee467a88 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionPatrolSingleTest.kt @@ -0,0 +1,151 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 + +import fr.gouv.dgampa.rapportnav.domain.repositories.mission.action.INavActionStatusRepository +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.crew.GetAgentsCrewByMissionId +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.FormatActionsForTimeline +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.GetInfoAboutNavAction +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.MapStatusDurations +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ExportMissionPatrolSingle +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.generalInfo.GetMissionGeneralInfoByMissionId +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.status.GetNbOfDaysAtSeaFromNavigationStatus +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.status.GetStatusDurations +import fr.gouv.dgampa.rapportnav.domain.use_cases.service.GetServiceById +import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.ComputeDurations +import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FormatDateTime +import fr.gouv.gmampa.rapportnav.mocks.mission.MissionEntityMock +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.mockito.Mockito.reset +import org.mockito.Mockito.`when` +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean + +@SpringBootTest( + classes = [ + ExportMissionPatrolSingle::class, + ComputeDurations::class, + GetInfoAboutNavAction::class, + MapStatusDurations::class, + GetStatusDurations::class, + GetNbOfDaysAtSeaFromNavigationStatus::class, + FormatDateTime::class, + ] +) +class ExportMissionPatrolSingleTest { + + @Autowired + private lateinit var exportMissionRapportPatrouille: ExportMissionPatrolSingle + + @MockBean + private lateinit var getMissionGeneralInfoByMissionId: GetMissionGeneralInfoByMissionId + + @MockBean + private lateinit var agentsCrewByMissionId: GetAgentsCrewByMissionId + + @MockBean + private lateinit var getMission: GetMission + + @MockBean + private lateinit var navActionStatus: INavActionStatusRepository + + @MockBean + private lateinit var formatActionsForTimeline: FormatActionsForTimeline + + @MockBean + private lateinit var getServiceById: GetServiceById + + @BeforeEach + fun setUp() { + reset(getMission) + } + + @Test + fun `execute should return null if mission is not found`() { + val missionId = 123 + `when`(getMission.execute(missionId)).thenReturn(null) + + val result = exportMissionRapportPatrouille.execute(missionId) + + assertThat(result).isNull() + } + + @Test + fun `execute should not throw`() { + val missionId = 123 + assertDoesNotThrow { + exportMissionRapportPatrouille.execute(missionId) + } + } + + @Test + fun `execute should return null when mission is null`() { + val missionId = 123 + `when`(getMission.execute(missionId)).thenReturn(null) + assertThat(exportMissionRapportPatrouille.execute(missionId)).isNull() + } + + @Test + fun `createFile should return null when mission throws`() { + val missionId = 123 + val mission = MissionEntityMock.create(id = missionId) + `when`(getMission.execute(missionId)).thenThrow() + assertThat(exportMissionRapportPatrouille.createFile(mission)).isNull() + } + +// @Test +// fun `execute should return MissionExportEntity`() { +// val missionId = 123 +// val output = MissionExportEntity( +// fileName = "rapport.odt", +// fileContent = "some content" +// ) +// // check why mockito.any() did not work, hence the creation of this mock var +// val exportParams = ExportParams( +// service = null, +// id = "2022-01-02", +// startDateTime = Instant.parse("2022-01-02T12:00:00Z"), +// endDateTime = null, +// presenceMer = mapOf( +// "navigationEffective" to 0, +// "mouillage" to 0, +// "total" to 0, +// ), +// presenceQuai = mapOf( +// "maintenance" to 0, +// "meteo" to 0, +// "representation" to 0, +// "adminFormation" to 0, +// "autre" to 0, +// "contrPol" to 0, +// "total" to 0, +// ), +// indisponibilite = mapOf( +// "technique" to 0, +// "personnel" to 0, +// "total" to 0, +// ), +// nbJoursMer = 0, +// dureeMission = 0, +// patrouilleEnv = 0, +// patrouilleMigrant = 0, +// distanceMilles = null, +// goMarine = null, +// essence = null, +// crew = emptyList(), +// timeline = emptyList(), +// rescueInfo = null, +// nauticalEventsInfo = null, +// antiPollutionInfo = null, +// baaemAndVigimerInfo = null, +// ) +// `when`(getMission.execute(missionId)).thenReturn(MissionEntityMock.create()) +// `when`(exportRepository.execute(exportParams)).thenReturn(output) +// +// val result = exportMission.execute(missionId) +// assertThat(result).isEqualTo(output) +// } +} diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReportsTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReportsTest.kt new file mode 100644 index 00000000..193beceb --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ExportMissionReportsTest.kt @@ -0,0 +1,167 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 + +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.ExportModeEnum +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.ExportReportTypeEnum +import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.* +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mockito +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean + +@SpringBootTest(classes = [ExportMissionReports::class]) +class ExportMissionReportsTest { + + @Autowired + private lateinit var exportMissionReports: ExportMissionReports + + @MockBean + private lateinit var exportMissionPatrolSingle: ExportMissionPatrolSingle + + @MockBean + private lateinit var exportMissionPatrolCombined: ExportMissionPatrolCombined + + @MockBean + private lateinit var exportMissionPatrolMultipleZipped: ExportMissionPatrolMultipleZipped + + @MockBean + private lateinit var exportMissionAEMSingle: ExportMissionAEMSingle + + @MockBean + private lateinit var exportMissionAEMCombined: ExportMissionAEMCombined + + @MockBean + private lateinit var exportMissionAEMMultipleZipped: ExportMissionAEMMultipleZipped + + @BeforeEach + fun setUp() { + Mockito.`when`(exportMissionPatrolSingle.execute(Mockito.anyInt())).thenReturn( + MissionExportEntity( + fileName = "exportMissionPatrolSingle.odt", + fileContent = "MockContent" + ) + ) + Mockito.`when`(exportMissionPatrolCombined.execute(Mockito.anyList())).thenReturn( + MissionExportEntity( + fileName = "exportMissionPatrolCombined.odt", + fileContent = "MockContent" + ) + ) + Mockito.`when`(exportMissionPatrolMultipleZipped.execute(Mockito.anyList())).thenReturn( + MissionExportEntity( + fileName = "exportMissionPatrolMultipleZipped.zip", + fileContent = "MockContent" + ) + ) + Mockito.`when`(exportMissionAEMSingle.execute(Mockito.anyInt())).thenReturn( + MissionExportEntity( + fileName = "exportMissionAEMSingle.ods", + fileContent = "MockContent" + ) + ) + Mockito.`when`(exportMissionAEMCombined.execute(Mockito.anyList())).thenReturn( + MissionExportEntity( + fileName = "exportMissionAEMCombined.ods", + fileContent = "MockContent" + ) + ) + Mockito.`when`(exportMissionAEMMultipleZipped.execute(Mockito.anyList())).thenReturn( + MissionExportEntity( + fileName = "exportMissionAEMMultipleZipped.zip", + fileContent = "MockContent" + ) + ) + } + + @Test + fun `should return null for empty mission list`() { + val result = exportMissionReports.execute( + emptyList(), + ExportModeEnum.INDIVIDUAL_MISSION, + ExportReportTypeEnum.AEM + ) + assertEquals(null, result) + } + + @Test + fun `should export Patrol single mission`() { + val missionIds = listOf(1) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.INDIVIDUAL_MISSION, + ExportReportTypeEnum.PATROL + ) + + assertNotNull(result) + assertEquals("exportMissionPatrolSingle.odt", result?.fileName) + } + + @Test + fun `should export AEM single mission`() { + val missionIds = listOf(1) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.INDIVIDUAL_MISSION, + ExportReportTypeEnum.AEM + ) + + assertNotNull(result) + assertEquals("exportMissionAEMSingle.ods", result?.fileName) + } + + @Test + fun `should export Patrol combined missions`() { + val missionIds = listOf(1, 2, 3) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.COMBINED_MISSIONS_IN_ONE, + ExportReportTypeEnum.PATROL + ) + + assertNotNull(result) + assertEquals("exportMissionPatrolCombined.odt", result?.fileName) + } + + @Test + fun `should export AEM combined missions`() { + val missionIds = listOf(1, 2, 3) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.COMBINED_MISSIONS_IN_ONE, + ExportReportTypeEnum.AEM + ) + + assertNotNull(result) + assertEquals("exportMissionAEMCombined.ods", result?.fileName) + } + + @Test + fun `should export Patrol multiple missions zipped`() { + val missionIds = listOf(1, 2, 3) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.MULTIPLE_MISSIONS_ZIPPED, + ExportReportTypeEnum.PATROL + ) + + assertNotNull(result) + assertEquals("exportMissionPatrolMultipleZipped.zip", result?.fileName) + } + + @Test + fun `should export AEM multiple missions zipped`() { + val missionIds = listOf(1, 2, 3) + val result = exportMissionReports.execute( + missionIds, + ExportModeEnum.MULTIPLE_MISSIONS_ZIPPED, + ExportReportTypeEnum.AEM + ) + + assertNotNull(result) + assertEquals("exportMissionAEMMultipleZipped.zip", result?.fileName) + } +} diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ZipFilesTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ZipFilesTest.kt new file mode 100644 index 00000000..867d4ef9 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/export/v2/ZipFilesTest.kt @@ -0,0 +1,40 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.export.v2 + +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2.ZipFiles +import fr.gouv.dgampa.rapportnav.infrastructure.utils.Base64Converter +import fr.gouv.dgampa.rapportnav.infrastructure.utils.FileUtils +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import java.io.File +import java.util.* + + +class ZipFilesTest { + + private val fileUtils = FileUtils() + private val base64Converter = Base64Converter() + private val zipFiles = ZipFiles() + + @Test + fun `should zip files and return base64 representation`() { + // Arrange + val testDir = File("testDir").apply { mkdir() } + val file1 = File(testDir, "file1.txt").apply { writeText("Content 1") } + val file2 = File(testDir, "file2.txt").apply { writeText("Content 2") } + val files = listOf(file1, file2) + + try { + // Act + val base64Result = zipFiles.execute(files) + + // Assert + assertNotNull(base64Result) + assertTrue(Base64.getDecoder().decode(base64Result).isNotEmpty()) + assertFalse(file1.exists(), "file1 should have been deleted") + assertFalse(file2.exists(), "file2 should have been deleted") + } finally { + // Cleanup + testDir.deleteRecursively() + } + } +}