Use try-with-resources 37/29337/1
authorPamela Dragosh <pdragosh@research.att.com>
Fri, 26 Jan 2018 18:32:21 +0000 (13:32 -0500)
committerPamela Dragosh <pdragosh@research.att.com>
Fri, 26 Jan 2018 18:32:48 +0000 (13:32 -0500)
Also enhanced JUnit tests to ensure this will work. Reduced code.

Issue-ID: POLICY-482
Change-Id: If07e17df274bdb709f7ca60078bd1fbd78d1aaaa
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java
POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java

index 1e6e3ef..e62c878 100644 (file)
@@ -25,7 +25,6 @@ import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -150,34 +149,22 @@ public class PolicyManagerServlet extends HttpServlet {
        protected static void initializeJSONLoad() {
                closedLoopJsonLocation = Paths.get(XACMLProperties
                                .getProperty(XACMLRestProperties.PROP_ADMIN_CLOSEDLOOP));
-               FileInputStream inputStream = null;
-               JsonReader jsonReader = null;
                String location = closedLoopJsonLocation.toString();
-               try {
-                       inputStream = new FileInputStream(location);
-                       if (location.endsWith("json")) {
-                               jsonReader = Json.createReader(inputStream);
-                               policyNames = jsonReader.readArray();
-                               serviceTypeNamesList = new ArrayList<>();
-                               for (int i = 0; i < policyNames.size(); i++) {
-                                       javax.json.JsonObject policyName = policyNames.getJsonObject(i);
-                                       String name = policyName.getJsonString("serviceTypePolicyName").getString();
-                                       serviceTypeNamesList.add(name);
-                               }
+               if (! location.endsWith("json")) {
+                       LOGGER.warn("JSONConfig file does not end with extension .json");
+                       return;
+               }
+               try (FileInputStream inputStream = new FileInputStream(location);
+                       JsonReader jsonReader = Json.createReader(inputStream)) {
+                       policyNames = jsonReader.readArray();
+                       serviceTypeNamesList = new ArrayList<>();
+                       for (int i = 0; i < policyNames.size(); i++) {
+                               javax.json.JsonObject policyName = policyNames.getJsonObject(i);
+                               String name = policyName.getJsonString("serviceTypePolicyName").getString();
+                               serviceTypeNamesList.add(name);
                        }
-               } catch (FileNotFoundException e) {
+               } catch (IOException e) {
                        LOGGER.error("Exception Occured while initializing the JSONConfig file"+e);
-               }finally{
-                       try {
-                               if(inputStream != null){
-                                       inputStream.close();
-                               }
-                               if(jsonReader != null){
-                                       jsonReader.close();
-                               }
-                       } catch (IOException e) {
-                               LOGGER.error("Exception Occured while closing the File InputStream"+e);
-                       }
                }
        }
 
index dc2159b..267d5fa 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.onap.policy.admin;
 
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.BufferedReader;
@@ -134,12 +135,46 @@ public class PolicyManagerServletTest extends Mockito{
                when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.admin.properties");
                System.setProperty("xacml.rest.admin.closedLoopJSON", new File(".").getCanonicalPath() + File.separator + "src"+ File.separator + "test" + File.separator + "resources" + File.separator + "JSONConfig.json");
                        servlet.init(servletConfig);
+                       
+                       assertTrue(PolicyManagerServlet.getServiceTypeNamesList().size() > 0);
+                       assertTrue(PolicyManagerServlet.getPolicyNames().size() > 0);
+                       
                } catch (Exception e1) {
                        logger.error("Exception Occured"+e1);
                        fail();
                }
        }
        
+       @Test
+       public void testBadInitJson() {
+               PolicyManagerServlet servlet = new PolicyManagerServlet();
+               ServletConfig servletConfig = mock(ServletConfig.class);       
+        try {
+               when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
+               when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.admin.properties");
+               System.setProperty("xacml.rest.admin.closedLoopJSON", new File(".").getCanonicalPath() + File.separator + "src"+ File.separator + "test" + File.separator + "resources" + File.separator + "JSONConfig.foo");
+                       servlet.init(servletConfig);
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+       }
+       
+       @Test
+       public void testBadInitJsonInvalidFile() {
+               PolicyManagerServlet servlet = new PolicyManagerServlet();
+               ServletConfig servletConfig = mock(ServletConfig.class);       
+        try {
+               when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
+               when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.admin.properties");
+               System.setProperty("xacml.rest.admin.closedLoopJSON", new File(".").getCanonicalPath() + File.separator + "src"+ File.separator + "test" + File.separator + "resources" + File.separator + "IDonotExist.json");
+                       servlet.init(servletConfig);
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+       }
+
        @SuppressWarnings("static-access")
        @Test
        public void testDescribePolicy(){