KAFKA-13577: Replace easymock with mockito in kafka:core - part 3 (#11674)

Reviewers: Tom Bentley <tbentley@redhat.com>
This commit is contained in:
Mickael Maison 2022-02-11 16:16:25 +01:00 committed by GitHub
parent 03af63d076
commit 0269edfc80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 799 additions and 982 deletions

View File

@ -859,7 +859,6 @@ project(':core') {
testImplementation project(':raft').sourceSets.test.output
testImplementation libs.bcpkix
testImplementation libs.mockitoCore
testImplementation libs.easymock
testImplementation(libs.apacheda) {
exclude group: 'xml-apis', module: 'xml-apis'
// `mina-core` is a transitive dependency for `apacheds` and `apacheda`.

View File

@ -28,7 +28,6 @@
<allow pkg="javax.management" />
<allow pkg="org.slf4j" />
<allow pkg="org.junit" />
<allow pkg="org.easymock" />
<allow pkg="java.security" />
<allow pkg="javax.net.ssl" />
<allow pkg="javax.security" />

View File

@ -26,22 +26,22 @@ import org.apache.kafka.common.requests.{RequestContext, RequestHeader}
import org.apache.kafka.common.resource.{PatternType, ResourcePattern, ResourceType}
import org.apache.kafka.common.security.auth.{KafkaPrincipal, SecurityProtocol}
import org.apache.kafka.server.authorizer.{Action, AuthorizationResult, Authorizer}
import org.easymock.EasyMock._
import org.easymock.{EasyMock, IArgumentMatcher}
import org.junit.jupiter.api.Assertions._
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.argThat
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.{mock, verify, when}
import scala.collection.Seq
import scala.jdk.CollectionConverters._
class AuthHelperTest {
import AuthHelperTest._
private val clientId = ""
@Test
def testAuthorize(): Unit = {
val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer])
val authorizer: Authorizer = mock(classOf[Authorizer])
val operation = AclOperation.WRITE
val resourceType = ResourceType.TOPIC
@ -56,23 +56,20 @@ class AuthHelperTest {
1, true, true)
)
EasyMock.expect(authorizer.authorize(requestContext, expectedActions.asJava))
.andReturn(Seq(AuthorizationResult.ALLOWED).asJava)
.once()
EasyMock.replay(authorizer)
when(authorizer.authorize(requestContext, expectedActions.asJava))
.thenReturn(Seq(AuthorizationResult.ALLOWED).asJava)
val result = new AuthHelper(Some(authorizer)).authorize(
requestContext, operation, resourceType, resourceName)
verify(authorizer)
verify(authorizer).authorize(requestContext, expectedActions.asJava)
assertEquals(true, result)
}
@Test
def testFilterByAuthorized(): Unit = {
val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer])
val authorizer: Authorizer = mock(classOf[Authorizer])
val operation = AclOperation.WRITE
val resourceType = ResourceType.TOPIC
@ -94,19 +91,17 @@ class AuthHelperTest {
1, true, true),
)
EasyMock.expect(authorizer.authorize(
EasyMock.eq(requestContext), matchSameElements(expectedActions.asJava)
)).andAnswer { () =>
val actions = EasyMock.getCurrentArguments.apply(1).asInstanceOf[util.List[Action]].asScala
when(authorizer.authorize(
ArgumentMatchers.eq(requestContext), argThat((t: java.util.List[Action]) => t.containsAll(expectedActions.asJava))
)).thenAnswer { invocation =>
val actions = invocation.getArgument(1).asInstanceOf[util.List[Action]].asScala
actions.map { action =>
if (Set(resourceName1, resourceName3).contains(action.resourcePattern.name))
AuthorizationResult.ALLOWED
else
AuthorizationResult.DENIED
}.asJava
}.once()
EasyMock.replay(authorizer)
}
val result = new AuthHelper(Some(authorizer)).filterByAuthorized(
requestContext,
@ -116,27 +111,11 @@ class AuthHelperTest {
Seq(resourceName1, resourceName2, resourceName1, resourceName3)
)(identity)
verify(authorizer)
verify(authorizer).authorize(
ArgumentMatchers.eq(requestContext), argThat((t: java.util.List[Action]) => t.containsAll(expectedActions.asJava))
)
assertEquals(Set(resourceName1, resourceName3), result)
}
}
object AuthHelperTest {
/**
* Similar to `EasyMock.eq`, but matches if both lists have the same elements irrespective of ordering.
*/
def matchSameElements[T](list: java.util.List[T]): java.util.List[T] = {
EasyMock.reportMatcher(new IArgumentMatcher {
def matches(argument: Any): Boolean = argument match {
case l: java.util.List[_] => list.asScala.toSet == l.asScala.toSet
case _ => false
}
def appendTo(buffer: StringBuffer): Unit = buffer.append(s"list($list)")
})
null
}
}

File diff suppressed because it is too large Load Diff