Skip to content

Commit d3a656e

Browse files
author
Stephen Powis
committed
Wrap up test cases
1 parent 294c5dc commit d3a656e

File tree

8 files changed

+107
-3
lines changed

8 files changed

+107
-3
lines changed

src/main/java/org/sourcelab/kafka/connect/apiclient/KafkaConnectClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient;
1919

20+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
2021
import org.apache.http.HttpStatus;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
24+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2325
import org.sourcelab.kafka.connect.apiclient.request.JacksonFactory;
2426
import org.sourcelab.kafka.connect.apiclient.request.Request;
2527
import org.sourcelab.kafka.connect.apiclient.request.RequestErrorResponse;
@@ -340,6 +342,8 @@ private <T> T submitRequest(final Request<T> request) {
340342

341343
try {
342344
return request.parseResponse(responseStr);
345+
} catch (final MismatchedInputException exception) {
346+
throw new ResponseParseException(exception.getMessage(), exception);
343347
} catch (final IOException exception) {
344348
throw new RuntimeException(exception.getMessage(), exception);
345349
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2018, 2019 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.kafka.connect.apiclient.exception;
19+
20+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
21+
22+
/**
23+
* Thrown when the library is unable to properly parse the response from Kafka-Connect.
24+
*/
25+
public class ResponseParseException extends RuntimeException {
26+
/**
27+
* Constructor.
28+
* @param message error msg
29+
* @param exception underlying exception, if available.
30+
*/
31+
public ResponseParseException(final String message, final MismatchedInputException exception) {
32+
super(message, exception);
33+
}
34+
}

src/main/java/org/sourcelab/kafka/connect/apiclient/request/get/GetConnectorsExpandAllDetails.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient.request.get;
1919

20+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2022
import org.sourcelab.kafka.connect.apiclient.request.JacksonFactory;
2123
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedMetadata;
2224

@@ -38,7 +40,15 @@ public String getApiEndpoint() {
3840

3941
@Override
4042
public ConnectorsWithExpandedMetadata parseResponse(final String responseStr) throws IOException {
41-
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
43+
try {
44+
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
45+
} catch (final MismatchedInputException exception) {
46+
throw new ResponseParseException(
47+
"Failed to parse response. The end point you requested requires Kafka-Connect 2.3.0+..."
48+
+ "are you sure you're querying against the right version?",
49+
exception
50+
);
51+
}
4252
}
4353
}
4454

src/main/java/org/sourcelab/kafka/connect/apiclient/request/get/GetConnectorsExpandInfo.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient.request.get;
1919

20+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2022
import org.sourcelab.kafka.connect.apiclient.request.JacksonFactory;
2123
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedInfo;
2224
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedMetadata;
@@ -37,7 +39,15 @@ public String getApiEndpoint() {
3739

3840
@Override
3941
public ConnectorsWithExpandedInfo parseResponse(final String responseStr) throws IOException {
40-
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
42+
try {
43+
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
44+
} catch (final MismatchedInputException exception) {
45+
throw new ResponseParseException(
46+
"Failed to parse response. The end point you requested requires Kafka-Connect 2.3.0+..."
47+
+ "are you sure you're querying against the right version?",
48+
exception
49+
);
50+
}
4151
}
4252
}
4353

src/main/java/org/sourcelab/kafka/connect/apiclient/request/get/GetConnectorsExpandStatus.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.sourcelab.kafka.connect.apiclient.request.get;
1919

20+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2022
import org.sourcelab.kafka.connect.apiclient.request.JacksonFactory;
2123
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedMetadata;
2224
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedStatus;
@@ -37,6 +39,14 @@ public String getApiEndpoint() {
3739

3840
@Override
3941
public ConnectorsWithExpandedStatus parseResponse(final String responseStr) throws IOException {
40-
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
42+
try {
43+
return JacksonFactory.newInstance().readValue(responseStr, ConnectorsWithExpandedMetadata.class);
44+
} catch (final MismatchedInputException exception) {
45+
throw new ResponseParseException(
46+
"Failed to parse response. The end point you requested requires Kafka-Connect 2.3.0+..."
47+
+ "are you sure you're querying against the right version?",
48+
exception
49+
);
50+
}
4151
}
4252
}

src/test/java/org/sourcelab/kafka/connect/apiclient/request/get/connector/GetConnectorsExpandAllDetailsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.sourcelab.kafka.connect.apiclient.request.get.connector;
1919

2020
import org.junit.Test;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2122
import org.sourcelab.kafka.connect.apiclient.request.AbstractRequestTest;
2223
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorDefinition;
2324
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorStatus;
@@ -75,6 +76,17 @@ public void testParseResponse() throws IOException {
7576
validateTestConnectorDefinition2(result.getMappedDefinitions().get("MyTestConnector2"));
7677
}
7778

79+
/**
80+
* Test what happens if we get back a pre 2.3.0 response for this request.
81+
*
82+
* It should throw a ResponseParseException.
83+
*/
84+
@Test(expected = ResponseParseException.class)
85+
public void testParseResponseForKafkaConnectVersionEarlierThan2_3_0() throws IOException {
86+
final String mockResponse = readFile("getConnector.json");
87+
final ConnectorsWithExpandedMetadata result = new GetConnectorsExpandAllDetails().parseResponse(mockResponse);
88+
}
89+
7890
private void validateTestConnectorDefinition(final ConnectorDefinition connector) {
7991
final String expectedConnectorName = "MyTestConnector";
8092

src/test/java/org/sourcelab/kafka/connect/apiclient/request/get/connector/GetConnectorsWithExpandInfoTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.sourcelab.kafka.connect.apiclient.request.get.connector;
1919

2020
import org.junit.Test;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2122
import org.sourcelab.kafka.connect.apiclient.request.AbstractRequestTest;
2223
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorDefinition;
2324
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedInfo;
@@ -64,6 +65,17 @@ public void testParseResponse() throws IOException {
6465
validateTestConnectorDefinition2(result.getMappedDefinitions().get("MyTestConnector2"));
6566
}
6667

68+
/**
69+
* Test what happens if we get back a pre 2.3.0 response for this request.
70+
*
71+
* It should throw a ResponseParseException.
72+
*/
73+
@Test(expected = ResponseParseException.class)
74+
public void testParseResponseForKafkaConnectVersionEarlierThan2_3_0() throws IOException {
75+
final String mockResponse = readFile("getConnector.json");
76+
final ConnectorsWithExpandedInfo result = new GetConnectorsExpandInfo().parseResponse(mockResponse);
77+
}
78+
6779
private void validateTestConnectorDefinition(final ConnectorDefinition connector) {
6880
final String expectedConnectorName = "MyTestConnector";
6981

src/test/java/org/sourcelab/kafka/connect/apiclient/request/get/connector/GetConnectorsWithExpandStatusTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.sourcelab.kafka.connect.apiclient.request.get.connector;
1919

2020
import org.junit.Test;
21+
import org.sourcelab.kafka.connect.apiclient.exception.ResponseParseException;
2122
import org.sourcelab.kafka.connect.apiclient.request.AbstractRequestTest;
2223
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorStatus;
2324
import org.sourcelab.kafka.connect.apiclient.request.dto.ConnectorsWithExpandedStatus;
@@ -63,6 +64,17 @@ public void testParseResponse() throws IOException {
6364
validateTestConnectorStatus2(result.getMappedStatuses().get("MyTestConnector2"));
6465
}
6566

67+
/**
68+
* Test what happens if we get back a pre 2.3.0 response for this request.
69+
*
70+
* It should throw a ResponseParseException.
71+
*/
72+
@Test(expected = ResponseParseException.class)
73+
public void testParseResponseForKafkaConnectVersionEarlierThan2_3_0() throws IOException {
74+
final String mockResponse = readFile("getConnector.json");
75+
final ConnectorsWithExpandedStatus result = new GetConnectorsExpandStatus().parseResponse(mockResponse);
76+
}
77+
6678
private void validateTestConnectorStatus(final ConnectorStatus connectorStatus) {
6779
final String expectedConnectorName = "MyTestConnector";
6880

0 commit comments

Comments
 (0)