package eu.dnetlib.dhp.doiboost.crossref

import eu.dnetlib.doiboost.crossref.CrossrefFunderRetriever
import org.json4s.DefaultFormats
import org.json4s.JsonAST.JString
import org.json4s.jackson.JsonMethods.parse
import org.junit.jupiter.api.Test

import java.io.PrintWriter
class CrossrefFunderTest {


  def parse_funder(input:String):(String, String, List[String]) = {

    implicit lazy val formats: DefaultFormats.type = org.json4s.DefaultFormats
    lazy val json: org.json4s.JValue = parse(input)


    val l:List[String] = for { JString(desc) <- json \ "descendants" } yield desc


    ((json \ "name").extract[String],(json \ "uri").extract[String], l)


  }


  @Test
  def testFunderRelationshipsMapping(): Unit = {
    val cf = new CrossrefFunderRetriever()

    var i = 0

    val w =new PrintWriter("/tmp/funder_names")





    cf.map(s=>parse_funder(s)).foreach(s=> w.write(s"${s._1} \t${s._2} \t${s._3.mkString("--")}\t\n"))


     w.close()

//    cf.foreach{_ =>
//
//      i = i+1
//
//      if (i % 1000 == 0)
//        println(s"get $i documents")
//
//    }
//
//
//
//
//    println(s"Total item $i")
  }



}
