1+ package uk .gov .companieshouse .pscdataapi .config ;
2+
3+ import java .time .format .DateTimeParseException ;
4+ import org .junit .jupiter .api .Assertions ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .springframework .dao .DataAccessException ;
7+ import org .springframework .dao .TransientDataAccessException ;
8+ import org .springframework .http .HttpStatus ;
9+ import org .springframework .http .ResponseEntity ;
10+ import org .springframework .web .servlet .NoHandlerFoundException ;
11+ import uk .gov .companieshouse .pscdataapi .exceptions .ConflictException ;
12+ import uk .gov .companieshouse .pscdataapi .exceptions .NotFoundException ;
13+ import uk .gov .companieshouse .pscdataapi .exceptions .ServiceUnavailableException ;
14+ import uk .gov .companieshouse .pscdataapi .exceptions .BadRequestException ;
15+ import uk .gov .companieshouse .pscdataapi .exceptions .InternalDataException ;
16+ import uk .gov .companieshouse .pscdataapi .exceptions .BadGatewayException ;
17+ import uk .gov .companieshouse .pscdataapi .exceptions .SerDesException ;
18+
19+ class ExceptionHandlerConfigTest {
20+
21+ @ Test
22+ void shouldReturnNotFoundStatusForNotFoundException () {
23+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
24+
25+ ResponseEntity <Object > response = exceptionHandlerConfig .handleNotFoundException (new NotFoundException ("Resource not found" ));
26+
27+ Assertions .assertNotNull (response );
28+ Assertions .assertEquals (HttpStatus .NOT_FOUND , response .getStatusCode ());
29+ Assertions .assertNull (response .getBody ());
30+ }
31+
32+ @ Test
33+ void shouldReturnNotFoundStatusForNoHandlerFoundException () {
34+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
35+
36+ ResponseEntity <Object > response = exceptionHandlerConfig .handleNotFoundException (new NoHandlerFoundException ("GET" , "/path" , null ));
37+
38+ Assertions .assertNotNull (response );
39+ Assertions .assertEquals (HttpStatus .NOT_FOUND , response .getStatusCode ());
40+ Assertions .assertNull (response .getBody ());
41+ }
42+
43+ @ Test
44+ void shouldReturnServiceUnavailableStatusForServiceUnavailableException () {
45+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
46+
47+ ResponseEntity <Object > response = exceptionHandlerConfig .handleServiceUnavailableException (new ServiceUnavailableException ("Service unavailable" ));
48+
49+ Assertions .assertNotNull (response );
50+ Assertions .assertEquals (HttpStatus .SERVICE_UNAVAILABLE , response .getStatusCode ());
51+ Assertions .assertNull (response .getBody ());
52+ }
53+
54+ @ Test
55+ void shouldReturnBadRequestStatusForBadRequestException () {
56+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
57+
58+ ResponseEntity <Object > response = exceptionHandlerConfig .handleBadRequestException (new BadRequestException ("Bad request" ));
59+
60+ Assertions .assertNotNull (response );
61+ Assertions .assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
62+ Assertions .assertNull (response .getBody ());
63+ }
64+
65+ @ Test
66+ void shouldReturnBadRequestStatusForDateTimeParseException () {
67+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
68+
69+ ResponseEntity <Object > response = exceptionHandlerConfig .handleBadRequestException (new DateTimeParseException ("Invalid date" , "2023-01-01" , 0 ));
70+
71+ Assertions .assertNotNull (response );
72+ Assertions .assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
73+ Assertions .assertNull (response .getBody ());
74+ }
75+
76+ @ Test
77+ void shouldReturnConflictStatusForConflictException () {
78+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
79+
80+ ResponseEntity <Object > response = exceptionHandlerConfig .handleConflictException (new ConflictException ("Conflict occurred" ));
81+
82+ Assertions .assertNotNull (response );
83+ Assertions .assertEquals (HttpStatus .CONFLICT , response .getStatusCode ());
84+ Assertions .assertNull (response .getBody ());
85+ }
86+
87+ @ Test
88+ void shouldReturnInternalServerErrorStatusForInternalDataException () {
89+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
90+
91+ ResponseEntity <Object > response = exceptionHandlerConfig .handleInternalDataException (new InternalDataException ("Internal data error" ));
92+
93+ Assertions .assertNotNull (response );
94+ Assertions .assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
95+ Assertions .assertNull (response .getBody ());
96+ }
97+
98+ @ Test
99+ void shouldReturnBadGatewayStatusForBadGatewayException () {
100+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
101+
102+ ResponseEntity <Object > response = exceptionHandlerConfig .handleBadGatewayException (new BadGatewayException ("Bad gateway" , null ));
103+
104+ Assertions .assertNotNull (response );
105+ Assertions .assertEquals (HttpStatus .BAD_GATEWAY , response .getStatusCode ());
106+ Assertions .assertNull (response .getBody ());
107+ }
108+
109+ @ Test
110+ void shouldReturnBadGatewayStatusForTransientDataAccessException () {
111+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
112+
113+ ResponseEntity <Object > response = exceptionHandlerConfig .handleTransientDataAccessException (new TransientDataAccessException ("Transient data access error" ) {});
114+
115+ Assertions .assertNotNull (response );
116+ Assertions .assertEquals (HttpStatus .BAD_GATEWAY , response .getStatusCode ());
117+ Assertions .assertNull (response .getBody ());
118+ }
119+
120+ @ Test
121+ void shouldReturnBadGatewayStatusForDataAccessException () {
122+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
123+
124+ ResponseEntity <Object > response = exceptionHandlerConfig .handleDataAccessException (new DataAccessException ("Data access error" ) {});
125+
126+ Assertions .assertNotNull (response );
127+ Assertions .assertEquals (HttpStatus .BAD_GATEWAY , response .getStatusCode ());
128+ Assertions .assertNull (response .getBody ());
129+ }
130+
131+ @ Test
132+ void shouldReturnInternalServerErrorStatusForSerDesException () {
133+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
134+
135+ ResponseEntity <Object > response = exceptionHandlerConfig .handleSerDesException (new SerDesException ("Serialization error" , null ));
136+
137+ Assertions .assertNotNull (response );
138+ Assertions .assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
139+ Assertions .assertNull (response .getBody ());
140+ }
141+
142+
143+ @ Test
144+ void shouldReturnInternalServerErrorStatusForGenericException () {
145+ ExceptionHandlerConfig exceptionHandlerConfig = new ExceptionHandlerConfig ();
146+
147+ ResponseEntity <Object > response = exceptionHandlerConfig .handleException (new Exception ("Generic error" ));
148+
149+ Assertions .assertNotNull (response );
150+ Assertions .assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
151+ Assertions .assertNull (response .getBody ());
152+ }
153+
154+
155+ }
0 commit comments