f35410e09c764cf87b37a1d8590b2ebd2bf2e7db
[appc.git] / appc-metric / appc-metric-bundle / src / test / java / org / openecomp / appc / metricservice / TestMetricServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.metricservice;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.openecomp.appc.metricservice.impl.MetricServiceImpl;
29 import org.openecomp.appc.metricservice.metric.MetricType;
30 import org.openecomp.appc.metricservice.metric.PrimitiveCounter;
31 import org.openecomp.appc.metricservice.metric.DmaapRequestCounterMetric;
32 import org.openecomp.appc.metricservice.metric.DispatchingFuntionMetric;
33 import org.openecomp.appc.metricservice.metric.impl.DefaultPrimitiveCounter;
34 import org.openecomp.appc.metricservice.metric.impl.DmaapRequestCounterMetricImpl;
35 import org.openecomp.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
36 import org.openecomp.appc.metricservice.metric.impl.DispatchingFunctionCounterBuilderImpl;
37 import org.openecomp.appc.metricservice.metric.impl.DmaapRequestCounterBuilderImpl;
38 import org.openecomp.appc.metricservice.metric.impl.PrimitiveCounterBuilderImpl;
39
40 import java.text.SimpleDateFormat;
41 import java.util.Calendar;
42 import java.util.TimeZone;
43
44
45 public class TestMetricServiceImpl {
46     @Test
47     public void createRegistryTest() {
48         MetricServiceImpl metricServiceImpl = new MetricServiceImpl();
49         metricServiceImpl.createRegistry("anyName");
50         MetricRegistry metricRegistry = metricServiceImpl.registry("anyName");
51         Assert.assertNotNull(metricRegistry);
52         Assert.assertTrue(metricServiceImpl.getAllRegistry().keySet().contains("anyName"));
53     }
54
55     @Test
56     public void testDefaultPrimitiveCounter(){
57         DefaultPrimitiveCounter df= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER);
58         df.increment();
59         Assert.assertEquals(1, df.value());
60         df.increment(25);
61         Assert.assertEquals(2, df.value());
62         df.decrement();
63         Assert.assertEquals(1, df.value());
64         Assert.assertNotNull(df.getLastModified());
65         Assert.assertEquals("TEST",df.name());
66         Assert.assertEquals(MetricType.COUNTER,df.type());
67         df.reset();
68         Assert.assertEquals(0,df.value());
69         Assert.assertNotNull(df.getMetricsOutput());
70
71     }
72
73     @Test
74     public void testDefaultPrimitiveCounterWithThreeArgsConstructor(){
75         DefaultPrimitiveCounter obj= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER,3);
76         obj.increment();
77         Assert.assertEquals(4, obj.value());
78         obj.increment(25);
79         Assert.assertEquals(5, obj.value());
80         obj.decrement();
81         Assert.assertEquals(4, obj.value());
82         Assert.assertNotNull(obj.getLastModified());
83         Assert.assertEquals("TEST",obj.name());
84         Assert.assertEquals(MetricType.COUNTER,obj.type());
85         obj.reset();
86         Assert.assertEquals(0,obj.value());
87         Assert.assertNotNull(obj.getMetricsOutput());
88     }
89
90     @Test
91     public void testDmaapRequestCounterMetricImpl() {
92
93         DmaapRequestCounterMetricImpl obj =new DmaapRequestCounterMetricImpl("TEST",MetricType.COUNTER,7,1);
94         String date = getCurrentDate();
95
96         obj.incrementPublishedMessage();
97         obj.incrementRecievedMessage();
98         Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
99         Assert.assertEquals(8,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
100         Assert.assertEquals(date+"[8],[2]",obj.value());
101         Assert.assertNotNull(obj.getLastModified());
102         Assert.assertEquals("TEST",obj.name());
103         Assert.assertEquals(MetricType.COUNTER,obj.type());
104         obj.reset();
105         Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
106         Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
107
108     }
109
110     @Test
111     public void testDispatchingFuntionMetricImpl() {
112
113         DispatchingFuntionMetricImpl obj= new DispatchingFuntionMetricImpl("TEST",MetricType.COUNTER,7,1);
114         String date = getCurrentDate();
115
116         obj.incrementAcceptedRequest();
117         obj.incrementRejectedRequest();
118         Assert.assertEquals(10,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
119         Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));
120         Assert.assertEquals(date+"[8,2]@10",obj.value());
121         Assert.assertNotNull(obj.getLastModified());
122         Assert.assertEquals("TEST",obj.name());
123         Assert.assertEquals(MetricType.COUNTER,obj.type());
124         obj.reset();
125         Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
126         Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));
127
128
129     }
130
131     @Test
132     public void testDispatchingFunctionCounterBuilderImpl(){
133         DispatchingFunctionCounterBuilderImpl obj=new DispatchingFunctionCounterBuilderImpl();
134         String date = getCurrentDate();
135         DispatchingFuntionMetric metric=obj.withName("TEST").withType(MetricType.COUNTER).withAcceptRequestValue(7).withRejectRequestValue(2).build();
136         metric.incrementAcceptedRequest();
137         metric.incrementRejectedRequest();
138         Assert.assertEquals(date+"[8,3]@11",metric.value());
139     }
140
141     @Test
142     public void testDmaapRequestCounterBuilderImpl(){
143         DmaapRequestCounterBuilderImpl obj=new DmaapRequestCounterBuilderImpl();
144         DmaapRequestCounterMetric metric =obj.withName("TEST").withPublishedMessage(1).withRecievedMessage(21).withType(MetricType.COUNTER).build();
145         metric.incrementPublishedMessage();
146         metric.incrementRecievedMessage();
147         Assert.assertEquals(2,Integer.parseInt(metric.getMetricsOutput().get("Total Published messages")));
148         Assert.assertEquals(22,Integer.parseInt(metric.getMetricsOutput().get("Total Received messages")));
149     }
150
151     @Test
152     public void testPrimitiveCounterBuilderImpl(){
153         PrimitiveCounterBuilderImpl obj=new PrimitiveCounterBuilderImpl();
154         PrimitiveCounter counter=obj.withName("TEST").withType(MetricType.COUNTER).withValue(1).build();
155         counter.increment();
156         Assert.assertEquals(2, counter.value());
157         counter.decrement();
158         Assert.assertEquals(1, counter.value());
159     }
160
161     private String getCurrentDate() {
162         Calendar cal = Calendar.getInstance();
163         cal.setTimeZone(TimeZone.getTimeZone("UTC"));
164         SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
165         return dateFormat.format(cal.getTime());
166
167     }
168
169 }