[GitHub] [nifi] ferencerdei commented on a diff in pull request #6160: NIFI-10166 improve MiNiFi bootstrap test coverage

2022-07-01 Thread GitBox


ferencerdei commented on code in PR #6160:
URL: https://github.com/apache/nifi/pull/6160#discussion_r911639888


##
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformer.java:
##
@@ -750,6 +756,72 @@ protected static void addTextElement(final Element 
element, final String name, f
 element.appendChild(toAdd);
 }
 
+public static ByteBuffer overrideNonFlowSectionsFromOriginalSchema(byte[] 
newSchema, ByteBuffer currentConfigScheme, Properties bootstrapProperties)
+throws IOException, InvalidConfigurationException, 
SchemaLoaderException {
+try {
+boolean overrideCoreProperties = 
ConfigTransformer.overrideCoreProperties(bootstrapProperties);
+boolean overrideSecurityProperties = 
ConfigTransformer.overrideSecurityProperties(bootstrapProperties);
+if (overrideCoreProperties && overrideSecurityProperties) {
+return ByteBuffer.wrap(newSchema);
+} else {
+ConvertableSchema schemaNew = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteArrayInputStream(newSchema)));
+ConfigSchema configSchemaNew = 
ConfigTransformer.throwIfInvalid(schemaNew.convert());
+ConvertableSchema schemaOld = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteBufferInputStream(currentConfigScheme)));
+ConfigSchema configSchemaOld = 
ConfigTransformer.throwIfInvalid(schemaOld.convert());
+
+
configSchemaNew.setNifiPropertiesOverrides(configSchemaOld.getNifiPropertiesOverrides());
+
+if (!overrideCoreProperties) {
+logger.debug("Preserving previous core properties...");
+
configSchemaNew.setCoreProperties(configSchemaOld.getCoreProperties());
+}
+
+if (!overrideSecurityProperties) {
+logger.debug("Preserving previous security properties...");
+
configSchemaNew.setSecurityProperties(configSchemaOld.getSecurityProperties());
+}
+
+StringWriter writer = new StringWriter();
+SchemaLoader.toYaml(configSchemaNew, writer);
+return 
ByteBuffer.wrap(writer.toString().getBytes()).asReadOnlyBuffer();
+}
+} catch (Exception e) {
+logger.error("Loading the old and the new schema for merging was 
not successful", e);
+throw e;
+}
+}
+
+private static boolean overrideSecurityProperties(Properties properties) {
+String overrideSecurityProperties = (String) 
properties.getOrDefault(OVERRIDE_SECURITY, "false");
+boolean overrideSecurity;
+if ("true".equalsIgnoreCase(overrideSecurityProperties) || 
"false".equalsIgnoreCase(overrideSecurityProperties)) {
+overrideSecurity = 
Boolean.parseBoolean(overrideSecurityProperties);
+} else {
+throw new IllegalArgumentException(
+"Property, " + OVERRIDE_SECURITY + ", to specify whether to 
override security properties must either be a value boolean value (\"true\" or 
\"false\")" +
+" or left to the default value of \"false\". It is set to 
\"" + overrideSecurityProperties + "\".");
+}

Review Comment:
   no reason, it was a leftover from the existing code. Changing both to simple 
parseBoolean



##
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformer.java:
##
@@ -750,6 +756,72 @@ protected static void addTextElement(final Element 
element, final String name, f
 element.appendChild(toAdd);
 }
 
+public static ByteBuffer overrideNonFlowSectionsFromOriginalSchema(byte[] 
newSchema, ByteBuffer currentConfigScheme, Properties bootstrapProperties)
+throws IOException, InvalidConfigurationException, 
SchemaLoaderException {
+try {
+boolean overrideCoreProperties = 
ConfigTransformer.overrideCoreProperties(bootstrapProperties);
+boolean overrideSecurityProperties = 
ConfigTransformer.overrideSecurityProperties(bootstrapProperties);
+if (overrideCoreProperties && overrideSecurityProperties) {
+return ByteBuffer.wrap(newSchema);
+} else {
+ConvertableSchema schemaNew = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteArrayInputStream(newSchema)));
+ConfigSchema configSchemaNew = 
ConfigTransformer.throwIfInvalid(schemaNew.convert());
+ConvertableSchema schemaOld = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteBufferInputStream(currentConfigScheme)));
+ConfigSchema configSchemaOld = 

[GitHub] [nifi] ferencerdei commented on a diff in pull request #6160: NIFI-10166 improve MiNiFi bootstrap test coverage

2022-07-01 Thread GitBox


ferencerdei commented on code in PR #6160:
URL: https://github.com/apache/nifi/pull/6160#discussion_r911653320


##
minifi/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/command/DumpRunnerTest.java:
##
@@ -0,0 +1,151 @@
+/*
+ * 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.bootstrap.command;
+
+import static org.apache.nifi.minifi.bootstrap.RunMiNiFi.CMD_LOGGER;
+import static org.apache.nifi.minifi.bootstrap.Status.ERROR;
+import static org.apache.nifi.minifi.bootstrap.Status.MINIFI_NOT_RUNNING;
+import static org.apache.nifi.minifi.bootstrap.Status.OK;
+import static org.apache.nifi.minifi.bootstrap.command.DumpRunner.DUMP_CMD;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Optional;
+import org.apache.nifi.minifi.bootstrap.TestLogAppender;
+import org.apache.nifi.minifi.bootstrap.service.CurrentPortProvider;
+import org.apache.nifi.minifi.bootstrap.service.MiNiFiCommandSender;
+import org.junit.jupiter.api.BeforeAll;
+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)
+class DumpRunnerTest {
+
+private static final int MINIFI_PORT = 1337;
+private static final String DUMP_CONTENT = "dump_content";
+private static final TestLogAppender LOG_APPENDER = new TestLogAppender();
+
+@Mock
+private MiNiFiCommandSender miNiFiCommandSender;
+@Mock
+private CurrentPortProvider currentPortProvider;
+
+@InjectMocks
+private DumpRunner dumpRunner;
+
+@BeforeAll
+static void setupAll() {
+((ch.qos.logback.classic.Logger) CMD_LOGGER).addAppender(LOG_APPENDER);
+LOG_APPENDER.start();
+}
+
+@BeforeEach
+void setup() {
+LOG_APPENDER.reset();
+}
+
+@Test
+void testRunCommandShouldDumpToConsoleIfNoFileDefined() throws IOException 
{
+when(currentPortProvider.getCurrentPort()).thenReturn(MINIFI_PORT);
+when(miNiFiCommandSender.sendCommand(DUMP_CMD, 
MINIFI_PORT)).thenReturn(Optional.of(DUMP_CONTENT));
+
+int statusCode = dumpRunner.runCommand(new String[0]);
+
+assertEquals(OK.getStatusCode(), statusCode);
+assertEquals(DUMP_CONTENT, 
LOG_APPENDER.getLastLoggedEvent().getMessage());
+verifyNoMoreInteractions(currentPortProvider, miNiFiCommandSender);
+}
+
+@Test
+void testRunCommandShouldDumpToFileIfItIsDefined() throws IOException {
+when(currentPortProvider.getCurrentPort()).thenReturn(MINIFI_PORT);
+when(miNiFiCommandSender.sendCommand(DUMP_CMD, 
MINIFI_PORT)).thenReturn(Optional.of(DUMP_CONTENT));
+File file = Files.createTempFile(null, null).toFile();
+file.deleteOnExit();
+String tmpFilePath = file.getAbsolutePath();
+
+int statusCode = dumpRunner.runCommand(new  String[] {DUMP_CMD, 
tmpFilePath});
+
+assertEquals(OK.getStatusCode(), statusCode);
+assertEquals(DUMP_CONTENT, getDumpContent(file));
+assertEquals("Successfully wrote thread dump to " + tmpFilePath, 
LOG_APPENDER.getLastLoggedEvent().getFormattedMessage());

Review Comment:
   done.



-- 
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] ferencerdei commented on a diff in pull request #6160: NIFI-10166 improve MiNiFi bootstrap test coverage

2022-07-01 Thread GitBox


ferencerdei commented on code in PR #6160:
URL: https://github.com/apache/nifi/pull/6160#discussion_r911779208


##
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java:
##
@@ -234,8 +218,9 @@ public void run() {
 .build();
 
 final Request.Builder requestBuilder = new Request.Builder()
-.get()
-.url(url);
+.get()
+.cacheControl(CacheControl.FORCE_NETWORK)

Review Comment:
   no, removed it.



##
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformer.java:
##
@@ -750,6 +756,72 @@ protected static void addTextElement(final Element 
element, final String name, f
 element.appendChild(toAdd);
 }
 
+public static ByteBuffer overrideNonFlowSectionsFromOriginalSchema(byte[] 
newSchema, ByteBuffer currentConfigScheme, Properties bootstrapProperties)
+throws IOException, InvalidConfigurationException, 
SchemaLoaderException {
+try {
+boolean overrideCoreProperties = 
ConfigTransformer.overrideCoreProperties(bootstrapProperties);
+boolean overrideSecurityProperties = 
ConfigTransformer.overrideSecurityProperties(bootstrapProperties);
+if (overrideCoreProperties && overrideSecurityProperties) {
+return ByteBuffer.wrap(newSchema);
+} else {
+ConvertableSchema schemaNew = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteArrayInputStream(newSchema)));
+ConfigSchema configSchemaNew = 
ConfigTransformer.throwIfInvalid(schemaNew.convert());
+ConvertableSchema schemaOld = ConfigTransformer
+
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteBufferInputStream(currentConfigScheme)));
+ConfigSchema configSchemaOld = 
ConfigTransformer.throwIfInvalid(schemaOld.convert());
+
+
configSchemaNew.setNifiPropertiesOverrides(configSchemaOld.getNifiPropertiesOverrides());
+
+if (!overrideCoreProperties) {
+logger.debug("Preserving previous core properties...");
+
configSchemaNew.setCoreProperties(configSchemaOld.getCoreProperties());
+}
+
+if (!overrideSecurityProperties) {
+logger.debug("Preserving previous security properties...");
+
configSchemaNew.setSecurityProperties(configSchemaOld.getSecurityProperties());
+}
+
+StringWriter writer = new StringWriter();
+SchemaLoader.toYaml(configSchemaNew, writer);
+return 
ByteBuffer.wrap(writer.toString().getBytes()).asReadOnlyBuffer();
+}
+} catch (Exception e) {
+logger.error("Loading the old and the new schema for merging was 
not successful", e);
+throw e;

Review Comment:
   Wrapped it now.



##
minifi/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/command/CommandRunnerFactoryTest.java:
##
@@ -0,0 +1,155 @@
+/*
+ * 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.bootstrap.command;
+
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.DUMP;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.ENV;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.FLOWSTATUS;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.RESTART;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.RUN;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.START;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.STATUS;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.STOP;
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.UNKNOWN;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+import java.io.File;
+import 

[GitHub] [nifi] ferencerdei commented on a diff in pull request #6160: NIFI-10166 improve MiNiFi bootstrap test coverage

2022-07-01 Thread GitBox


ferencerdei commented on code in PR #6160:
URL: https://github.com/apache/nifi/pull/6160#discussion_r911657359


##
minifi/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/service/BootstrapCodecTest.java:
##
@@ -0,0 +1,189 @@
+/*
+ * 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.bootstrap.service;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
+import org.apache.nifi.minifi.bootstrap.RunMiNiFi;
+import 
org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeCoordinator;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class BootstrapCodecTest {
+
+private static final int VALID_PORT = 1;
+private static final String SECRET = "secret";
+private static final String OK = "OK";
+private static final String EMPTY_STRING = "";
+private RunMiNiFi runner;
+
+@BeforeEach
+void setup() {
+runner = mock(RunMiNiFi.class);
+}
+
+@Test
+void testCommunicateShouldThrowIOExceptionIfThereIsNoCommand() {
+InputStream inputStream = new ByteArrayInputStream(new byte[0]);
+ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+BootstrapCodec bootstrapCodec = new BootstrapCodec(runner, 
inputStream, outputStream);
+
+IOException expectedException = assertThrows(IOException.class, 
bootstrapCodec::communicate);
+assertEquals("Received invalid command from MiNiFi: null", 
expectedException.getMessage());

Review Comment:
   removed



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