1
1
package io .harness .cf .client .api ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
- import static org .junit .jupiter .api .Assertions .fail ;
3
+ import static org .junit .jupiter .api .Assertions .*;
4
+ import static org .mockito .Mockito .doNothing ;
5
+ import static org .mockito .Mockito .when ;
5
6
6
7
import com .google .common .collect .Maps ;
7
8
import io .harness .cf .client .connector .Connector ;
9
+ import io .harness .cf .client .connector .ConnectorException ;
8
10
import io .harness .cf .client .dto .Target ;
9
- import io .harness .cf .model .FeatureConfig ;
10
- import io .harness .cf .model .Metrics ;
11
- import io .harness .cf .model .Variation ;
12
- import java .util .HashSet ;
13
- import java .util .Map ;
14
- import java .util .Set ;
11
+ import io .harness .cf .model .*;
12
+ import java .util .*;
15
13
import java .util .concurrent .*;
16
14
import lombok .NonNull ;
17
15
import lombok .extern .slf4j .Slf4j ;
18
16
import org .junit .jupiter .api .BeforeEach ;
19
17
import org .junit .jupiter .api .Test ;
18
+ import org .junit .jupiter .params .ParameterizedTest ;
19
+ import org .junit .jupiter .params .provider .ValueSource ;
20
20
import org .mockito .*;
21
21
22
22
@ Slf4j
@@ -141,7 +141,7 @@ public void onMetricsFailure() {
141
141
}
142
142
143
143
@ Test
144
- public void testPrepareSummaryMetricsBody () {
144
+ void testPrepareSummaryMetricsBody () {
145
145
Target target = Target .builder ().identifier ("harness" ).build ();
146
146
FeatureConfig feature = FeatureConfig .builder ().feature ("bool-flag" ).build ();
147
147
Variation variation = Variation .builder ().identifier ("true" ).value ("true" ).build ();
@@ -150,12 +150,69 @@ public void testPrepareSummaryMetricsBody() {
150
150
Set <Target > uniqueTargets = new HashSet <>();
151
151
uniqueTargets .add (target );
152
152
153
- Map <MetricEvent , Long > map = Maps .newHashMap ();
154
- map .put (event , 6L );
153
+ Map <MetricEvent , Long > freqMap = Maps .newHashMap ();
154
+ freqMap .put (event , 6L );
155
155
156
- Metrics metrics = metricsProcessor .prepareSummaryMetricsBody (map , uniqueTargets );
156
+ Metrics metrics = metricsProcessor .prepareSummaryMetricsBody (freqMap , uniqueTargets );
157
157
158
- assert metrics .getTargetData () != null ;
159
- assert metrics .getTargetData ().get (1 ).getIdentifier ().equals (target .getIdentifier ());
158
+ assertNotNull (metrics );
159
+ assertNotNull (metrics .getTargetData ());
160
+ assertNotNull (metrics .getMetricsData ());
161
+
162
+ assertEquals (target .getIdentifier (), metrics .getTargetData ().get (0 ).getIdentifier ());
163
+ assertEquals (6 , metrics .getMetricsData ().get (0 ).getCount ());
164
+ }
165
+
166
+ @ ParameterizedTest
167
+ @ ValueSource (booleans = {true , false })
168
+ void shouldPostCorrectMetrics_WhenGlobalTargetEnabledOrDisabled (boolean globalTargetEnabled )
169
+ throws ConnectorException {
170
+ final Connector mockConnector = Mockito .mock (Connector .class );
171
+ final BaseConfig mockConfig = Mockito .mock (BaseConfig .class );
172
+ final ArgumentCaptor <Metrics > metricsArgumentCaptor = ArgumentCaptor .forClass (Metrics .class );
173
+
174
+ when (mockConfig .isGlobalTargetEnabled ()).thenReturn (globalTargetEnabled );
175
+ when (mockConfig .getBufferSize ()).thenReturn (10 );
176
+ doNothing ().when (mockConnector ).postMetrics (metricsArgumentCaptor .capture ());
177
+
178
+ final MetricsProcessor processor =
179
+ new MetricsProcessor (mockConnector , mockConfig , Mockito .mock (MetricsCallback .class ));
180
+
181
+ final Target target = Target .builder ().identifier ("target123" ).build ();
182
+ final Variation variation = Variation .builder ().identifier ("true" ).value ("true" ).build ();
183
+ processor .pushToQueue (target , "feature1" , variation );
184
+ processor .pushToQueue (target , "feature1" , variation );
185
+ processor .pushToQueue (target , "feature2" , variation );
186
+ processor .pushToQueue (target , "feature2" , variation );
187
+ processor .pushToQueue (target , "feature3" , variation );
188
+ processor .pushToQueue (target , "feature3" , variation );
189
+ processor .runOneIteration (); // Mimic scheduled job
190
+
191
+ final Metrics sentMetrics = metricsArgumentCaptor .getValue ();
192
+
193
+ assertNotNull (sentMetrics .getMetricsData ());
194
+ assertNotNull (sentMetrics .getTargetData ());
195
+
196
+ assertEquals (1 , sentMetrics .getTargetData ().size ());
197
+ assertEquals ("target123" , sentMetrics .getTargetData ().get (0 ).getIdentifier ());
198
+
199
+ assertEquals (3 , sentMetrics .getMetricsData ().size ());
200
+ for (MetricsData md : sentMetrics .getMetricsData ()) {
201
+ final Map <String , String > map = keyValueArrayToMap (md .getAttributes ());
202
+ assertEquals (2 , md .getCount ());
203
+ if (globalTargetEnabled ) {
204
+ assertEquals ("__global__cf_target" , map .get ("target" ));
205
+ } else {
206
+ assertEquals ("target123" , map .get ("target" ));
207
+ }
208
+ }
209
+ }
210
+
211
+ private Map <String , String > keyValueArrayToMap (List <KeyValue > keyValueList ) {
212
+ final Map <String , String > map = new HashMap <>();
213
+ for (KeyValue kv : keyValueList ) {
214
+ map .put (kv .getKey (), kv .getValue ());
215
+ }
216
+ return map ;
160
217
}
161
218
}
0 commit comments