1010import com .google .api .client .http .HttpStatusCodes ;
1111import jakarta .ws .rs .BadRequestException ;
1212import jakarta .ws .rs .ServerErrorException ;
13+ import jakarta .ws .rs .WebApplicationException ;
1314import java .util .ArrayList ;
1415import java .util .Collections ;
1516import java .util .EnumSet ;
1617import java .util .List ;
1718import org .broadinstitute .consent .http .AbstractTestHelper ;
1819import org .broadinstitute .consent .http .configurations .ServicesConfiguration ;
1920import org .broadinstitute .consent .http .enumeration .SupportRequestType ;
21+ import org .broadinstitute .consent .http .exceptions .UnprocessableEntityException ;
2022import org .broadinstitute .consent .http .models .support .DuosTicket ;
2123import org .broadinstitute .consent .http .models .support .TicketFactory ;
2224import org .broadinstitute .consent .http .models .support .TicketFields ;
@@ -93,6 +95,19 @@ void testPostTicketToSupportNotificationsNotActivated() {
9395 assertThrows (BadRequestException .class , () -> service .postTicketToSupport (ticket ));
9496 }
9597
98+ @ Test
99+ void testPostTicketToSupportNotificationsUnprocessableEntity () {
100+ DuosTicket ticket = generateTicket ();
101+ when (config .isActivateSupportNotifications ()).thenReturn (true );
102+ when (config .postSupportRequestUrl ()).thenReturn (
103+ "http://" + container .getHost () + ":" + container .getServerPort () + "/" );
104+ mockServerClient .when (request ())
105+ .respond (response ()
106+ .withHeader (Header .header ("Content-Type" , "application/json" ))
107+ .withStatusCode (HttpStatusCodes .STATUS_CODE_UNPROCESSABLE_ENTITY ));
108+ assertThrows (UnprocessableEntityException .class , () -> service .postTicketToSupport (ticket ));
109+ }
110+
96111 @ Test
97112 void testPostTicketToSupportServerError () {
98113 DuosTicket ticket = generateTicket ();
@@ -109,7 +124,7 @@ void testPostTicketToSupportServerError() {
109124 @ Test
110125 void testPostAttachmentToSupport () throws Exception {
111126 String expectedBody = """
112- { "upload": { "token": { "token": "token string" } } }
127+ { "upload": { "token": "token string" } }
113128 """ ;
114129 when (config .isActivateSupportNotifications ()).thenReturn (true );
115130 when (config .postSupportUploadUrl ()).thenReturn (
@@ -126,6 +141,25 @@ void testPostAttachmentToSupport() throws Exception {
126141 assertEquals (1 , requests .length );
127142 }
128143
144+ @ Test
145+ void testPostTicketToSupportUnableToParseResponse () {
146+ // This case should never happen, but we do inspect the response for a valid "upload" object.
147+ // We need to ensure that the service handles invalid response formats correctly.
148+ String expectedBody = """
149+ { "invalid": { "missing_token": "token string" } }
150+ """ ;
151+ when (config .isActivateSupportNotifications ()).thenReturn (true );
152+ when (config .postSupportUploadUrl ()).thenReturn (
153+ "http://" + container .getHost () + ":" + container .getServerPort () + "/" );
154+ mockServerClient .when (request ().withMethod ("POST" ))
155+ .respond (response ()
156+ .withHeader (Header .header ("Content-Type" , "application/json" ))
157+ .withStatusCode (HttpStatusCodes .STATUS_CODE_CREATED )
158+ .withBody (expectedBody )
159+ );
160+ assertThrows (ServerErrorException .class , () -> service .postAttachmentToSupport ("Test" .getBytes ()));
161+ }
162+
129163 @ Test
130164 void testPostAttachmentToSupportNotificationsNotActivated () {
131165 when (config .isActivateSupportNotifications ()).thenReturn (false );
@@ -146,6 +180,18 @@ void testPostAttachmentToSupportServerError() {
146180 assertThrows (ServerErrorException .class , () -> service .postAttachmentToSupport ("Test" .getBytes ()));
147181 }
148182
183+ @ Test
184+ void testPostAttachmentToSupportUnprocessableEntity () {
185+ when (config .isActivateSupportNotifications ()).thenReturn (true );
186+ when (config .postSupportUploadUrl ()).thenReturn (
187+ "http://" + container .getHost () + ":" + container .getServerPort () + "/" );
188+ mockServerClient .when (request ())
189+ .respond (response ()
190+ .withHeader (Header .header ("Content-Type" , "application/json" ))
191+ .withStatusCode (HttpStatusCodes .STATUS_CODE_UNPROCESSABLE_ENTITY ));
192+ assertThrows (UnprocessableEntityException .class , () -> service .postAttachmentToSupport ("Test" .getBytes ()));
193+ }
194+
149195 // Creates support ticket with random values
150196 private DuosTicket generateTicket () {
151197 List <SupportRequestType > types = new ArrayList <>(EnumSet .allOf (SupportRequestType .class ));
0 commit comments