aboutsummaryrefslogtreecommitdiffstats
path: root/appc-metric/appc-metric-bundle/src/test/java/org/openecomp/appc/metricservice/TestMetricServiceImpl.java
blob: 7c8011e81d6b583fc3a5a0834b5f78e161b608d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.metricservice;
import org.junit.Assert;
import org.junit.Test;
import org.onap.appc.metricservice.impl.MetricServiceImpl;
import org.onap.appc.metricservice.metric.MetricType;
import org.onap.appc.metricservice.metric.PrimitiveCounter;
import org.onap.appc.metricservice.metric.DmaapRequestCounterMetric;
import org.onap.appc.metricservice.metric.DispatchingFuntionMetric;
import org.onap.appc.metricservice.metric.impl.DefaultPrimitiveCounter;
import org.onap.appc.metricservice.metric.impl.DmaapRequestCounterMetricImpl;
import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
import org.onap.appc.metricservice.metric.impl.DispatchingFunctionCounterBuilderImpl;
import org.onap.appc.metricservice.metric.impl.DmaapRequestCounterBuilderImpl;
import org.onap.appc.metricservice.metric.impl.PrimitiveCounterBuilderImpl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;


public class TestMetricServiceImpl {
    @Test
    public void createRegistryTest() {
        MetricServiceImpl metricServiceImpl = new MetricServiceImpl();
        metricServiceImpl.createRegistry("anyName");
        MetricRegistry metricRegistry = metricServiceImpl.registry("anyName");
        Assert.assertNotNull(metricRegistry);
        Assert.assertTrue(metricServiceImpl.getAllRegistry().keySet().contains("anyName"));
    }

    @Test
    public void testDefaultPrimitiveCounter(){
        DefaultPrimitiveCounter df= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER);
        df.increment();
        Assert.assertEquals(1, df.value());
        df.increment(25);
        Assert.assertEquals(2, df.value());
        df.decrement();
        Assert.assertEquals(1, df.value());
        Assert.assertNotNull(df.getLastModified());
        Assert.assertEquals("TEST",df.name());
        Assert.assertEquals(MetricType.COUNTER,df.type());
        df.reset();
        Assert.assertEquals(0,df.value());
        Assert.assertNotNull(df.getMetricsOutput());

    }

    @Test
    public void testDefaultPrimitiveCounterWithThreeArgsConstructor(){
        DefaultPrimitiveCounter obj= new DefaultPrimitiveCounter("TEST", MetricType.COUNTER,3);
        obj.increment();
        Assert.assertEquals(4, obj.value());
        obj.increment(25);
        Assert.assertEquals(5, obj.value());
        obj.decrement();
        Assert.assertEquals(4, obj.value());
        Assert.assertNotNull(obj.getLastModified());
        Assert.assertEquals("TEST",obj.name());
        Assert.assertEquals(MetricType.COUNTER,obj.type());
        obj.reset();
        Assert.assertEquals(0,obj.value());
        Assert.assertNotNull(obj.getMetricsOutput());
    }

    @Test
    public void testDmaapRequestCounterMetricImpl() {

        DmaapRequestCounterMetricImpl obj =new DmaapRequestCounterMetricImpl("TEST",MetricType.COUNTER,7,1);
        String date = getCurrentDate();

        obj.incrementPublishedMessage();
        obj.incrementRecievedMessage();
        Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
        Assert.assertEquals(8,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
        Assert.assertEquals(date+"[8],[2]",obj.value());
        Assert.assertNotNull(obj.getLastModified());
        Assert.assertEquals("TEST",obj.name());
        Assert.assertEquals(MetricType.COUNTER,obj.type());
        obj.reset();
        Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages")));
        Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));

    }

    @Test
    public void testDispatchingFuntionMetricImpl() {

        DispatchingFuntionMetricImpl obj= new DispatchingFuntionMetricImpl("TEST",MetricType.COUNTER,7,1);
        String date = getCurrentDate();

        obj.incrementAcceptedRequest();
        obj.incrementRejectedRequest();
        Assert.assertEquals(10,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
        Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));
        Assert.assertEquals(date+"[8,2]@10",obj.value());
        Assert.assertNotNull(obj.getLastModified());
        Assert.assertEquals("TEST",obj.name());
        Assert.assertEquals(MetricType.COUNTER,obj.type());
        obj.reset();
        Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages")));
        Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages")));


    }

    @Test
    public void testDispatchingFunctionCounterBuilderImpl(){
        DispatchingFunctionCounterBuilderImpl obj=new DispatchingFunctionCounterBuilderImpl();
        String date = getCurrentDate();
        DispatchingFuntionMetric metric=obj.withName("TEST").withType(MetricType.COUNTER).withAcceptRequestValue(7).withRejectRequestValue(2).build();
        metric.incrementAcceptedRequest();
        metric.incrementRejectedRequest();
        Assert.assertEquals(date+"[8,3]@11",metric.value());
    }

    @Test
    public void testDmaapRequestCounterBuilderImpl(){
        DmaapRequestCounterBuilderImpl obj=new DmaapRequestCounterBuilderImpl();
        DmaapRequestCounterMetric metric =obj.withName("TEST").withPublishedMessage(1).withRecievedMessage(21).withType(MetricType.COUNTER).build();
        metric.incrementPublishedMessage();
        metric.incrementRecievedMessage();
        Assert.assertEquals(2,Integer.parseInt(metric.getMetricsOutput().get("Total Published messages")));
        Assert.assertEquals(22,Integer.parseInt(metric.getMetricsOutput().get("Total Received messages")));
    }

    @Test
    public void testPrimitiveCounterBuilderImpl(){
        PrimitiveCounterBuilderImpl obj=new PrimitiveCounterBuilderImpl();
        PrimitiveCounter counter=obj.withName("TEST").withType(MetricType.COUNTER).withValue(1).build();
        counter.increment();
        Assert.assertEquals(2, counter.value());
        counter.decrement();
        Assert.assertEquals(1, counter.value());
    }

    private String getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
        return dateFormat.format(cal.getTime());

    }

}