Skip to content

Commit

Permalink
Change key to ID in all variable names within UserProfile (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh Raja authored Jan 18, 2017
1 parent 38b060a commit a4e843b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,131 +80,131 @@ public void teardown() {
@Test
public void saveActivation() {
String userId = "user1";
String expKey = "exp1";
String varKey = "var1";
assertTrue(androidUserProfile.save(userId, expKey, varKey));
String expId = "1";
String varId = "2";
assertTrue(androidUserProfile.save(userId, expId, varId));
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Time out");
}
assertEquals(varKey, androidUserProfile.lookup(userId, expKey));
assertEquals(varId, androidUserProfile.lookup(userId, expId));
}

@Test
public void saveActivationNullUserId() {
assertFalse(androidUserProfile.save(null, "exp1", "var1"));
verify(logger).error("Received null userId, unable to save activation");
assertFalse(androidUserProfile.save(null, "1", "2"));
verify(logger).error("Received null userId, unable to save activation.");
}

@Test
public void saveActivationNullExperimentKey() {
assertFalse(androidUserProfile.save("foo", null, "var1"));
verify(logger).error("Received null experiment key, unable to save activation");
public void saveActivationNullExperimentId() {
assertFalse(androidUserProfile.save("foo", null, "1"));
verify(logger).error("Received null experiment ID, unable to save activation.");
}

@Test
public void saveActivationNullVariationKey() {
assertFalse(androidUserProfile.save("foo", "exp1", null));
verify(logger).error("Received null variation key, unable to save activation");
public void saveActivationNullVariationId() {
assertFalse(androidUserProfile.save("foo", "1", null));
verify(logger).error("Received null variation ID, unable to save activation.");
}

@Test
public void saveActivationEmptyUserId() {
assertFalse(androidUserProfile.save("", "exp1", "var1"));
verify(logger).error("Received empty user id, unable to save activation");
assertFalse(androidUserProfile.save("", "1", "2"));
verify(logger).error("Received empty user ID, unable to save activation.");
}

@Test
public void saveActivationEmptyExperimentKey() {
assertFalse(androidUserProfile.save("foo", "", "var1"));
verify(logger).error("Received empty experiment key, unable to save activation");
public void saveActivationEmptyExperimentId() {
assertFalse(androidUserProfile.save("foo", "", "1"));
verify(logger).error("Received empty experiment ID, unable to save activation.");
}

@Test
public void saveActivationEmptyVariationKey() {
assertFalse(androidUserProfile.save("foo", "exp1", ""));
verify(logger).error("Received empty variation key, unable to save activation");
public void saveActivationEmptyVariationId() {
assertFalse(androidUserProfile.save("foo", "1", ""));
verify(logger).error("Received empty variation ID, unable to save activation.");
}

@Test
public void lookupActivationNullUserId() {
assertNull(androidUserProfile.lookup(null, "exp1"));
verify(logger).error("Received null user id, unable to lookup activation");
assertNull(androidUserProfile.lookup(null, "1"));
verify(logger).error("Received null user ID, unable to lookup activation.");
}

@Test
public void lookupActivationNullExperimentKey() {
assertNull(androidUserProfile.lookup("foo", null));
verify(logger).error("Received null experiment key, unable to lookup activation");
public void lookupActivationNullExperimentId() {
assertNull(androidUserProfile.lookup("1", null));
verify(logger).error("Received null experiment ID, unable to lookup activation.");
}

@Test
public void lookupActivationEmptyUserId() {
assertNull(androidUserProfile.lookup("", "exp1"));
verify(logger).error("Received empty user id, unable to lookup activation");
assertNull(androidUserProfile.lookup("", "1"));
verify(logger).error("Received empty user ID, unable to lookup activation.");
}

@Test
public void lookupActivationEmptyExperimentKey() {
public void lookupActivationEmptyExperimentId() {
assertNull(androidUserProfile.lookup("foo", ""));
verify(logger).error("Received empty experiment key, unable to lookup activation");
verify(logger).error("Received empty experiment ID, unable to lookup activation.");
}

@Test
public void removeExistingActivation() {
androidUserProfile.save("user1", "exp1", "var1");
assertTrue(androidUserProfile.remove("user1", "exp1"));
assertNull(androidUserProfile.lookup("user1", "exp1"));
androidUserProfile.save("user1", "1", "2");
assertTrue(androidUserProfile.remove("user1", "1"));
assertNull(androidUserProfile.lookup("user1", "1"));
}

@Test
public void removeNonExistingActivation() {
assertFalse(androidUserProfile.remove("user1", "exp1"));
assertFalse(androidUserProfile.remove("user1", "1"));
}

@Test
public void removeActivationNullUserId() {
assertFalse(androidUserProfile.remove(null, "exp1"));
verify(logger).error("Received null user id, unable to remove activation");
assertFalse(androidUserProfile.remove(null, "1"));
verify(logger).error("Received null user ID, unable to remove activation.");
}

@Test
public void removeActivationNullExperimentKey() {
public void removeActivationNullExperimentId() {
assertFalse(androidUserProfile.remove("foo", null));
verify(logger).error("Received null experiment key, unable to remove activation");
verify(logger).error("Received null experiment ID, unable to remove activation.");
}

@Test
public void removeActivationEmptyUserId() {
assertFalse(androidUserProfile.remove("", "exp1"));
verify(logger).error("Received empty user id, unable to remove activation");
assertFalse(androidUserProfile.remove("", "1"));
verify(logger).error("Received empty user ID, unable to remove activation.");
}

@Test
public void removeActivationEmptyExperimentKey() {
public void removeActivationEmptyExperimentId() {
assertFalse(androidUserProfile.remove("foo", ""));
verify(logger).error("Received empty experiment key, unable to remove activation");
verify(logger).error("Received empty experiment ID, unable to remove activation.");
}

@Test
public void startHandlesJSONException() throws IOException {
assertTrue(cache.save(diskUserProfileCache.getFileName(), "{"));
androidUserProfile.start();
verify(logger).error(eq("Unable to parse user profile cache"), any(JSONException.class));
verify(logger).error(eq("Unable to parse user profile cache."), any(JSONException.class));
}

@Test
public void start() throws JSONException {
androidUserProfile.start();
androidUserProfile.save("user1", "exp1", "var1");
androidUserProfile.save("user1", "exp2", "var2");
androidUserProfile.save("user1", "1", "3");
androidUserProfile.save("user1", "2", "4");

Map<String, String> expKeyToVarKeyMap = new HashMap<>();
expKeyToVarKeyMap.put("exp1", "var1");
expKeyToVarKeyMap.put("exp2", "var2");
Map<String, String> expIdToVarIdMap = new HashMap<>();
expIdToVarIdMap.put("1", "3");
expIdToVarIdMap.put("2", "4");
Map<String, Map<String, String>> profileMap = new HashMap<>();
profileMap.put("user1", expKeyToVarKeyMap);
profileMap.put("user1", expIdToVarIdMap);

assertEquals(profileMap, memoryUserProfileCache);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ public void startWriteCacheTask() throws JSONException {
new AndroidUserProfile.WriteThroughCacheTaskFactory(diskUserProfileCache,
new HashMap<String, Map<String, String>>(), executor, logger);

writeThroughCacheTaskFactory.startWriteCacheTask("user1", "exp1", "var1");
writeThroughCacheTaskFactory.startWriteCacheTask("user1", "1", "2");
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}

verify(logger).info("Updated in memory user profile");
verify(logger).info("Updated in memory user profile.");
assertTrue(writeThroughCacheTaskFactory.getMemoryUserProfileCache().containsKey("user1"));
Map<String, String> activation = writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1");
assertTrue(activation.containsKey("exp1"));
assertEquals("var1", activation.get("exp1"));
assertTrue(activation.containsKey("1"));
assertEquals("2", activation.get("1"));

final JSONObject json = diskUserProfileCache.load();
assertTrue(json.has("user1"));
final JSONObject user1 = json.getJSONObject("user1");
assertTrue(user1.has("exp1"));
assertEquals("var1", user1.getString("exp1"));
verify(logger).info("Persisted user in variation {} for experiment {}.", "var1", "exp1");
assertTrue(user1.has("1"));
assertEquals("2", user1.getString("1"));
verify(logger).info("Persisted user in variation {} for experiment {}.", "2", "1");

cache.delete(diskUserProfileCache.getFileName());
}
Expand All @@ -105,19 +105,19 @@ public void startWriteCacheTaskFail() throws JSONException {

when(cache.save(diskUserProfileCache.getFileName(), json.toString())).thenReturn(false);

writeThroughCacheTaskFactory.startWriteCacheTask("user1", "exp1", "var1");
writeThroughCacheTaskFactory.startWriteCacheTask("user1", "1", "2");
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}

verify(logger).info("Updated in memory user profile");
verify(logger).info("Updated in memory user profile.");
assertTrue(writeThroughCacheTaskFactory.getMemoryUserProfileCache().containsKey("user1"));
Map<String, String> activation = writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1");
assertFalse(activation.containsKey("exp1"));
assertFalse(activation.containsKey("1"));

verify(logger).error("Failed to persist user in variation {} for experiment {}.", "var1", "exp1");
verify(logger).error("Failed to persist user in variation {} for experiment {}.", "2", "1");
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
Expand All @@ -129,26 +129,26 @@ public void startRemoveCacheTask() throws JSONException, IOException {
new AndroidUserProfile.WriteThroughCacheTaskFactory(diskUserProfileCache,
new HashMap<String, Map<String, String>>(), executor, logger);

diskUserProfileCache.save("user1", "exp1", "var1");
diskUserProfileCache.save("user1", "1", "2");

Map<String, String> activation = new HashMap<>();
activation.put("exp1", "var1");
activation.put("1", "2");
writeThroughCacheTaskFactory.getMemoryUserProfileCache().put("user1", activation);

writeThroughCacheTaskFactory.startRemoveCacheTask("user1", "exp1", "var1");
writeThroughCacheTaskFactory.startRemoveCacheTask("user1", "1", "2");
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}

verify(logger).info("Removed experimentKey: {} variationKey: {} record for user: {} from memory", "exp1", "var1", "user1");
verify(logger).info("Removed experimentId: {} variationId: {} record for user: {} from memory.", "1", "2", "user1");
JSONObject json = diskUserProfileCache.load();
assertTrue(json.has("user1"));
json = json.getJSONObject("user1");
assertFalse(json.has("exp1"));
assertFalse(writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1").containsKey("exp1"));
verify(logger).info("Removed experimentKey: {} variationKey: {} record for user: {} from disk", "exp1", "var1", "user1");
assertFalse(json.has("1"));
assertFalse(writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1").containsKey("1"));
verify(logger).info("Removed experimentId: {} variationId: {} record for user: {} from disk.", "1", "2", "user1");
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
Expand All @@ -163,25 +163,25 @@ public void startRemoveCacheTaskFail() throws JSONException, IOException {
when(cache.save(diskUserProfileCache.getFileName(), getJsonObject().toString())).thenReturn(false);

Map<String, String> activation = new HashMap<>();
activation.put("exp1", "var1");
activation.put("1", "2");
writeThroughCacheTaskFactory.getMemoryUserProfileCache().put("user1", activation);
writeThroughCacheTaskFactory.startRemoveCacheTask("user1", "exp1", "var1");
writeThroughCacheTaskFactory.startRemoveCacheTask("user1", "1", "2");
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}

verify(logger).info("Removed experimentKey: {} variationKey: {} record for user: {} from memory", "exp1", "var1", "user1");
verify(logger).error("Restored experimentKey: {} variationKey: {} record for user: {} to memory", "exp1", "var1","user1");
assertTrue(writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1").containsKey("exp1"));
verify(logger).info("Removed experimentId: {} variationId: {} record for user: {} from memory.", "1", "2", "user1");
verify(logger).error("Restored experimentId: {} variationId: {} record for user: {} to memory.", "1", "2","user1");
assertTrue(writeThroughCacheTaskFactory.getMemoryUserProfileCache().get("user1").containsKey("1"));
}

@NonNull
private JSONObject getJsonObject() throws JSONException {
JSONObject json = new JSONObject();
JSONObject user1 = new JSONObject();
user1.put("exp1", "var1");
user1.put("1", "2");
json.put("user1", user1);
return json;
}
Expand Down
Loading

0 comments on commit a4e843b

Please sign in to comment.