bejancsaba commented on code in PR #6434:
URL: https://github.com/apache/nifi/pull/6434#discussion_r984842572


##########
c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/DebugOperationHandlerTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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 java.nio.charset.StandardCharsets.UTF_8;
+import static java.nio.file.Files.write;
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+import static java.util.Collections.singletonMap;
+import static java.util.function.Function.identity;
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toMap;
+import static 
org.apache.nifi.c2.client.service.operation.DebugOperationHandler.NEW_LINE;
+import static 
org.apache.nifi.c2.client.service.operation.DebugOperationHandler.TARGET_ARG;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.FULLY_APPLIED;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.NOT_APPLIED;
+import static org.apache.nifi.c2.protocol.api.OperandType.DEBUG;
+import static org.apache.nifi.c2.protocol.api.OperationType.TRANSFER;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UncheckedIOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Stream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.nifi.c2.client.api.C2Client;
+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.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class DebugOperationHandlerTest {
+
+    private static final String OPERATION_ID = "operationId";
+    private static final String C2_DEBUG_UPLOAD_ENDPOINT = 
"https://host/c2/api/upload";;
+    private static final String DEFAULT_FILE_CONTENT = "some_textual_data";
+    private static final List<Path> VALID_BUNDLE_FILE_LIST = 
singletonList(Paths.get("path_to_file"));
+    private static final Predicate<String> DEFAULT_CONTENT_FILTER = text -> 
true;
+
+    @Mock
+    private C2Client c2Client;
+
+    @TempDir
+    private File tempDir;
+
+    private static Stream<Arguments> invalidConstructorArguments() {
+        C2Client mockC2Client = mock(C2Client.class);
+        return Stream.of(
+            Arguments.of(null, null, null),
+            Arguments.of(null, VALID_BUNDLE_FILE_LIST, DEFAULT_CONTENT_FILTER),
+            Arguments.of(mockC2Client, null, DEFAULT_CONTENT_FILTER),
+            Arguments.of(mockC2Client, emptyList(), DEFAULT_CONTENT_FILTER),
+            Arguments.of(mockC2Client, VALID_BUNDLE_FILE_LIST, null)
+        );
+    }
+
+    @ParameterizedTest
+    @MethodSource("invalidConstructorArguments")
+    public void 
testAttemptingCreateWithInvalidParametersWillThrowException(C2Client c2Client, 
List<Path> bundleFilePaths, Predicate<String> contentFilter) {
+        assertThrows(IllegalArgumentException.class, () -> 
DebugOperationHandler.create(c2Client, bundleFilePaths, contentFilter));
+    }
+
+    @Test
+    public void testOperationAndOperandTypesAreMatching() {
+        // given
+        DebugOperationHandler testHandler = 
DebugOperationHandler.create(c2Client, VALID_BUNDLE_FILE_LIST, 
DEFAULT_CONTENT_FILTER);
+
+        // when + then
+        assertEquals(TRANSFER, testHandler.getOperationType());
+        assertEquals(DEBUG, testHandler.getOperandType());
+    }
+
+    @Test
+    public void testC2CallbackUrlIsNullInArgs() {
+        // given
+        DebugOperationHandler testHandler = 
DebugOperationHandler.create(c2Client, VALID_BUNDLE_FILE_LIST, 
DEFAULT_CONTENT_FILTER);
+        C2Operation c2Operation = operation(null);
+
+        // when
+        C2OperationAck result = testHandler.handle(c2Operation);
+
+        // then
+        assertEquals(OPERATION_ID, result.getOperationId());
+        assertEquals(NOT_APPLIED, result.getOperationState().getState());
+    }
+
+    @Test
+    public void testFilesAreCollectedAndUploadedAsATarGzBundle() {
+        // given
+        Map<String, String> bundleFileNamesWithContents = asList("file.log", 
"application.conf", "default.properties")
+            .stream()
+            .collect(toMap(identity(), __ -> DEFAULT_FILE_CONTENT));
+        List<Path> createBundleFiles = 
bundleFileNamesWithContents.entrySet().stream()
+            .map(entry -> placeFileWithContent(entry.getKey(), 
entry.getValue()))
+            .collect(toList());
+        DebugOperationHandler testHandler = 
DebugOperationHandler.create(c2Client, createBundleFiles, 
DEFAULT_CONTENT_FILTER);
+        C2Operation c2Operation = operation(C2_DEBUG_UPLOAD_ENDPOINT);
+
+        // when
+        C2OperationAck result = testHandler.handle(c2Operation);
+
+        // then
+        ArgumentCaptor<String> uploadUrlCaptor = 
ArgumentCaptor.forClass(String.class);
+        ArgumentCaptor<byte[]> uploadBundleCaptor = 
ArgumentCaptor.forClass(byte[].class);
+        verify(c2Client).uploadDebugBundle(uploadUrlCaptor.capture(), 
uploadBundleCaptor.capture());
+        assertEquals(OPERATION_ID, result.getOperationId());
+        assertEquals(FULLY_APPLIED, result.getOperationState().getState());
+        assertEquals(C2_DEBUG_UPLOAD_ENDPOINT, uploadUrlCaptor.getValue());
+        Map<String, String> resultBundle = 
extractBundle(uploadBundleCaptor.getValue());
+        assertTrue(mapEqual(bundleFileNamesWithContents, resultBundle));
+    }
+
+    @Test
+    public void testFileToCollectDoesNotExist() {
+        // given
+        DebugOperationHandler testHandler = 
DebugOperationHandler.create(c2Client, 
singletonList(Paths.get(tempDir.getAbsolutePath(), "missing_file")), 
DEFAULT_CONTENT_FILTER);
+        C2Operation c2Operation = operation(C2_DEBUG_UPLOAD_ENDPOINT);
+
+        // when
+        C2OperationAck result = testHandler.handle(c2Operation);
+
+        // then
+        assertEquals(OPERATION_ID, result.getOperationId());
+        assertEquals(NOT_APPLIED, result.getOperationState().getState());
+    }
+
+    @Test
+    public void testContentIsFilteredOut() {
+        // given
+        String filterKeyword = "minifi";
+        String bundleFileName = "filter_content.file";
+        String fileContent = Stream.of("line one", "line two " + 
filterKeyword, filterKeyword + "line three", "line 
four").collect(joining(NEW_LINE));

Review Comment:
   Thanks for extending it



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