Skip to content

Commit

Permalink
Tech & Corrections - Correction de plusieurs bugs (#3977)
Browse files Browse the repository at this point in the history
## Linked issues

- Resolve #3926
- Resolve #3784
- Resolve #3976
- Resolve #3969
- Resolve #3774
- Resolve #3975

----

- [ ] Tests E2E (Cypress)
  • Loading branch information
louptheron authored Dec 18, 2024
2 parents 531841c + 037f823 commit 95db4d7
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,27 @@ data class LogbookMessage(
}

private fun enrichAcknowledgeCorrectionAndDeletion(contextLogbookMessages: List<LogbookMessage>) {
val referenceLogbookMessage = findReferencedLogbookMessage(contextLogbookMessages)
val referenceLogbookMessages = findReferencedLogbookMessages(contextLogbookMessages)
val relatedLogbookMessages = filterRelatedLogbookMessages(contextLogbookMessages)

when (true) {
(operationType == LogbookOperationType.COR) -> {
if (referenceLogbookMessage == null) {
if (referenceLogbookMessages.isEmpty()) {
logger.warn(
"Original message $referencedReportId corrected by message COR $operationNumber is not found.",
)
}

referenceLogbookMessage?.isCorrectedByNewerMessage = true
referenceLogbookMessages.forEach {
it.isCorrectedByNewerMessage = true
}
setIsCorrectedByNewerMessage(relatedLogbookMessages)
}

(operationType == LogbookOperationType.RET && !referencedReportId.isNullOrEmpty()) -> {
referenceLogbookMessage?.setAcknowledge(this.copy())
referenceLogbookMessages.forEach {
it.setAcknowledge(this.copy())
}
}

(transmissionFormat == LogbookTransmissionFormat.FLUX),
Expand All @@ -162,7 +166,9 @@ data class LogbookMessage(
}

(operationType == LogbookOperationType.DEL && !referencedReportId.isNullOrEmpty()) -> {
referenceLogbookMessage?.isDeleted = true
referenceLogbookMessages.forEach {
it.isDeleted = true
}
}

else -> {}
Expand All @@ -186,11 +192,11 @@ data class LogbookMessage(
}
}

private fun findReferencedLogbookMessage(messages: List<LogbookMessage>): LogbookMessage? {
private fun findReferencedLogbookMessages(messages: List<LogbookMessage>): List<LogbookMessage> {
return if (!referencedReportId.isNullOrEmpty()) {
messages.find { it.reportId == referencedReportId }
messages.filter { it.reportId == referencedReportId }
} else {
null
listOf()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class GetPriorNotifications(
vesselInternalReferenceNumbers = internalReferenceNumbers,
isArchived = false,
isDeleted = false,
types = listOf(ReportingType.INFRACTION_SUSPICION),
types = listOf(ReportingType.INFRACTION_SUSPICION, ReportingType.ALERT),
),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.public_api

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.cache.CacheManager
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1/ports")
@Tag(name = "Public APIs for ports")
class PublicPortController(
private val cacheManager: CacheManager,
) {
@PutMapping(value = ["/invalidate"])
@Operation(summary = "Invalidate ports cache")
fun invalidatePorts() {
cacheManager.getCache("ports")?.invalidate()
cacheManager.getCache("port")?.invalidate()
cacheManager.getCache("active_ports")?.invalidate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ object TestUtils {
id = 2,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#1",
operationType = LogbookOperationType.DAT,
messageType = "FAR",
software = "TurboCatch (3.7-1)",
Expand All @@ -201,7 +201,7 @@ object TestUtils {
id = 1,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#2",
operationType = LogbookOperationType.DAT,
messageType = "DEP",
software = "e-Sacapt Secours ERSV3 V 1.0.10",
Expand All @@ -226,7 +226,7 @@ object TestUtils {
id = 3,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#3",
operationType = LogbookOperationType.DAT,
messageType = "PNO",
software = "e-Sacapt Secours ERSV3 V 1.0.7",
Expand All @@ -251,7 +251,7 @@ object TestUtils {
id = 3,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#4",
operationType = LogbookOperationType.DAT,
messageType = "COE",
software = "e-Sacapt Secours ERSV3 V 1.0.7",
Expand All @@ -276,7 +276,7 @@ object TestUtils {
id = 4,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#4",
operationType = LogbookOperationType.DAT,
messageType = "COX",
software = "e-Sacapt Secours ERSV3 V 1.0.7",
Expand All @@ -294,7 +294,7 @@ object TestUtils {
id = 5,
operationNumber = "",
tripNumber = "345",
reportId = "",
reportId = "REPORT_ID#5",
operationType = LogbookOperationType.DAT,
messageType = "CPS",
software = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.time.ZoneOffset
import java.time.ZoneOffset.UTC
import java.time.ZonedDateTime

@ExtendWith(SpringExtension::class)
Expand Down Expand Up @@ -141,26 +142,6 @@ class GetLogbookMessagesUTests {
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyCorrectedLogbookMessages(),
)
given(logbookRawMessageRepository.findRawMessage(any())).willReturn("<xml>DUMMY XML MESSAGE</xml>")
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(portRepository.findAll()).willReturn(
listOf(
PortFaker.fakePort(locode = "AEFAT", name = "Al Jazeera Port"),
PortFaker.fakePort(locode = "AEJAZ", name = "Arzanah Island"),
),
)
given(speciesRepository.findAll()).willReturn(
listOf(
Species("TTV", "TORPILLE OCELLÉE"),
Species("SMV", "STOMIAS BREVIBARBATUS"),
Species("PNB", "CREVETTE ROYALE ROSE"),
),
)

// When
val ersMessages =
Expand Down Expand Up @@ -200,26 +181,6 @@ class GetLogbookMessagesUTests {
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyRETLogbookMessages(),
)
given(logbookRawMessageRepository.findRawMessage(any())).willReturn("<xml>DUMMY XML MESSAGE</xml>")
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(portRepository.findAll()).willReturn(
listOf(
PortFaker.fakePort(locode = "AEFAT", name = "Al Jazeera Port"),
PortFaker.fakePort(locode = "AEJAZ", name = "Arzanah Island"),
),
)
given(speciesRepository.findAll()).willReturn(
listOf(
Species("TTV", "TORPILLE OCELLÉE"),
Species("SMV", "STOMIAS BREVIBARBATUS"),
Species("PNB", "CREVETTE ROYALE ROSE"),
),
)

// When
val ersMessages =
Expand Down Expand Up @@ -329,26 +290,6 @@ class GetLogbookMessagesUTests {
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyRETLogbookMessages(),
)
given(logbookRawMessageRepository.findRawMessage(any())).willReturn("<xml>DUMMY XML MESSAGE</xml>")
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(portRepository.findAll()).willReturn(
listOf(
PortFaker.fakePort(locode = "AEFAT", name = "Al Jazeera Port"),
PortFaker.fakePort(locode = "AEJAZ", name = "Arzanah Island"),
),
)
given(speciesRepository.findAll()).willReturn(
listOf(
Species("TTV", "TORPILLE OCELLÉE"),
Species("SMV", "STOMIAS BREVIBARBATUS"),
Species("PNB", "CREVETTE ROYALE ROSE"),
),
)

// When
val ersMessages =
Expand Down Expand Up @@ -376,26 +317,6 @@ class GetLogbookMessagesUTests {
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyFluxAndVisioCaptureLogbookMessages(),
)
given(logbookRawMessageRepository.findRawMessage(any())).willReturn("<xml>DUMMY XML MESSAGE</xml>")
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(portRepository.findAll()).willReturn(
listOf(
PortFaker.fakePort(locode = "AEFAT", name = "Al Jazeera Port"),
PortFaker.fakePort(locode = "AEJAZ", name = "Arzanah Island"),
),
)
given(speciesRepository.findAll()).willReturn(
listOf(
Species("TTV", "TORPILLE OCELLÉE"),
Species("SMV", "STOMIAS BREVIBARBATUS"),
Species("PNB", "CREVETTE ROYALE ROSE"),
),
)

// When
val ersMessages =
Expand All @@ -416,6 +337,10 @@ class GetLogbookMessagesUTests {
assertThat(ersMessages[2].acknowledgment?.isSuccess).isTrue
}

/**
* add two report_id for the same RET in the stub
*/

@Test
fun `execute Should flag messages sent by the failover software e-Sacapt`() {
// Given
Expand All @@ -425,26 +350,6 @@ class GetLogbookMessagesUTests {
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyLogbookMessages(),
)
given(logbookRawMessageRepository.findRawMessage(any())).willReturn("<xml>DUMMY XML MESSAGE</xml>")
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(portRepository.findAll()).willReturn(
listOf(
PortFaker.fakePort(locode = "AEFAT", name = "Al Jazeera Port"),
PortFaker.fakePort(locode = "AEJAZ", name = "Arzanah Island"),
),
)
given(speciesRepository.findAll()).willReturn(
listOf(
Species("TTV", "TORPILLE OCELLÉE"),
Species("SMV", "STOMIAS BREVIBARBATUS"),
Species("PNB", "CREVETTE ROYALE ROSE"),
),
)

// When
val ersMessages =
Expand All @@ -466,4 +371,57 @@ class GetLogbookMessagesUTests {
assertThat(ersMessages[3].isSentByFailoverSoftware).isTrue
assertThat(ersMessages[4].isSentByFailoverSoftware).isFalse
}

@Test
fun `execute Should acknowledge two messages with the same report id`() {
// Given
val retTargetingTwoMessages =
LogbookMessage(
id = 123,
operationNumber = "",
reportId = "906564681689",
referencedReportId = "REPORT_ID#4",
operationType = LogbookOperationType.RET,
messageType = "",
message = Acknowledgment().apply { returnStatus = "000" },
reportDateTime =
ZonedDateTime.of(
2020,
5,
5,
3,
4,
5,
3,
UTC,
).minusHours(12),
transmissionFormat = LogbookTransmissionFormat.ERS,
integrationDateTime = ZonedDateTime.now(),
isEnriched = false,
operationDateTime = ZonedDateTime.now(),
)
given(logbookReportRepository.findLastTripBeforeDateTime(any(), any())).willReturn(
VoyageDatesAndTripNumber("123", ZonedDateTime.now(), ZonedDateTime.now()),
)
given(logbookReportRepository.findAllMessagesByTripNumberBetweenDates(any(), any(), any(), any())).willReturn(
getDummyLogbookMessages() + retTargetingTwoMessages,
)

// When
val ersMessages =
GetLogbookMessages(
logbookReportRepository,
gearRepository,
speciesRepository,
portRepository,
logbookRawMessageRepository,
)
.execute("FR224226850", ZonedDateTime.now().minusMinutes(5), ZonedDateTime.now(), "345")

// Then
assertThat(ersMessages).hasSize(6)

assertThat(ersMessages[2].acknowledgment?.isSuccess).isTrue
assertThat(ersMessages[3].acknowledgment?.isSuccess).isTrue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.public_api

import fr.gouv.cnsp.monitorfish.config.SentryConfig
import fr.gouv.cnsp.monitorfish.fakers.PortFaker
import fr.gouv.cnsp.monitorfish.infrastructure.cache.CaffeineConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.cache.CacheManager
import org.springframework.context.annotation.Import
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@Import(SentryConfig::class, CaffeineConfiguration::class)
@AutoConfigureMockMvc(addFilters = false)
@WebMvcTest(value = [PublicPortController::class])
class PublicPortControllerITests {
@Autowired
private lateinit var api: MockMvc

@Autowired
lateinit var cacheManager: CacheManager

@Test
fun `Should invalidate the cache`() {
// Given
cacheManager.getCache("ports")?.put("PORT123", PortFaker.fakePort(locode = "AY", name = "Auray"))
assertThat(cacheManager.getCache("ports")?.get("PORT123")).isNotNull()

// When
api.perform(put("/api/v1/ports/invalidate"))
.andExpect(status().isOk)

// Then
val cache = cacheManager.getCache("ports")?.get("PORT123")
assertThat(cache).isNull()
}
}
Loading

0 comments on commit 95db4d7

Please sign in to comment.