Skip to content

Commit 882dd04

Browse files
committed
Add get all vessel groups api
1 parent 0a3c86a commit 882dd04

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fr.gouv.cnsp.monitorfish.domain.use_cases.vessel_groups
2+
3+
import fr.gouv.cnsp.monitorfish.config.UseCase
4+
import fr.gouv.cnsp.monitorfish.domain.entities.vessel_group.VesselGroupBase
5+
import fr.gouv.cnsp.monitorfish.domain.repositories.VesselGroupRepository
6+
import org.slf4j.Logger
7+
import org.slf4j.LoggerFactory
8+
9+
@UseCase
10+
class GetAllVesselGroups(
11+
private val vesselGroupRepository: VesselGroupRepository,
12+
) {
13+
private val logger: Logger = LoggerFactory.getLogger(GetAllVesselGroups::class.java)
14+
15+
fun execute(userEmail: String): List<VesselGroupBase> = vesselGroupRepository.findAllByUser(userEmail)
16+
}

backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/infrastructure/api/bff/VesselGroupController.kt

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package fr.gouv.cnsp.monitorfish.infrastructure.api.bff
22

3+
import fr.gouv.cnsp.monitorfish.domain.entities.vessel_group.DynamicVesselGroup
4+
import fr.gouv.cnsp.monitorfish.domain.entities.vessel_group.FixedVesselGroup
35
import fr.gouv.cnsp.monitorfish.domain.exceptions.BackendUsageErrorCode
46
import fr.gouv.cnsp.monitorfish.domain.exceptions.BackendUsageException
57
import fr.gouv.cnsp.monitorfish.domain.use_cases.vessel_groups.AddOrUpdateDynamicVesselGroup
8+
import fr.gouv.cnsp.monitorfish.domain.use_cases.vessel_groups.GetAllVesselGroups
69
import fr.gouv.cnsp.monitorfish.infrastructure.api.input.DynamicVesselGroupDataInput
710
import fr.gouv.cnsp.monitorfish.infrastructure.api.outputs.DynamicVesselGroupDataOutput
11+
import fr.gouv.cnsp.monitorfish.infrastructure.api.outputs.FixedVesselGroupDataOutput
812
import fr.gouv.cnsp.monitorfish.infrastructure.api.security.UserAuthorizationCheckFilter
913
import io.swagger.v3.oas.annotations.Operation
1014
import io.swagger.v3.oas.annotations.tags.Tag
@@ -17,6 +21,7 @@ import org.springframework.web.bind.annotation.*
1721
@Tag(name = "APIs for vessel groups")
1822
class VesselGroupController(
1923
private val addOrUpdateDynamicVesselGroup: AddOrUpdateDynamicVesselGroup,
24+
private val getAllVesselGroups: GetAllVesselGroups,
2025
) {
2126
private val logger = LoggerFactory.getLogger(VesselGroupController::class.java)
2227

@@ -27,15 +32,32 @@ class VesselGroupController(
2732
@RequestBody
2833
vesselGroupInput: DynamicVesselGroupDataInput,
2934
): DynamicVesselGroupDataOutput {
30-
val email: String =
35+
val email: String = getEmail(response)
36+
37+
return DynamicVesselGroupDataOutput.fromDynamicVesselGroup(
38+
addOrUpdateDynamicVesselGroup.execute(email, vesselGroupInput.toDynamicVesselGroup()),
39+
)
40+
}
41+
42+
@GetMapping("")
43+
@Operation(summary = "Get all dynamic and fixed vessel groups")
44+
fun getVesselGroups(response: HttpServletResponse): List<Any> {
45+
val email: String = getEmail(response)
46+
47+
return getAllVesselGroups.execute(email).map {
48+
when (it) {
49+
is DynamicVesselGroup -> DynamicVesselGroupDataOutput.fromDynamicVesselGroup(it)
50+
is FixedVesselGroup -> FixedVesselGroupDataOutput.fromFixedVesselGroup(it)
51+
}
52+
}
53+
}
54+
55+
private fun getEmail(response: HttpServletResponse) =
56+
(
3157
response.getHeader(UserAuthorizationCheckFilter.EMAIL_HEADER)
3258
?: throw BackendUsageException(
3359
BackendUsageErrorCode.COULD_NOT_UPDATE,
3460
message = "Email not found. Rejecting request.",
3561
)
36-
37-
return DynamicVesselGroupDataOutput.fromDynamicVesselGroup(
38-
addOrUpdateDynamicVesselGroup.execute(email, vesselGroupInput.toDynamicVesselGroup()),
3962
)
40-
}
4163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fr.gouv.cnsp.monitorfish.infrastructure.api.outputs
2+
3+
import fr.gouv.cnsp.monitorfish.domain.entities.vessel_group.*
4+
import java.time.ZonedDateTime
5+
6+
data class FixedVesselGroupDataOutput(
7+
val id: Int?,
8+
val name: String,
9+
val isDeleted: Boolean,
10+
val description: String,
11+
val pointsOfAttention: String?,
12+
val color: String,
13+
val sharing: Sharing,
14+
val createdBy: String,
15+
val createdAtUtc: ZonedDateTime,
16+
val updatedAtUtc: ZonedDateTime? = null,
17+
val endOfValidityUtc: ZonedDateTime? = null,
18+
val vessels: List<VesselIdentity>,
19+
) {
20+
companion object {
21+
fun fromFixedVesselGroup(vesselGroup: FixedVesselGroup) =
22+
FixedVesselGroupDataOutput(
23+
id = vesselGroup.id,
24+
name = vesselGroup.name,
25+
isDeleted = vesselGroup.isDeleted,
26+
description = vesselGroup.description,
27+
pointsOfAttention = vesselGroup.pointsOfAttention,
28+
color = vesselGroup.color,
29+
sharing = vesselGroup.sharing,
30+
createdBy = vesselGroup.createdBy,
31+
createdAtUtc = vesselGroup.createdAtUtc,
32+
updatedAtUtc = vesselGroup.updatedAtUtc,
33+
endOfValidityUtc = vesselGroup.endOfValidityUtc,
34+
vessels = vesselGroup.vessels,
35+
)
36+
}
37+
}

backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/infrastructure/api/bff/VesselGroupControllerITests.kt

+30-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.nhaarman.mockitokotlin2.given
66
import fr.gouv.cnsp.monitorfish.config.*
77
import fr.gouv.cnsp.monitorfish.domain.use_cases.authorization.GetIsAuthorizedUser
88
import fr.gouv.cnsp.monitorfish.domain.use_cases.vessel_groups.AddOrUpdateDynamicVesselGroup
9+
import fr.gouv.cnsp.monitorfish.domain.use_cases.vessel_groups.GetAllVesselGroups
910
import fr.gouv.cnsp.monitorfish.infrastructure.api.log.CustomAuthenticationEntryPoint
1011
import fr.gouv.cnsp.monitorfish.infrastructure.api.security.TestUtils.Companion.getMockApiClient
1112
import fr.gouv.cnsp.monitorfish.infrastructure.api.security.UserAuthorizationCheckFilter
@@ -23,6 +24,7 @@ import org.springframework.context.annotation.Import
2324
import org.springframework.http.MediaType
2425
import org.springframework.security.oauth2.jwt.JwtDecoder
2526
import org.springframework.test.web.servlet.MockMvc
27+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
2628
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
2729
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
2830
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@@ -49,9 +51,6 @@ class VesselGroupControllerITests {
4951
@Autowired
5052
private lateinit var api: MockMvc
5153

52-
@MockBean
53-
private lateinit var addOrUpdateDynamicVesselGroup: AddOrUpdateDynamicVesselGroup
54-
5554
@Autowired
5655
private lateinit var jwtDecoder: JwtDecoder
5756

@@ -64,6 +63,12 @@ class VesselGroupControllerITests {
6463
fun mockApiClient(): ApiClient = getMockApiClient()
6564
}
6665

66+
@MockBean
67+
private lateinit var addOrUpdateDynamicVesselGroup: AddOrUpdateDynamicVesselGroup
68+
69+
@MockBean
70+
private lateinit var getAllVesselGroups: GetAllVesselGroups
71+
6772
@Autowired
6873
private lateinit var objectMapper: ObjectMapper
6974

@@ -88,4 +93,26 @@ class VesselGroupControllerITests {
8893

8994
Mockito.verify(addOrUpdateDynamicVesselGroup).execute("email@domain-name.com", groupToSave.copy(id = null))
9095
}
96+
97+
@Test
98+
fun `Should get all dynamic vessel groups`() {
99+
// Given
100+
given(getIsAuthorizedUser.execute(any(), any())).willReturn(true)
101+
given(getAllVesselGroups.execute(any())).willReturn(TestUtils.getDynamicVesselGroups())
102+
103+
// When
104+
api
105+
.perform(
106+
get("/bff/v1/vessel_groups")
107+
.header("Authorization", "Bearer ${UserAuthorizationControllerITests.VALID_JWT}")
108+
.contentType(MediaType.APPLICATION_JSON),
109+
)
110+
// Then
111+
.andExpect(status().isOk)
112+
.andExpect(jsonPath("$.length()", equalTo(2)))
113+
.andExpect(jsonPath("$[0].name", equalTo("Mission Thémis – chaluts de fonds")))
114+
.andExpect(jsonPath("$[0].filters.hasLogbook", equalTo(true)))
115+
116+
Mockito.verify(getAllVesselGroups).execute("email@domain-name.com")
117+
}
91118
}

0 commit comments

Comments
 (0)