Arsnael commented on code in PR #1673: URL: https://github.com/apache/james-project/pull/1673#discussion_r1300885950
########## mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraQuotaCurrentValueDaoTest.java: ########## @@ -0,0 +1,136 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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 org.apache.james.mailbox.cassandra.quota; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatRuntimeException; + +import org.apache.james.backends.cassandra.CassandraClusterExtension; +import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.core.Username; +import org.apache.james.core.quota.QuotaComponent; +import org.apache.james.core.quota.QuotaCurrentValue; +import org.apache.james.core.quota.QuotaType; +import org.apache.james.mailbox.cassandra.mail.utils.GuiceUtils; +import org.apache.james.mailbox.cassandra.modules.CassandraQuotaModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class CassandraQuotaCurrentValueDaoTest { + + private static final CassandraQuotaCurrentValueDao.QuotaKey QUOTA_KEY + = CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.MAILBOX, Username.of("[email protected]"), QuotaType.SIZE); + + private CassandraQuotaCurrentValueDao cassandraQuotaCurrentValueDao; + + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraModule.aggregateModules(CassandraQuotaModule.MODULE)); + + @BeforeEach + private void setup() { + cassandraQuotaCurrentValueDao = GuiceUtils.testInjector(cassandraCluster.getCassandraCluster()).getInstance(CassandraQuotaCurrentValueDao.class); + resetCounterToZero(); Review Comment: Why not just dropping the quota, that it exists or not? Takes one line :) ``` cassandraQuotaCurrentValueDao.deleteQuotaCurrentValue(QUOTA_KEY).block(); ``` ########## mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraQuotaCurrentValueDaoTest.java: ########## @@ -0,0 +1,136 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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 org.apache.james.mailbox.cassandra.quota; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatRuntimeException; + +import org.apache.james.backends.cassandra.CassandraClusterExtension; +import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.core.Username; +import org.apache.james.core.quota.QuotaComponent; +import org.apache.james.core.quota.QuotaCurrentValue; +import org.apache.james.core.quota.QuotaType; +import org.apache.james.mailbox.cassandra.mail.utils.GuiceUtils; +import org.apache.james.mailbox.cassandra.modules.CassandraQuotaModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class CassandraQuotaCurrentValueDaoTest { + + private static final CassandraQuotaCurrentValueDao.QuotaKey QUOTA_KEY + = CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.MAILBOX, Username.of("[email protected]"), QuotaType.SIZE); + + private CassandraQuotaCurrentValueDao cassandraQuotaCurrentValueDao; + + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraModule.aggregateModules(CassandraQuotaModule.MODULE)); + + @BeforeEach + private void setup() { + cassandraQuotaCurrentValueDao = GuiceUtils.testInjector(cassandraCluster.getCassandraCluster()).getInstance(CassandraQuotaCurrentValueDao.class); + resetCounterToZero(); + } + + private void resetCounterToZero() { + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 0).block(); + QuotaCurrentValue quotaCurrentValue = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + cassandraQuotaCurrentValueDao.decrease(QUOTA_KEY, quotaCurrentValue.getCurrentValue()).block(); + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual.getCurrentValue()).isEqualTo(0l); + } + + @Test + void increaseQuotaCurrentValueShouldCreateNewRowSuccessfully() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(100l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldCreateNewRowSuccessfullyWhenIncreaseAmountIsZero() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(0l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 0l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldIncreaseValueSuccessfully() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(200l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldDecreaseValueSuccessfullyWhenIncreaseAmountIsNegative() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(100l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 200l).block(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, -100l).block(); Review Comment: Is that supposed to be possible? Should use decrease instead of increase with negative? Should we have a precondition check on increase and decrease operations that it should be strictly positive numbers? ########## mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraQuotaCurrentValueDaoTest.java: ########## @@ -0,0 +1,136 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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 org.apache.james.mailbox.cassandra.quota; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatRuntimeException; + +import org.apache.james.backends.cassandra.CassandraClusterExtension; +import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.core.Username; +import org.apache.james.core.quota.QuotaComponent; +import org.apache.james.core.quota.QuotaCurrentValue; +import org.apache.james.core.quota.QuotaType; +import org.apache.james.mailbox.cassandra.mail.utils.GuiceUtils; +import org.apache.james.mailbox.cassandra.modules.CassandraQuotaModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class CassandraQuotaCurrentValueDaoTest { + + private static final CassandraQuotaCurrentValueDao.QuotaKey QUOTA_KEY + = CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.MAILBOX, Username.of("[email protected]"), QuotaType.SIZE); + + private CassandraQuotaCurrentValueDao cassandraQuotaCurrentValueDao; + + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraModule.aggregateModules(CassandraQuotaModule.MODULE)); + + @BeforeEach + private void setup() { + cassandraQuotaCurrentValueDao = GuiceUtils.testInjector(cassandraCluster.getCassandraCluster()).getInstance(CassandraQuotaCurrentValueDao.class); + resetCounterToZero(); + } + + private void resetCounterToZero() { + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 0).block(); + QuotaCurrentValue quotaCurrentValue = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + cassandraQuotaCurrentValueDao.decrease(QUOTA_KEY, quotaCurrentValue.getCurrentValue()).block(); + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual.getCurrentValue()).isEqualTo(0l); + } + + @Test + void increaseQuotaCurrentValueShouldCreateNewRowSuccessfully() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(100l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldCreateNewRowSuccessfullyWhenIncreaseAmountIsZero() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(0l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 0l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldIncreaseValueSuccessfully() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(200l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void increaseQuotaCurrentValueShouldDecreaseValueSuccessfullyWhenIncreaseAmountIsNegative() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(100l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 200l).block(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, -100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + + @Test + void decreaseQuotaCurrentValueShouldDecreaseValueSuccessfully() { + QuotaCurrentValue expected = QuotaCurrentValue.builder().quotaComponent(QUOTA_KEY.getQuotaComponent()) + .identifier(QUOTA_KEY.getIdentifier()).quotaType(QUOTA_KEY.getQuotaType()).currentValue(100l).build(); + cassandraQuotaCurrentValueDao.increase(QUOTA_KEY, 200l).block(); + cassandraQuotaCurrentValueDao.decrease(QUOTA_KEY, 100l).block(); + + QuotaCurrentValue actual = cassandraQuotaCurrentValueDao.getQuotaCurrentValue(QUOTA_KEY).block(); + assertThat(actual).isEqualTo(expected); + } + Review Comment: What happens if I try to decrease below 0? ########## mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraQuotaLimitDaoTest.java: ########## @@ -0,0 +1,91 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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 org.apache.james.mailbox.cassandra.quota; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.apache.james.backends.cassandra.CassandraClusterExtension; +import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.blob.cassandra.CassandraBlobModule; +import org.apache.james.core.quota.QuotaComponent; +import org.apache.james.core.quota.QuotaLimit; +import org.apache.james.core.quota.QuotaScope; +import org.apache.james.core.quota.QuotaType; +import org.apache.james.mailbox.cassandra.mail.utils.GuiceUtils; +import org.apache.james.mailbox.cassandra.modules.CassandraQuotaModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class CassandraQuotaLimitDaoTest { + + private CassandraQuotaLimitDao cassandraQuotaLimitDao; + + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraModule.aggregateModules(CassandraQuotaModule.MODULE)); + + @BeforeEach + private void setup() { + cassandraQuotaLimitDao = GuiceUtils.testInjector(cassandraCluster.getCassandraCluster()).getInstance(CassandraQuotaLimitDao.class); + } + + @Test + void getQuotaLimitsShouldGetSomeQuotaLimitsSuccessfully() { + QuotaLimit expectedOne = QuotaLimit.builder().quotaComponent(QuotaComponent.MAILBOX).quotaScope(QuotaScope.DOMAIN).identifier("A").quotaType(QuotaType.COUNT).quotaLimit(200l).build(); + QuotaLimit expectedTwo = QuotaLimit.builder().quotaComponent(QuotaComponent.MAILBOX).quotaScope(QuotaScope.DOMAIN).identifier("A").quotaType(QuotaType.SIZE).quotaLimit(100l).build(); + cassandraQuotaLimitDao.setQuotaLimit(expectedOne).block(); + cassandraQuotaLimitDao.setQuotaLimit(expectedTwo).block(); + + assertThat(cassandraQuotaLimitDao.getQuotaLimits(QuotaComponent.MAILBOX, QuotaScope.DOMAIN, "A").collectList().block()) + .containsExactlyInAnyOrder(expectedOne, expectedTwo); + } + + @Test + void setQuotaLimitShouldSaveObjectSuccessfully() { + QuotaLimit expected = QuotaLimit.builder().quotaComponent(QuotaComponent.MAILBOX).quotaScope(QuotaScope.DOMAIN).identifier("A").quotaType(QuotaType.COUNT).quotaLimit(100l).build(); + cassandraQuotaLimitDao.setQuotaLimit(expected).block(); + + assertThat(cassandraQuotaLimitDao.getQuotaLimit(CassandraQuotaLimitDao.QuotaLimitKey.of(QuotaComponent.MAILBOX, QuotaScope.DOMAIN, "A", QuotaType.COUNT)).block()) + .isEqualTo(expected); + } + + @Test + void setQuotaLimitShouldSaveObjectSuccessfullyWhenLimitIsMinusOne() { + QuotaLimit expected = QuotaLimit.builder().quotaComponent(QuotaComponent.MAILBOX).quotaScope(QuotaScope.DOMAIN).identifier("A").quotaType(QuotaType.COUNT).quotaLimit(-1l).build(); + cassandraQuotaLimitDao.setQuotaLimit(expected).block(); + + assertThat(cassandraQuotaLimitDao.getQuotaLimit(CassandraQuotaLimitDao.QuotaLimitKey.of(QuotaComponent.MAILBOX, QuotaScope.DOMAIN, "A", QuotaType.COUNT)).block()) + .isEqualTo(expected); + } + + @Test + void deleteQuotaLimitShouldDeleteObjectSuccessfully() { + QuotaLimit quotaLimit = QuotaLimit.builder().quotaComponent(QuotaComponent.MAILBOX).quotaScope(QuotaScope.DOMAIN).identifier("A").quotaType(QuotaType.COUNT).quotaLimit(100l).build(); + cassandraQuotaLimitDao.setQuotaLimit(quotaLimit).block(); + cassandraQuotaLimitDao.deleteQuotaLimit(CassandraQuotaLimitDao.QuotaLimitKey.of(QuotaComponent.MAILBOX, QuotaScope.DOMAIN, "A", QuotaType.COUNT)).block(); + + assertThat(cassandraQuotaLimitDao.getQuotaLimit(CassandraQuotaLimitDao.QuotaLimitKey.of(QuotaComponent.MAILBOX, QuotaScope.DOMAIN, "A", QuotaType.COUNT)).block()) + .isNull(); + } + Review Comment: What happens if I try to set quota limit to -100? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
