Remove code smells in Policy Agent 64/2664/3
authorelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 4 Mar 2020 08:13:50 +0000 (09:13 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 4 Mar 2020 14:46:59 +0000 (15:46 +0100)
Change-Id: I3885bd0e1c7940c2929f8890705f0d3e31c89470
Issue-ID: NONRTRIC-142
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/Application.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumer.java
policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java

index 154ab1d..18120e5 100644 (file)
@@ -35,7 +35,7 @@ public class Application {
     private StartupService startupService;
 
     public static void main(String[] args) {
-        SpringApplication.run(Application.class, args);
+        SpringApplication.run(Application.class);
     }
 
     /**
index 21a0eee..3a49ff1 100644 (file)
@@ -216,12 +216,10 @@ public class PolicyController {
     private Mono<Object> validateModifiedPolicy(Policy policy) {
         // Check that ric is not updated
         Policy current = this.policies.get(policy.id());
-        if (current != null) {
-            if (!current.ric().name().equals(policy.ric().name())) {
-                return Mono.error(new Exception("Policy cannot change RIC, policyId: " + current.id() + //
-                    ", RIC name: " + current.ric().name() + //
-                    ", new name: " + policy.ric().name()));
-            }
+        if (current != null && !current.ric().name().equals(policy.ric().name())) {
+            return Mono.error(new Exception("Policy cannot change RIC, policyId: " + current.id() + //
+                ", RIC name: " + current.ric().name() + //
+                ", new name: " + policy.ric().name()));
         }
         return Mono.just("OK");
     }
index d2daead..46dd2ee 100644 (file)
@@ -44,7 +44,9 @@ public class DmaapMessageConsumer implements Runnable {
 
     private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumer.class);
 
+    @SuppressWarnings("squid:S00116") // To avoid warning about DMAAP abbreviation.
     final Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10);
+
     private final ApplicationConfig applicationConfig;
 
     @Value("${server.port}")
index ab32065..c50a0f0 100644 (file)
 
 package org.oransc.policyagent.repository;
 
-import java.util.ArrayList;
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
@@ -39,7 +38,7 @@ import org.oransc.policyagent.configuration.RicConfig;
 public class Ric {
 
     private final RicConfig ricConfig;
-    private final List<String> managedElementIds;
+    private final ImmutableList<String> managedElementIds;
 
     private RicState state = RicState.UNDEFINED;
     private Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
@@ -57,8 +56,7 @@ public class Ric {
      */
     public Ric(RicConfig ricConfig) {
         this.ricConfig = ricConfig;
-        this.managedElementIds = new ArrayList<>(ricConfig.managedElementIds()); // TODO, this is config why is it
-                                                                                 // copied here?
+        this.managedElementIds = ricConfig.managedElementIds();
     }
 
     public String name() {
@@ -96,26 +94,6 @@ public class Ric {
         return managedElementIds.contains(managedElementId);
     }
 
-    /**
-     * Adds the given node as managed by this Ric.
-     *
-     * @param managedElementId the node to add.
-     */
-    public synchronized void addManagedElement(String managedElementId) {
-        if (!managedElementIds.contains(managedElementId)) {
-            managedElementIds.add(managedElementId);
-        }
-    }
-
-    /**
-     * Removes the given node as managed by this Ric.
-     *
-     * @param managedElementId the node to remove.
-     */
-    public synchronized void removeManagedElement(String managedElementId) {
-        managedElementIds.remove(managedElementId);
-    }
-
     /**
      * Gets the policy types supported by this Ric.
      *
index 917aca0..db5e2bd 100644 (file)
@@ -34,7 +34,6 @@ import java.time.Duration;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Vector;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.junit.jupiter.api.AfterEach;
@@ -217,9 +216,8 @@ public class ApplicationTest {
     @Test
     public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
         String ricName = "ric1";
-        Ric ric = addRic(ricName);
         String managedElementId = "kista_1";
-        ric.addManagedElement(managedElementId);
+        addRic(ricName, managedElementId);
 
         String url = baseUrl() + "/ric?managedElementId=" + managedElementId;
         String rsp = this.restTemplate.getForObject(url, String.class);
@@ -490,6 +488,7 @@ public class ApplicationTest {
             this.supervision = supervision;
         }
 
+        @Override
         public void run() {
             for (int i = 0; i < 100; ++i) {
                 if (i % 10 == 0) {
@@ -547,10 +546,17 @@ public class ApplicationTest {
     }
 
     private Ric addRic(String ricName) {
+        return addRic(ricName, null);
+    }
+
+    private Ric addRic(String ricName, String managedElement) {
         if (rics.get(ricName) != null) {
             return rics.get(ricName);
         }
-        Vector<String> mes = new Vector<>();
+        List<String> mes = new ArrayList<>();
+        if (managedElement != null) {
+            mes.add(managedElement);
+        }
         RicConfig conf = ImmutableRicConfig.builder() //
             .name(ricName) //
             .baseUrl(ricName) //