C0urante commented on code in PR #12320:
URL: https://github.com/apache/kafka/pull/12320#discussion_r914203732


##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/rest/RestClientTest.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.connect.runtime.rest;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.connect.runtime.WorkerConfig;
+import org.apache.kafka.connect.runtime.rest.entities.ErrorMessage;
+import org.apache.kafka.connect.runtime.rest.errors.ConnectRestException;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.api.Request;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import javax.ws.rs.core.Response;
+import java.util.Collections;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.niceMock;
+import static org.easymock.EasyMock.replay;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class RestClientTest {
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final TypeReference<TestDTO> TEST_TYPE = new 
TypeReference<TestDTO>() {
+    };
+    private HttpClient httpClient;
+
+    private static String toJsonString(Object obj) {
+        try {
+            return OBJECT_MAPPER.writeValueAsString(obj);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static <T> RestClient.HttpResponse<T> httpRequest(HttpClient 
httpClient, TypeReference<T> typeReference) {
+        return RestClient.httpRequest(
+                httpClient, null, null, null, null, typeReference, null, null);
+    }
+
+    @BeforeEach
+    public void mockSetup() {
+        httpClient = niceMock(HttpClient.class);
+    }
+
+    @Test
+    public void testSuccess() throws ExecutionException, InterruptedException, 
TimeoutException {
+        int statusCode = Response.Status.OK.getStatusCode();
+        String expectedResponse = toJsonString(new TestDTO("someContent"));
+        setupHttpClient(statusCode, expectedResponse);
+
+        RestClient.HttpResponse<TestDTO> httpResp = httpRequest(httpClient, 
TEST_TYPE);
+        assertEquals(httpResp.status(), statusCode);
+        assertEquals(toJsonString(httpResp.body()), expectedResponse);
+    }
+
+    @Test
+    public void testNoContent() throws ExecutionException, 
InterruptedException, TimeoutException {
+        int statusCode = Response.Status.NO_CONTENT.getStatusCode();
+        setupHttpClient(statusCode, null);
+
+        RestClient.HttpResponse<TestDTO> httpResp = httpRequest(httpClient, 
TEST_TYPE);
+        assertEquals(httpResp.status(), statusCode);
+        assertNull(httpResp.body());
+    }
+
+    @Test
+    public void testError() throws ExecutionException, InterruptedException, 
TimeoutException {

Review Comment:
   Yeah, parameterized tests would be a great fit for this case. One workaround 
I've adopted in a recent PR is extracting the common logic of the test case 
into a reusable method, then writing individual test cases that each delegate 
to that reusable method with different arguments. For an example, see the 
`DistributedHerderTest` suite: 
https://github.com/apache/kafka/blob/277c4c2e97d2aef096ab0e998fdd1ae513508798/connect/runtime/src/test/java/org/apache/kafka/connect/runtime/distributed/DistributedHerderTest.java#L2976-L3050
   
   It shouldn't be too much work to convert these tests to that style, but it's 
up to you if you think it'd be more or less readable. I think the style you 
have right now is acceptable as-is, though obviously it'd be better if we could 
replace it with JUnit 5-style generated test cases.



##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/rest/RestClientTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.connect.runtime.rest;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.connect.runtime.rest.entities.ErrorMessage;
+import org.apache.kafka.connect.runtime.rest.errors.ConnectRestException;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.api.Request;
+import org.jose4j.keys.HmacKey;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.ws.rs.core.Response;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(Enclosed.class)
+public class RestClientTest {
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final TypeReference<TestDTO> TEST_TYPE = new 
TypeReference<TestDTO>() {
+    };
+
+    private static void assertIsInternalServerError(ConnectRestException e) {
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), 
e.statusCode());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), 
e.errorCode());
+    }
+
+    private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient 
httpClient, String requestSignatureAlgorithm) {
+        return RestClient.httpRequest(
+                httpClient,
+                "https://localhost:1234/api/endpoint";,
+                "GET",
+                null,
+                new TestDTO("requestBodyData"),
+                TEST_TYPE,
+                new HmacKey("HMAC".getBytes(StandardCharsets.UTF_8)),
+                requestSignatureAlgorithm);
+    }
+
+    private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient 
httpClient) {
+        String validRequestSignatureAlgorithm = "HmacMD5";

Review Comment:
   We should probably change this to either `HmacSHA1` or `HmacSHA256`, since 
those are the only two algorithms guaranteed to be present on JVMs that comply 
with the Java platform specs. See the [Mac 
Javadocs](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/javax/crypto/Mac.html)
 for reference.



##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/rest/RestClientTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.connect.runtime.rest;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.connect.runtime.rest.entities.ErrorMessage;
+import org.apache.kafka.connect.runtime.rest.errors.ConnectRestException;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.api.Request;
+import org.jose4j.keys.HmacKey;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.ws.rs.core.Response;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(Enclosed.class)
+public class RestClientTest {
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final TypeReference<TestDTO> TEST_TYPE = new 
TypeReference<TestDTO>() {
+    };
+
+    private static void assertIsInternalServerError(ConnectRestException e) {
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), 
e.statusCode());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), 
e.errorCode());
+    }
+
+    private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient 
httpClient, String requestSignatureAlgorithm) {
+        return RestClient.httpRequest(
+                httpClient,
+                "https://localhost:1234/api/endpoint";,
+                "GET",
+                null,
+                new TestDTO("requestBodyData"),
+                TEST_TYPE,
+                new HmacKey("HMAC".getBytes(StandardCharsets.UTF_8)),
+                requestSignatureAlgorithm);
+    }
+
+    private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient 
httpClient) {
+        String validRequestSignatureAlgorithm = "HmacMD5";
+        return httpRequest(httpClient, validRequestSignatureAlgorithm);
+    }
+
+
+    @RunWith(Parameterized.class)
+    public static class RequestFailureParameterizedTest {
+        private static final HttpClient httpClient = mock(HttpClient.class);

Review Comment:
   Can we use the `@Mock` annotation here? And if not, is it safe to establish 
a `static` field here, which will be reused across test cases?



##########
connect/runtime/src/main/java/org/apache/kafka/connect/runtime/rest/RestClient.java:
##########
@@ -143,15 +156,12 @@ public static <T> HttpResponse<T> httpRequest(String url, 
String method, HttpHea
         } catch (IOException | InterruptedException | TimeoutException | 
ExecutionException e) {
             log.error("IO error forwarding REST request: ", e);
             throw new 
ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, "IO Error trying to 
forward REST request: " + e.getMessage(), e);
+        } catch (ConnectRestException e) {

Review Comment:
   👍 looks good to me, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to