exceptionfactory commented on code in PR #10471:
URL: https://github.com/apache/nifi/pull/10471#discussion_r2560778166


##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/api/TestVersionsResource.java:
##########
@@ -81,4 +110,240 @@ public void testExportFlowVersion() {
         
verify(innerInnerVersionedProcessGroup).setVersionedFlowCoordinates(null);
     }
 
-}
\ No newline at end of file
+
+
+    @Test
+    public void testCreateFlowBranchRequiresBranchName() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(0L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+
+        assertThrows(IllegalArgumentException.class, () -> 
versionsResource.createFlowBranch(groupId, requestEntity));
+    }
+
+    @Test
+    public void testCreateFlowBranchInvokesService() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("feature");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setVersion("3");
+        currentDto.setState(VersionControlInformationDTO.UP_TO_DATE);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        final VersionControlInformationEntity expectedEntity = new 
VersionControlInformationEntity();
+        when(serviceFacade.createFlowBranch(any(Revision.class), eq(groupId), 
eq("feature"), any(), any()))
+                .thenReturn(expectedEntity);
+
+        final Response response = versionsResource.createFlowBranch(groupId, 
requestEntity);
+        assertEquals(200, response.getStatus());
+        assertEquals(expectedEntity, response.getEntity());
+
+        ArgumentCaptor<Revision> revisionCaptor = 
ArgumentCaptor.forClass(Revision.class);
+        verify(serviceFacade).createFlowBranch(revisionCaptor.capture(), 
eq(groupId), eq("feature"), isNull(), isNull());
+
+        final Revision capturedRevision = revisionCaptor.getValue();
+        assertEquals(1L, capturedRevision.getVersion());
+        assertEquals("client-id", capturedRevision.getClientId());
+        assertEquals(groupId, capturedRevision.getComponentId());
+    }
+
+    @Test
+    public void testCreateFlowBranchFailsWhenBranchExists() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("main");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setState(VersionControlInformationDTO.UP_TO_DATE);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        when(serviceFacade.createFlowBranch(any(Revision.class), eq(groupId), 
eq("main"), any(), any()))
+                .thenThrow(new IllegalArgumentException("Process Group is 
already tracking branch main"));
+
+        assertThrows(IllegalArgumentException.class, () -> 
versionsResource.createFlowBranch(groupId, requestEntity));
+    }
+
+    @Test
+    public void testCreateFlowBranchUnsupported() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("feature");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setVersion("2");
+        currentDto.setState(VersionControlInformationDTO.UP_TO_DATE);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        when(serviceFacade.createFlowBranch(any(Revision.class), eq(groupId), 
eq("feature"), any(), any()))
+                .thenThrow(new IllegalArgumentException("Registry does not 
support branching"));
+
+        assertThrows(IllegalArgumentException.class, () -> 
versionsResource.createFlowBranch(groupId, requestEntity));
+    }
+
+    @Test
+    public void testCreateFlowBranchAllowedWhenLocallyModified() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("feature");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setVersion("1");
+        currentDto.setState(VersionControlInformationDTO.LOCALLY_MODIFIED);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        final VersionControlInformationEntity expectedEntity = new 
VersionControlInformationEntity();
+        when(serviceFacade.createFlowBranch(any(Revision.class), eq(groupId), 
eq("feature"), any(), any()))
+                .thenReturn(expectedEntity);
+
+        final Response response = versionsResource.createFlowBranch(groupId, 
requestEntity);
+        assertEquals(HttpURLConnection.HTTP_OK, response.getStatus());
+        assertEquals(expectedEntity, response.getEntity());
+    }
+
+    @Test
+    public void testCreateFlowBranchBlockedWhenSyncFailure() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("feature");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setVersion("1");
+        currentDto.setState(VersionControlInformationDTO.SYNC_FAILURE);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        assertThrows(IllegalStateException.class, () -> 
versionsResource.createFlowBranch(groupId, requestEntity));
+
+        verify(serviceFacade, never()).createFlowBranch(any(), anyString(), 
anyString(), any(), any());
+    }
+
+    @Test
+    public void testCreateFlowBranchAllowedWhenLocallyModifiedAndStale() {
+        final String groupId = UUID.randomUUID().toString();
+        final CreateFlowBranchRequestEntity requestEntity = new 
CreateFlowBranchRequestEntity();
+        final RevisionDTO revisionDTO = new RevisionDTO();
+        revisionDTO.setClientId("client-id");
+        revisionDTO.setVersion(1L);
+        requestEntity.setProcessGroupRevision(revisionDTO);
+        requestEntity.setBranch("feature");
+
+        versionsResource.httpServletRequest = new MockHttpServletRequest();
+
+        final VersionControlInformationDTO currentDto = new 
VersionControlInformationDTO();
+        currentDto.setGroupId(groupId);
+        currentDto.setBranch("main");
+        currentDto.setVersion("1");
+        
currentDto.setState(VersionControlInformationDTO.LOCALLY_MODIFIED_AND_STALE);
+        final VersionControlInformationEntity currentEntity = new 
VersionControlInformationEntity();
+        currentEntity.setVersionControlInformation(currentDto);
+        
when(serviceFacade.getVersionControlInformation(groupId)).thenReturn(currentEntity);
+
+        final VersionControlInformationEntity expectedEntity = new 
VersionControlInformationEntity();
+        when(serviceFacade.createFlowBranch(any(Revision.class), eq(groupId), 
eq("feature"), any(), any()))
+                .thenReturn(expectedEntity);
+
+        final Response response = versionsResource.createFlowBranch(groupId, 
requestEntity);
+        assertEquals(200, response.getStatus());

Review Comment:
   It looks like there are several additional places where `200` can be 
replaced with `HttpURLConnection.HTTP_OK`



-- 
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]

Reply via email to