Add Research Megaphone.

This commit is contained in:
Cody Henthorne
2020-09-18 17:32:56 -04:00
committed by Greyson Parrelli
parent 9dbb77c10a
commit ca442970a3
28 changed files with 685 additions and 67 deletions

View File

@@ -0,0 +1,26 @@
package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.signal.zkgroup.util.UUIDUtil;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.UUID;
import static org.junit.Assert.*;
public class BucketingUtilTest {
@Test
public void bucket() {
// GIVEN
String key = "research.megaphone.1";
UUID uuid = UuidUtil.parseOrThrow("15b9729c-51ea-4ddb-b516-652befe78062");
long partPer = 1_000_000;
// WHEN
long countEnabled = BucketingUtil.bucket(key, uuid, partPer);
// THEN
assertEquals(243315, countEnabled);
}
}

View File

@@ -0,0 +1,85 @@
package org.thoughtcrime.securesms.util;
import androidx.annotation.NonNull;
import com.google.protobuf.Empty;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.testutil.EmptyLogger;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@RunWith(Parameterized.class)
public class ResearchMegaphoneTest_determineCountEnabled {
private final String phoneNumber;
private final Map<String, Integer> countryCounts;
private final long output;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"+1 555 555 5555", new HashMap<String, Integer>() {{
put("1", 10000);
put("*", 400);
}}, 10000},
{"+1 555 555 5555", new HashMap<String, Integer>() {{
put("011", 1000);
put("1", 20000);
}}, 20000},
{"+1 555 555 5555", new HashMap<String, Integer>() {{
put("011", 1000);
put("a", 123);
put("abba", 0);
}}, 0},
{"+1 555", new HashMap<String, Integer>() {{
put("011", 1000);
put("1", 1000);
}}, 1000},
{"+81 555 555 5555", new HashMap<String, Integer>() {{
put("81", 6000);
put("1", 1000);
put("*", 2000);
}}, 6000},
{"+81 555 555 5555", new HashMap<String, Integer>() {{
put("0011", 6000);
put("1", 1000);
put("*", 2000);
}}, 2000},
{"+49 555 555 5555", new HashMap<String, Integer>() {{
put("0011", 6000);
put("1", 1000);
put("*", 2000);
}}, 2000}
});
}
@BeforeClass
public static void setup() {
Log.initialize(new EmptyLogger());
}
public ResearchMegaphoneTest_determineCountEnabled(@NonNull String phoneNumber,
@NonNull Map<String, Integer> countryCounts,
long output)
{
this.phoneNumber = phoneNumber;
this.countryCounts = countryCounts;
this.output = output;
}
@Test
public void determineCountEnabled() {
assertEquals(output, ResearchMegaphone.determineCountEnabled(countryCounts, phoneNumber));
}
}

View File

@@ -0,0 +1,59 @@
package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class ResearchMegaphoneTest_parseCountryCounts {
private final String input;
private final Map<String, Integer> output;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"1:10000,*:400", new HashMap<String, Integer>() {{
put("1", 10000);
put("*", 400);
}}},
{"011:1000,1:1000", new HashMap<String, Integer>() {{
put("011", 1000);
put("1", 1000);
}}},
{"011:1000,1:1000,a:123,abba:abba", new HashMap<String, Integer>() {{
put("011", 1000);
put("1", 1000);
put("a", 123);
put("abba", 0);
}}},
{":,011:1000,1:1000,1:,:1,1:1:1", new HashMap<String, Integer>() {{
put("011", 1000);
put("1", 1000);
}}},
{"asdf", new HashMap<String, Integer>()},
{"asdf:", new HashMap<String, Integer>()},
{":,:,:", new HashMap<String, Integer>()},
{",,", new HashMap<String, Integer>()},
{"", new HashMap<String, Integer>()}
});
}
public ResearchMegaphoneTest_parseCountryCounts(String input, Map<String, Integer> output) {
this.input = input;
this.output = output;
}
@Test
public void parseCountryCounts() {
assertEquals(output, ResearchMegaphone.parseCountryCounts(input));
}
}