mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 21:47:51 +02:00
The quotas can be configured in the yaml configuration file. Default quota will be applied to any userId that is not matched in the custom quota list. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=178804649
71 lines
2.5 KiB
Java
71 lines
2.5 KiB
Java
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google.registry.proxy.quota;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static google.registry.testing.JUnitBackports.expectThrows;
|
|
import static google.registry.util.ResourceUtils.readResourceUtf8;
|
|
|
|
import google.registry.proxy.ProxyConfig.Quota;
|
|
import org.joda.time.Duration;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
/** Unit Tests for {@link QuotaConfig} */
|
|
@RunWith(JUnit4.class)
|
|
public class QuotaConfigTest {
|
|
|
|
private QuotaConfig quotaConfig;
|
|
|
|
private static QuotaConfig loadQuotaConfig(String filename) {
|
|
return new QuotaConfig(
|
|
new Yaml()
|
|
.loadAs(readResourceUtf8(QuotaConfigTest.class, "testdata/" + filename), Quota.class));
|
|
}
|
|
|
|
private void validateQuota(String userId, int tokenAmount, int refillSeconds) {
|
|
assertThat(quotaConfig.getTokenAmount(userId)).isEqualTo(tokenAmount);
|
|
assertThat(quotaConfig.getRefillPeriod(userId))
|
|
.isEqualTo(Duration.standardSeconds(refillSeconds));
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_regularConfig() {
|
|
quotaConfig = loadQuotaConfig("quota_config_regular.yaml");
|
|
assertThat(quotaConfig.getRefreshPeriod()).isEqualTo(Duration.standardSeconds(3600));
|
|
validateQuota("abc", 10, 60);
|
|
validateQuota("987lol", 500, 10);
|
|
validateQuota("no_match", 100, 60);
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_onlyDefault() {
|
|
quotaConfig = loadQuotaConfig("quota_config_default.yaml");
|
|
assertThat(quotaConfig.getRefreshPeriod()).isEqualTo(Duration.standardSeconds(3600));
|
|
validateQuota("abc", 100, 60);
|
|
validateQuota("987lol", 100, 60);
|
|
validateQuota("no_match", 100, 60);
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_duplicateUserId() {
|
|
IllegalArgumentException e =
|
|
expectThrows(
|
|
IllegalArgumentException.class, () -> loadQuotaConfig("quota_config_duplicate.yaml"));
|
|
assertThat(e).hasMessageThat().contains("Multiple entries with same key");
|
|
}
|
|
}
|