exceptionfactory commented on code in PR #11400:
URL: https://github.com/apache/nifi/pull/11400#discussion_r3777784951
##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/dao/impl/StandardConnectionDAOTest.java:
##########
@@ -199,4 +210,124 @@ void testGetConnectionWithMultipleConnectors() {
assertEquals(connectionInSecondConnector, result);
}
+
+ private ConnectionDTO connectionDtoWithNameOnly() {
+ final ConnectionDTO dto = new ConnectionDTO();
+ dto.setId(ROOT_CONNECTION_ID);
+ dto.setName("renamed-connection");
+ return dto;
+ }
+
+ private ConnectionDTO connectionDtoChangingDestination(final String
newDestinationId) {
+ final ConnectionDTO dto = new ConnectionDTO();
+ dto.setId(ROOT_CONNECTION_ID);
+ final ConnectableDTO newDestination = new ConnectableDTO();
+ newDestination.setId(newDestinationId);
+ newDestination.setType(ConnectableType.PROCESSOR.name());
+ dto.setDestination(newDestination);
+ return dto;
+ }
+
+ private void stubRootConnectionDestination(final String destinationId) {
+ final ProcessGroup group = mock(ProcessGroup.class);
+ when(group.getIdentifier()).thenReturn("group-id");
+ when(rootConnection.getProcessGroup()).thenReturn(group);
+
+ final Connectable currentDestination = mock(Connectable.class);
+ when(currentDestination.getIdentifier()).thenReturn(destinationId);
+ when(currentDestination.isRunning()).thenReturn(true);
+
when(currentDestination.getConnectableType()).thenReturn(ConnectableType.PROCESSOR);
+ when(rootConnection.getDestination()).thenReturn(currentDestination);
+ }
+
+ @Test
+ void testVerifyUpdateDoesNotCheckDestinationForNonDestinationEdit() {
+ stubRootConnectionDestination("current-destination-id");
+
+ assertDoesNotThrow(() ->
connectionDAO.verifyUpdate(connectionDtoWithNameOnly()));
+ verify(rootConnection, never()).verifyCanUpdateDestination();
+ }
+
+ @Test
+ void
testVerifyUpdateWrapsIllegalStateFromDestinationGuardAsValidationException() {
+ stubRootConnectionDestination("current-destination-id");
+ final String guardMessage = "Cannot change destination of Connection
because the current destination ([proc]) is running";
+ org.mockito.Mockito.doThrow(new IllegalStateException(guardMessage))
+ .when(rootConnection).verifyCanUpdateDestination();
+
+ final ValidationException thrown =
assertThrows(ValidationException.class,
+ () ->
connectionDAO.verifyUpdate(connectionDtoChangingDestination("new-destination-id")));
+ assertTrue(thrown.getValidationErrors().contains(guardMessage),
+ "ValidationException should carry the guard's message; was: "
+ thrown.getValidationErrors());
+ }
+
+ @Test
+ void testVerifyUpdateChecksDestinationGuardWhenDestinationChanges() {
+ stubRootConnectionDestination("current-destination-id");
+
+ assertDoesNotThrow(() ->
connectionDAO.verifyUpdate(connectionDtoChangingDestination("new-destination-id")));
+ verify(rootConnection).verifyCanUpdateDestination();
+ }
+
+ @Test
+ void testIsDestinationChangingReturnsFalseForSameDestinationId() {
+ final Connectable currentDestination = mock(Connectable.class);
+ when(currentDestination.getIdentifier()).thenReturn("dest-1");
Review Comment:
Many of these methods reuse Strings for identifiers, so it would be helpful
to declare these as static variables for reuse.
##########
nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/connectable/Connection.java:
##########
@@ -68,6 +68,16 @@ public interface Connection extends Authorizable,
VersionedComponent {
void setDestination(final Connectable newDestination);
+ /**
+ * Verifies that this Connection's destination may be changed, based
solely on the current (existing) destination
+ * and the FlowFiles the Connection is holding. This applies the same
guards as {@link #setDestination(Connectable)}
+ * so that a pre-check (e.g. the two-phase cluster verify) rejects exactly
what the subsequent mutation would reject.
+ *
+ * @throws IllegalStateException if the current destination is running and
is not exempt, or FlowFiles from this
+ * Connection are currently held by the destination
+ */
+ void verifyCanUpdateDestination() throws IllegalStateException;
Review Comment:
Declaring a `RuntimeException` type in `throws` is generally unnecessary,
and also implies a particular implementation. I recommend removing the
declaration and instead documenting it on the implementation method
--
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]