-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
580 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ data class ExtendedFishActionEntity( | |
) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...c/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/GetEnvMissionByIdTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package fr.gouv.gmampa.rapportnav.domain.use_cases.mission | ||
|
||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.ExtendedEnvMissionEntity | ||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionEntity | ||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.envActions.EnvActionControlEntity | ||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.action.ExtendedEnvActionControlEntity | ||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.action.ExtendedEnvActionEntity | ||
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.IEnvMissionRepository | ||
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetEnvMissionById | ||
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.action.AttachControlsToActionControl | ||
import fr.gouv.gmampa.rapportnav.mocks.mission.EnvMissionMock | ||
import fr.gouv.gmampa.rapportnav.mocks.mission.action.EnvActionControlMock | ||
import org.assertj.core.api.Assertions.assertThat | ||
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 | ||
import org.springframework.cache.CacheManager | ||
import org.springframework.cache.annotation.EnableCaching | ||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.test.context.ContextConfiguration | ||
import java.util.* | ||
|
||
@SpringBootTest(classes = [GetEnvMissionById::class]) | ||
@ContextConfiguration(classes = [GetEnvMissionByIdTest.TestConfig::class]) | ||
class GetEnvMissionByIdTest { | ||
|
||
@Configuration | ||
@EnableCaching | ||
class TestConfig { | ||
@Bean | ||
fun cacheManager(): CacheManager { | ||
return ConcurrentMapCacheManager("envMission") | ||
} | ||
} | ||
|
||
@Autowired | ||
private lateinit var getEnvMissionById: GetEnvMissionById | ||
|
||
@Autowired | ||
private lateinit var cacheManager: CacheManager | ||
|
||
@MockBean | ||
private lateinit var monitorEnvApiRepo: IEnvMissionRepository | ||
|
||
@MockBean | ||
private lateinit var attachControlsToActionControl: AttachControlsToActionControl | ||
|
||
private val envControlActionId: UUID = UUID.randomUUID() | ||
private val envControlAction: EnvActionControlEntity = EnvActionControlMock.create(id = envControlActionId) | ||
private val extendedEnvActionEntity: ExtendedEnvActionEntity = ExtendedEnvActionEntity( | ||
controlAction = ExtendedEnvActionControlEntity.fromEnvActionControlEntity(envControlAction) | ||
) | ||
private val mockMissionEntity: MissionEntity = EnvMissionMock.create(id = 1, envActions = listOf(envControlAction)) | ||
private val extendedEnvMissionEntity: ExtendedEnvMissionEntity = | ||
ExtendedEnvMissionEntity.fromEnvMission(mockMissionEntity) | ||
|
||
@BeforeEach | ||
fun setup() { | ||
cacheManager.getCache("envMission")?.clear() | ||
|
||
// Mock the repository and service methods | ||
`when`(monitorEnvApiRepo.findMissionById(1)).thenReturn(mockMissionEntity) | ||
`when`( | ||
attachControlsToActionControl.toEnvAction(envControlActionId.toString(), extendedEnvActionEntity) | ||
).thenReturn( | ||
extendedEnvActionEntity | ||
) | ||
} | ||
|
||
|
||
@Test | ||
fun `test execute returns ExtendedEnvMissionEntity`() { | ||
val missionId = 1 | ||
|
||
// First call | ||
val result = getEnvMissionById.execute(missionId) | ||
assertThat(result).isEqualTo(extendedEnvMissionEntity) | ||
|
||
// Verify repository method was called | ||
verify(monitorEnvApiRepo, times(1)).findMissionById(missionId) | ||
|
||
// Verify cache contains the result | ||
val cachedResult = cacheManager.getCache("envMission")?.get(missionId)?.get() | ||
assertThat(cachedResult).isEqualTo(extendedEnvMissionEntity) | ||
} | ||
|
||
@Test | ||
fun `test execute uses cache`() { | ||
val missionId = 1 | ||
|
||
// First call to cache the result | ||
getEnvMissionById.execute(missionId) | ||
|
||
// Second call | ||
val result = getEnvMissionById.execute(missionId) | ||
assertThat(result).isEqualTo(extendedEnvMissionEntity) | ||
|
||
// Verify repository method was only called once | ||
verify(monitorEnvApiRepo, times(1)).findMissionById(missionId) | ||
} | ||
|
||
@Test | ||
fun `test execute returns null when repository throws exception`() { | ||
// Clear the cache and setup repository to throw an exception | ||
cacheManager.getCache("envMission")?.clear() | ||
`when`(monitorEnvApiRepo.findMissionById(anyInt())).thenThrow(RuntimeException("API error")) | ||
|
||
// Call execute | ||
val missionId = 1 | ||
val result = getEnvMissionById.execute(missionId) | ||
|
||
// Verify result is null | ||
assertThat(result).isNull() | ||
|
||
// Verify repository method was called | ||
verify(monitorEnvApiRepo, times(1)).findMissionById(missionId) | ||
|
||
// Verify cache doesn't contain a result | ||
val cachedResult = cacheManager.getCache("envMission")?.get(missionId)?.get() | ||
assertThat(cachedResult).isNull() | ||
} | ||
|
||
} |
152 changes: 152 additions & 0 deletions
152
.../src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/GetEnvMissionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package fr.gouv.gmampa.rapportnav.domain.use_cases.mission | ||
|
||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.ExtendedEnvMissionEntity | ||
import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionEntity | ||
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.IEnvMissionRepository | ||
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetEnvMissions | ||
import fr.gouv.gmampa.rapportnav.mocks.mission.EnvMissionMock | ||
import org.assertj.core.api.Assertions.assertThat | ||
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 | ||
import org.springframework.cache.CacheManager | ||
import org.springframework.cache.annotation.EnableCaching | ||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.test.context.ContextConfiguration | ||
|
||
@SpringBootTest(classes = [GetEnvMissions::class]) | ||
@ContextConfiguration(classes = [GetEnvMissionsTest.TestConfig::class]) | ||
class GetEnvMissionsTest { | ||
|
||
@Configuration | ||
@EnableCaching | ||
class TestConfig { | ||
@Bean | ||
fun cacheManager(): CacheManager { | ||
return ConcurrentMapCacheManager("envMissions") | ||
} | ||
} | ||
|
||
@Autowired | ||
private lateinit var getEnvMissions: GetEnvMissions | ||
|
||
@Autowired | ||
private lateinit var cacheManager: CacheManager | ||
|
||
@MockBean | ||
private lateinit var envMissionRepository: IEnvMissionRepository | ||
|
||
private val mockEnvMissions = listOf( | ||
EnvMissionMock.create(id = 1), | ||
EnvMissionMock.create(id = 2), | ||
EnvMissionMock.create(id = 3) | ||
) | ||
|
||
private val expectedMissions = listOf( | ||
MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(EnvMissionMock.create(id = 1))), | ||
MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(EnvMissionMock.create(id = 2))), | ||
MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(EnvMissionMock.create(id = 3))), | ||
) | ||
|
||
@BeforeEach | ||
fun setup() { | ||
cacheManager.getCache("envMissions")?.clear() | ||
`when`( | ||
envMissionRepository.findAllMissions( | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any() | ||
) | ||
).thenReturn(mockEnvMissions) | ||
} | ||
|
||
@Test | ||
fun `test execute caches results`() { | ||
// First call | ||
val result1 = getEnvMissions.execute() | ||
assertThat(result1).isNotNull | ||
assertThat(result1).isEqualTo(expectedMissions) | ||
|
||
// Verify repository method was called | ||
verify(envMissionRepository, times(1)).findAllMissions( | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any() | ||
) | ||
|
||
// Second call | ||
val result2 = getEnvMissions.execute() | ||
assertThat(result2).isEqualTo(result1) | ||
|
||
// Verify repository method was not called again | ||
verify(envMissionRepository, times(1)).findAllMissions( | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any() | ||
) | ||
} | ||
|
||
@Test | ||
fun `test execute returns null when repository throws exception`() { | ||
// Clear the cache and setup repository to throw an exception | ||
cacheManager.getCache("envMissions")?.clear() | ||
`when`( | ||
envMissionRepository.findAllMissions( | ||
any(), | ||
any(), | ||
any(), | ||
any(), any(), | ||
any(), | ||
any(), | ||
any(), | ||
any() | ||
) | ||
).thenThrow(RuntimeException("API error")) | ||
|
||
// Call execute | ||
val result = getEnvMissions.execute() | ||
|
||
// Verify result is null | ||
assertThat(result).isNull() | ||
|
||
// Verify repository method was called | ||
verify(envMissionRepository, times(1)).findAllMissions( | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any(), | ||
any() | ||
) | ||
|
||
// Verify cache doesn't contain a result | ||
val cachedResult = cacheManager.getCache("envMissions")?.get("execute")?.get() | ||
assertThat(cachedResult).isNull() | ||
} | ||
} |
Oops, something went wrong.