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


##########
minifi/minifi-commons/minifi-utils/src/main/java/org/apache/nifi/minifi/commons/status/util/PropertyUtil.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.status.util;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class PropertyUtil {
+    private static final String DOT = ".";
+    private static final String UNDERSCORE = "_";
+    private static final String HYPHEN = "-";
+
+    private PropertyUtil() {
+
+    }
+
+    public static String resolvePropertyValue(String name, Map<?, ?> 
properties) {
+        return Optional.of(name)
+            .map(n -> getPropertyValue(n, properties))
+            .or(() -> Optional.of(name.toUpperCase())

Review Comment:
   There is a lot happening here, wouldn't it be cleaner to do uppercase for 
both name and the properties keys? I know  it would mean extra iteration over 
properties but would make the code cleaner in my opinion. What do you think?



##########
minifi/minifi-commons/minifi-utils/src/main/java/org/apache/nifi/minifi/commons/status/util/PropertyUtil.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.status.util;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class PropertyUtil {
+    private static final String DOT = ".";
+    private static final String UNDERSCORE = "_";
+    private static final String HYPHEN = "-";
+
+    private PropertyUtil() {
+
+    }
+
+    public static String resolvePropertyValue(String name, Map<?, ?> 
properties) {
+        return Optional.of(name)
+            .map(n -> getPropertyValue(n, properties))
+            .or(() -> Optional.of(name.toUpperCase())
+                .filter(uppercasedName -> !name.equals(uppercasedName))
+                .map(n -> getPropertyValue(n, properties)))
+            .orElse(null);
+    }
+
+    private static String getPropertyValue(String name, Map<?, ?> properties) {
+        return Stream.concat(
+                Stream.of(name).filter(properties::containsKey),
+                Stream.of(name.replace(DOT, UNDERSCORE), name.replace(HYPHEN, 
UNDERSCORE), name.replace(DOT, UNDERSCORE).replace(HYPHEN, UNDERSCORE))
+                    .distinct()
+                    .filter(candidate -> !name.equals(candidate) && 
properties.containsKey(candidate)))
+            .findFirst()
+            .map(properties::get)
+            .map(String::valueOf)
+            .orElse(null);

Review Comment:
   You could keep it as optional here as well and flatmap on the caller side as 
needed.



##########
minifi/minifi-commons/minifi-utils/src/main/java/org/apache/nifi/minifi/commons/status/util/PropertyUtil.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.status.util;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class PropertyUtil {
+    private static final String DOT = ".";
+    private static final String UNDERSCORE = "_";
+    private static final String HYPHEN = "-";
+
+    private PropertyUtil() {
+
+    }
+
+    public static String resolvePropertyValue(String name, Map<?, ?> 
properties) {
+        return Optional.of(name)
+            .map(n -> getPropertyValue(n, properties))
+            .or(() -> Optional.of(name.toUpperCase())
+                .filter(uppercasedName -> !name.equals(uppercasedName))
+                .map(n -> getPropertyValue(n, properties)))
+            .orElse(null);
+    }
+
+    private static String getPropertyValue(String name, Map<?, ?> properties) {
+        return Stream.concat(
+                Stream.of(name).filter(properties::containsKey),
+                Stream.of(name.replace(DOT, UNDERSCORE), name.replace(HYPHEN, 
UNDERSCORE), name.replace(DOT, UNDERSCORE).replace(HYPHEN, UNDERSCORE))
+                    .distinct()
+                    .filter(candidate -> !name.equals(candidate) && 
properties.containsKey(candidate)))

Review Comment:
   Just out of curiosity isn't this a problem when the properties key contains 
dot?



##########
minifi/minifi-commons/minifi-utils/src/main/java/org/apache/nifi/minifi/commons/status/util/PropertyUtil.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.status.util;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class PropertyUtil {
+    private static final String DOT = ".";
+    private static final String UNDERSCORE = "_";
+    private static final String HYPHEN = "-";
+
+    private PropertyUtil() {
+
+    }
+
+    public static String resolvePropertyValue(String name, Map<?, ?> 
properties) {
+        return Optional.of(name)
+            .map(n -> getPropertyValue(n, properties))
+            .or(() -> Optional.of(name.toUpperCase())
+                .filter(uppercasedName -> !name.equals(uppercasedName))
+                .map(n -> getPropertyValue(n, properties)))
+            .orElse(null);
+    }
+
+    private static String getPropertyValue(String name, Map<?, ?> properties) {
+        return Stream.concat(
+                Stream.of(name).filter(properties::containsKey),
+                Stream.of(name.replace(DOT, UNDERSCORE), name.replace(HYPHEN, 
UNDERSCORE), name.replace(DOT, UNDERSCORE).replace(HYPHEN, UNDERSCORE))

Review Comment:
   Can we create a namePermutations or similarly named function? You could also 
add the unchanged one from the line above so you would have only one stream and 
extract the properties.containsKey "filter" from each. It would make the code 
cleaner in my opinion. What do you think?



##########
minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-runtime/src/main/java/org/apache/nifi/minifi/util/MiNiFiProperties.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.util;
+
+import static 
org.apache.nifi.minifi.commons.status.util.PropertyUtil.resolvePropertyValue;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.nifi.util.NiFiProperties;
+
+/**
+ * Extends NiFi properties functionality with System and Environment property 
override possibility. The property resolution also works with
+ * dots and hyphens that are not supported in some shells.
+ */
+public class MiNiFiProperties extends NiFiProperties {

Review Comment:
   We also have a MiNiFiProperties under commons/api, can we distinguish 
somehow?



##########
minifi/minifi-commons/minifi-utils/src/main/java/org/apache/nifi/minifi/commons/status/util/PropertyUtil.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.status.util;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class PropertyUtil {
+    private static final String DOT = ".";
+    private static final String UNDERSCORE = "_";
+    private static final String HYPHEN = "-";
+
+    private PropertyUtil() {
+
+    }
+
+    public static String resolvePropertyValue(String name, Map<?, ?> 
properties) {
+        return Optional.of(name)
+            .map(n -> getPropertyValue(n, properties))
+            .or(() -> Optional.of(name.toUpperCase())
+                .filter(uppercasedName -> !name.equals(uppercasedName))
+                .map(n -> getPropertyValue(n, properties)))
+            .orElse(null);

Review Comment:
   You could just return the optional (as I see it is used as optional on the 
caller side anyway) so we wouldn't play around with nulls.



##########
minifi/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/util/PropertyUtilTest.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.commons.status.util;
+
+import static 
org.apache.nifi.minifi.commons.status.util.PropertyUtil.resolvePropertyValue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+class PropertyUtilTest {
+
+    private static final String PROPERTY_KEY_WITH_DOTS = "property.foo.bar";
+    private static final String PROPERTY_KEY_WITH_HYPHENS = "property-foo-bar";
+    private static final String PROPERTY_KEY_WITH_DOTS_AND_HYPHENS = 
"property.foo-bar";
+    private static final String PROPERTY_KEY_WITH_UNDERSCORE = 
"property_foo_bar";
+    private static final String VALUE = "value";
+
+    @Test
+    void 
testResolveParameterValueReturnsNullWhenPropertiesDoesNotContainTheKey() {
+        assertNull(resolvePropertyValue(PROPERTY_KEY_WITH_DOTS, Map.of()));
+    }
+
+    @Test

Review Comment:
   You can control the test name for the parameters I think as well also with 
descriptive parameter names it would be self explanatory + for the 
parameetrized tests



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