1
1
/****************************************************************************
2
- * Copyright 2020-2021, Optimizely, Inc. and contributors *
2
+ * Copyright 2020-2021, 2023, Optimizely, Inc. and contributors *
3
3
* *
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); *
5
5
* you may not use this file except in compliance with the License. *
15
15
***************************************************************************/
16
16
package com .optimizely .ab .optimizelyconfig ;
17
17
18
+ import ch .qos .logback .classic .Level ;
18
19
import com .optimizely .ab .config .*;
19
20
import com .optimizely .ab .config .audience .Audience ;
21
+ import com .optimizely .ab .internal .LogbackVerifier ;
20
22
import org .junit .Before ;
23
+ import org .junit .Rule ;
21
24
import org .junit .Test ;
22
- import org .junit .runner .RunWith ;
23
- import org .mockito .runners .MockitoJUnitRunner ;
24
25
25
26
import java .util .*;
26
27
import static java .util .Arrays .asList ;
27
28
import static org .junit .Assert .*;
29
+ import static org .mockito .Mockito .mock ;
30
+ import static org .mockito .Mockito .when ;
28
31
29
32
public class OptimizelyConfigServiceTest {
30
33
31
34
private ProjectConfig projectConfig ;
32
35
private OptimizelyConfigService optimizelyConfigService ;
33
36
private OptimizelyConfig expectedConfig ;
34
37
38
+ @ Rule
39
+ public LogbackVerifier logbackVerifier = new LogbackVerifier ();
40
+
35
41
@ Before
36
42
public void initialize () {
37
43
projectConfig = generateOptimizelyConfig ();
@@ -46,6 +52,33 @@ public void testGetExperimentsMap() {
46
52
assertEquals (expectedConfig .getExperimentsMap (), optimizelyExperimentMap );
47
53
}
48
54
55
+ @ Test
56
+ public void testGetExperimentsMapWithDuplicateKeys () {
57
+ List <Experiment > experiments = Arrays .asList (
58
+ new Experiment (
59
+ "first" ,
60
+ "duplicate_key" ,
61
+ null , null , Collections .<String >emptyList (), null ,
62
+ Collections .<Variation >emptyList (), Collections .<String , String >emptyMap (), Collections .<TrafficAllocation >emptyList ()
63
+ ),
64
+ new Experiment (
65
+ "second" ,
66
+ "duplicate_key" ,
67
+ null , null , Collections .<String >emptyList (), null ,
68
+ Collections .<Variation >emptyList (), Collections .<String , String >emptyMap (), Collections .<TrafficAllocation >emptyList ()
69
+ )
70
+ );
71
+
72
+ ProjectConfig projectConfig = mock (ProjectConfig .class );
73
+ OptimizelyConfigService optimizelyConfigService = new OptimizelyConfigService (projectConfig );
74
+ when (projectConfig .getExperiments ()).thenReturn (experiments );
75
+
76
+ Map <String , OptimizelyExperiment > optimizelyExperimentMap = optimizelyConfigService .getExperimentsMap ();
77
+ assertEquals ("Duplicate keys should be overwritten" , optimizelyExperimentMap .size (), 1 );
78
+ assertEquals ("Duplicate keys should be overwritten" , optimizelyExperimentMap .get ("duplicate_key" ).getId (), "second" );
79
+ logbackVerifier .expectMessage (Level .WARN , "Duplicate experiment keys found in datafile: duplicate_key" );
80
+ }
81
+
49
82
@ Test
50
83
public void testRevision () {
51
84
String revision = optimizelyConfigService .getConfig ().getRevision ();
0 commit comments