elkkhan commented on code in PR #12320: URL: https://github.com/apache/kafka/pull/12320#discussion_r915638083
########## connect/runtime/src/test/java/org/apache/kafka/connect/runtime/rest/RestClientTest.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.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.crypto.SecretKey; +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 final SecretKey MOCK_SECRET_KEY = getMockSecretKey(); + + 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 SecretKey getMockSecretKey() { + SecretKey mockKey = mock(SecretKey.class); + when(mockKey.getFormat()).thenReturn("RAW");// supported format by + when(mockKey.getEncoded()).thenReturn("SomeKey".getBytes(StandardCharsets.UTF_8)); + return mockKey; + } + + 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, + MOCK_SECRET_KEY, + requestSignatureAlgorithm); + } + + private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient httpClient) { + String validRequestSignatureAlgorithm = "HmacSHA1"; + return httpRequest(httpClient, validRequestSignatureAlgorithm); + } + + + @RunWith(Parameterized.class) + public static class RequestFailureParameterizedTest { + private final HttpClient httpClient = mock(HttpClient.class); + + @Parameterized.Parameter + public Throwable requestException; + + @Parameterized.Parameters + public static Collection<Object[]> requestExceptions() { + return Arrays.asList(new Object[][]{ + {new InterruptedException()}, + {new ExecutionException(null)}, + {new TimeoutException()} + }); + } + + private static Request buildThrowingMockRequest(Throwable t) throws ExecutionException, InterruptedException, TimeoutException { + Request req = mock(Request.class); + when(req.send()).thenThrow(t); Review Comment: nice catch, thx. setupHttpClient does this but I forgot to do it here and didn't notice the NPEs. fixed -- 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