2121import org .junit .Test ;
2222import org .sourcelab .kafka .connect .apiclient .rest .RestClient ;
2323import org .sourcelab .kafka .connect .apiclient .rest .RestResponse ;
24+ import org .sourcelab .kafka .connect .apiclient .rest .exceptions .ConcurrentConfigModificationException ;
25+ import org .sourcelab .kafka .connect .apiclient .rest .exceptions .ResourceNotFoundException ;
2426import org .sourcelab .kafka .connect .apiclient .rest .exceptions .UnauthorizedRequestException ;
2527
2628import static org .mockito .ArgumentMatchers .any ;
3234 */
3335public class KafkaConnectClientUnitTest {
3436
37+ private final Configuration configuration = new Configuration ("http://localhost:9092" );
38+
3539 /**
3640 * This test verifies that if the underlying RestClient returns a response with Http Status Code 401,
3741 * then KafkaConnectClient will throw an UnauthorizedRequestException.
3842 */
3943 @ Test (expected = UnauthorizedRequestException .class )
4044 public void unAuthorizedException () {
41- // Create configuration
42- final Configuration configuration = new Configuration ("http://localhost:9092" );
43-
4445 // Create mock RestResponse
4546 final RestResponse restResponse = new RestResponse ("Invalid credentials." , HttpStatus .SC_UNAUTHORIZED );
4647
@@ -55,4 +56,52 @@ public void unAuthorizedException() {
5556 // Call any method that makes a request via RestClient.
5657 client .getConnectors ();
5758 }
58- }
59+
60+ /**
61+ * This test verifies that if the underlying RestClient returns a response with Http Status Code 404,
62+ * then KafkaConnectClient will throw a ResourceNotFoundException.
63+ */
64+ @ Test (expected = ResourceNotFoundException .class )
65+ public void on404_resourceNotFoundException () {
66+ // Create mock RestResponse
67+ final String connectorName = "DoesNotExist" ;
68+ final String result = "{\" error_code\" :404,\" message\" :\" Connector " + connectorName + " not found\" }" ;
69+
70+ final RestResponse restResponse = new RestResponse (result , HttpStatus .SC_NOT_FOUND );
71+
72+ // Create mock RestClient
73+ final RestClient mockRestClient = mock (RestClient .class );
74+ when (mockRestClient .submitRequest (any ()))
75+ .thenReturn (restResponse );
76+
77+ // Create client
78+ final KafkaConnectClient client = new KafkaConnectClient (configuration , mockRestClient );
79+
80+ // Call any method that makes a request via RestClient.
81+ client .getConnector (connectorName );
82+ }
83+
84+ /**
85+ * This test verifies that if the underlying RestClient returns a response with Http Status Code 409,
86+ * then KafkaConnectClient will throw a ConcurrentConfigModificationException.
87+ */
88+ @ Test (expected = ConcurrentConfigModificationException .class )
89+ public void on409_concurrentConfigModificationException () {
90+ // Create mock RestResponse
91+ final String connectorName = "DoesNotExist" ;
92+ final String result = "{\" error_code\" :409,\" message\" :\" Rebalance in progress.\" }" ;
93+
94+ final RestResponse restResponse = new RestResponse (result , HttpStatus .SC_CONFLICT );
95+
96+ // Create mock RestClient
97+ final RestClient mockRestClient = mock (RestClient .class );
98+ when (mockRestClient .submitRequest (any ()))
99+ .thenReturn (restResponse );
100+
101+ // Create client
102+ final KafkaConnectClient client = new KafkaConnectClient (configuration , mockRestClient );
103+
104+ // Call any method that makes a request via RestClient.
105+ client .getConnector (connectorName );
106+ }
107+ }
0 commit comments