C0urante commented on code in PR #12725:
URL: https://github.com/apache/kafka/pull/12725#discussion_r1009655795
##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/rest/resources/ConnectorsResourceTest.java:
##########
@@ -262,127 +254,96 @@ public void testFullExpandConnectors() {
assertEquals(connectorInfo, expanded.get(CONNECTOR_NAME).get("info"));
assertEquals(connector2, expanded.get(CONNECTOR2_NAME).get("status"));
assertEquals(connector, expanded.get(CONNECTOR_NAME).get("status"));
- PowerMock.verifyAll();
}
@Test
public void testExpandConnectorsWithConnectorNotFound() {
-
EasyMock.expect(herder.connectors()).andReturn(Arrays.asList(CONNECTOR2_NAME,
CONNECTOR_NAME));
- ConnectorStateInfo connector = EasyMock.mock(ConnectorStateInfo.class);
- ConnectorStateInfo connector2 =
EasyMock.mock(ConnectorStateInfo.class);
-
EasyMock.expect(herder.connectorStatus(CONNECTOR2_NAME)).andReturn(connector2);
-
EasyMock.expect(herder.connectorStatus(CONNECTOR_NAME)).andThrow(EasyMock.mock(NotFoundException.class));
+ when(herder.connectors()).thenReturn(Arrays.asList(CONNECTOR2_NAME,
CONNECTOR_NAME));
+ ConnectorStateInfo connector = mock(ConnectorStateInfo.class);
+ ConnectorStateInfo connector2 = mock(ConnectorStateInfo.class);
+ when(herder.connectorStatus(CONNECTOR2_NAME)).thenReturn(connector2);
+
doThrow(mock(NotFoundException.class)).when(herder).connectorStatus(CONNECTOR_NAME);
- forward = EasyMock.mock(UriInfo.class);
+ forward = mock(UriInfo.class);
MultivaluedMap<String, String> queryParams = new
MultivaluedHashMap<>();
queryParams.putSingle("expand", "status");
-
EasyMock.expect(forward.getQueryParameters()).andReturn(queryParams).anyTimes();
- EasyMock.replay(forward);
-
- PowerMock.replayAll();
+ when(forward.getQueryParameters()).thenReturn(queryParams);
Map<String, Map<String, Object>> expanded = (Map<String, Map<String,
Object>>) connectorsResource.listConnectors(forward, NULL_HEADERS).getEntity();
// Ordering isn't guaranteed, compare sets
assertEquals(Collections.singleton(CONNECTOR2_NAME),
expanded.keySet());
assertEquals(connector2, expanded.get(CONNECTOR2_NAME).get("status"));
- PowerMock.verifyAll();
}
@Test
public void testCreateConnector() throws Throwable {
CreateConnectorRequest body = new
CreateConnectorRequest(CONNECTOR_NAME,
Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));
- final Capture<Callback<Herder.Created<ConnectorInfo>>> cb =
Capture.newInstance();
- herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME),
EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));
+ final ArgumentCaptor<Callback<Herder.Created<ConnectorInfo>>> cb =
ArgumentCaptor.forClass(Callback.class);
expectAndCallbackResult(cb, new Herder.Created<>(true, new
ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG,
- CONNECTOR_TASK_NAMES, ConnectorType.SOURCE)));
-
- PowerMock.replayAll();
+ CONNECTOR_TASK_NAMES, ConnectorType.SOURCE))
+ ).when(herder).putConnectorConfig(eq(CONNECTOR_NAME),
eq(body.config()), eq(false), cb.capture());
connectorsResource.createConnector(FORWARD, NULL_HEADERS, body);
-
- PowerMock.verifyAll();
}
@Test
public void testCreateConnectorNotLeader() throws Throwable {
CreateConnectorRequest body = new
CreateConnectorRequest(CONNECTOR_NAME,
Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));
- final Capture<Callback<Herder.Created<ConnectorInfo>>> cb =
Capture.newInstance();
- herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME),
EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));
- expectAndCallbackNotLeaderException(cb);
- // Should forward request
- EasyMock.expect(RestClient.httpRequest(EasyMock.eq(LEADER_URL +
"connectors?forward=false"), EasyMock.eq("POST"), EasyMock.isNull(),
EasyMock.eq(body), EasyMock.anyObject(),
EasyMock.anyObject(WorkerConfig.class)))
- .andReturn(new RestClient.HttpResponse<>(201, new HashMap<>(),
new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES,
- ConnectorType.SOURCE)));
+ final ArgumentCaptor<Callback<Herder.Created<ConnectorInfo>>> cb =
ArgumentCaptor.forClass(Callback.class);
+ expectAndCallbackNotLeaderException(cb).when(herder)
+ .putConnectorConfig(eq(CONNECTOR_NAME), eq(body.config()),
eq(false), cb.capture());
- PowerMock.replayAll();
+ // Should forward request
+ restClientStatic.when(() ->
+ RestClient.httpRequest(eq(LEADER_URL +
"connectors?forward=false"), eq("POST"), isNull(), eq(body), any(),
any(WorkerConfig.class))
+ ).thenReturn(new RestClient.HttpResponse<>(201, new HashMap<>(), new
ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES,
ConnectorType.SOURCE)));
connectorsResource.createConnector(FORWARD, NULL_HEADERS, body);
-
- PowerMock.verifyAll();
-
-
}
@Test
public void testCreateConnectorWithHeaderAuthorization() throws Throwable {
CreateConnectorRequest body = new
CreateConnectorRequest(CONNECTOR_NAME,
Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));
- final Capture<Callback<Herder.Created<ConnectorInfo>>> cb =
Capture.newInstance();
- HttpHeaders httpHeaders = EasyMock.mock(HttpHeaders.class);
-
EasyMock.expect(httpHeaders.getHeaderString("Authorization")).andReturn("Basic
YWxhZGRpbjpvcGVuc2VzYW1l").times(1);
- EasyMock.replay(httpHeaders);
- herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME),
EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));
- expectAndCallbackNotLeaderException(cb);
+ final ArgumentCaptor<Callback<Herder.Created<ConnectorInfo>>> cb =
ArgumentCaptor.forClass(Callback.class);
+ HttpHeaders httpHeaders = mock(HttpHeaders.class);
+ expectAndCallbackNotLeaderException(cb)
+ .when(herder).putConnectorConfig(eq(CONNECTOR_NAME),
eq(body.config()), eq(false), cb.capture());
- EasyMock.expect(RestClient.httpRequest(EasyMock.eq(LEADER_URL +
"connectors?forward=false"),
- EasyMock.eq("POST"), EasyMock.eq(httpHeaders),
EasyMock.anyObject(), EasyMock.anyObject(),
EasyMock.anyObject(WorkerConfig.class)))
- .andReturn(new RestClient.HttpResponse<>(202, new HashMap<>(),
null));
-
- PowerMock.replayAll();
+ restClientStatic.when(() ->
+ RestClient.httpRequest(eq(LEADER_URL +
"connectors?forward=false"), eq("POST"), eq(httpHeaders), any(), any(),
any(WorkerConfig.class))
+ ).thenReturn(new RestClient.HttpResponse<>(202, new HashMap<>(),
null));
connectorsResource.createConnector(FORWARD, httpHeaders, body);
-
- PowerMock.verifyAll();
}
@Test
public void testCreateConnectorWithoutHeaderAuthorization() throws
Throwable {
Review Comment:
I think we can just consolidate the test cases. The coverage we need here is
to ensure that the headers that were given to
`connectorsResource::createConnector` are propagated to
`RestClient::httpRequest`. The actual content of those headers doesn't matter
in this test suite.
Probably worth renaming to something like `testCreateConnectorWithHeaders`
since authorization no longer really has anything to do with it.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]