Skip to content

Commit

Permalink
Suggestion to remove parameterized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vagaerg committed Nov 30, 2023
1 parent b89b35c commit ecda42a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 143 deletions.
28 changes: 16 additions & 12 deletions plugin/trino-opa/src/test/java/io/trino/plugin/opa/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -105,38 +106,41 @@ public static SystemSecurityContext systemSecurityContextFromIdentity(Identity i
return new SystemSecurityContext(identity, new QueryIdGenerator().createNextQueryId(), Instant.now());
}

public abstract static class MethodWrapper<T> {
public abstract boolean isAccessAllowed(OpaAccessControl opaAccessControl, SystemSecurityContext systemSecurityContext, T argument);
public abstract static class MethodWrapper {
public abstract boolean isAccessAllowed(OpaAccessControl opaAccessControl);
}

public static class ThrowingMethodWrapper<T> extends MethodWrapper<T> {
private final FunctionalHelpers.Consumer3<OpaAccessControl, SystemSecurityContext, T> callable;
public static class ThrowingMethodWrapper extends MethodWrapper {
private final Consumer<OpaAccessControl> callable;

public ThrowingMethodWrapper(FunctionalHelpers.Consumer3<OpaAccessControl, SystemSecurityContext, T> callable) {
public ThrowingMethodWrapper(Consumer<OpaAccessControl> callable) {
this.callable = callable;
}

@Override
public boolean isAccessAllowed(OpaAccessControl opaAccessControl, SystemSecurityContext systemSecurityContext, T argument) {
public boolean isAccessAllowed(OpaAccessControl opaAccessControl) {
try {
this.callable.accept(opaAccessControl, systemSecurityContext, argument);
this.callable.accept(opaAccessControl);
return true;
} catch (AccessDeniedException e) {
if (!e.getMessage().contains("Access Denied")) {
throw new AssertionError("Expected AccessDenied exception to contain 'Access Denied' in the message");
}
return false;
}
}
}

public static class ReturningMethodWrapper<T> extends MethodWrapper<T> {
private final FunctionalHelpers.Function3<OpaAccessControl, SystemSecurityContext, T, Boolean> callable;
public static class ReturningMethodWrapper extends MethodWrapper {
private final Function<OpaAccessControl, Boolean> callable;

public ReturningMethodWrapper(FunctionalHelpers.Function3<OpaAccessControl, SystemSecurityContext, T, Boolean> callable) {
public ReturningMethodWrapper(Function<OpaAccessControl, Boolean> callable) {
this.callable = callable;
}

@Override
public boolean isAccessAllowed(OpaAccessControl opaAccessControl, SystemSecurityContext systemSecurityContext, T argument) {
return this.callable.apply(opaAccessControl, systemSecurityContext, argument);
public boolean isAccessAllowed(OpaAccessControl opaAccessControl) {
return this.callable.apply(opaAccessControl);
}
}

Expand Down

0 comments on commit ecda42a

Please sign in to comment.