Fix bugs identified by Sonar on drools-pdp 97/37197/2
authorliamfallon <liam.fallon@ericsson.com>
Tue, 20 Mar 2018 17:28:29 +0000 (17:28 +0000)
committerliamfallon <liam.fallon@ericsson.com>
Wed, 21 Mar 2018 14:29:10 +0000 (14:29 +0000)
Three bugs fixed:
1. Set of static variable in DMaaPSimulatorJaxR
2. Use of opssibly null variable "output" in RepositoryAudit
3. Unreachable statement error in RepositoryAudit

Change-Id: I72e028cfc51a82250afd02fb4109d3dea08072dc
Issue-ID: POLICY-691
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java

index 2513a04..4470089 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 import java.util.concurrent.BlockingQueue;
 
 import javax.servlet.http.HttpServletResponse;
@@ -43,83 +44,96 @@ import org.slf4j.LoggerFactory;
 
 @Path("/events")
 public class DMaaPSimulatorJaxRs {
-       
        private static final String NO_DATA_MSG = "No Data";
        private static final Map<String, BlockingQueue<String>> queues = new ConcurrentHashMap<>();
        private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class);
        private static int responseCode = 200;
-       
+
        @GET
        @Path("/{topicName}/{consumeGroup}/{consumerId}")
        public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") String topicName,
-               @Context final HttpServletResponse httpResponse) {
-           int currentRespCode = responseCode;
-        httpResponse.setStatus(currentRespCode);
-        try {
-            httpResponse.flushBuffer();
-        } catch (IOException e) {
-            logger.error("flushBuffer threw: ", e);
-            return "Got an error";
-        }
-        
-           if (currentRespCode < 200 || currentRespCode >= 300)
-           {
-               return "You got response code: " + currentRespCode;
-           }
+                       @Context final HttpServletResponse httpResponse) {
+               int currentRespCode = responseCode;
+               httpResponse.setStatus(currentRespCode);
+               try {
+                       httpResponse.flushBuffer();
+               } catch (IOException e) {
+                       logger.error("flushBuffer threw: ", e);
+                       return "Got an error";
+               }
+
+               if (currentRespCode < 200 || currentRespCode >= 300)
+               {
+                       return "You got response code: " + currentRespCode;
+               }
+
                if (queues.containsKey(topicName)) {
-                       BlockingQueue<String> queue = queues.get(topicName);
-                       String response = NO_DATA_MSG;
-                       try {
-                               response = queue.poll(timeout, TimeUnit.MILLISECONDS);
-                       } catch (InterruptedException e) {
-                               logger.debug("error in DMaaP simulator", e);
-                               Thread.currentThread().interrupt();
-                       }
-                       if (response == null) {
-                           response = NO_DATA_MSG;
-                       }
-                       return response;
+                       return getNextMessageFromQueue(timeout, topicName);
                }
                else if (timeout > 0) {
-                       try {
-                               Thread.sleep(timeout);
-                               if (queues.containsKey(topicName)) {
-                                       BlockingQueue<String> queue = queues.get(topicName);
-                                       String response = queue.poll();
-                                       if (response == null) {
-                                           response = NO_DATA_MSG;
-                                       }
-                                       return response;
+                       return waitForNextMessageFromQueue(timeout, topicName);
+               }
+               return "No topic";
+       }
+
+       private String getNextMessageFromQueue(final int timeout, final String topicName) {
+               BlockingQueue<String> queue = queues.get(topicName);
+               String response = NO_DATA_MSG;
+               try {
+                       response = queue.poll(timeout, TimeUnit.MILLISECONDS);
+               } catch (InterruptedException e) {
+                       logger.debug("error in DMaaP simulator", e);
+                       Thread.currentThread().interrupt();
+               }
+               if (response == null) {
+                       response = NO_DATA_MSG;
+               }
+               return response;
+       }
+
+       private String waitForNextMessageFromQueue(int timeout, String topicName) {
+               try {
+                       Thread.sleep(timeout);
+                       if (queues.containsKey(topicName)) {
+                               BlockingQueue<String> queue = queues.get(topicName);
+                               String response = queue.poll();
+                               if (response == null) {
+                                       response = NO_DATA_MSG;
                                }
-                       } catch (InterruptedException e) {
-                               logger.debug("error in DMaaP simulator", e);
-                               Thread.currentThread().interrupt();
+                               return response;
                        }
+               } catch (InterruptedException e) {
+                       logger.debug("error in DMaaP simulator", e);
+                       Thread.currentThread().interrupt();
                }
                return "No topic";
        }
-       
+
        @POST
        @Path("/{topicName}")
        @Consumes(MediaType.TEXT_PLAIN)
-       public String publish(@PathParam("topicName") String topicName, String body) { 
-               if (queues.containsKey(topicName)) {
-                       BlockingQueue<String> queue = queues.get(topicName);
-                       queue.offer(body);
-               }
-               else {
-                       BlockingQueue<String> queue = new LinkedBlockingQueue<>();
-                       queue.offer(body);
-                       queues.put(topicName, queue);
-               }
+       public String publish(@PathParam("topicName") String topicName, String body) {
+               BlockingQueue<String> queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>());
                
+               if (!queue.offer(body)) {
+                       logger.warn("error on topic {}, failed to place body {} on queue", topicName, body);
+               }
+
                return "";
        }
-       
+
        @POST
        @Path("/setStatus")
        public String setStatus(@QueryParam("statusCode") int statusCode) {
-           responseCode = statusCode;
-           return "Status code set";
+               setResponseCode(statusCode);
+               return "Status code set";
+       }
+
+       /**
+        * Static method to set static response code, synchronized for multiple possible uses
+        * @param incomingResponseCode
+        */
+       private static synchronized void setResponseCode(final int incomingResponseCode) {
+               responseCode = incomingResponseCode; 
        }
 }
index 1113444..6ddf0c7 100644 (file)
@@ -330,7 +330,7 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase
         *     Note: at present, this step just generates log messages,
         *     but doesn't do any verification.
         */
-       if (rval == 0)
+       if (rval == 0 && output != null)
          {
                // place output in 'fileContents' (replacing the Return characters
                // with Newline)
index 3cc2790..cb1700e 100644 (file)
@@ -84,12 +84,13 @@ public class StateManagementFeature implements StateManagementFeatureAPI,
                        droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
                        stateManagement = droolsPdpIntegrityMonitor.getStateManager();
                        
-                       logger.debug("StateManagementFeature.globalInit(): "
-                               + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
-                       
-                       if(stateManagement == null){
+                       if (stateManagement == null) {
                                logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
                        }
+                       else {
+                               logger.debug("StateManagementFeature.globalInit(): "
+                                               + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
+                       }
                } catch (Exception e1) {
                        logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
                                + " initialization failed with exception:", e1);