<commit_before>package  io.hydrosphere.mist

import java.io.{File, FileInputStream, FileOutputStream}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, MediaTypes}
import akka.stream.ActorMaterializer
import akka.testkit.TestKit
import io.hydrosphere.mist.jobs.FullJobConfiguration
import io.hydrosphere.mist.master.JsonFormatSupport
import org.apache.commons.lang.SerializationUtils
import org.mapdb.{DBMaker, Serializer}
import org.scalatest.concurrent.Eventually
import org.scalatest.{BeforeAndAfterAll, FunSuite}
import spray.json.{DefaultJsonProtocol, DeserializationException, pimpString}

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import scala.sys.process._
import scala.util.{Failure, Success}

class IntegrationTests extends FunSuite with Eventually with BeforeAndAfterAll with JsonFormatSupport with DefaultJsonProtocol{

  implicit val system = ActorSystem("test-mist")
  implicit val materializer: ActorMaterializer = ActorMaterializer()

  val clientHTTP = Http(system)

  val contextName: String = MistConfig.Contexts.precreated.headOption.getOrElse("foo")

  object StartMist {
  val threadMaster = {
    new Thread {
      override def run() = {
        s"./bin/mist start master --config configs/integration.conf --jar ${TestConfig.assemblyJar}" !
      }
    }
  }
}

  override def beforeAll(): Unit = {
    Thread.sleep(5000)
    if (MistConfig.Recovery.recoveryOn) {
      val db = DBMaker
        .fileDB(MistConfig.Recovery.recoveryDbFileName + "b")
        .make

      // Map
      val map = db
        .hashMap("map", Serializer.STRING, Serializer.BYTE_ARRAY)
        .createOrOpen

      val stringMessage = TestConfig.requestJar
      val json = stringMessage.parseJson
      val jobCreatingRequest = try {
        json.convertTo[FullJobConfiguration]
      } catch {
        case _: DeserializationException => None
      }
      val w_job = SerializationUtils.serialize(jobCreatingRequest)
      map.clear()
      for (i <- 1 to 3) {
        map.put("3e72eaa8-682a-45aa-b0a5-655ae8854c" + i.toString, w_job)
      }

      map.close()
      db.close()

      val src = new File(MistConfig.Recovery.recoveryDbFileName + "b")
      val dest = new File(MistConfig.Recovery.recoveryDbFileName)
      new FileOutputStream(dest) getChannel() transferFrom(
        new FileInputStream(src) getChannel, 0, Long.MaxValue)
    }

    MQTTTest.subscribe(system)

    StartMist.threadMaster.start()

    Thread.sleep(30000)
  }

  test("HTTP bad request") {

    var http_response_success = false
    val httpRequest = HttpRequest(POST, uri = TestConfig.httpUrlIt, entity = HttpEntity(MediaTypes.`application/json`, TestConfig.requestBad))
    val future_response = clientHTTP.singleRequest(httpRequest)

    future_response onComplete {
      case Success(msg) => msg match {
        case HttpResponse(BadRequest, _, _, _) =>
          println(msg)
          http_response_success = true
        case _ =>
          println(msg)
          http_response_success = false
      }
      case Failure(e) =>
        println(e)<commit_msg>deliberately broken tests for Jenkins<commit_after>package  io.hydrosphere.mist

import java.io.{File, FileInputStream, FileOutputStream}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, MediaTypes}
import akka.stream.ActorMaterializer
import akka.testkit.TestKit
import io.hydrosphere.mist.jobs.FullJobConfiguration
import io.hydrosphere.mist.master.JsonFormatSupport
import org.apache.commons.lang.SerializationUtils
import org.mapdb.{DBMaker, Serializer}
import org.scalatest.concurrent.Eventually
import org.scalatest.{BeforeAndAfterAll, FunSuite}
import spray.json.{DefaultJsonProtocol, DeserializationException, pimpString}

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import scala.sys.process._
import scala.util.{Failure, Success}

class IntegrationTests extends FunSuite with Eventually with BeforeAndAfterAll with JsonFormatSupport with DefaultJsonProtocol{

  implicit val system = ActorSystem("test-mist")
  implicit val materializer: ActorMaterializer = ActorMaterializer()

  val clientHTTP = Http(system)

  val contextName: String = MistConfig.Contexts.precreated.headOption.getOrElse("foo")

  object StartMist {
  val threadMaster = {
    new Thread {
      override def run() = {
        s"./bin/mist start master --config configs/integration.conf --jar ${TestConfig.assemblyJar}" !
      }
    }
  }
}

  override def beforeAll(): Unit = {
    Thread.sleep(5000)
    if (MistConfig.Recovery.recoveryOn) {
      val db = DBMaker
        .fileDB(MistConfig.Recovery.recoveryDbFileName + "b")
        .make

      // Map
      val map = db
        .hashMap("map", Serializer.STRING, Serializer.BYTE_ARRAY)
        .createOrOpen

      val stringMessage = TestConfig.requestJar
      val json = stringMessage.parseJson
      val jobCreatingRequest = try {
        json.convertTo[FullJobConfiguration]
      } catch {
        case _: DeserializationException => None
      }
      val w_job = SerializationUtils.serialize(jobCreatingRequest)
      map.clear()
      for (i <- 1 to 3) {
        map.put("3e72eaa8-682a-45aa-b0a5-655ae8854c" + i.toString, w_job)
      }

      map.close()
      db.close()

      val src = new File(MistConfig.Recovery.recoveryDbFileName + "b")
      val dest = new File(MistConfig.Recovery.recoveryDbFileName)
      new FileOutputStream(dest) getChannel() transferFrom(
        new FileInputStream(src) getChannel, 0, Long.MaxValue)
    }

    MQTTTest.subscribe(system)

    StartMist.threadMaster.start()

    Thread.sleep(30000)
  }

  test("Hello mr.Jenkins") {
    assert(false)
  }

  test("HTTP bad request") {

    var http_response_success = false
    val httpRequest = HttpRequest(POST, uri = TestConfig.httpUrlIt, entity = HttpEntity(MediaTypes.`application/json`, TestConfig.requestBad))
    val future_response = clientHTTP.singleRequest(httpRequest)

    future_response onComplete {
      case Success(msg) => msg match {
        case HttpResponse(BadRequest, _, _, _) =>
          println(msg)
          http_response_success = true
        case _ =>
          println(msg)
          http_response_success = false
      }
      case Failure(e) =>
        println(e)
{'fraction_non_alphanumeric': 0.09401072705601907, 'fraction_numerical': 0.008939213349225268, 'mean_word_length': 3.5114247311827955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5009350', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1972, 'n_words': 408}