Fix sonar issues 29/61729/2
authorParshad Patel <pars.patel@samsung.com>
Wed, 22 Aug 2018 02:09:49 +0000 (11:09 +0900)
committerParshad Patel <pars.patel@samsung.com>
Thu, 23 Aug 2018 04:09:54 +0000 (13:09 +0900)
Fix use try-with-resources issues

Issue-ID: AAI-1368
Change-Id: I267df61915ac1596cc8645c62ccf43eb04fd7ac5
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
aai-core/src/main/java/org/onap/aai/util/PojoUtils.java
aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java
aai-core/src/test/java/org/onap/aai/util/PojoUtilsTest.java

index 8354d84..f3e6738 100644 (file)
@@ -162,16 +162,17 @@ public class PojoUtils {
         * @param clazz the clazz
         * @return the xml from object
         * @throws JAXBException the JAXB exception
+        * @throws IOException 
         */
-       public <T> String getXmlFromObject(T clazz) throws JAXBException {
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();
-               JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName());
-
-               Marshaller marshaller = jc.createMarshaller();
-               marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-               marshaller.marshal(clazz, baos);
-
-               return baos.toString();
+       public <T> String getXmlFromObject(T clazz) throws JAXBException, IOException {
+               try(ByteArrayOutputStream baos = new ByteArrayOutputStream()){
+                       JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName());
+       
+                       Marshaller marshaller = jc.createMarshaller();
+                       marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+                       marshaller.marshal(clazz, baos);
+                       return baos.toString();
+               }
        }
 
        /**
index 930e2fb..f90d54e 100644 (file)
@@ -111,7 +111,7 @@ public class YAMLfromOXM extends OxmFileProcessor {
                try {
                        init();
                } catch(Exception e) {
-                       logger.error( "Error initializing " + this.getClass());
+                       logger.error( "Error initializing " + this.getClass(),e);
                        throw e;
                }               
                pathSb.append(getDocumentHeader());
@@ -441,7 +441,7 @@ public class YAMLfromOXM extends OxmFileProcessor {
                                results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} );
                        }
                } catch(Exception e) {
-                       logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e);
+                       logger.debug("xmlRootElementName: "+xmlRootElementName+"\n",e);
                }
                try {
                        EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build();
@@ -454,7 +454,7 @@ public class YAMLfromOXM extends OxmFileProcessor {
                                results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} );
                        }
                } catch(Exception e) {
-                       logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e);
+                       logger.debug("xmlRootElementName: "+xmlRootElementName+"\n",e);
                }
                if(preventDelete.size() > 0) {
                        prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete);
@@ -505,7 +505,7 @@ public class YAMLfromOXM extends OxmFileProcessor {
                                javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString());
                        }
                } catch (Exception e) {
-                       e.printStackTrace();
+                       logger.error("Exception adding in javaTypeDefinitions",e);
                }
                if ( xmlRootElementName.equals("inventory") ) {
                        logger.trace("skip xmlRootElementName(2)="+xmlRootElementName);
@@ -535,21 +535,16 @@ public class YAMLfromOXM extends OxmFileProcessor {
                try {
                        outfile.createNewFile();
                } catch (IOException e) {
-                       logger.error( "Exception creating output file " + outfileName);
-                       e.printStackTrace();
+                       logger.error( "Exception creating output file " + outfileName,e);
                }
-               BufferedWriter bw = null;
                try {
                        Charset charset = Charset.forName("UTF-8");
                        Path path = Paths.get(outfileName);
-                       bw = Files.newBufferedWriter(path, charset);
-                       bw.write(fileContent);
-                       if ( bw != null ) {
-                               bw.close();
+                       try(BufferedWriter bw = Files.newBufferedWriter(path, charset)){
+                               bw.write(fileContent);
                        }
                } catch ( IOException e) {
-                       logger.error( "Exception writing output file " + outfileName);
-                       e.printStackTrace();
+                       logger.error( "Exception writing output file " + outfileName,e);
                } 
        }
        
index 3658704..35065fd 100644 (file)
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
 import java.time.LocalDateTime;
 import java.time.Month;
 import java.util.ArrayList;
@@ -159,7 +160,7 @@ public class PojoUtilsTest {
        }
        
        @Test
-       public void testGetXmlFromObject() throws JAXBException {
+       public void testGetXmlFromObject() throws JAXBException, IOException {
                NotificationEvent notificationEvent = new NotificationEvent();
                notificationEvent.setCambriaPartition("partition");