exceptionfactory commented on code in PR #11587: URL: https://github.com/apache/nifi/pull/11587#discussion_r3847227377
########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMerger.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.manager.NodeResponse; +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.apache.nifi.web.api.entity.DropRequestEntity; + +import java.net.URI; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +public class ConnectorPurgeRequestEndpointMerger extends AbstractSingleDTOEndpoint<DropRequestEntity, DropRequestDTO> { + private static final Pattern CONNECTOR_PURGE_REQUESTS_URI = Pattern.compile("/nifi-api/connectors/[a-f0-9\\-]{36}/purge-requests"); + private static final Pattern CONNECTOR_PURGE_REQUEST_URI = Pattern.compile("/nifi-api/connectors/[a-f0-9\\-]{36}/purge-requests/[a-f0-9\\-]{36}"); + + @Override + public boolean canHandle(final URI uri, final String method) { + final String path = uri.getPath(); + if ("POST".equalsIgnoreCase(method)) { + return CONNECTOR_PURGE_REQUESTS_URI.matcher(path).matches(); + } + + return ("GET".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method)) + && CONNECTOR_PURGE_REQUEST_URI.matcher(path).matches(); + } + + @Override + protected Class<DropRequestEntity> getEntityClass() { + return DropRequestEntity.class; + } + + @Override + protected DropRequestDTO getDto(final DropRequestEntity entity) { + return entity.getDropRequest(); + } + + @Override + protected void mergeResponses(final DropRequestDTO clientDto, final Map<NodeIdentifier, DropRequestDTO> dtoMap, + final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses) { + final Set<String> failureReasons = new LinkedHashSet<>(); + boolean allFinished = true; + int percentCompleted = 100; Review Comment: The `100` value is reused multiple times, so it should be defined as a static variable named `TOTAL_PERCENT_COMPLETED` or similar and reused ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMerger.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.manager.NodeResponse; +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.apache.nifi.web.api.entity.DropRequestEntity; + +import java.net.URI; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +public class ConnectorPurgeRequestEndpointMerger extends AbstractSingleDTOEndpoint<DropRequestEntity, DropRequestDTO> { + private static final Pattern CONNECTOR_PURGE_REQUESTS_URI = Pattern.compile("/nifi-api/connectors/[a-f0-9\\-]{36}/purge-requests"); + private static final Pattern CONNECTOR_PURGE_REQUEST_URI = Pattern.compile("/nifi-api/connectors/[a-f0-9\\-]{36}/purge-requests/[a-f0-9\\-]{36}"); + + @Override + public boolean canHandle(final URI uri, final String method) { + final String path = uri.getPath(); + if ("POST".equalsIgnoreCase(method)) { + return CONNECTOR_PURGE_REQUESTS_URI.matcher(path).matches(); + } + + return ("GET".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method)) + && CONNECTOR_PURGE_REQUEST_URI.matcher(path).matches(); + } + + @Override + protected Class<DropRequestEntity> getEntityClass() { + return DropRequestEntity.class; + } + + @Override + protected DropRequestDTO getDto(final DropRequestEntity entity) { + return entity.getDropRequest(); + } + + @Override + protected void mergeResponses(final DropRequestDTO clientDto, final Map<NodeIdentifier, DropRequestDTO> dtoMap, + final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses) { + final Set<String> failureReasons = new LinkedHashSet<>(); + boolean allFinished = true; + int percentCompleted = 100; + + for (final DropRequestDTO nodeDto : dtoMap.values()) { + if (nodeDto.getFailureReason() != null) { + failureReasons.add(nodeDto.getFailureReason()); + } + + allFinished &= nodeDto.isFinished(); + percentCompleted = Math.min(percentCompleted, nodeDto.getPercentCompleted()); + } + + if (!failureReasons.isEmpty()) { + final String failureReason = String.join("; ", failureReasons); + clientDto.setFinished(true); + clientDto.setPercentCompleted(100); + clientDto.setFailureReason(failureReason); + clientDto.setState("Failed: " + failureReason); + return; + } + + clientDto.setFinished(allFinished); + clientDto.setPercentCompleted(allFinished ? 100 : percentCompleted); + clientDto.setState(allFinished ? "Complete" : "In Progress"); Review Comment: This approach should be refactored to avoid the short-circuit return ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/StandardHttpResponseMapperTest.java: ########## @@ -0,0 +1,46 @@ +/* + * 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.nifi.cluster.coordination.http; + +import org.apache.nifi.util.NiFiProperties; +import org.junit.jupiter.api.Test; + +import java.net.URI; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class StandardHttpResponseMapperTest { Review Comment: I recommend removing this test class, since it is more narrowly focused. It could be considered as a separate addition covering more URIs, but better left out for now. ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMergerTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ConnectorPurgeRequestEndpointMergerTest { + private static final String CONNECTOR_ID = "abcdef00-abcd-abcd-abcd-abcdef000000"; + private static final String REQUEST_ID = "00000000-0000-0000-0000-000000000001"; + + private final ConnectorPurgeRequestEndpointMerger merger = new ConnectorPurgeRequestEndpointMerger(); + + @Test + void testCanHandleConnectorPurgeRequests() { + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "GET")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "DELETE")); + } + + @Test + void testCanHandleRejectsUnrelatedRequests() { + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "GET")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "DELETE")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "PUT")); Review Comment: Instead of concatenating all the values in the test method, recommend defining more static strings and reusing them across applicable test methods ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMergerTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ConnectorPurgeRequestEndpointMergerTest { + private static final String CONNECTOR_ID = "abcdef00-abcd-abcd-abcd-abcdef000000"; + private static final String REQUEST_ID = "00000000-0000-0000-0000-000000000001"; + + private final ConnectorPurgeRequestEndpointMerger merger = new ConnectorPurgeRequestEndpointMerger(); + + @Test + void testCanHandleConnectorPurgeRequests() { + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "GET")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "DELETE")); Review Comment: The HTTP method strings can be defined once and reused across methods ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMergerTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ConnectorPurgeRequestEndpointMergerTest { + private static final String CONNECTOR_ID = "abcdef00-abcd-abcd-abcd-abcdef000000"; + private static final String REQUEST_ID = "00000000-0000-0000-0000-000000000001"; + + private final ConnectorPurgeRequestEndpointMerger merger = new ConnectorPurgeRequestEndpointMerger(); + + @Test + void testCanHandleConnectorPurgeRequests() { + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "GET")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "DELETE")); + } + + @Test + void testCanHandleRejectsUnrelatedRequests() { + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "GET")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "DELETE")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "PUT")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/not-a-uuid/purge-requests"), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/not-a-uuid"), "GET")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/backlog-requests"), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/processors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + } + + @Test + void testMergeWaitsForSlowestNode() { + final DropRequestDTO clientRequest = request(true, 100, null); Review Comment: The numeric percent completed values should be defined once and reused ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/endpoints/DropRequestEndpointMergerTest.java: ########## @@ -0,0 +1,88 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.controller.queue.DropFlowFileState; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DropRequestEndpointMergerTest { + private static final String CONNECTION_ID = "12345678-1234-1234-1234-123456789012"; + private static final String REQUEST_ID = "00000000-0000-0000-0000-000000000001"; + + private final DropRequestEndpointMerger merger = new DropRequestEndpointMerger(); + + @Test + void testCanHandleFlowFileQueueDropRequests() { + assertTrue(merger.canHandle(URI.create("/nifi-api/flowfile-queues/" + CONNECTION_ID + "/drop-requests"), "POST")); + assertTrue(merger.canHandle(URI.create("/nifi-api/flowfile-queues/" + CONNECTION_ID + "/drop-requests/" + REQUEST_ID), "GET")); + assertTrue(merger.canHandle(URI.create("/nifi-api/flowfile-queues/" + CONNECTION_ID + "/drop-requests/" + REQUEST_ID), "DELETE")); + } + + @Test + void testCanHandleRejectsUnsupportedRequests() { + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/abcdef00-abcd-abcd-abcd-abcdef000000/purge-requests"), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/flowfile-queues/not-a-uuid/drop-requests"), "POST")); + } + + @Test + void testMergeWaitsForAllNodesAndAggregatesCounts() { + final DropRequestDTO clientRequest = dropRequest(true, 0, 4, 40L, 6, 60L); + final Map<NodeIdentifier, DropRequestDTO> requests = new LinkedHashMap<>(); + requests.put(nodeIdentifier(1), clientRequest); + requests.put(nodeIdentifier(2), dropRequest(false, 3, 6, 60L, 7, 70L)); + + merger.mergeResponses(clientRequest, requests, null, null); + + assertFalse(clientRequest.isFinished()); + assertEquals(3, clientRequest.getCurrentCount()); + assertEquals(30L, clientRequest.getCurrentSize()); + assertEquals(10, clientRequest.getDroppedCount()); + assertEquals(100L, clientRequest.getDroppedSize()); + assertEquals(13, clientRequest.getOriginalCount()); + assertEquals(130L, clientRequest.getOriginalSize()); + assertEquals(76, clientRequest.getPercentCompleted()); + } + + private static DropRequestDTO dropRequest(final boolean finished, final int currentCount, final int droppedCount, + final long droppedSize, final int originalCount, final long originalSize) { + final DropRequestDTO request = new DropRequestDTO(); + request.setFinished(finished); + request.setCurrentCount(currentCount); + request.setCurrentSize(currentCount * 10L); + request.setDroppedCount(droppedCount); + request.setDroppedSize(droppedSize); + request.setOriginalCount(originalCount); + request.setOriginalSize(originalSize); + request.setState(finished ? DropFlowFileState.COMPLETE.toString() : DropFlowFileState.DROPPING_FLOWFILES.toString()); + return request; + } + + private static NodeIdentifier nodeIdentifier(final int index) { + return new NodeIdentifier("node-" + index, "localhost", 8000 + index, "localhost", 8100 + index, + "localhost", 8200 + index, 8300 + index, false); Review Comment: See above comment on argument formatting ########## nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/http/endpoints/ConnectorPurgeRequestEndpointMergerTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.nifi.cluster.coordination.http.endpoints; + +import org.apache.nifi.cluster.protocol.NodeIdentifier; +import org.apache.nifi.web.api.dto.DropRequestDTO; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ConnectorPurgeRequestEndpointMergerTest { + private static final String CONNECTOR_ID = "abcdef00-abcd-abcd-abcd-abcdef000000"; + private static final String REQUEST_ID = "00000000-0000-0000-0000-000000000001"; + + private final ConnectorPurgeRequestEndpointMerger merger = new ConnectorPurgeRequestEndpointMerger(); + + @Test + void testCanHandleConnectorPurgeRequests() { + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "GET")); + assertTrue(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "DELETE")); + } + + @Test + void testCanHandleRejectsUnrelatedRequests() { + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "GET")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests"), "DELETE")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/" + REQUEST_ID), "PUT")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/not-a-uuid/purge-requests"), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/purge-requests/not-a-uuid"), "GET")); + assertFalse(merger.canHandle(URI.create("/nifi-api/connectors/" + CONNECTOR_ID + "/backlog-requests"), "POST")); + assertFalse(merger.canHandle(URI.create("/nifi-api/processors/" + CONNECTOR_ID + "/purge-requests"), "POST")); + } + + @Test + void testMergeWaitsForSlowestNode() { + final DropRequestDTO clientRequest = request(true, 100, null); + final Map<NodeIdentifier, DropRequestDTO> requests = new LinkedHashMap<>(); + requests.put(nodeIdentifier(1), clientRequest); + requests.put(nodeIdentifier(2), request(false, 40, null)); + + merger.mergeResponses(clientRequest, requests, null, null); + + assertFalse(clientRequest.isFinished()); + assertEquals(40, clientRequest.getPercentCompleted()); + assertEquals("In Progress", clientRequest.getState()); + } + + @Test + void testMergeCompletesWhenAllNodesFinish() { + final DropRequestDTO clientRequest = request(true, 100, null); + final Map<NodeIdentifier, DropRequestDTO> requests = new LinkedHashMap<>(); + requests.put(nodeIdentifier(1), clientRequest); + requests.put(nodeIdentifier(2), request(true, 100, null)); + + merger.mergeResponses(clientRequest, requests, null, null); + + assertTrue(clientRequest.isFinished()); + assertEquals(100, clientRequest.getPercentCompleted()); + assertEquals("Complete", clientRequest.getState()); + } + + @Test + void testMergePropagatesFailure() { + final DropRequestDTO clientRequest = request(false, 10, null); + final Map<NodeIdentifier, DropRequestDTO> requests = new LinkedHashMap<>(); + requests.put(nodeIdentifier(1), clientRequest); + requests.put(nodeIdentifier(2), request(true, 0, "Failed to purge queue")); + + merger.mergeResponses(clientRequest, requests, null, null); + + assertTrue(clientRequest.isFinished()); + assertEquals(100, clientRequest.getPercentCompleted()); + assertEquals("Failed to purge queue", clientRequest.getFailureReason()); + assertEquals("Failed: Failed to purge queue", clientRequest.getState()); + } + + private static DropRequestDTO request(final boolean finished, final int percentCompleted, final String failureReason) { + final DropRequestDTO request = new DropRequestDTO(); + request.setFinished(finished); + request.setPercentCompleted(percentCompleted); + request.setFailureReason(failureReason); + return request; + } + + private static NodeIdentifier nodeIdentifier(final int index) { + return new NodeIdentifier("node-" + index, "localhost", 8000 + index, "localhost", 8100 + index, + "localhost", 8200 + index, 8300 + index, false); Review Comment: For readability, it would be helpful to declare each argument on its own line -- 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]
