mirror of https://github.com/apache/kafka.git
KAFKA-13577: Replace easymock with mockito in kafka:core - part 3 (#11674)
Reviewers: Tom Bentley <tbentley@redhat.com>
This commit is contained in:
parent
03af63d076
commit
0269edfc80
|
@ -859,7 +859,6 @@ project(':core') {
|
||||||
testImplementation project(':raft').sourceSets.test.output
|
testImplementation project(':raft').sourceSets.test.output
|
||||||
testImplementation libs.bcpkix
|
testImplementation libs.bcpkix
|
||||||
testImplementation libs.mockitoCore
|
testImplementation libs.mockitoCore
|
||||||
testImplementation libs.easymock
|
|
||||||
testImplementation(libs.apacheda) {
|
testImplementation(libs.apacheda) {
|
||||||
exclude group: 'xml-apis', module: 'xml-apis'
|
exclude group: 'xml-apis', module: 'xml-apis'
|
||||||
// `mina-core` is a transitive dependency for `apacheds` and `apacheda`.
|
// `mina-core` is a transitive dependency for `apacheds` and `apacheda`.
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
<allow pkg="javax.management" />
|
<allow pkg="javax.management" />
|
||||||
<allow pkg="org.slf4j" />
|
<allow pkg="org.slf4j" />
|
||||||
<allow pkg="org.junit" />
|
<allow pkg="org.junit" />
|
||||||
<allow pkg="org.easymock" />
|
|
||||||
<allow pkg="java.security" />
|
<allow pkg="java.security" />
|
||||||
<allow pkg="javax.net.ssl" />
|
<allow pkg="javax.net.ssl" />
|
||||||
<allow pkg="javax.security" />
|
<allow pkg="javax.security" />
|
||||||
|
|
|
@ -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.resource.{PatternType, ResourcePattern, ResourceType}
|
||||||
import org.apache.kafka.common.security.auth.{KafkaPrincipal, SecurityProtocol}
|
import org.apache.kafka.common.security.auth.{KafkaPrincipal, SecurityProtocol}
|
||||||
import org.apache.kafka.server.authorizer.{Action, AuthorizationResult, Authorizer}
|
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.Assertions._
|
||||||
import org.junit.jupiter.api.Test
|
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.collection.Seq
|
||||||
import scala.jdk.CollectionConverters._
|
import scala.jdk.CollectionConverters._
|
||||||
|
|
||||||
class AuthHelperTest {
|
class AuthHelperTest {
|
||||||
import AuthHelperTest._
|
|
||||||
|
|
||||||
private val clientId = ""
|
private val clientId = ""
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testAuthorize(): Unit = {
|
def testAuthorize(): Unit = {
|
||||||
val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer])
|
val authorizer: Authorizer = mock(classOf[Authorizer])
|
||||||
|
|
||||||
val operation = AclOperation.WRITE
|
val operation = AclOperation.WRITE
|
||||||
val resourceType = ResourceType.TOPIC
|
val resourceType = ResourceType.TOPIC
|
||||||
|
@ -56,23 +56,20 @@ class AuthHelperTest {
|
||||||
1, true, true)
|
1, true, true)
|
||||||
)
|
)
|
||||||
|
|
||||||
EasyMock.expect(authorizer.authorize(requestContext, expectedActions.asJava))
|
when(authorizer.authorize(requestContext, expectedActions.asJava))
|
||||||
.andReturn(Seq(AuthorizationResult.ALLOWED).asJava)
|
.thenReturn(Seq(AuthorizationResult.ALLOWED).asJava)
|
||||||
.once()
|
|
||||||
|
|
||||||
EasyMock.replay(authorizer)
|
|
||||||
|
|
||||||
val result = new AuthHelper(Some(authorizer)).authorize(
|
val result = new AuthHelper(Some(authorizer)).authorize(
|
||||||
requestContext, operation, resourceType, resourceName)
|
requestContext, operation, resourceType, resourceName)
|
||||||
|
|
||||||
verify(authorizer)
|
verify(authorizer).authorize(requestContext, expectedActions.asJava)
|
||||||
|
|
||||||
assertEquals(true, result)
|
assertEquals(true, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testFilterByAuthorized(): Unit = {
|
def testFilterByAuthorized(): Unit = {
|
||||||
val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer])
|
val authorizer: Authorizer = mock(classOf[Authorizer])
|
||||||
|
|
||||||
val operation = AclOperation.WRITE
|
val operation = AclOperation.WRITE
|
||||||
val resourceType = ResourceType.TOPIC
|
val resourceType = ResourceType.TOPIC
|
||||||
|
@ -94,19 +91,17 @@ class AuthHelperTest {
|
||||||
1, true, true),
|
1, true, true),
|
||||||
)
|
)
|
||||||
|
|
||||||
EasyMock.expect(authorizer.authorize(
|
when(authorizer.authorize(
|
||||||
EasyMock.eq(requestContext), matchSameElements(expectedActions.asJava)
|
ArgumentMatchers.eq(requestContext), argThat((t: java.util.List[Action]) => t.containsAll(expectedActions.asJava))
|
||||||
)).andAnswer { () =>
|
)).thenAnswer { invocation =>
|
||||||
val actions = EasyMock.getCurrentArguments.apply(1).asInstanceOf[util.List[Action]].asScala
|
val actions = invocation.getArgument(1).asInstanceOf[util.List[Action]].asScala
|
||||||
actions.map { action =>
|
actions.map { action =>
|
||||||
if (Set(resourceName1, resourceName3).contains(action.resourcePattern.name))
|
if (Set(resourceName1, resourceName3).contains(action.resourcePattern.name))
|
||||||
AuthorizationResult.ALLOWED
|
AuthorizationResult.ALLOWED
|
||||||
else
|
else
|
||||||
AuthorizationResult.DENIED
|
AuthorizationResult.DENIED
|
||||||
}.asJava
|
}.asJava
|
||||||
}.once()
|
}
|
||||||
|
|
||||||
EasyMock.replay(authorizer)
|
|
||||||
|
|
||||||
val result = new AuthHelper(Some(authorizer)).filterByAuthorized(
|
val result = new AuthHelper(Some(authorizer)).filterByAuthorized(
|
||||||
requestContext,
|
requestContext,
|
||||||
|
@ -116,27 +111,11 @@ class AuthHelperTest {
|
||||||
Seq(resourceName1, resourceName2, resourceName1, resourceName3)
|
Seq(resourceName1, resourceName2, resourceName1, resourceName3)
|
||||||
)(identity)
|
)(identity)
|
||||||
|
|
||||||
verify(authorizer)
|
verify(authorizer).authorize(
|
||||||
|
ArgumentMatchers.eq(requestContext), argThat((t: java.util.List[Action]) => t.containsAll(expectedActions.asJava))
|
||||||
|
)
|
||||||
|
|
||||||
assertEquals(Set(resourceName1, resourceName3), result)
|
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
Loading…
Reference in New Issue