@@ -45,44 +45,71 @@ void handleResponse() throws IOException {
4545
4646 @ Test
4747 void handleResponse_whenServerError_thenThrowIbanityServerSideException () {
48- //language=JSON
48+ // language=JSON
4949 String expected = errorPayloadWithJson ();
5050
5151 when (httpResponse .getEntity ()).thenReturn (EntityBuilder .create ().setText (expected ).build ());
5252 when (httpResponse .getStatusLine ()).thenReturn (new BasicStatusLine (dummyProtocolVersion (), 500 , "" ));
53- when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER )).thenReturn (new BasicHeader (IBANITY_REQUEST_ID_HEADER , REQUEST_ID ));
53+ when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER ))
54+ .thenReturn (new BasicHeader (IBANITY_REQUEST_ID_HEADER , REQUEST_ID ));
5455
55- IbanityServerException actual = assertThrows (IbanityServerException .class , () -> ibanityResponseHandler .handleResponse (httpResponse ));
56+ IbanityServerException actual = assertThrows (IbanityServerException .class ,
57+ () -> ibanityResponseHandler .handleResponse (httpResponse ));
5658
57- assertThat (actual ).isEqualToComparingFieldByFieldRecursively (new IbanityServerException (createExpectedErrorsWithJson (), 500 , REQUEST_ID ));
59+ assertThat (actual ).isEqualToComparingFieldByFieldRecursively (
60+ new IbanityServerException (createExpectedErrorsWithJson (), 500 , REQUEST_ID ));
61+ }
62+
63+ @ Test
64+ void handleResponse_whenServerErrorWithResponseID_thenThrowIbanityServiceSideExceptionWithResponseID () {
65+ // language=JSON
66+ String expected = errorPayloadWithResponseIDJson ();
67+
68+ when (httpResponse .getEntity ()).thenReturn (EntityBuilder .create ().setText (expected ).build ());
69+ when (httpResponse .getStatusLine ()).thenReturn (new BasicStatusLine (dummyProtocolVersion (), 500 , "" ));
70+ when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER ))
71+ .thenReturn (new BasicHeader (IBANITY_REQUEST_ID_HEADER , REQUEST_ID ));
72+
73+ IbanityServerException actual = assertThrows (IbanityServerException .class ,
74+ () -> ibanityResponseHandler .handleResponse (httpResponse ));
75+
76+ actual .getErrors ().forEach (error ->
77+ assertThat (error .getMeta ().getFinancialInstitutionResponse ().getResponseId ())
78+ .isEqualTo ("gdhed515hrtzehg" )
79+ );
5880 }
5981
6082 @ Test
6183 void handleResponse_whenResourceNotFound_thenThrowIbanityClientSideException () {
62- //language=JSON
84+ // language=JSON
6385 String expected = errorPayloadWithJson ();
6486
6587 when (httpResponse .getEntity ()).thenReturn (EntityBuilder .create ().setText (expected ).build ());
6688 when (httpResponse .getStatusLine ()).thenReturn (new BasicStatusLine (dummyProtocolVersion (), 404 , "" ));
67- when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER )).thenReturn (new BasicHeader (IBANITY_REQUEST_ID_HEADER , REQUEST_ID ));
89+ when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER ))
90+ .thenReturn (new BasicHeader (IBANITY_REQUEST_ID_HEADER , REQUEST_ID ));
6891
69- IbanityClientException actual = assertThrows (IbanityClientException .class , () -> ibanityResponseHandler .handleResponse (httpResponse ));
92+ IbanityClientException actual = assertThrows (IbanityClientException .class ,
93+ () -> ibanityResponseHandler .handleResponse (httpResponse ));
7094
71- assertThat (actual ).isEqualToComparingFieldByFieldRecursively (new IbanityServerException (createExpectedErrorsWithJson (), 404 , REQUEST_ID ));
95+ assertThat (actual ).isEqualToComparingFieldByFieldRecursively (
96+ new IbanityServerException (createExpectedErrorsWithJson (), 404 , REQUEST_ID ));
7297 }
7398
7499 @ Test
75100 void handleResponse_whenResourceNotFoundAndNoRequestId_thenThrowIbanityClientSideException () {
76- //language=JSON
101+ // language=JSON
77102 String expected = errorPayloadWithHtml ();
78103
79104 when (httpResponse .getEntity ()).thenReturn (EntityBuilder .create ().setText (expected ).build ());
80105 when (httpResponse .getStatusLine ()).thenReturn (new BasicStatusLine (dummyProtocolVersion (), 404 , "" ));
81106 when (httpResponse .getFirstHeader (IBANITY_REQUEST_ID_HEADER )).thenReturn (null );
82107
83- IbanityClientException actual = assertThrows (IbanityClientException .class , () -> ibanityResponseHandler .handleResponse (httpResponse ));
108+ IbanityClientException actual = assertThrows (IbanityClientException .class ,
109+ () -> ibanityResponseHandler .handleResponse (httpResponse ));
84110
85- assertThat (actual ).isEqualToComparingFieldByFieldRecursively (new IbanityServerException (createExpectedErrorsWithHtml (), 404 , null ));
111+ assertThat (actual ).isEqualToComparingFieldByFieldRecursively (
112+ new IbanityServerException (createExpectedErrorsWithHtml (), 404 , null ));
86113 }
87114
88115 private List <IbanityError > createExpectedErrors (String body ) {
@@ -102,7 +129,8 @@ private List<IbanityError> createExpectedErrors(String body) {
102129 }
103130
104131 private List <IbanityError > createExpectedErrorsWithJson () {
105- return createExpectedErrors ("{\" tppMessages\" :[{\" category\" :\" ERROR\" ,\" code\" :\" NOT_FOUND\" ,\" text\" :\" 3.2 - Not Found\" }]}" );
132+ return createExpectedErrors (
133+ "{\" tppMessages\" :[{\" category\" :\" ERROR\" ,\" code\" :\" NOT_FOUND\" ,\" text\" :\" 3.2 - Not Found\" }]}" );
106134 }
107135
108136 private List <IbanityError > createExpectedErrorsWithHtml () {
@@ -131,7 +159,7 @@ private String errorPayloadWithHtml() {
131159 }
132160
133161 private String errorPayloadWithJson () {
134- //language=JSON
162+ // language=JSON
135163 return "{\n " +
136164 " \" errors\" : [\n " +
137165 " {\n " +
@@ -160,6 +188,37 @@ private String errorPayloadWithJson() {
160188 "}" ;
161189 }
162190
191+ private String errorPayloadWithResponseIDJson () {
192+ // language=JSON
193+ return "{\n " +
194+ " \" errors\" : [\n " +
195+ " {\n " +
196+ " \" code\" : \" invalidCredentials\" ,\n " +
197+ " \" detail\" : \" Your credentials are invalid.\" ,\n " +
198+ " \" meta\" : {\n " +
199+ " \" financialInstitutionResponse\" : {\n " +
200+ " \" statusCode\" : 500,\n " +
201+ " \" body\" : {\n " +
202+ " \" tppMessages\" : [\n " +
203+ " {\n " +
204+ " \" category\" : \" ERROR\" ,\n " +
205+ " \" code\" : \" NOT_FOUND\" ,\n " +
206+ " \" text\" : \" 3.2 - Not Found\" \n " +
207+ " }\n " +
208+ " ]\n " +
209+ " },\n " +
210+ " \" requestId\" : \" 354fwfwef4w684\" ,\n " +
211+ " \" timestamp\" : \" 2019-05-09T09:18:00.000Z\" ,\n " +
212+ " \" requestUri\" : \" http://google.com\" ,\n " +
213+ " \" responseId\" : \" gdhed515hrtzehg\" \n " +
214+ " }\n " +
215+ " " +
216+ "}\n " +
217+ " }\n " +
218+ " ]\n " +
219+ "}" ;
220+ }
221+
163222 private ProtocolVersion dummyProtocolVersion () {
164223 return new ProtocolVersion ("" , 0 , 0 );
165224 }
0 commit comments