|
| 1 | +package uk.gov.justice.digital.hmpps.organisationsapi.integration.resource.sync |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import org.junit.jupiter.api.BeforeEach |
| 5 | +import org.junit.jupiter.api.Nested |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | +import org.springframework.http.MediaType |
| 8 | +import org.springframework.test.context.TestPropertySource |
| 9 | +import uk.gov.justice.digital.hmpps.organisationsapi.integration.PostgresIntegrationTestBase |
| 10 | +import uk.gov.justice.digital.hmpps.organisationsapi.model.request.sync.SyncCreateOrganisationRequest |
| 11 | +import uk.gov.justice.digital.hmpps.organisationsapi.model.request.sync.SyncCreatePhoneRequest |
| 12 | +import uk.gov.justice.digital.hmpps.organisationsapi.model.request.sync.SyncUpdatePhoneRequest |
| 13 | +import uk.gov.justice.digital.hmpps.organisationsapi.model.response.sync.SyncOrganisationResponse |
| 14 | +import uk.gov.justice.digital.hmpps.organisationsapi.model.response.sync.SyncPhoneResponse |
| 15 | +import uk.gov.justice.digital.hmpps.organisationsapi.service.events.OrganisationInfo |
| 16 | +import uk.gov.justice.digital.hmpps.organisationsapi.service.events.OutboundEvent |
| 17 | +import uk.gov.justice.digital.hmpps.organisationsapi.service.events.Source |
| 18 | +import java.time.LocalDateTime |
| 19 | + |
| 20 | +@TestPropertySource(properties = ["feature.events.sns.enabled=true"]) |
| 21 | +class SyncPhoneIntegrationTest : PostgresIntegrationTestBase() { |
| 22 | + |
| 23 | + @Nested |
| 24 | + inner class PhoneSyncTests { |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + fun resetEvents() { |
| 28 | + stubEvents.reset() |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `Sync endpoints should return unauthorized if no token provided`() { |
| 33 | + webTestClient.get() |
| 34 | + .uri("/sync/organisation-phone/1") |
| 35 | + .accept(MediaType.APPLICATION_JSON) |
| 36 | + .exchange() |
| 37 | + .expectStatus() |
| 38 | + .isUnauthorized |
| 39 | + |
| 40 | + webTestClient.post() |
| 41 | + .uri("/sync/organisation-phone") |
| 42 | + .accept(MediaType.APPLICATION_JSON) |
| 43 | + .contentType(MediaType.APPLICATION_JSON) |
| 44 | + .bodyValue(syncCreatePhoneRequest(1L)) |
| 45 | + .exchange() |
| 46 | + .expectStatus() |
| 47 | + .isUnauthorized |
| 48 | + |
| 49 | + webTestClient.put() |
| 50 | + .uri("/sync/organisation-phone/1") |
| 51 | + .accept(MediaType.APPLICATION_JSON) |
| 52 | + .contentType(MediaType.APPLICATION_JSON) |
| 53 | + .bodyValue(syncUpdatePhoneRequest(1L)) |
| 54 | + .exchange() |
| 55 | + .expectStatus() |
| 56 | + .isUnauthorized |
| 57 | + |
| 58 | + webTestClient.delete() |
| 59 | + .uri("/sync/organisation-phone/1") |
| 60 | + .accept(MediaType.APPLICATION_JSON) |
| 61 | + .exchange() |
| 62 | + .expectStatus() |
| 63 | + .isUnauthorized |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `Sync endpoints should return forbidden without an authorised role on the token`() { |
| 68 | + webTestClient.get() |
| 69 | + .uri("/sync/organisation-phone/1") |
| 70 | + .accept(MediaType.APPLICATION_JSON) |
| 71 | + .headers(setAuthorisation(roles = listOf("ROLE_WRONG"))) |
| 72 | + .exchange() |
| 73 | + .expectStatus() |
| 74 | + .isForbidden |
| 75 | + |
| 76 | + webTestClient.post() |
| 77 | + .uri("/sync/organisation-phone") |
| 78 | + .accept(MediaType.APPLICATION_JSON) |
| 79 | + .contentType(MediaType.APPLICATION_JSON) |
| 80 | + .bodyValue(syncCreatePhoneRequest(1L)) |
| 81 | + .headers(setAuthorisation(roles = listOf("ROLE_WRONG"))) |
| 82 | + .exchange() |
| 83 | + .expectStatus() |
| 84 | + .isForbidden |
| 85 | + |
| 86 | + webTestClient.put() |
| 87 | + .uri("/sync/organisation-phone/1") |
| 88 | + .accept(MediaType.APPLICATION_JSON) |
| 89 | + .contentType(MediaType.APPLICATION_JSON) |
| 90 | + .bodyValue(syncUpdatePhoneRequest(1L)) |
| 91 | + .headers(setAuthorisation(roles = listOf("ROLE_WRONG"))) |
| 92 | + .exchange() |
| 93 | + .expectStatus() |
| 94 | + .isForbidden |
| 95 | + |
| 96 | + webTestClient.delete() |
| 97 | + .uri("/sync/organisation-phone/1") |
| 98 | + .accept(MediaType.APPLICATION_JSON) |
| 99 | + .headers(setAuthorisation(roles = listOf("ROLE_WRONG"))) |
| 100 | + .exchange() |
| 101 | + .expectStatus() |
| 102 | + .isForbidden |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `should create a phone number linked to an organisation`() { |
| 107 | + val organisationCreated = createOrganisationWithFixedId(6001L) |
| 108 | + val phone = createPhone(organisationCreated.organisationId) |
| 109 | + |
| 110 | + with(phone) { |
| 111 | + assertThat(this.organisationId).isEqualTo(organisationCreated.organisationId) |
| 112 | + assertThat(phoneType).isEqualTo("MOB") |
| 113 | + assertThat(phoneNumber).isEqualTo("07999 123456") |
| 114 | + assertThat(extNumber).isNull() |
| 115 | + assertThat(createdBy).isEqualTo("CREATOR") |
| 116 | + assertThat(createdTime).isAfter(LocalDateTime.now().minusMinutes(5)) |
| 117 | + assertThat(updatedBy).isNull() |
| 118 | + assertThat(updatedTime).isNull() |
| 119 | + } |
| 120 | + |
| 121 | + stubEvents.assertHasEvent( |
| 122 | + event = OutboundEvent.ORGANISATION_PHONE_CREATED, |
| 123 | + additionalInfo = OrganisationInfo(phone.organisationId, phone.organisationPhoneId, Source.NOMIS), |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `should update a phone number linked to an organisation`() { |
| 129 | + val organisation = createOrganisationWithFixedId(6002L) |
| 130 | + val phone = createPhone(organisation.organisationId) |
| 131 | + val updatedPhone = updatePhone(phone.organisationPhoneId, phone.organisationId) |
| 132 | + |
| 133 | + with(updatedPhone) { |
| 134 | + assertThat(organisationPhoneId).isEqualTo(phone.organisationPhoneId) |
| 135 | + assertThat(organisationId).isEqualTo(organisation.organisationId) |
| 136 | + assertThat(phoneType).isEqualTo("HOME") |
| 137 | + assertThat(phoneNumber).isEqualTo("07999 654321") |
| 138 | + assertThat(extNumber).isEqualTo("3") |
| 139 | + assertThat(createdBy).isEqualTo("CREATOR") |
| 140 | + assertThat(createdTime).isAfter(LocalDateTime.now().minusMinutes(5)) |
| 141 | + assertThat(updatedBy).isEqualTo("UPDATER") |
| 142 | + assertThat(updatedTime).isAfter(LocalDateTime.now().minusMinutes(5)) |
| 143 | + } |
| 144 | + |
| 145 | + stubEvents.assertHasEvent( |
| 146 | + event = OutboundEvent.ORGANISATION_PHONE_UPDATED, |
| 147 | + additionalInfo = OrganisationInfo(organisation.organisationId, phone.organisationPhoneId, Source.NOMIS), |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `should delete a phone number linked to an organisation`() { |
| 153 | + val organisation = createOrganisationWithFixedId(6003L) |
| 154 | + val phone = createPhone(organisation.organisationId) |
| 155 | + |
| 156 | + webTestClient.delete() |
| 157 | + .uri("/sync/organisation-phone/{organisationPhoneId}", phone.organisationPhoneId) |
| 158 | + .accept(MediaType.APPLICATION_JSON) |
| 159 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 160 | + .exchange() |
| 161 | + .expectStatus() |
| 162 | + .isOk |
| 163 | + |
| 164 | + webTestClient.get() |
| 165 | + .uri("/sync/organisation-phone/{organisationPhoneId}", phone.organisationPhoneId) |
| 166 | + .accept(MediaType.APPLICATION_JSON) |
| 167 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 168 | + .exchange() |
| 169 | + .expectStatus() |
| 170 | + .isNotFound |
| 171 | + |
| 172 | + stubEvents.assertHasEvent( |
| 173 | + event = OutboundEvent.ORGANISATION_PHONE_DELETED, |
| 174 | + additionalInfo = OrganisationInfo(organisation.organisationId, phone.organisationPhoneId, Source.NOMIS), |
| 175 | + ) |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + fun `should get a phone number by ID`() { |
| 180 | + val organisation = createOrganisationWithFixedId(6004L) |
| 181 | + val phone = createPhone(organisation.organisationId) |
| 182 | + val phoneRetrieved = getPhoneById(phone.organisationPhoneId) |
| 183 | + |
| 184 | + with(phoneRetrieved) { |
| 185 | + assertThat(organisationPhoneId).isEqualTo(phone.organisationPhoneId) |
| 186 | + assertThat(this.organisationId).isEqualTo(phone.organisationId) |
| 187 | + assertThat(phoneType).isEqualTo("MOB") |
| 188 | + assertThat(phoneNumber).isEqualTo("07999 123456") |
| 189 | + assertThat(extNumber).isNull() |
| 190 | + assertThat(createdBy).isEqualTo("CREATOR") |
| 191 | + assertThat(createdTime).isAfter(LocalDateTime.now().minusMinutes(5)) |
| 192 | + assertThat(updatedBy).isNull() |
| 193 | + assertThat(updatedTime).isNull() |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private fun syncUpdatePhoneRequest(organisationId: Long) = SyncUpdatePhoneRequest( |
| 198 | + organisationId = organisationId, |
| 199 | + phoneType = "HOME", |
| 200 | + phoneNumber = "07999 654321", |
| 201 | + extNumber = "3", |
| 202 | + updatedBy = "UPDATER", |
| 203 | + updatedTime = LocalDateTime.now(), |
| 204 | + ) |
| 205 | + |
| 206 | + private fun syncCreatePhoneRequest(organisationId: Long) = SyncCreatePhoneRequest( |
| 207 | + organisationId = organisationId, |
| 208 | + phoneType = "MOB", |
| 209 | + phoneNumber = "07999 123456", |
| 210 | + extNumber = null, |
| 211 | + createdTime = LocalDateTime.now(), |
| 212 | + createdBy = "CREATOR", |
| 213 | + ) |
| 214 | + |
| 215 | + private fun syncCreateOrganisationRequest(organisationId: Long) = SyncCreateOrganisationRequest( |
| 216 | + // Sync creates supply a fixed ID from NOMIS (i.e. the corporate ID) |
| 217 | + organisationId = organisationId, |
| 218 | + organisationName = "Organisation123", |
| 219 | + programmeNumber = "PRG123", |
| 220 | + vatNumber = "VAT123", |
| 221 | + caseloadId = "HEI", |
| 222 | + comments = "comment123", |
| 223 | + active = true, |
| 224 | + createdTime = LocalDateTime.now(), |
| 225 | + createdBy = "CREATOR", |
| 226 | + ) |
| 227 | + |
| 228 | + private fun createOrganisationWithFixedId(organisationId: Long) = |
| 229 | + webTestClient.post() |
| 230 | + .uri("/sync/organisation") |
| 231 | + .accept(MediaType.APPLICATION_JSON) |
| 232 | + .contentType(MediaType.APPLICATION_JSON) |
| 233 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 234 | + .bodyValue(syncCreateOrganisationRequest(organisationId)) |
| 235 | + .exchange() |
| 236 | + .expectStatus() |
| 237 | + .isOk |
| 238 | + .expectHeader().contentType(MediaType.APPLICATION_JSON) |
| 239 | + .expectBody(SyncOrganisationResponse::class.java) |
| 240 | + .returnResult().responseBody!! |
| 241 | + |
| 242 | + private fun createPhone(organisationId: Long) = |
| 243 | + webTestClient.post() |
| 244 | + .uri("/sync/organisation-phone") |
| 245 | + .accept(MediaType.APPLICATION_JSON) |
| 246 | + .contentType(MediaType.APPLICATION_JSON) |
| 247 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 248 | + .bodyValue(syncCreatePhoneRequest(organisationId)) |
| 249 | + .exchange() |
| 250 | + .expectStatus() |
| 251 | + .isOk |
| 252 | + .expectHeader().contentType(MediaType.APPLICATION_JSON) |
| 253 | + .expectBody(SyncPhoneResponse::class.java) |
| 254 | + .returnResult().responseBody!! |
| 255 | + |
| 256 | + private fun getPhoneById(organisationPhoneId: Long) = |
| 257 | + webTestClient.get() |
| 258 | + .uri("/sync/organisation-phone/{organisationPhoneId}", organisationPhoneId) |
| 259 | + .accept(MediaType.APPLICATION_JSON) |
| 260 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 261 | + .exchange() |
| 262 | + .expectStatus() |
| 263 | + .isOk |
| 264 | + .expectHeader().contentType(MediaType.APPLICATION_JSON) |
| 265 | + .expectBody(SyncPhoneResponse::class.java) |
| 266 | + .returnResult().responseBody!! |
| 267 | + |
| 268 | + private fun updatePhone(organisationPhoneId: Long, organisationId: Long) = |
| 269 | + webTestClient.put() |
| 270 | + .uri("/sync/organisation-phone/{organisationPhoneId}", organisationPhoneId) |
| 271 | + .accept(MediaType.APPLICATION_JSON) |
| 272 | + .contentType(MediaType.APPLICATION_JSON) |
| 273 | + .headers(setAuthorisation(roles = listOf("ROLE_ORGANISATIONS_MIGRATION"))) |
| 274 | + .bodyValue(syncUpdatePhoneRequest(organisationId)) |
| 275 | + .exchange() |
| 276 | + .expectStatus() |
| 277 | + .isOk |
| 278 | + .expectHeader().contentType(MediaType.APPLICATION_JSON) |
| 279 | + .expectBody(SyncPhoneResponse::class.java) |
| 280 | + .returnResult().responseBody!! |
| 281 | + } |
| 282 | +} |
0 commit comments