fix for presence of component home environment variables in tests

Project: http://git-wip-us.apache.org/repos/asf/oodt/repo
Commit: http://git-wip-us.apache.org/repos/asf/oodt/commit/2fbd08f0
Tree: http://git-wip-us.apache.org/repos/asf/oodt/tree/2fbd08f0
Diff: http://git-wip-us.apache.org/repos/asf/oodt/diff/2fbd08f0

Branch: refs/heads/feature/zookeeper-config
Commit: 2fbd08f0d4aa9cced62f286ef7a1dd29536ace16
Parents: 22a1e59
Author: Imesha Sudasingha <imesha.sudasin...@gmail.com>
Authored: Fri Jul 14 21:23:54 2017 +0530
Committer: Imesha Sudasingha <imesha.sudasin...@gmail.com>
Committed: Fri Jul 14 21:23:54 2017 +0530

----------------------------------------------------------------------
 .../DistributedConfigurationManager.java        | 15 ++----
 .../config/distributed/utils/FilePathUtils.java | 53 ++++++++++++++++++++
 config/src/main/resources/etc/log4j.xml         |  2 +-
 .../DistributedConfigurationManagerTest.java    | 18 +++++--
 4 files changed, 71 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/oodt/blob/2fbd08f0/config/src/main/java/org/apache/oodt/config/distributed/DistributedConfigurationManager.java
----------------------------------------------------------------------
diff --git 
a/config/src/main/java/org/apache/oodt/config/distributed/DistributedConfigurationManager.java
 
b/config/src/main/java/org/apache/oodt/config/distributed/DistributedConfigurationManager.java
index 6572270..530416d 100644
--- 
a/config/src/main/java/org/apache/oodt/config/distributed/DistributedConfigurationManager.java
+++ 
b/config/src/main/java/org/apache/oodt/config/distributed/DistributedConfigurationManager.java
@@ -24,6 +24,7 @@ import org.apache.oodt.config.ConfigurationManager;
 import org.apache.oodt.config.Constants;
 import org.apache.oodt.config.Constants.Properties;
 import org.apache.oodt.config.distributed.utils.CuratorUtils;
+import org.apache.oodt.config.distributed.utils.FilePathUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -141,7 +142,7 @@ public class DistributedConfigurationManager extends 
ConfigurationManager {
             logger.info("Properties loaded from ZNode at : {}", 
propertiesFileZNodePath);
 
             String localFilePath = 
zNodePaths.getLocalPropertiesFilePath(propertiesFileZNodePath);
-            localFilePath = fixForComponentHome(localFilePath);
+            localFilePath = FilePathUtils.fixForComponentHome(component, 
localFilePath);
             FileUtils.writeByteArrayToFile(new File(localFilePath), bytes);
             logger.info("Properties file from ZNode at {} saved to {}", 
propertiesFileZNodePath, localFilePath);
         }
@@ -167,22 +168,12 @@ public class DistributedConfigurationManager extends 
ConfigurationManager {
             byte[] bytes = client.getData().forPath(configFileZNodePath);
 
             String localFilePath = 
zNodePaths.getLocalConfigFilePath(configFileZNodePath);
-            localFilePath = fixForComponentHome(localFilePath);
+            localFilePath = FilePathUtils.fixForComponentHome(component, 
localFilePath);
             FileUtils.writeByteArrayToFile(new File(localFilePath), bytes);
             logger.info("Config file from ZNode at {} saved to {}", 
configFileZNodePath, localFilePath);
         }
     }
 
-    private String fixForComponentHome(String suffixPath) {
-        String prefix = System.getenv().get(component.getHome());
-        StringBuilder path = new StringBuilder();
-        if (prefix != null) {
-            path.append(prefix.endsWith(SEPARATOR) ? prefix : prefix + 
SEPARATOR);
-        }
-        path.append(suffixPath.startsWith(SEPARATOR) ? suffixPath.substring(1) 
: suffixPath);
-        return path.toString();
-    }
-
     public Component getComponent() {
         return component;
     }

http://git-wip-us.apache.org/repos/asf/oodt/blob/2fbd08f0/config/src/main/java/org/apache/oodt/config/distributed/utils/FilePathUtils.java
----------------------------------------------------------------------
diff --git 
a/config/src/main/java/org/apache/oodt/config/distributed/utils/FilePathUtils.java
 
