Skip to content

Commit

Permalink
add backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Nov 29, 2024
1 parent 6c2668b commit a48f997
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExportMissionAEMMultipleZipped(
val output = zipFiles.execute(filesToZip)

return MissionExportEntity(
fileName = "Rapports de patrouille.odt",
fileName = "tableaux-AEM.zip",
fileContent = output
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,14 +25,14 @@ class ExportMissionPatrolCombined(
* @return a MissionExportEntity with file name and content
*/
fun execute(missionIds: List<Int>): MissionExportEntity? {

try {
if (missionIds.isEmpty()) return null

// retrieve missions
var missions = mutableListOf<MissionEntity>()

for (missionId in missionIds) {
val mission = getMissionById.execute(missionId)
val mission = getMission.execute(missionId)
if (mission != null) {
missions.add(mission)
}
Expand All @@ -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()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +25,8 @@ class ExportMissionPatrolMultipleZipped(
*/
fun execute(missionIds: List<Int>): MissionExportEntity? {
try {
if (missionIds.isEmpty()) return null

val filesToZip = mutableListOf<File>();

// retrieve missions
Expand All @@ -44,7 +45,7 @@ class ExportMissionPatrolMultipleZipped(
val output = zipFiles.execute(filesToZip)

return MissionExportEntity(
fileName = "Rapports de patrouille.odt",
fileName = "rapports-patrouille.zip",
fileContent = output
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)

}
}
Original file line number Diff line number Diff line change
@@ -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)
}

}
Loading

0 comments on commit a48f997

Please sign in to comment.