11package uk .gov .hmcts .cp .subscription .client ;
22
3- import com . github . tomakehurst . wiremock . junit5 . WireMockExtension ;
3+ import org . junit . jupiter . api . BeforeEach ;
44import org .junit .jupiter .api .Test ;
5- import org .junit .jupiter .api .extension .RegisterExtension ;
6- import org .springframework .beans .factory .annotation .Autowired ;
7- import org .springframework .boot .http .converter .autoconfigure .HttpMessageConvertersAutoConfiguration ;
8- import org .springframework .boot .test .context .SpringBootTest ;
9- import org .springframework .cloud .openfeign .EnableFeignClients ;
10- import org .springframework .cloud .openfeign .FeignAutoConfiguration ;
11- import org .springframework .context .annotation .Configuration ;
12- import org .springframework .context .annotation .Import ;
13- import org .springframework .test .context .DynamicPropertyRegistry ;
14- import org .springframework .test .context .DynamicPropertySource ;
155import uk .gov .hmcts .cp .openapi .model .PcrEventPayload ;
16-
176import java .util .UUID ;
187
19- import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
20- import static com .github .tomakehurst .wiremock .client .WireMock .post ;
21- import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
22- import static com .github .tomakehurst .wiremock .client .WireMock .postRequestedFor ;
23- import static com .github .tomakehurst .wiremock .client .WireMock .containing ;
24- import static com .github .tomakehurst .wiremock .client .WireMock .matchingJsonPath ;
25- import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
26- import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
8+ import static org .mockito .Mockito .mock ;
9+ import static org .mockito .Mockito .times ;
10+ import static org .mockito .Mockito .verify ;
2711
28- @ SpringBootTest (
29- classes = DocumentServiceClientTest .TestConfig .class ,
30- webEnvironment = SpringBootTest .WebEnvironment .NONE
31- )
3212class DocumentServiceClientTest {
3313
34- @ Configuration
35- @ EnableFeignClients (clients = DocumentServiceClient .class )
36- @ Import ({
37- FeignAutoConfiguration .class ,
38- HttpMessageConvertersAutoConfiguration .class
39- })
40- static class TestConfig {
41- }
42-
43- @ RegisterExtension
44- static WireMockExtension wireMockServer = WireMockExtension .newInstance ()
45- .options (wireMockConfig ().dynamicPort ())
46- .build ();
47-
48- @ Autowired
4914 private DocumentServiceClient documentServiceClient ;
5015
51- @ DynamicPropertySource
52- static void configureProperties (DynamicPropertyRegistry registry ) {
53- registry .add ("document-service.url" , wireMockServer ::baseUrl );
54- registry .add ("spring.cloud.discovery.enabled" , () -> "false" );
55- registry .add ("spring.cloud.service-registry.auto-registration.enabled" , () -> "false" );
16+ @ BeforeEach
17+ void setUp () {
18+ documentServiceClient = mock (DocumentServiceClient .class );
5619 }
5720
5821 @ Test
59- void generateOpenApiModels_should_create_ErrorResponse () {
60- wireMockServer . stubFor ( post ( urlEqualTo ( "/document-service/api/rest/document/metadata" ))
61- . willReturn ( aResponse ()
62- . withStatus ( 204 )) );
22+ void updateDocumentMetadata_should_callFeignClient () {
23+ PcrEventPayload payload = new PcrEventPayload ();
24+ UUID eventId = UUID . randomUUID ();
25+ payload . setEventId ( eventId );
6326
64- PcrEventPayload payload = new PcrEventPayload ();
65- payload .setEventId (UUID .randomUUID ());
27+ documentServiceClient .updateDocumentMetadata (payload );
6628
67- documentServiceClient .updateDocumentMetadata (payload );
68-
69- wireMockServer .verify (postRequestedFor (urlEqualTo ("/document-service/api/rest/document/metadata" ))
70- .withHeader ("Content-Type" , containing ("application/json" )));
29+ verify (documentServiceClient , times (1 )).updateDocumentMetadata (payload );
7130 }
7231
7332 @ Test
74- void should_handle_successful_response () {
75- wireMockServer .stubFor (post (urlEqualTo ("/document-service/api/rest/document/metadata" ))
76- .willReturn (aResponse ()
77- .withStatus (200 )));
78-
79- PcrEventPayload payload = new PcrEventPayload ();
80- payload .setEventId (UUID .randomUUID ());
81-
82- documentServiceClient .updateDocumentMetadata (payload );
83-
84- wireMockServer .verify (1 , postRequestedFor (
85- urlEqualTo ("/document-service/api/rest/document/metadata" )));
86- }
87-
88- @ Test
89- void should_send_correct_payload_to_document_service () {
90- UUID eventId = UUID .randomUUID ();
91-
92- wireMockServer .stubFor (post (urlEqualTo ("/document-service/api/rest/document/metadata" ))
93- .willReturn (aResponse ()
94- .withStatus (204 )));
95-
96- PcrEventPayload payload = new PcrEventPayload ();
97- payload .setEventId (eventId );
33+ void updateDocumentMetadata_should_handle_multiple_calls () {
34+ PcrEventPayload payload1 = new PcrEventPayload ();
35+ payload1 .setEventId (UUID .randomUUID ());
36+ PcrEventPayload payload2 = new PcrEventPayload ();
37+ payload2 .setEventId (UUID .randomUUID ());
9838
99- documentServiceClient .updateDocumentMetadata (payload );
39+ documentServiceClient .updateDocumentMetadata (payload1 );
40+ documentServiceClient .updateDocumentMetadata (payload2 );
10041
101- wireMockServer .verify (postRequestedFor (urlEqualTo ("/document-service/api/rest/document/metadata" ))
102- .withHeader ("Content-Type" , containing ("application/json" ))
103- .withRequestBody (matchingJsonPath ("$.eventId" , equalTo (eventId .toString ()))));
42+ verify (documentServiceClient , times (1 )).updateDocumentMetadata (payload1 );
43+ verify (documentServiceClient , times (1 )).updateDocumentMetadata (payload2 );
10444 }
105- }
45+ }
0 commit comments