Fix for casting/subclassing in MetricRegistry 95/79095/4
authorJoss Armstrong <joss.armstrong@ericsson.com>
Mon, 25 Feb 2019 10:32:13 +0000 (10:32 +0000)
committerTakamune Cho <takamune.cho@att.com>
Tue, 26 Feb 2019 15:33:58 +0000 (15:33 +0000)
Put in code to avoid cast exceptions from this class
and updated tests

Issue-ID: APPC-1480
Change-Id: I8b54d93e52eb523f08d13e874b4d18d05feede6b
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/impl/MetricRegistryImpl.java
appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/impl/MetricRegistryImplTest.java

index 48d1c6f..d371fed 100644 (file)
 
 package org.onap.appc.metricservice.impl;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-
 import org.onap.appc.metricservice.MetricRegistry;
 import org.onap.appc.metricservice.metric.Counter;
 import org.onap.appc.metricservice.metric.Metric;
@@ -40,6 +41,8 @@ import org.onap.appc.metricservice.policy.impl.PolicyBuilderFactoryImpl;
 
 public class MetricRegistryImpl implements MetricRegistry {
     private String name;
+    // Map can contain Counters, DispatchingFunctionMetrics and DMaapRequestCounterMetrics
+    // and there are methods to retrieve only the 'Counter' types
     private Map<String, Metric> concurrentMetricMap = new ConcurrentHashMap<>();
 
     public MetricRegistryImpl(String name) {
@@ -68,12 +71,22 @@ public class MetricRegistryImpl implements MetricRegistry {
 
     @Override
     public Counter counter(String value) {
-        return (Counter)concurrentMetricMap.get(value);
+        Metric metric = concurrentMetricMap.get(value);
+        if (metric instanceof Counter) {
+            return (Counter)metric;
+        }
+        else return null;
     }
 
     @Override
     public Counter[] counters() {
-        return (Counter[])concurrentMetricMap.values().toArray();
+        List<Counter> counterList = new ArrayList<>();
+        for (Metric m: concurrentMetricMap.values()) {
+            if (m instanceof Counter) {
+                counterList.add((Counter) m);
+            }
+        }
+        return counterList.toArray(new Counter[counterList.size()]);
     }
 
     @Override
index 09e5ba9..0bde7f7 100644 (file)
@@ -29,30 +29,46 @@ import static org.junit.Assert.assertTrue;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.appc.metricservice.metric.Counter;
-import org.onap.appc.metricservice.metric.Metric;
+import org.onap.appc.metricservice.metric.MetricType;
 import org.onap.appc.metricservice.metric.impl.DefaultPrimitiveCounter;
+import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
 
 public class MetricRegistryImplTest {
 
-    private static final String NAME = "NAME";
+    private static final String METRIC_NAME = "METRIC_NAME";
+    private static final String COUNTER_NAME = "COUNTER_NAME";
     private MetricRegistryImpl registry;
+    private DispatchingFuntionMetricImpl metric;
+    private DefaultPrimitiveCounter counter;
+
     @Before
     public void setup() {
         registry = new MetricRegistryImpl(null);
+        counter = new DefaultPrimitiveCounter(COUNTER_NAME, null);
+        metric = new DispatchingFuntionMetricImpl(METRIC_NAME, MetricType.COUNTER, 0, 0);
+
     }
 
     @Test
     public void testRegister() {
-        Metric metric = new DefaultPrimitiveCounter(NAME, null);
-        assertNull(registry.counter(NAME));
+        assertEquals(0, registry.counters().length);
+        assertEquals(0, registry.metrics().length);
+        assertNull(registry.counter(COUNTER_NAME));
+        assertTrue(registry.register(counter));
+        assertFalse(registry.register(counter));
+        assertTrue(registry.counter(COUNTER_NAME) instanceof Counter);
+        assertSame(counter, registry.counter(COUNTER_NAME));
         assertTrue(registry.register(metric));
-        assertFalse(registry.register(metric));
-        assertTrue(registry.counter(NAME) instanceof Counter);
-        assertSame(metric, registry.metric(NAME));
+        assertSame(metric, registry.metric(METRIC_NAME));
+        assertEquals(1, registry.counters().length);
+        assertEquals(2, registry.metrics().length);
     }
 
     @Test
-    public void testCounter() {
+    public void testDispose() {
+        registry.register(metric);
+        registry.register(counter);
+        assertEquals(2, registry.metrics().length);
         registry.dispose();
         assertEquals(0, registry.metrics().length);
     }