Merge "Fix file stream closing"
authorTian Lee <TianL@amdocs.com>
Tue, 26 Jun 2018 15:03:42 +0000 (15:03 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 26 Jun 2018 15:03:42 +0000 (15:03 +0000)
src/main/java/org/onap/aai/datarouter/util/DataRouterProperties.java
src/main/java/org/onap/aai/datarouter/util/SearchServiceAgent.java
src/test/java/org/onap/aai/datarouter/util/DataRouterPropertiesTest.java
src/test/resources/data-router.properties [new file with mode: 0644]

index 2b17020..36e4a10 100644 (file)
@@ -9,7 +9,7 @@
  * 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
+ * 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,
@@ -24,31 +24,35 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Properties;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class DataRouterProperties {
 
-  private static final Logger logger = LoggerFactory.getLogger(DataRouterProperties.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(DataRouterProperties.class);
+
+    private static Properties properties;
 
-  private static Properties properties;
+    static {
+        File file = new File(DataRouterConstants.DR_CONFIG_FILE);
+        loadProperties(file);
+    }
 
-  static {
-    properties = new Properties();
-    File file = new File(DataRouterConstants.DR_CONFIG_FILE);
-    try {
-      properties.load(new FileInputStream(file));
-    } catch (FileNotFoundException e) {
-      logger.error("FileNotFoundException: ", e);
-    } catch (IOException e) {
-      logger.error("IOException: ", e);
+    static void loadProperties(File file) {
+        properties = new Properties();
+        try (InputStream props = new FileInputStream(file)) {
+            properties.load(props);
+        } catch (FileNotFoundException e) {
+            LOGGER.error("FileNotFoundException: ", e);
+        } catch (IOException e) {
+            LOGGER.error("IOException: ", e);
+        }
     }
-  }
 
-  public static String get(String key) {
-    return properties.getProperty(key);
-  }
+    public static String get(String key) {
+        return properties.getProperty(key);
+    }
 
 }
index 42861b4..bbdb4c8 100644 (file)
@@ -320,8 +320,7 @@ public class SearchServiceAgent {
    * Removes a document from the Search Service.\r
    * \r
    * @param index   - The index to create the document in.\r
-   * @param id      - The identifier for the document.\r
-   * @param payload - The document contents.\r
+   * @param documentId      - The identifier for the document.\r
    * @param headers - HTTP headers.\r
    */\r
   public void deleteDocument(String index, String documentId, Map<String, List<String>> headers) {\r
@@ -340,12 +339,13 @@ public class SearchServiceAgent {
    */\r
   protected String loadFileData(String filename) throws Exception {\r
     StringBuilder data = new StringBuilder();\r
-    try {\r
-      BufferedReader in = new BufferedReader(new InputStreamReader(\r
-          EntityEventPolicy.class.getClassLoader().getResourceAsStream("/" + filename),\r
-          StandardCharsets.UTF_8));\r
-      String line;\r
 \r
+    try (InputStreamReader inputStreamReader = new InputStreamReader(EntityEventPolicy.class.getClassLoader()\r
+        .getResourceAsStream("/" + filename), StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(\r
+        inputStreamReader)\r
+    ) {\r
+\r
+      String line;\r
       while ((line = in.readLine()) != null) {\r
         data.append(line);\r
       }\r
index 024a23d..51505a9 100644 (file)
@@ -3,13 +3,13 @@
  * org.onap.aai\r
  * ================================================================================\r
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
- * Copyright © 2017-2018 Amdocs\r
+ * Copyright © 2017-2018 Nokia\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
  *\r
- *       http://www.apache.org/licenses/LICENSE-2.0\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
  *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  */\r
 package org.onap.aai.datarouter.util;\r
 \r
+import java.io.File;\r
+import java.net.URISyntaxException;\r
+import org.junit.Assert;\r
 import org.junit.Test;\r
 \r
-import static org.junit.Assert.*;\r
-\r
+/**\r
+ * @author Bogumil Zebek\r
+ */\r
 public class DataRouterPropertiesTest {\r
 \r
     @Test\r
-    public void testGet(){\r
-        DataRouterProperties.get("key");\r
+    public void shouldLoadDataRouterPropertiesProperlyWhenSpecifiedFileExists() throws URISyntaxException {\r
+        // given\r
+        ClassLoader classLoader = getClass().getClassLoader();\r
+\r
+        File file = new File(\r
+            classLoader.getResource("data-router.properties").getFile()\r
+        );\r
+\r
+        // when\r
+        DataRouterProperties.loadProperties(file);\r
+\r
+        //then\r
+        Assert.assertEquals("value1", DataRouterProperties.get("key1"));\r
+        Assert.assertEquals("value2", DataRouterProperties.get("key2"));\r
+        Assert.assertNull(DataRouterProperties.get("nonExistingKey"));\r
+    }\r
+\r
+    @Test\r
+    public void shouldCreateEmptyDataRouterPropertiesContainerWhenSpecifiedFileDoesNotExist() {\r
+        // given\r
+        File nonExistingFile = new File("nonExistingFile.properties");\r
+\r
+        // when\r
+        DataRouterProperties.loadProperties(nonExistingFile);\r
+\r
+        // then\r
+        Assert.assertNull(DataRouterProperties.get("key"));\r
     }\r
 }
\ No newline at end of file
diff --git a/src/test/resources/data-router.properties b/src/test/resources/data-router.properties
new file mode 100644 (file)
index 0000000..f995a3b
--- /dev/null
@@ -0,0 +1,2 @@
+key1=value1
+key2=value2
\ No newline at end of file