11/****************************************************************************
2- * Copyright 2020-2021, Optimizely, Inc. and contributors *
2+ * Copyright 2020-2021, 2023, Optimizely, Inc. and contributors *
33 * *
44 * Licensed under the Apache License, Version 2.0 (the "License"); *
55 * you may not use this file except in compliance with the License. *
1515 ***************************************************************************/
1616package com .optimizely .ab .optimizelyconfig ;
1717
18+ import ch .qos .logback .classic .Level ;
1819import com .optimizely .ab .config .*;
1920import com .optimizely .ab .config .audience .Audience ;
21+ import com .optimizely .ab .internal .LogbackVerifier ;
2022import org .junit .Before ;
23+ import org .junit .Rule ;
2124import org .junit .Test ;
22- import org .junit .runner .RunWith ;
23- import org .mockito .runners .MockitoJUnitRunner ;
2425
2526import java .util .*;
2627import static java .util .Arrays .asList ;
2728import static org .junit .Assert .*;
29+ import static org .mockito .Mockito .mock ;
30+ import static org .mockito .Mockito .when ;
2831
2932public class OptimizelyConfigServiceTest {
3033
3134 private ProjectConfig projectConfig ;
3235 private OptimizelyConfigService optimizelyConfigService ;
3336 private OptimizelyConfig expectedConfig ;
3437
38+ @ Rule
39+ public LogbackVerifier logbackVerifier = new LogbackVerifier ();
40+
3541 @ Before
3642 public void initialize () {
3743 projectConfig = generateOptimizelyConfig ();
@@ -46,6 +52,33 @@ public void testGetExperimentsMap() {
4652 assertEquals (expectedConfig .getExperimentsMap (), optimizelyExperimentMap );
4753 }
4854
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+
4982 @ Test
5083 public void testRevision () {
5184 String revision = optimizelyConfigService .getConfig ().getRevision ();
0 commit comments