Increase junit coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / analytics / AveragingRingBufferTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
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 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.analytics;
27
28 import static org.junit.Assert.assertEquals;
29
30 import java.security.SecureRandom;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.aai.sparky.analytics.AveragingRingBuffer;
36 import org.mockito.runners.MockitoJUnitRunner;
37
38 /**
39  * The Class AveragingRingBufferTest.
40  */
41 @RunWith(MockitoJUnitRunner.class) 
42 public class AveragingRingBufferTest {
43
44   protected SecureRandom random = new SecureRandom();
45
46   /**
47    * @return the random
48    */
49   public SecureRandom getRandom() {
50     return random;
51   }
52
53   /**
54    * @param random the random to set
55    */
56   public void setRandom(SecureRandom random) {
57     this.random = random;
58   }
59
60   /**
61    * Inits the.
62    *
63    * @throws Exception the exception
64    */
65   @Before
66   public void init() throws Exception {
67     // nothing at the moment
68   }
69
70   /**
71    * Validate pre index roll averaging.
72    */
73   @Test
74   public void validatePreIndexRollAveraging() {
75
76     AveragingRingBuffer arb = new AveragingRingBuffer(5);
77     assertEquals(0, arb.getAvg());
78
79     /*
80      * On initial buffer fill, the average will be re-calculated on the fly for the first nth data
81      * points until the data buffer has been filled the first time, and then the buffer
82      * automatically recalculates the average every time the buffer index rolls over, to the keep
83      * the average relative to the last "nth" data points.
84      */
85
86     // [ 1, 0, 0, 0, 0 ], sum = 1, avg = 1/1 =1
87     arb.addSample(1);
88     assertEquals(1, arb.getAvg());
89
90     // [ 1, 2, 0, 0, 0 ], sum = 3, avg = 3/2 = 1
91     arb.addSample(2);
92     assertEquals(1, arb.getAvg());
93
94     // [ 1, 2, 3, 0, 0 ], sum = 6, avg = 6/3 = 2
95     arb.addSample(3);
96     assertEquals(2, arb.getAvg());
97
98     // [ 1, 2, 3, 4, 0 ], sum = 10, avg = 10/4 = 2
99     arb.addSample(4);
100     assertEquals(2, arb.getAvg());
101
102     // [ 1, 2, 3, 4, 5 ], sum = 15, avg = 15/5 = 3
103     arb.addSample(5);
104     assertEquals(3, arb.getAvg());
105
106   }
107
108   /**
109    * Validate post index roll averaging.
110    */
111   @Test
112   public void validatePostIndexRollAveraging() {
113
114     AveragingRingBuffer arb = new AveragingRingBuffer(5);
115     arb.addSample(1);
116     arb.addSample(2);
117     arb.addSample(3);
118     arb.addSample(4);
119     arb.addSample(5);
120
121     /*
122      * The behavior switches, and now doesn't re-calculate the average until each nth data point, to
123      * reduce the computational over-head of re-calculating on each value.
124      */
125
126     // [ 10, 2, 3, 4, 5 ],
127     arb.addSample(10);
128     assertEquals(3, arb.getAvg());
129
130     // [ 10, 20, 3, 4, 5 ],
131     arb.addSample(20);
132     assertEquals(3, arb.getAvg());
133
134     // [ 10, 20, 30, 4, 5 ],
135     arb.addSample(30);
136     assertEquals(3, arb.getAvg());
137
138     // [ 10, 20, 30, 40, 5 ],
139     arb.addSample(40);
140     assertEquals(3, arb.getAvg());
141
142     // [ 10, 20, 30, 40, 50 ], s=150, avg=150/5=30
143     arb.addSample(50);
144     assertEquals(30, arb.getAvg());
145
146   }
147
148 }