briansolo1985 commented on code in PR #6733:
URL: https://github.com/apache/nifi/pull/6733#discussion_r1065810905


##########
c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2ClientService.java:
##########
@@ -59,11 +120,30 @@ private void processResponse(C2HeartbeatResponse response) 
{
         }
     }
 
-    private void handleRequestedOperations(List<C2Operation> 
requestedOperations) {
-        for (C2Operation requestedOperation : requestedOperations) {
-            operationService.handleOperation(requestedOperation)
-                .ifPresent(client::acknowledgeOperation);
+    private boolean requiresRestart(C2OperationHandler c2OperationHandler, 
C2OperationAck c2OperationAck) {
+        return c2OperationHandler.requiresRestart() && 
isOperationFullyApplied(c2OperationAck);
+    }
+
+    private static boolean isOperationFullyApplied(C2OperationAck 
c2OperationAck) {
+        return !Optional.ofNullable(c2OperationAck)

Review Comment:
   This is equivalent to
   ```suggestion
           return Optional.ofNullable(c2OperationAck)
               .map(C2OperationAck::getOperationState)
               .map(C2OperationState::getState)
               .filter(FULLY_APPLIED::equals)
               .isPresent();
   ```



##########
minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/test/java/org/apache/nifi/minifi/c2/FileBasedRequestedOperationDAOTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.minifi.c2;
+
+import static 
org.apache.nifi.minifi.c2.FileBasedRequestedOperationDAO.REQUESTED_OPERATIONS_FILE_NAME;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Optional;
+import org.apache.nifi.c2.client.service.operation.OperationQueue;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class FileBasedRequestedOperationDAOTest {
+
+    @Mock
+    private ObjectMapper objectMapper;
+
+    @TempDir
+    File tmpDir;
+
+    private FileBasedRequestedOperationDAO fileBasedRequestedOperationDAO;
+
+    @BeforeEach
+    void setup() {
+        fileBasedRequestedOperationDAO = new 
FileBasedRequestedOperationDAO(tmpDir.getAbsolutePath(), objectMapper);
+    }
+
+    @Test
+    void shouldSaveRequestedOperationsToFile() throws IOException {
+        OperationQueue operationQueue = getOperationQueue();
+        fileBasedRequestedOperationDAO.save(operationQueue);
+
+        verify(objectMapper).writeValue(any(File.class), eq(operationQueue));
+    }
+
+    @Test
+    void shouldThrowRuntimeExceptionWhenExceptionHappensDuringSave() throws 
IOException {
+        doThrow(new 
RuntimeException()).when(objectMapper).writeValue(any(File.class), anyList());
+
+        assertThrows(RuntimeException.class, () -> 
fileBasedRequestedOperationDAO.save(mock(OperationQueue.class)));
+    }
+
+    @Test
+    void shouldGetReturnEmptyWhenFileDoesntExists() {
+        assertEquals(Optional.empty(), fileBasedRequestedOperationDAO.load());
+    }
+
+    @Test
+    void shouldGetReturnEmptyWhenExceptionHappens() throws IOException {
+        new File(tmpDir.getAbsolutePath() + "/" + 
REQUESTED_OPERATIONS_FILE_NAME).createNewFile();
+
+        doThrow(new 
RuntimeException()).when(objectMapper).readValue(any(File.class), 
eq(OperationQueue.class));
+
+        assertEquals(Optional.empty(), fileBasedRequestedOperationDAO.load());
+    }
+
+    @Test
+    void shouldGetRequestedOperations() throws IOException {
+        new File(tmpDir.getAbsolutePath() + "/" + 
REQUESTED_OPERATIONS_FILE_NAME).createNewFile();
+
+        OperationQueue operationQueue = getOperationQueue();
+        when(objectMapper.readValue(any(File.class), 
eq(OperationQueue.class))).thenReturn(operationQueue);
+
+        assertEquals(Optional.of(operationQueue), 
fileBasedRequestedOperationDAO.load());
+    }
+
+    private static OperationQueue getOperationQueue() {

Review Comment:
   No need to use static here



##########
c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2ClientService.java:
##########
@@ -59,11 +120,30 @@ private void processResponse(C2HeartbeatResponse response) 
{
         }
     }
 
-    private void handleRequestedOperations(List<C2Operation> 
requestedOperations) {
-        for (C2Operation requestedOperation : requestedOperations) {
-            operationService.handleOperation(requestedOperation)
-                .ifPresent(client::acknowledgeOperation);
+    private boolean requiresRestart(C2OperationHandler c2OperationHandler, 
C2OperationAck c2OperationAck) {
+        return c2OperationHandler.requiresRestart() && 
isOperationFullyApplied(c2OperationAck);
+    }
+
+    private static boolean isOperationFullyApplied(C2OperationAck 
c2OperationAck) {

Review Comment:
   No need to use static here



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to