Skip to content

Commit

Permalink
test(cache): add tests for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Jul 25, 2024
1 parent f958209 commit 0e162cd
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ data class ExtendedFishActionEntity(
)
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.gouv.dgampa.rapportnav.domain.use_cases.mission

import com.fasterxml.jackson.databind.ObjectMapper
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.ExtendedEnvMissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.ActionCompletionEnum
Expand All @@ -26,8 +25,6 @@ fun createMockMultiPoint(coordinates: List<Coordinate>): MultiPoint {

@UseCase
class GetEnvMissionById(
private val mapper: ObjectMapper,
private val getEnvMissions: GetEnvMissions,
private val monitorEnvApiRepo: IEnvMissionRepository,
private val attachControlsToActionControl: AttachControlsToActionControl,
) {
Expand Down
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()
}

}
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()
}
}
Loading

0 comments on commit 0e162cd

Please sign in to comment.