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


##########
c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandler.java:
##########
@@ -36,8 +38,9 @@
 public class UpdateConfigurationOperationHandler implements C2OperationHandler 
{
 
     private static final Logger logger = 
LoggerFactory.getLogger(UpdateConfigurationOperationHandler.class);
+    private static final Pattern FLOW_ID_PATTERN = 
Pattern.compile("/.*/.*/.*/(.*)/?.*");

Review Comment:
   This regular expression pattern is not optimal given the multiple uses of 
greedy matching using `.*`. Does the Flow ID follow a standard structure, such 
as UUID? If so, it would be better to search of a UUID structure. If not, 
adjusting the pattern matching to be more specific for path elements, using 
something like `[^/]+?` to find all characters except for a forward slash, and 
including the question mark for non-greedy matching, would be better.



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/C2HeartbeatFactoryTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.c2.client.service;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.nifi.c2.client.C2ClientConfig;
+import org.apache.nifi.c2.client.service.model.RuntimeInfoWrapper;
+import org.apache.nifi.c2.protocol.api.AgentRepositories;
+import org.apache.nifi.c2.protocol.api.C2Heartbeat;
+import org.apache.nifi.c2.protocol.api.FlowQueueStatus;
+import org.apache.nifi.c2.protocol.component.api.RuntimeManifest;
+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.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class C2HeartbeatFactoryTest {
+
+    private static final String AGENT_CLASS = "agentClass";
+    private static final String FLOW_ID = "flowId";
+
+    @Mock
+    private C2ClientConfig clientConfig;
+
+    @Mock
+    private FlowIdHolder flowIdHolder;
+
+    @InjectMocks
+    private C2HeartbeatFactory c2HeartbeatFactory;
+
+    @TempDir
+    private File tempDir;
+
+    @BeforeEach
+    public void setup() {
+        
when(clientConfig.getConfDirectory()).thenReturn(tempDir.getAbsolutePath());
+    }
+
+    @Test
+    void testCreateHeartbeat() {
+        when(flowIdHolder.getFlowId()).thenReturn(FLOW_ID);
+        when(clientConfig.getAgentClass()).thenReturn(AGENT_CLASS);
+
+        C2Heartbeat heartbeat = 
c2HeartbeatFactory.create(mock(RuntimeInfoWrapper.class));
+
+        assertEquals(FLOW_ID, heartbeat.getFlowId());
+        assertEquals(AGENT_CLASS, heartbeat.getAgentClass());
+    }
+
+    @Test
+    void testCreateGeneratesAgentAndDeviceIdIfNotPresent() {
+        C2Heartbeat heartbeat = 
c2HeartbeatFactory.create(mock(RuntimeInfoWrapper.class));
+
+        assertNotNull(heartbeat.getAgentId());
+        assertNotNull(heartbeat.getDeviceId());
+    }
+
+    @Test
+    void testCreatePopulatesFromRuntimeInfoWrapper() {
+        AgentRepositories repos = new AgentRepositories();
+        RuntimeManifest manifest = new RuntimeManifest();
+        Map<String, FlowQueueStatus> queueStatus = new HashMap<>();
+
+        C2Heartbeat heartbeat = c2HeartbeatFactory.create(new 
RuntimeInfoWrapper(repos, manifest, queueStatus));
+
+        assertEquals(repos, 
heartbeat.getAgentInfo().getStatus().getRepositories());
+        assertEquals(manifest, heartbeat.getAgentInfo().getAgentManifest());
+        assertEquals(queueStatus, heartbeat.getFlowInfo().getQueues());
+    }
+
+    @Test
+    void testCreateThrowsExceptionWhenConfDirNotSet() {
+        when(clientConfig.getConfDirectory()).thenReturn("dummy");

Review Comment:
   Use of the word `dummy` should be avoided. Some other placeholder, or even 
something like `String.class.getSimpleName()` would better.



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/C2ClientServiceTest.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.c2.client.service;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.model.RuntimeInfoWrapper;
+import org.apache.nifi.c2.client.service.operation.C2OperationService;
+import org.apache.nifi.c2.protocol.api.C2Heartbeat;
+import org.apache.nifi.c2.protocol.api.C2HeartbeatResponse;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class C2ClientServiceTest {
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private C2HeartbeatFactory c2HeartbeatFactory;
+
+    @Mock
+    private C2OperationService operationService;
+
+    @InjectMocks
+    private C2ClientService c2ClientService;
+
+    @Test
+    void testSendHeartbeatAndAckWhenOperationPresent() {
+        C2Heartbeat heartbeat = mock(C2Heartbeat.class);
+        when(c2HeartbeatFactory.create(any())).thenReturn(heartbeat);
+        C2HeartbeatResponse hbResponse = new C2HeartbeatResponse();
+        hbResponse.setRequestedOperations(generateOperation(1));
+        
when(client.publishHeartbeat(heartbeat)).thenReturn(Optional.of(hbResponse));
+        
when(operationService.handleOperation(any())).thenReturn(Optional.of(new 
C2OperationAck()));
+
+        c2ClientService.sendHeartbeat(mock(RuntimeInfoWrapper.class));

Review Comment:
   Changing `RuntimeInfoWrapper` to a `Mock` annotated member variable would 
simplify this and other test references.



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandlerTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.c2.client.service.operation;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+import static 
org.apache.nifi.c2.client.service.operation.UpdateConfigurationOperationHandler.LOCATION;
+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.Mockito.when;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.FlowIdHolder;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.protocol.api.C2OperationState;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UpdateConfigurationOperationHandlerTest {
+
+    private static final String FLOW_ID = "flowId";
+    private static final String OPERATION_ID = "operationId";
+    private static final Map<String, String> CORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "/path/for/the/" + FLOW_ID);
+    private static final Map<String, String> INCORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "incorrect/location");
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private FlowIdHolder flowIdHolder;
+
+    @Test
+    void testUpdateConfigurationOperationHandlerCreateSuccess() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+
+        assertEquals(OperationType.UPDATE, handler.getOperationType());
+        assertEquals(OperandType.CONFIGURATION, handler.getOperandType());
+    }
+
+    @Test
+    void testHandleThrowsExceptionForIncorrectArg() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(INCORRECT_LOCATION_MAP);
+
+        IllegalStateException exception = 
assertThrows(IllegalStateException.class, () -> handler.handle(operation));
+
+        assertEquals("Could not get flow id from the provided URL", 
exception.getMessage());
+    }
+
+    @Test
+    void testHandleReturnsNotAppliedWithNoContent() {
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");
+        when(client.retrieveUpdateContent(any())).thenReturn(Optional.empty());
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(client, flowIdHolder, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(CORRECT_LOCATION_MAP);
+
+        C2OperationAck response = handler.handle(operation);
+
+        assertEquals(EMPTY, response.getOperationId());
+        assertEquals(C2OperationState.OperationState.NOT_APPLIED, 
response.getOperationState().getState());
+    }
+
+    @Test
+    void testHandleReturnsNotAppliedWithContentApplyIssues() {
+        Function<byte[], Boolean> failedToUpdate = x -> false;
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");

Review Comment:
   ```suggestion
           when(flowIdHolder.getFlowId()).thenReturn(FLOW_ID);
   ```



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/C2ClientServiceTest.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.c2.client.service;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.model.RuntimeInfoWrapper;
+import org.apache.nifi.c2.client.service.operation.C2OperationService;
+import org.apache.nifi.c2.protocol.api.C2Heartbeat;
+import org.apache.nifi.c2.protocol.api.C2HeartbeatResponse;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class C2ClientServiceTest {
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private C2HeartbeatFactory c2HeartbeatFactory;
+
+    @Mock
+    private C2OperationService operationService;
+
+    @InjectMocks
+    private C2ClientService c2ClientService;
+
+    @Test
+    void testSendHeartbeatAndAckWhenOperationPresent() {
+        C2Heartbeat heartbeat = mock(C2Heartbeat.class);
+        when(c2HeartbeatFactory.create(any())).thenReturn(heartbeat);
+        C2HeartbeatResponse hbResponse = new C2HeartbeatResponse();
+        hbResponse.setRequestedOperations(generateOperation(1));
+        
when(client.publishHeartbeat(heartbeat)).thenReturn(Optional.of(hbResponse));
+        
when(operationService.handleOperation(any())).thenReturn(Optional.of(new 
C2OperationAck()));
+
+        c2ClientService.sendHeartbeat(mock(RuntimeInfoWrapper.class));
+
+        verify(c2HeartbeatFactory, times(1)).create(any());

Review Comment:
   The default `verify()` method with a single argument defaults to `times(1)`, 
so this reference and others could be simplified to remove `times(1)`.
   



##########
c2/c2-client-bundle/c2-client-http/src/test/java/org/apache/nifi/c2/client/http/C2HttpClientTest.java:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.c2.client.http;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.apache.nifi.c2.client.C2ClientConfig;
+import org.apache.nifi.c2.protocol.api.C2Heartbeat;
+import org.apache.nifi.c2.protocol.api.C2HeartbeatResponse;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.serializer.C2Serializer;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class C2HttpClientTest {
+
+    private static final String HEARTBEAT_PATH = "c2/heartbeat";
+    private static final String UPDATE_PATH = "c2/update";
+    private static final String ACK_PATH = "c2/acknowledge";
+    private static final int HTTP_STATUS_OK = 200;
+    private static final int HTTP_STATUS_BAD_REQUEST = 400;
+
+    @Mock
+    private C2ClientConfig c2ClientConfig;
+
+    @Mock
+    private C2Serializer serializer;
+
+    @InjectMocks
+    private C2HttpClient c2HttpClient;
+
+    private MockWebServer mockWebServer;
+
+    private String baseUrl;
+
+    @BeforeEach
+    public void startServer() {
+        mockWebServer = new MockWebServer();
+        baseUrl = 
mockWebServer.url("/").newBuilder().host("localhost").build().toString();
+    }
+
+    @AfterEach
+    public void shutdownServer() throws IOException {
+        mockWebServer.shutdown();
+    }
+
+    @Test
+    void testPublishHeartbeatSuccess() throws InterruptedException {
+        C2HeartbeatResponse hbResponse = new C2HeartbeatResponse();
+        mockWebServer.enqueue(new MockResponse().setBody("dummyResponseBody"));

Review Comment:
   Use of `dummy` should be avoided, recommend changing to simply 
`responseBody`.



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandlerTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.c2.client.service.operation;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+import static 
org.apache.nifi.c2.client.service.operation.UpdateConfigurationOperationHandler.LOCATION;
+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.Mockito.when;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.FlowIdHolder;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.protocol.api.C2OperationState;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UpdateConfigurationOperationHandlerTest {
+
+    private static final String FLOW_ID = "flowId";
+    private static final String OPERATION_ID = "operationId";
+    private static final Map<String, String> CORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "/path/for/the/" + FLOW_ID);
+    private static final Map<String, String> INCORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "incorrect/location");
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private FlowIdHolder flowIdHolder;
+
+    @Test
+    void testUpdateConfigurationOperationHandlerCreateSuccess() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+
+        assertEquals(OperationType.UPDATE, handler.getOperationType());
+        assertEquals(OperandType.CONFIGURATION, handler.getOperandType());
+    }
+
+    @Test
+    void testHandleThrowsExceptionForIncorrectArg() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(INCORRECT_LOCATION_MAP);
+
+        IllegalStateException exception = 
assertThrows(IllegalStateException.class, () -> handler.handle(operation));
+
+        assertEquals("Could not get flow id from the provided URL", 
exception.getMessage());

Review Comment:
   Recommend adjusting this check to avoid testing for the exact exception 
message.



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandlerTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.c2.client.service.operation;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+import static 
org.apache.nifi.c2.client.service.operation.UpdateConfigurationOperationHandler.LOCATION;
+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.Mockito.when;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.FlowIdHolder;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.protocol.api.C2OperationState;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UpdateConfigurationOperationHandlerTest {
+
+    private static final String FLOW_ID = "flowId";
+    private static final String OPERATION_ID = "operationId";
+    private static final Map<String, String> CORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "/path/for/the/" + FLOW_ID);
+    private static final Map<String, String> INCORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "incorrect/location");
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private FlowIdHolder flowIdHolder;
+
+    @Test
+    void testUpdateConfigurationOperationHandlerCreateSuccess() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+
+        assertEquals(OperationType.UPDATE, handler.getOperationType());
+        assertEquals(OperandType.CONFIGURATION, handler.getOperandType());
+    }
+
+    @Test
+    void testHandleThrowsExceptionForIncorrectArg() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(INCORRECT_LOCATION_MAP);
+
+        IllegalStateException exception = 
assertThrows(IllegalStateException.class, () -> handler.handle(operation));
+
+        assertEquals("Could not get flow id from the provided URL", 
exception.getMessage());
+    }
+
+    @Test
+    void testHandleReturnsNotAppliedWithNoContent() {
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");

Review Comment:
   ```suggestion
           when(flowIdHolder.getFlowId()).thenReturn(FLOW_ID);
   ```



##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandlerTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.c2.client.service.operation;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+import static 
org.apache.nifi.c2.client.service.operation.UpdateConfigurationOperationHandler.LOCATION;
+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.Mockito.when;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.client.service.FlowIdHolder;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.protocol.api.C2OperationState;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UpdateConfigurationOperationHandlerTest {
+
+    private static final String FLOW_ID = "flowId";
+    private static final String OPERATION_ID = "operationId";
+    private static final Map<String, String> CORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "/path/for/the/" + FLOW_ID);
+    private static final Map<String, String> INCORRECT_LOCATION_MAP = 
Collections.singletonMap(LOCATION, "incorrect/location");
+
+    @Mock
+    private C2Client client;
+
+    @Mock
+    private FlowIdHolder flowIdHolder;
+
+    @Test
+    void testUpdateConfigurationOperationHandlerCreateSuccess() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+
+        assertEquals(OperationType.UPDATE, handler.getOperationType());
+        assertEquals(OperandType.CONFIGURATION, handler.getOperandType());
+    }
+
+    @Test
+    void testHandleThrowsExceptionForIncorrectArg() {
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(null, null, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(INCORRECT_LOCATION_MAP);
+
+        IllegalStateException exception = 
assertThrows(IllegalStateException.class, () -> handler.handle(operation));
+
+        assertEquals("Could not get flow id from the provided URL", 
exception.getMessage());
+    }
+
+    @Test
+    void testHandleReturnsNotAppliedWithNoContent() {
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");
+        when(client.retrieveUpdateContent(any())).thenReturn(Optional.empty());
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(client, flowIdHolder, null);
+        C2Operation operation = new C2Operation();
+        operation.setArgs(CORRECT_LOCATION_MAP);
+
+        C2OperationAck response = handler.handle(operation);
+
+        assertEquals(EMPTY, response.getOperationId());
+        assertEquals(C2OperationState.OperationState.NOT_APPLIED, 
response.getOperationState().getState());
+    }
+
+    @Test
+    void testHandleReturnsNotAppliedWithContentApplyIssues() {
+        Function<byte[], Boolean> failedToUpdate = x -> false;
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");
+        
when(client.retrieveUpdateContent(any())).thenReturn(Optional.of("content".getBytes()));
+        UpdateConfigurationOperationHandler handler = new 
UpdateConfigurationOperationHandler(client, flowIdHolder, failedToUpdate);
+        C2Operation operation = new C2Operation();
+        operation.setIdentifier(OPERATION_ID);
+        operation.setArgs(CORRECT_LOCATION_MAP);
+
+        C2OperationAck response = handler.handle(operation);
+
+        assertEquals(OPERATION_ID, response.getOperationId());
+        assertEquals(C2OperationState.OperationState.NOT_APPLIED, 
response.getOperationState().getState());
+    }
+
+    @Test
+    void testHandleReturnsFullyApplied() {
+        Function<byte[], Boolean> successUpdate = x -> true;
+        when(flowIdHolder.getFlowId()).thenReturn("dummy");

Review Comment:
   This reference and others could be replaced with a static `FLOW_ID`:
   ```suggestion
           when(flowIdHolder.getFlowId()).thenReturn(FLOW_ID);
   ```



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