[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6149: NIFI-9908 C2 refactor and test coverage

2022-06-24 Thread GitBox


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 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() {
+

[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6149: NIFI-9908 C2 refactor and test coverage

2022-06-24 Thread GitBox


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


##
c2/c2-client-bundle/c2-client-http/src/test/java/org/apache/nifi/c2/client/http/C2HttpClientTest.java:
##
@@ -0,0 +1,158 @@
+/*
+ * 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 com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.containing;
+import static com.github.tomakehurst.wiremock.client.WireMock.matching;
+import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.verify;
+import static 
org.apache.nifi.c2.client.http.C2HttpClient.HTTP_STATUS_BAD_REQUEST;
+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.BDDMockito.given;
+
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
+import com.github.tomakehurst.wiremock.junit5.WireMockTest;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import org.apache.nifi.c2.client.C2ClientConfig;
+import org.apache.nifi.c2.client.api.C2Serializer;
+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.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;
+
+@WireMockTest
+@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;
+
+@Mock
+private C2ClientConfig c2ClientConfig;
+
+@Mock
+private C2Serializer serializer;
+
+@InjectMocks
+private C2HttpClient c2HttpClient;

Review Comment:
   Thanks for making the adjustments, injecting the mocked components is fine, 
and the change to use MockWebServer looks good.



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



[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6149: NIFI-9908 C2 refactor and test coverage

2022-06-24 Thread GitBox


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


##
c2/c2-client-bundle/c2-client-http/src/test/java/org/apache/nifi/c2/client/http/C2HttpClientTest.java:
##
@@ -0,0 +1,158 @@
+/*
+ * 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 com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.containing;
+import static com.github.tomakehurst.wiremock.client.WireMock.matching;
+import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.verify;
+import static 
org.apache.nifi.c2.client.http.C2HttpClient.HTTP_STATUS_BAD_REQUEST;
+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.BDDMockito.given;
+
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
+import com.github.tomakehurst.wiremock.junit5.WireMockTest;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import org.apache.nifi.c2.client.C2ClientConfig;
+import org.apache.nifi.c2.client.api.C2Serializer;
+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.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;
+
+@WireMockTest
+@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;
+
+@Mock
+private C2ClientConfig c2ClientConfig;
+
+@Mock
+private C2Serializer serializer;
+
+@InjectMocks
+private C2HttpClient c2HttpClient;
+
+@Test
+void shouldReturnC2HeartbeatResponse(WireMockRuntimeInfo wmRuntimeInfo) {
+// given

Review Comment:
   Thanks for making the adjustments.



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



[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6149: NIFI-9908 C2 refactor and test coverage

2022-06-24 Thread GitBox


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


##
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/C2HttpClient.java:
##
@@ -91,6 +92,45 @@ public Optional 
publishHeartbeat(C2Heartbeat heartbeat) {
 return serializer.serialize(heartbeat).flatMap(this::sendHeartbeat);
 }
 
+@Override
+public Optional retrieveUpdateContent(String flowUpdateUrl) {
+final Request.Builder requestBuilder = new Request.Builder()
+.get()
+.url(flowUpdateUrl);
+final Request request = requestBuilder.build();
+
+Optional body;
+try (Response response = 
httpClientReference.get().newCall(request).execute()) {
+int code = response.code();
+body = Optional.ofNullable(response.body());
+
+if (code >= HTTP_STATUS_BAD_REQUEST) {
+StringBuilder messageBuilder = new 
StringBuilder(String.format("Configuration retrieval failed: HTTP %d", code));
+body.map(Object::toString).ifPresent(messageBuilder::append);
+throw new C2ServerException(messageBuilder.toString());
+}
+
+if (!body.isPresent()) {
+logger.warn("No body returned when pulling a new 
configuration");
+return Optional.empty();
+}
+
+return Optional.of(body.get().bytes());
+} catch (Exception e) {
+logger.warn("Configuration retrieval failed", e);
+return Optional.empty();
+}
+}
+
+@Override
+public void acknowledgeOperation(C2OperationAck operationAck) {
+logger.info("Performing acknowledgement request to {} for operation 
{}", clientConfig.getC2AckUrl(), operationAck.getOperationId());

Review Comment:
   Thanks, that sounds good.



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



[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6149: NIFI-9908 C2 refactor and test coverage

2022-06-23 Thread GitBox


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


##
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/C2ServerException.java:
##
@@ -0,0 +1,26 @@
+/*
+ * 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 java.io.IOException;
+

Review Comment:
   It would be helpful to add a class-level comment describing the basic 
purpose of this exception class.



##
c2/c2-client-bundle/c2-client-http/pom.xml:
##
@@ -47,5 +47,32 @@ limitations under the License.
 com.squareup.okhttp3
 logging-interceptor
 
+
+
+org.junit.jupiter
+junit-jupiter-api
+test
+
+
+org.junit.jupiter
+junit-jupiter-engine
+test
+
+
+org.mockito
+mockito-core
+test
+
+
+org.mockito
+mockito-junit-jupiter
+test
+
+
+com.github.tomakehurst
+wiremock-jre8
+2.33.2
+test
+

Review Comment:
   Instead of introducing a new dependency on WireMock, and the Java 8 
dependency, recommend using OkHttp 
[MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver), 
which is already used for testing `InvokeHTTP` and `HttpNotificationService`, 
among other components.



##
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/C2HttpClient.java:
##
@@ -91,6 +92,45 @@ public Optional 
publishHeartbeat(C2Heartbeat heartbeat) {
 return serializer.serialize(heartbeat).flatMap(this::sendHeartbeat);
 }
 
+@Override
+public Optional retrieveUpdateContent(String flowUpdateUrl) {

Review Comment:
   Although not required, it is helpful to use the `final` keyword on method 
arguments.
   ```suggestion
   public Optional retrieveUpdateContent(final String 
flowUpdateUrl) {
   ```



##
c2/c2-client-bundle/c2-client-http/src/test/java/org/apache/nifi/c2/client/http/C2HttpClientTest.java:
##
@@ -0,0 +1,158 @@
+/*
+ * 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 com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.containing;
+import static com.github.tomakehurst.wiremock.client.WireMock.matching;
+import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.verify;
+import static 
org.apache.nifi.c2.client.http.C2HttpClient.HTTP_STATUS_BAD_REQUEST;
+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.BDDMockito.given;
+
+import com.github.tomakehurst.wiremock.client.WireMock;
+import