chibenwa commented on code in PR #2197:
URL: https://github.com/apache/james-project/pull/2197#discussion_r1562094408
##########
server/data/data-api/src/main/java/org/apache/james/droplists/api/DropListEntry.java:
##########
@@ -62,7 +67,7 @@ public Builder deniedEntity(String deniedEntity) {
return this;
}
- public DropListEntry build() {
+ public DropListEntry build() throws AddressException {
Review Comment:
With below suggestion no parsing would be needed wich means this method
would never throw.
##########
server/data/data-api/src/test/java/org/apache/james/droplists/api/DropListContract.java:
##########
@@ -0,0 +1,139 @@
+/****************************************************************
+ * 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.droplists.api;
+
+import static org.apache.james.droplists.api.OwnerScope.GLOBAL;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.util.Objects;
+
+import jakarta.mail.internet.AddressException;
+
+import org.apache.james.core.MailAddress;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public interface DropListContract {
+
+ DropList dropList();
+
+ @Test
+ default void shouldAddEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ Mono<Void> result = dropList().add(dropListEntry);
+
+ assertThat(1).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @Test
+ default void shouldRemoveEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ dropList().add(dropListEntry);
+
+ Mono<Void> result = dropList().remove(dropListEntry);
+
+ assertThat(0).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @ParameterizedTest(name = "{index} ownerScope: {0}, owner: {1},")
+ @CsvSource(value = {
+ "GLOBAL, [email protected]",
+ "DOMAIN, [email protected]",
Review Comment:
I would expect the value to be
```suggestion
"DOMAIN, example.com",
```
##########
server/data/data-memory/src/main/java/org/apache/james/droplists/memory/MemoryDropList.java:
##########
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.droplists.memory;
+
+import static org.apache.james.droplists.api.DeniedEntityType.DOMAIN;
+
+import jakarta.mail.internet.AddressException;
+
+import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
+import org.apache.james.droplists.api.DropList;
+import org.apache.james.droplists.api.DropListEntry;
+import org.apache.james.droplists.api.OwnerScope;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class MemoryDropList implements DropList {
+
+ private final Multimap<String, DropListEntry> dropList =
Multimaps.synchronizedMultimap(HashMultimap.create());
+
+ @Override
+ public Mono<Void> add(DropListEntry entry) {
+ dropList.put(entry.getOwnerScope().name(), entry);
+ return Mono.empty();
+ }
+
+ @Override
+ public Mono<Void> remove(DropListEntry entry) {
+ dropList.remove(entry.getOwnerScope().name(), entry);
+ return Mono.empty();
+ }
+
+ @Override
+ public Flux<DropListEntry> list(OwnerScope ownerScope, String owner) {
+ return Flux.fromIterable(dropList.get(ownerScope.name()).stream()
+ .filter(entry -> entry.getOwner().equals(owner))
+ .toList());
+ }
+
+ @Override
+ public Mono<Status> query(OwnerScope ownerScope, String owner, MailAddress
sender) {
+ boolean isBlocked = dropList.get(ownerScope.name()).stream()
+ .anyMatch(entry -> isEntryMatchingOwnerAndDeniedEntity(owner,
sender, entry));
+
+ return Mono.just(isBlocked ? Status.BLOCKED : Status.ALLOWED);
+ }
+
+ private static boolean isEntryMatchingOwnerAndDeniedEntity(String owner,
MailAddress sender, DropListEntry entry) {
Review Comment:
And in a method name is a code smell that tells that maybe two methods are
needed.
##########
server/data/data-memory/src/main/java/org/apache/james/droplists/memory/MemoryDropList.java:
##########
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.droplists.memory;
+
+import static org.apache.james.droplists.api.DeniedEntityType.DOMAIN;
+
+import jakarta.mail.internet.AddressException;
+
+import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
+import org.apache.james.droplists.api.DropList;
+import org.apache.james.droplists.api.DropListEntry;
+import org.apache.james.droplists.api.OwnerScope;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class MemoryDropList implements DropList {
+
+ private final Multimap<String, DropListEntry> dropList =
Multimaps.synchronizedMultimap(HashMultimap.create());
+
+ @Override
+ public Mono<Void> add(DropListEntry entry) {
+ dropList.put(entry.getOwnerScope().name(), entry);
+ return Mono.empty();
+ }
+
+ @Override
+ public Mono<Void> remove(DropListEntry entry) {
+ dropList.remove(entry.getOwnerScope().name(), entry);
+ return Mono.empty();
+ }
+
+ @Override
+ public Flux<DropListEntry> list(OwnerScope ownerScope, String owner) {
+ return Flux.fromIterable(dropList.get(ownerScope.name()).stream()
+ .filter(entry -> entry.getOwner().equals(owner))
+ .toList());
+ }
+
+ @Override
+ public Mono<Status> query(OwnerScope ownerScope, String owner, MailAddress
sender) {
+ boolean isBlocked = dropList.get(ownerScope.name()).stream()
+ .anyMatch(entry -> isEntryMatchingOwnerAndDeniedEntity(owner,
sender, entry));
+
+ return Mono.just(isBlocked ? Status.BLOCKED : Status.ALLOWED);
+ }
+
+ private static boolean isEntryMatchingOwnerAndDeniedEntity(String owner,
MailAddress sender, DropListEntry entry) {
+ String entityFromSender;
+ String deniedEntity;
Review Comment:
Variable reallocation
##########
server/data/data-api/src/test/java/org/apache/james/droplists/api/DropListContract.java:
##########
@@ -0,0 +1,139 @@
+/****************************************************************
+ * 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.droplists.api;
+
+import static org.apache.james.droplists.api.OwnerScope.GLOBAL;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.util.Objects;
+
+import jakarta.mail.internet.AddressException;
+
+import org.apache.james.core.MailAddress;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public interface DropListContract {
+
+ DropList dropList();
+
+ @Test
+ default void shouldAddEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ Mono<Void> result = dropList().add(dropListEntry);
+
+ assertThat(1).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @Test
+ default void shouldRemoveEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ dropList().add(dropListEntry);
+
+ Mono<Void> result = dropList().remove(dropListEntry);
+
+ assertThat(0).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @ParameterizedTest(name = "{index} ownerScope: {0}, owner: {1},")
+ @CsvSource(value = {
+ "GLOBAL, [email protected]",
+ "DOMAIN, [email protected]",
+ "USER, [email protected]",
+ })
+ default void shouldGetEntryListForSpecifiedScopeAndOwner(OwnerScope
ownerScope, String owner) throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .ownerScope(ownerScope)
+ .owner(owner)
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
Review Comment:
Definitly the builder is not convenient.
I would expect to be able to write:
```
DropListEntry.builder()
.userOwner(username)
.denyAddress(address)
.build();
DropListEntry.builder()
.domainOwner(domain1)
.denyDomain(domain2)
.build();
DropListEntry.builder()
.forAll()
.denyDomain(domain2)
.build();
```
(of course in any combinaison)
##########
server/data/data-api/src/main/java/org/apache/james/droplists/api/DropListEntry.java:
##########
@@ -71,6 +76,11 @@ public DropListEntry build() {
Preconditions.checkArgument(deniedEntity != null &&
!deniedEntity.isBlank(), "`deniedEntity` must not be null, empty, or blank");
Preconditions.checkArgument(deniedEntity.length() <=
MAXIMUM_DOMAIN_LENGTH,
"deniedEntity length should not be longer than %s characters",
MAXIMUM_DOMAIN_LENGTH);
+ if (deniedEntityType.equals(DeniedEntityType.DOMAIN)) {
+ deniedEntity = Domain.of(deniedEntity).asString();
+ } else {
+ deniedEntity = new MailAddress(deniedEntity).toString();
+ }
Review Comment:
I would suggest dedicated bulder method `denyDomain(Domain domain)` and
`denyAddress(MailAddress mailAdress)` that would be both more fluent and more
type safe.
##########
server/data/data-api/src/test/java/org/apache/james/droplists/api/DropListContract.java:
##########
@@ -0,0 +1,139 @@
+/****************************************************************
+ * 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.droplists.api;
+
+import static org.apache.james.droplists.api.OwnerScope.GLOBAL;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import java.util.Objects;
+
+import jakarta.mail.internet.AddressException;
+
+import org.apache.james.core.MailAddress;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public interface DropListContract {
+
+ DropList dropList();
+
+ @Test
+ default void shouldAddEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ Mono<Void> result = dropList().add(dropListEntry);
+
+ assertThat(1).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @Test
+ default void shouldRemoveEntry() throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .owner("[email protected]")
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+
+ dropList().add(dropListEntry);
+
+ Mono<Void> result = dropList().remove(dropListEntry);
+
+ assertThat(0).isEqualTo(Objects.requireNonNull(dropList().list(GLOBAL,
dropListEntry.getOwner()).collectList().block()).size());
+ assertThat(Mono.empty()).isEqualTo(result);
+ }
+
+ @ParameterizedTest(name = "{index} ownerScope: {0}, owner: {1},")
+ @CsvSource(value = {
+ "GLOBAL, [email protected]",
+ "DOMAIN, [email protected]",
+ "USER, [email protected]",
+ })
+ default void shouldGetEntryListForSpecifiedScopeAndOwner(OwnerScope
ownerScope, String owner) throws AddressException {
+ DropListEntry dropListEntry = DropListEntry.builder()
+ .ownerScope(ownerScope)
+ .owner(owner)
+ .deniedEntity("[email protected]")
+ .deniedEntityType(DeniedEntityType.ADDRESS)
+ .build();
+ dropList().add(dropListEntry);
+
+ Flux<DropListEntry> result = dropList().list(ownerScope, owner);
+
+
assertThat(1).isEqualTo(Objects.requireNonNull(result.collectList().block()).size());
+ }
+
+ @ParameterizedTest(name = "{index} ownerScope: {0}, owner: {1},
deniedEntity: {2}, deniedEntityType: {3}, senderMailAddress: {4}")
+ @CsvSource(value = {
+ "GLOBAL, [email protected], [email protected], ADDRESS,
[email protected]",
+ "GLOBAL, [email protected], denied.com, DOMAIN,
[email protected]",
+ "DOMAIN, [email protected], [email protected], ADDRESS,
[email protected]",
+ "DOMAIN, [email protected], denied.com, DOMAIN,
[email protected]",
+ "USER, [email protected], [email protected], ADDRESS,
[email protected]",
+ "USER, [email protected], denied.com, DOMAIN,
[email protected]",
+ })
+ default void shouldReturnAllowed(OwnerScope ownerScope, String owner,
String deniedEntity, DeniedEntityType deniedEntityType,
+ String senderMailAddress) throws
AddressException {
+ MailAddress allowedSender = new MailAddress(senderMailAddress);
+ dropList().add(DropListEntry.builder()
+ .owner(owner)
+ .ownerScope(ownerScope)
+ .deniedEntity(deniedEntity)
+ .deniedEntityType(deniedEntityType)
+ .build());
+
+ Mono<DropList.Status> result = dropList().query(ownerScope, owner,
allowedSender);
+
+ assertThat(DropList.Status.ALLOWED).isEqualTo(result.block());
Review Comment:
With assertJ the order of assertions is by contention the reverse than Junit
(IE reads like english)
assertThat(result.block()).isEqualTo(ALLOWED);
--
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]