Skip to content

Commit

Permalink
Fixes #266: added test to ensure arrives to the Neo4j Driver (#290)
Browse files Browse the repository at this point in the history
* Fixes #266: added test to ensure the correct token is passed down to the driver

* changed import for BaseEncoding

* moved test to scala
  • Loading branch information
utnaf authored Feb 16, 2021
1 parent 0979e01 commit f1ccd65
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.neo4j.spark.service

import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.neo4j.driver.AuthToken
import org.neo4j.driver.Config
import org.neo4j.driver.GraphDatabase
import org.neo4j.spark.util.DriverCache
import org.neo4j.spark.util.Neo4jOptions
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.testcontainers.shaded.com.google.common.io.BaseEncoding

import java.util
import org.junit.Assert.assertEquals
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito.times

@PrepareForTest(Array(classOf[GraphDatabase]))
@RunWith(classOf[PowerMockRunner])
class AuthenticationTest {

@Test
def testLdapConnectionToken(): Unit = {
val options = new util.HashMap[String, String]
options.put("url", "bolt://localhost:7687")
options.put("authentication.type", "custom")
options.put("authentication.custom.credentials", BaseEncoding.base64.encode("user:password".getBytes))
options.put("labels", "Person")

val argumentCaptor = ArgumentCaptor.forClass(classOf[AuthToken])
val neo4jOptions = new Neo4jOptions(options)
val neo4jDriverOptions = neo4jOptions.connection
val driverCache = new DriverCache(neo4jDriverOptions, "jobId")

PowerMockito.mockStatic(classOf[GraphDatabase])

driverCache.getOrCreate()

PowerMockito.verifyStatic(classOf[GraphDatabase], times(1))
GraphDatabase.driver(anyString, argumentCaptor.capture, any(classOf[Config]))

assertEquals(neo4jDriverOptions.toNeo4jAuth, argumentCaptor.getValue)
}
}
16 changes: 16 additions & 0 deletions test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<version>4.0.0</version>
</parent>

<properties>
<powermock.version>2.0.9</powermock.version>
</properties>

<dependencies>

<dependency>
Expand Down Expand Up @@ -49,6 +53,18 @@
<version>2.12.0</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
</dependency>

</dependencies>

</project>

0 comments on commit f1ccd65

Please sign in to comment.