Changed to unmaintained
[appc.git] / appc-metric / appc-metric-bundle / src / test / java / org / onap / appc / metricservice / TestMetricServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.metricservice;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.appc.metricservice.impl.MetricServiceImpl;
30 import org.onap.appc.metricservice.metric.MetricType;
31 import org.onap.appc.metricservice.metric.PrimitiveCounter;
32 import org.onap.appc.metricservice.metric.DmaapRequestCounterMetric;
33 import org.onap.appc.metricservice.metric.DispatchingFuntionMetric;
34 import org.onap.appc.metricservice.metric.impl.DefaultPrimitiveCounter;
35 import org.onap.appc.metricservice.metric.impl.DmaapRequestCounterMetricImpl;
36 import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
37 import org.onap.appc.metricservice.metric.impl.DispatchingFunctionCounterBuilderImpl;
38 import org.onap.appc.metricservice.metric.impl.DmaapRequestCounterBuilderImpl;
39 import org.onap.appc.metricservice.metric.impl.PrimitiveCounterBuilderImpl;
40
41 import java.text.SimpleDateFormat;
42 import java.util.Calendar;
43 import java.util.TimeZone;
44
45
46 public class TestMetricServiceImpl {
47     @Test
48     public void createRegistryTest() {
49         MetricServiceImpl metricServiceImpl = new MetricServiceImpl();
50         metricServiceImpl.createRegistry("anyName");
51         MetricRegistry metricRegistry = metricServiceImpl.registry("anyName");
52         Assert.assertNotNull(metricRegistry);
53         Assert.assertTrue(metricServiceImpl.getAllRegistry().keySet().contains("anyName"));
54     }
55
56     @Test
57     public void testDefaultPrimitiveCounter(){
58         DefaultPrimitiveCounter df= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER);
59         df.increment();
60         Assert.assertEquals(1, df.value());
61         df.increment(25);
62         Assert.assertEquals(2, df.value());
63         df.decrement();
64         Assert.assertEquals(1, df.value());
65         Assert.assertNotNull(df.getLastModified());
66         Assert.assertEquals("TEST", df.name());
67         Assert.assertEquals(MetricType.COUNTER, df.type());
68         df.reset();
69         Assert.assertEquals(0, df.value());
70         Assert.assertNotNull(df.getMetricsOutput());
71
72     }
73
74     @Test
75     public void testDefaultPrimitiveCounterWithThreeArgsConstructor(){
76         DefaultPrimitiveCounter obj= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER,3);
77         obj.increment();
78         Assert.assertEquals(4, obj.value());
79         obj.increment(25);
80         Assert.assertEquals(5, obj.value());
81         obj.decrement();
82         Assert.assertEquals(4, obj.value());
83         Assert.assertNotNull(obj.getLastModified());
84         Assert.assertEquals("TEST", obj.name());
85         Assert.assertEquals(MetricType.COUNTER, obj.type());
86         obj.reset();
87         Assert.assertEquals(0, obj.value());
88         Assert.assertNotNull(obj.getMetricsOutput());
89     }
90
91     @Test
92     public void testDmaapRequestCounterMetricImpl() {
93
94         DmaapRequestCounterMetricImpl obj =new DmaapRequestCounterMetricImpl("TEST", MetricType.COUNTER,7,1);
95         String date = getCurrentDate();
96
97         obj.incrementPublishedMessage();
98         obj.incrementRecievedMessage();
99         Assert.assertEquals(2, Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
100         Assert.assertEquals(8, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
101         Assert.assertEquals(date + "[8],[2]", obj.value());
102         Assert.assertNotNull(obj.getLastModified());
103         Assert.assertEquals("TEST", obj.name());
104         Assert.assertEquals(MetricType.COUNTER, obj.type());
105         obj.reset();
106         Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
107         Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
108
109     }
110
111     @Test
112     public void testDispatchingFuntionMetricImpl() {
113
114         DispatchingFuntionMetricImpl obj= new DispatchingFuntionMetricImpl("TEST", MetricType.COUNTER,7,1);
115         String date = getCurrentDate();
116
117         obj.incrementAcceptedRequest();
118         obj.incrementRejectedRequest();
119         Assert.assertEquals(10, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
120         Assert.assertEquals(2, Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));
121         Assert.assertEquals(date + "[8,2]@10", obj.value());
122         Assert.assertNotNull(obj.getLastModified());
123         Assert.assertEquals("TEST", obj.name());
124         Assert.assertEquals(MetricType.COUNTER, obj.type());
125         obj.reset();
126         Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
127         Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));
128
129
130     }
131
132     @Test
133     public void testDispatchingFunctionCounterBuilderImpl(){
134         DispatchingFunctionCounterBuilderImpl obj = new DispatchingFunctionCounterBuilderImpl();
135         String date = getCurrentDate();
136         DispatchingFuntionMetric metric = obj.withName("TEST").withType(MetricType.COUNTER).withAcceptRequestValue(7).withRejectRequestValue(2).build();
137         metric.incrementAcceptedRequest();
138         metric.incrementRejectedRequest();
139         Assert.assertEquals(date+"[8,3]@11", metric.value());
140     }
141
142     @Test
143     public void testDmaapRequestCounterBuilderImpl(){
144         DmaapRequestCounterBuilderImpl obj = new DmaapRequestCounterBuilderImpl();
145         DmaapRequestCounterMetric metric = obj.withName("TEST").withPublishedMessage(1).withRecievedMessage(21).withType(MetricType.COUNTER).build();
146         metric.incrementPublishedMessage();
147         metric.incrementRecievedMessage();
148         Assert.assertEquals(2, Integer.parseInt(metric.getMetricsOutput().get("Total Published messages")));
149         Assert.assertEquals(22, Integer.parseInt(metric.getMetricsOutput().get("Total Received messages")));
150     }
151
152     @Test
153     public void testPrimitiveCounterBuilderImpl(){
154         PrimitiveCounterBuilderImpl obj = new PrimitiveCounterBuilderImpl();
155         PrimitiveCounter counter = obj.withName("TEST").withType(MetricType.COUNTER).withValue(1).build();
156         counter.increment();
157         Assert.assertEquals(2, counter.value());
158         counter.decrement();
159         Assert.assertEquals(1, counter.value());
160     }
161
162     private String getCurrentDate() {
163         Calendar cal = Calendar.getInstance();
164         cal.setTimeZone(TimeZone.getTimeZone("UTC"));
165         SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
166         return dateFormat.format(cal.getTime());
167
168     }
169
170 }