File name calculation refactoring 40/91840/1
authorPiotr Darosz <piotr.darosz@nokia.com>
Tue, 23 Jul 2019 06:00:04 +0000 (08:00 +0200)
committerPiotr Darosz <piotr.darosz@nokia.com>
Tue, 23 Jul 2019 06:00:43 +0000 (08:00 +0200)
FSConfigurationSource file name calculation refactoring and tests

Change-Id: Idf1c45f860e12c14cfe88417500f2169b5dc86f7
Issue-ID: SDC-2474
Signed-off-by: Piotr Darosz <piotr.darosz@nokia.com>
common-app-api/src/main/java/org/openecomp/sdc/common/impl/FSConfigurationSource.java
common-app-api/src/test/java/org/openecomp/sdc/common/impl/FSConfigurationSourceTest.java [new file with mode: 0644]

index 3f2f6a7..3aa220c 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
+ * Modifications copyright (c) 2019 Nokia
+ * ================================================================================
  */
 
 package org.openecomp.sdc.common.impl;
 
+import java.util.Arrays;
+import java.util.stream.Collectors;
 import org.openecomp.sdc.common.api.ConfigurationListener;
 import org.openecomp.sdc.common.api.ConfigurationSource;
 import org.openecomp.sdc.common.api.Constants;
@@ -32,10 +36,10 @@ import org.openecomp.sdc.common.util.YamlToObjectConverter;
  */
 public class FSConfigurationSource implements ConfigurationSource {
 
-    private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
+    private final YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
 
-    private ConfigFileChangeListener changeListener = null;
-    private String appConfigDir = null;
+    private final ConfigFileChangeListener changeListener;
+    private final String appConfigDir;
 
     public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
         super();
@@ -81,34 +85,15 @@ public class FSConfigurationSource implements ConfigurationSource {
      * @param className
      * @return file name based on the class name
      */
-    private static <T> String calculateFileName(Class<T> className) {
-
+    static <T> String calculateFileName(Class<T> className) {
         String[] words = className.getSimpleName().split("(?=\\p{Upper})");
 
-        StringBuilder builder = new StringBuilder();
-
-        // There cannot be a null value returned from "split" - words != null is
-        // redundant
-        // if (words != null) {
-        boolean isFirst = true;
-        for (int i = 0; i < words.length; i++) {
-
-            String word = words[i];
-            if (word != null && !word.isEmpty()) {
-                if (!isFirst) {
-                    builder.append("-");
-                } else {
-                    isFirst = false;
-                }
-                builder.append(words[i].toLowerCase());
-            }
-        }
-        return builder.toString() + Constants.YAML_SUFFIX;
-
-        /*
-         * } else { return className.getSimpleName().toLowerCase() + Constants.YAML_SUFFIX; }
-         */
-
+        return Arrays.stream(words)
+            .map(String::toLowerCase)
+            .collect(Collectors.collectingAndThen(
+                Collectors.joining("-"),
+                str -> str + Constants.YAML_SUFFIX)
+            );
     }
 
 }
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/impl/FSConfigurationSourceTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/impl/FSConfigurationSourceTest.java
new file mode 100644 (file)
index 0000000..b9116bb
--- /dev/null
@@ -0,0 +1,61 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.common.impl;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.openecomp.sdc.be.config.Configuration;
+import org.openecomp.sdc.be.config.ErrorConfiguration;
+import org.openecomp.sdc.be.config.Neo4jErrorsConfiguration;
+import org.openecomp.sdc.common.api.Constants;
+
+public class FSConfigurationSourceTest {
+    @Test
+    public void calculateFileNameWhenSplitRequired() {
+        Class<ErrorConfiguration> clazz = ErrorConfiguration.class;
+
+        String expected = "error-configuration" + Constants.YAML_SUFFIX;
+        String actual = FSConfigurationSource.calculateFileName(clazz);
+
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void calculateFileNameWhenNoSplitRequired() {
+        Class<Configuration> clazz = Configuration.class;
+
+        String expected = "configuration" + Constants.YAML_SUFFIX;
+        String actual = FSConfigurationSource.calculateFileName(clazz);
+
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void calculateFileNameWithCamelCaseAndDigits() {
+        Class<Neo4jErrorsConfiguration> clazz = Neo4jErrorsConfiguration.class;
+
+        String expected = "neo4j-errors-configuration" + Constants.YAML_SUFFIX;
+        String actual = FSConfigurationSource.calculateFileName(clazz);
+
+        assertEquals(expected, actual);
+    }
+}
\ No newline at end of file