Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwinship committed Feb 21, 2024
1 parent e71ad91 commit 05552a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package edu.harvard.iq.dataverse.util.cache;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.authorization.users.GuestUser;
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.util.SystemConfig;
import jakarta.json.*;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbException;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import javax.cache.Cache;
import java.io.StringReader;
import java.util.ArrayList;
Expand Down Expand Up @@ -111,9 +114,10 @@ static void getRateLimitsFromJson(SystemConfig systemConfig) {
JsonReader jr = Json.createReader(new StringReader(setting));
JsonObject obj= jr.readObject();
JsonArray lst = obj.getJsonArray("rateLimits");
rateLimits.addAll(JsonbBuilder.create().fromJson(String.valueOf(lst),
Gson gson = new Gson();
rateLimits.addAll(gson.fromJson(String.valueOf(lst),
new ArrayList<RateLimitSetting>() {}.getClass().getGenericSuperclass()));
} catch (JsonException | JsonbException e) {
} catch (JsonException | JsonParseException e) {
logger.warning("Unable to parse Rate Limit Json: " + e.getLocalizedMessage() + " Json:(" + setting + ")");
rateLimits.add(new RateLimitSetting()); // add a default entry to prevent re-initialization
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ static String getJsonSetting() {

@BeforeAll
public static void setUp() {
doReturn(getJsonSetting()).when(mockedSystemConfig).getRateLimitsJson();
doReturn("100,200").when(mockedSystemConfig).getRateLimitingDefaultCapacityTiers();
doReturn(settingJsonBad).when(mockedSystemConfigBad).getRateLimitsJson();
doReturn("100,200").when(mockedSystemConfigBad).getRateLimitingDefaultCapacityTiers();
}
@BeforeEach
public void resetSettings() {
doReturn(getJsonSetting()).when(mockedSystemConfig).getRateLimitsJson();
doReturn("100,200").when(mockedSystemConfig).getRateLimitingDefaultCapacityTiers();
public void resetRateLimitUtilSettings() {
RateLimitUtil.rateLimitMap.clear();
RateLimitUtil.rateLimits.clear();
}
Expand Down Expand Up @@ -123,25 +123,32 @@ public void testGenerateCacheKey() {
}
@Test
public void testGetCapacity() {
SystemConfig config = mock(SystemConfig.class);
resetRateLimitUtil(config, true);

GuestUser guestUser = GuestUser.get();
assertEquals(10, RateLimitUtil.getCapacity(mockedSystemConfig, guestUser, "GetPrivateUrlCommand"));
assertEquals(10, RateLimitUtil.getCapacity(config, guestUser, "GetPrivateUrlCommand"));

AuthenticatedUser authUser = new AuthenticatedUser();
authUser.setRateLimitTier(1);
assertEquals(30, RateLimitUtil.getCapacity(mockedSystemConfig, authUser, "GetPrivateUrlCommand"));
assertEquals(30, RateLimitUtil.getCapacity(config, authUser, "GetPrivateUrlCommand"));
authUser.setSuperuser(true);
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, authUser, "GetPrivateUrlCommand"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, authUser, "GetPrivateUrlCommand"));

// no setting means rate limiting is not on
doReturn("").when(mockedSystemConfig).getRateLimitsJson();
doReturn("").when(mockedSystemConfig).getRateLimitingDefaultCapacityTiers();
resetRateLimitUtil(config, false);

assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, guestUser, "GetPrivateUrlCommand"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, guestUser, "xyz"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, authUser, "GetPrivateUrlCommand"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, authUser, "abc"));
authUser.setRateLimitTier(99);
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(config, authUser, "def"));
}
private void resetRateLimitUtil(SystemConfig config, boolean enable) {
doReturn(enable ? getJsonSetting() : "").when(config).getRateLimitsJson();
doReturn(enable ? "100,200" : "").when(config).getRateLimitingDefaultCapacityTiers();
RateLimitUtil.rateLimitMap.clear();
RateLimitUtil.rateLimits.clear();
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, guestUser, "GetPrivateUrlCommand"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, guestUser, "xyz"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, authUser, "GetPrivateUrlCommand"));
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, authUser, "abc"));
authUser.setRateLimitTier(99);
assertEquals(RateLimitUtil.NO_LIMIT, RateLimitUtil.getCapacity(mockedSystemConfig, authUser, "def"));
}
}

0 comments on commit 05552a5

Please sign in to comment.