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