b/config/src/main/java/org/apache/oodt/config/distributed/utils/FilePathUtils.java
new file mode 100644
index 0000000..12fcff5
--- /dev/null
+++ 
b/config/src/main/java/org/apache/oodt/config/distributed/utils/FilePathUtils.java
@@ -0,0 +1,53 @@
+/*
+ * 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.oodt.config.distributed.utils;
+
+import org.apache.oodt.config.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.oodt.config.Constants.SEPARATOR;
+
+public class FilePathUtils {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(FilePathUtils.class);
+
+    private FilePathUtils() {
+    }
+
+    public static String fixForComponentHome(Component component, String 
suffixPath) {
+        String prefix = System.getenv().get(component.getHome());
+        StringBuilder path = new StringBuilder();
+        if (prefix != null) {
+            logger.debug("Found prefix {}:{} for suffixPath: {}", 
component.getHome(), prefix, suffixPath);
+            path.append(prefix.endsWith(SEPARATOR) ? prefix : prefix + 
SEPARATOR);
+        }
+        path.append(suffixPath.startsWith(SEPARATOR) ? suffixPath.substring(1) 
: suffixPath);
+        logger.debug("Fixed path for {} is {}", suffixPath, path.toString());
+        return path.toString();
+    }
+
+    public static String unfixForComponentHome(Component component, String 
path) {
+        String prefix = System.getenv().get(component.getHome());
+        if (prefix != null && path.startsWith(prefix)) {
+            return path.substring(prefix.length() + SEPARATOR.length());
+        }
+
+        return path;
+    }
+}

http://git-wip-us.apache.org/repos/asf/oodt/blob/2fbd08f0/config/src/main/resources/etc/log4j.xml
----------------------------------------------------------------------
diff --git a/config/src/main/resources/etc/log4j.xml 
b/config/src/main/resources/etc/log4j.xml
index 0421de2..1b3dbdf 100644
--- a/config/src/main/resources/etc/log4j.xml
+++ b/config/src/main/resources/etc/log4j.xml
@@ -27,7 +27,7 @@
     </appender>
 
     <root>
-        <priority value="INFO"/>
+        <priority value="DEBUG"/>
         <appender-ref ref="console"/>
     </root>
 

http://git-wip-us.apache.org/repos/asf/oodt/blob/2fbd08f0/config/src/test/java/org/apache/oodt/config/distributed/DistributedConfigurationManagerTest.java
----------------------------------------------------------------------
diff --git 
a/config/src/test/java/org/apache/oodt/config/distributed/DistributedConfigurationManagerTest.java
 
b/config/src/test/java/org/apache/oodt/config/distributed/DistributedConfigurationManagerTest.java
index 1d8c716..fde20f8 100644
--- 
a/config/src/test/java/org/apache/oodt/config/distributed/DistributedConfigurationManagerTest.java
+++ 
b/config/src/test/java/org/apache/oodt/config/distributed/DistributedConfigurationManagerTest.java
@@ -20,6 +20,7 @@ package org.apache.oodt.config.distributed;
 import org.apache.commons.io.FileUtils;
 import org.apache.oodt.config.ConfigurationManager;
 import 
org.apache.oodt.config.distributed.cli.DistributedConfigurationPublisher;
+import org.apache.oodt.config.distributed.utils.FilePathUtils;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
@@ -31,9 +32,11 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 import static org.apache.oodt.config.Constants.CONFIG_PUBLISHER_XML;
 import static org.apache.oodt.config.Constants.SEPARATOR;
@@ -85,7 +88,7 @@ public class DistributedConfigurationManagerTest extends 
AbstractDistributedConf
                     Assert.assertEquals(properties.getProperty(key), 
System.getProperty(key));
                 }
 
-                String fileName = entry.getValue();
+                String fileName = 
FilePathUtils.fixForComponentHome(publisher.getComponent(), entry.getValue());
                 fileName = fileName.startsWith(SEPARATOR) ? 
fileName.substring(1) : fileName;
                 File downloadedFile = new File(fileName);
                 Assert.assertTrue(downloadedFile.exists());
@@ -93,7 +96,7 @@ public class DistributedConfigurationManagerTest extends 
AbstractDistributedConf
 
             // Checking for configuration files
             for (Map.Entry<String, String> entry : 
publisher.getConfigFiles().entrySet()) {
-                String fileName = entry.getValue();
+                String fileName = 
FilePathUtils.fixForComponentHome(publisher.getComponent(), entry.getValue());
                 fileName = fileName.startsWith(SEPARATOR) ? 
fileName.substring(1) : fileName;
                 File file = new File(fileName);
                 Assert.assertTrue(file.exists());
@@ -106,10 +109,17 @@ public class DistributedConfigurationManagerTest extends 
AbstractDistributedConf
         for (DistributedConfigurationPublisher publisher : publishers) {
             publisher.destroy();
 
-            for (Map.Entry<String, String> entry : 
publisher.getConfigFiles().entrySet()) {
+            // deleting all locally created conf file directories
+            Set<Map.Entry<String, String>> files = new 
HashSet<>(publisher.getConfigFiles().entrySet());
+            files.addAll(publisher.getPropertiesFiles().entrySet());
+
+            for (Map.Entry<String, String> entry : files) {
                 String fileName = entry.getValue();
                 fileName = fileName.startsWith(SEPARATOR) ? 
fileName.substring(1) : fileName;
-                String confDir = fileName.split(SEPARATOR)[0];
+
+                String confDir = 
System.getenv(publisher.getComponent().getHome()) != null ?
+                        System.getenv(publisher.getComponent().getHome()) + 
SEPARATOR + fileName.split(SEPARATOR)[0] : fileName.split(SEPARATOR)[0];
+
                 File dir = new File(confDir);
                 FileUtils.deleteDirectory(dir);
             }

Reply via email to