11package com .launchdarkly .sdk .server .integrations ;
22
3- import com .google .gson .Gson ;
4- import com .google .gson .JsonElement ;
5- import com .google .gson .JsonObject ;
63import com .launchdarkly .sdk .LDValue ;
74import com .launchdarkly .sdk .server .integrations .FileDataSourceImpl .DataBuilder ;
85import com .launchdarkly .sdk .server .integrations .FileDataSourceImpl .DataLoader ;
2926
3027@ SuppressWarnings ("javadoc" )
3128public class DataLoaderTest {
32- private static final Gson gson = new Gson ();
33- private DataBuilder builder = new DataBuilder ();
29+ private DataBuilder builder = new DataBuilder (FileData .DuplicateKeysHandling .FAIL );
3430
3531 @ Test
3632 public void canLoadFromFilePath () throws Exception {
@@ -75,22 +71,20 @@ public void canLoadMultipleFiles() throws Exception {
7571 @ Test
7672 public void flagValueIsConvertedToFlag () throws Exception {
7773 DataLoader ds = new DataLoader (FileData .dataSource ().filePaths (resourceFilePath ("value-only.json" )).sources );
78- JsonObject expected = gson . fromJson (
74+ LDValue expected = LDValue . parse (
7975 "{\" key\" :\" flag2\" ,\" on\" :true,\" fallthrough\" :{\" variation\" :0},\" variations\" :[\" value2\" ]," +
80- "\" trackEvents\" :false,\" deleted\" :false,\" version\" :1}" ,
81- JsonObject .class );
76+ "\" trackEvents\" :false,\" deleted\" :false,\" version\" :1}" );
8277 ds .load (builder );
83- ItemDescriptor flag = toDataMap (builder .build ()).get (FEATURES ).get (FLAG_VALUE_1_KEY );
84- JsonObject actual = gson .toJsonTree (flag .getItem ()).getAsJsonObject ();
78+ LDValue actual = getItemAsJson (builder , FEATURES , FLAG_VALUE_1_KEY );
8579 // Note, we're comparing one property at a time here because the version of the Java SDK we're
8680 // building against may have more properties than it did when the test was written.
87- for (Map . Entry < String , JsonElement > e : expected .entrySet ()) {
88- assertThat (actual .get (e . getKey ()) , equalTo (e . getValue ( )));
81+ for (String key : expected .keys ()) {
82+ assertThat (actual .get (key ) , equalTo (expected . get ( key )));
8983 }
9084 }
9185
9286 @ Test
93- public void duplicateFlagKeyInFlagsThrowsException () throws Exception {
87+ public void duplicateFlagKeyInFlagsThrowsExceptionByDefault () throws Exception {
9488 try {
9589 DataLoader ds = new DataLoader (FileData .dataSource ().filePaths (
9690 resourceFilePath ("flag-only.json" ),
@@ -103,7 +97,7 @@ public void duplicateFlagKeyInFlagsThrowsException() throws Exception {
10397 }
10498
10599 @ Test
106- public void duplicateFlagKeyInFlagsAndFlagValuesThrowsException () throws Exception {
100+ public void duplicateFlagKeyInFlagsAndFlagValuesThrowsExceptionByDefault () throws Exception {
107101 try {
108102 DataLoader ds = new DataLoader (FileData .dataSource ().filePaths (
109103 resourceFilePath ("flag-only.json" ),
@@ -116,7 +110,7 @@ public void duplicateFlagKeyInFlagsAndFlagValuesThrowsException() throws Excepti
116110 }
117111
118112 @ Test
119- public void duplicateSegmentKeyThrowsException () throws Exception {
113+ public void duplicateSegmentKeyThrowsExceptionByDefault () throws Exception {
120114 try {
121115 DataLoader ds = new DataLoader (FileData .dataSource ().filePaths (
122116 resourceFilePath ("segment-only.json" ),
@@ -128,6 +122,35 @@ public void duplicateSegmentKeyThrowsException() throws Exception {
128122 }
129123 }
130124
125+ @ Test
126+ public void duplicateKeysCanBeAllowed () throws Exception {
127+ DataBuilder data1 = new DataBuilder (FileData .DuplicateKeysHandling .IGNORE );
128+ DataLoader loader1 = new DataLoader (FileData .dataSource ().filePaths (
129+ resourceFilePath ("flag-only.json" ),
130+ resourceFilePath ("flag-with-duplicate-key.json" )
131+ ).sources );
132+ loader1 .load (data1 );
133+ assertThat (getItemAsJson (data1 , FEATURES , "flag1" ).get ("on" ), equalTo (LDValue .of (true ))); // value from first file
134+
135+ DataBuilder data2 = new DataBuilder (FileData .DuplicateKeysHandling .IGNORE );
136+ DataLoader loader2 = new DataLoader (FileData .dataSource ().filePaths (
137+ resourceFilePath ("value-with-duplicate-key.json" ),
138+ resourceFilePath ("flag-only.json" )
139+ ).sources );
140+ loader2 .load (data2 );
141+ assertThat (getItemAsJson (data2 , FEATURES , "flag2" ).get ("variations" ),
142+ equalTo (LDValue .buildArray ().add (LDValue .of ("value2a" )).build ())); // value from first file
143+
144+ DataBuilder data3 = new DataBuilder (FileData .DuplicateKeysHandling .IGNORE );
145+ DataLoader loader3 = new DataLoader (FileData .dataSource ().filePaths (
146+ resourceFilePath ("segment-only.json" ),
147+ resourceFilePath ("segment-with-duplicate-key.json" )
148+ ).sources );
149+ loader3 .load (data3 );
150+ assertThat (getItemAsJson (data3 , SEGMENTS , "seg1" ).get ("included" ),
151+ equalTo (LDValue .buildArray ().add (LDValue .of ("user1" )).build ())); // value from first file
152+ }
153+
131154 @ Test
132155 public void versionsAreIncrementedForEachLoad () throws Exception {
133156 DataLoader ds = new DataLoader (FileData .dataSource ().filePaths (
@@ -136,11 +159,11 @@ public void versionsAreIncrementedForEachLoad() throws Exception {
136159 resourceFilePath ("value-only.json" )
137160 ).sources );
138161
139- DataBuilder data1 = new DataBuilder ();
162+ DataBuilder data1 = new DataBuilder (FileData . DuplicateKeysHandling . FAIL );
140163 ds .load (data1 );
141164 assertVersionsMatch (data1 .build (), 1 );
142165
143- DataBuilder data2 = new DataBuilder ();
166+ DataBuilder data2 = new DataBuilder (FileData . DuplicateKeysHandling . FAIL );
144167 ds .load (data2 );
145168 assertVersionsMatch (data2 .build (), 2 );
146169 }
@@ -164,4 +187,9 @@ private void assertVersionsMatch(FullDataSet<ItemDescriptor> data, int expectedV
164187 }
165188 }
166189 }
190+
191+ private LDValue getItemAsJson (DataBuilder builder , DataKind kind , String key ) {
192+ ItemDescriptor flag = toDataMap (builder .build ()).get (kind ).get (key );
193+ return LDValue .parse (kind .serialize (flag ));
194+ }
167195}
0 commit comments