Testing and Mocking ScalaGraph with gremlin

118 Views Asked by At

I have a Service class that i am trying to unit test. The service class is as follows -:

class BtoService @Inject()(db: GrDbConnection,
                           businessService: BusinessService,
                           vertexIdGenerator: VertexIdGenerator) {

def updateBto(id: String, updateBTOReq: UpdateBTORequest) = {
implicit val g = db.g
    val btoVertex = g
      .V(
        vertexIdGenerator.vertexId(VertexLabels.BTO, id, PropertyLabels.BTO_ID))
      .headOption() match {
      case Some(value) => value
      case None        => throw BtoDoesNotExistException(s"BTO $id Does not exists")
}

So while testing this class, i create a mock of injected services(BusinessService, GrDbConnection) -:

  val db: GrDbConnection = mock[GrDbConnection]
  val businessService: BusinessService = mock[BusinessService]
  val vertexIdGenerator: VertexIdGenerator = mock[VertexIdGenerator]

  val btoService: BtoService = new BtoService(db, businessService, vertexIdGenerator)
  val g : ScalaGraph = EmptyGraph
        .instance()
        .asScala()

    btoService.updateBto("101",updateBTORequest)
    Mockito.when(db.g).thenReturn(g)

The GrDbConnection.scala has the defined db.g -:

val g: ScalaGraph = EmptyGraph
    .instance()
    .asScala
    .configure(_.withRemote(connection))

here connection has the necessary details to connect to the actuall db.

Since i can return an empty scala graph using Mockito.when().thenReturn(), i preferred not to use .configure() option in my test class.

My real problem that i face is that, i am not able to add a vertex to the test graph, I need to add a btoModel as the vertex to the graph since, in service class :

val btoVertex = g.V(vertex......()).headOption() - returns a GremlinScala.Aux[scala.vertex, Hnil]

How do i proceed with that? please contact me at - [email protected] for more info.

0

There are 0 best solutions below