Skip to content

Commit

Permalink
Fix test; refactor duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Sep 7, 2023
1 parent a0b9a08 commit f4fd41f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public Map<String, String> getTreatments(List<String> featureFlagNames, Map<Stri
Logger.e(exception);

Map<String, String> result = new HashMap<>();

for (String featureFlagName : featureFlagNames) {
result.put(featureFlagName, Treatments.CONTROL);
}
Expand All @@ -132,13 +131,7 @@ public Map<String, SplitResult> getTreatmentsWithConfig(List<String> featureFlag
} catch (Exception exception) {
Logger.e(exception);

Map<String, SplitResult> result = new HashMap<>();

for (String featureFlagName : featureFlagNames) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}

return result;
return buildExceptionResultWithConfig(featureFlagNames);
}
}

Expand All @@ -149,13 +142,7 @@ public Map<String, String> getTreatmentsByFlagSet(@NonNull String flagSet, @Null
} catch (Exception exception) {
Logger.e(exception);

Map<String, String> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(Collections.singletonList(flagSet));
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, Treatments.CONTROL);
}

return result;
return buildExceptionResult(Collections.singletonList(flagSet));
}
}

Expand All @@ -166,13 +153,7 @@ public Map<String, String> getTreatmentsByFlagSets(@NonNull List<String> flagSet
} catch (Exception exception) {
Logger.e(exception);

Map<String, String> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(flagSets);
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, Treatments.CONTROL);
}

return result;
return buildExceptionResult(flagSets);
}
}

Expand All @@ -183,13 +164,7 @@ public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(@NonNull String
} catch (Exception exception) {
Logger.e(exception);

Map<String, SplitResult> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(Collections.singletonList(flagSet));
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}

return result;
return buildExceptionResultWithConfig(Collections.singletonList(flagSet));
}
}

Expand All @@ -200,13 +175,7 @@ public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(@NonNull List<
} catch (Exception exception) {
Logger.e(exception);

Map<String, SplitResult> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(flagSets);
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}

return result;
return buildExceptionResultWithConfig(flagSets);
}
}

Expand Down Expand Up @@ -324,4 +293,23 @@ public boolean removeAttribute(String attributeName) {
public boolean clearAttributes() {
return true;
}

@NonNull
private Map<String, String> buildExceptionResult(@NonNull List<String> flagSets) {
Map<String, String> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(flagSets);
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, Treatments.CONTROL);
}
return result;
}

@NonNull
private static Map<String, SplitResult> buildExceptionResultWithConfig(List<String> featureFlagNames) {
Map<String, SplitResult> result = new HashMap<>();
for (String featureFlagName : featureFlagNames) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -72,7 +73,7 @@ public void setUp() throws Exception {
splitClientConfig,
"api_key",
"test_database_name",
SplitFilter.bySet(Arrays.asList("set_1", "set_2"))
Collections.singletonList(SplitFilter.bySet(Arrays.asList("set_1", "set_2")))
);
}

Expand Down

0 comments on commit f4fd41f

Please sign in to comment.