<commit_before>/*
 * Copyright 2017 Human Brain Project MIP by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hbp.mip.woken.test

import java.util.concurrent.TimeUnit

import akka.{Done, NotUsed}
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.model.ws.{Message, TextMessage, WebSocketRequest}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Keep, Sink, Source}
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.PatienceConfiguration.Timeout
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Minutes, Span}
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
import spray.json._

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.collection.immutable.Seq

class WokenWebSocketAPITest
    extends FlatSpec
    with Matchers
    with Queries
    with ScalaFutures
    with BeforeAndAfterAll {

  val configuration = ConfigFactory.load()
  implicit val system = ActorSystem("WebSocketAPITest")
  implicit val materializer = ActorMaterializer()

  implicit val executionContext = system.dispatcher

  override def afterAll = {
    system.terminate().onComplete { result =>
      println("Actor system shutdown: " + result)
    }
  }

  "Woken" should "respond to a query for the list of methods using websocket" in {

    executeQuery(None, None, "ws://woken:8087/mining/methods")

  }

  "Woken" should "respond to a mining query using websocket" in {

    executeQuery(Some("/knn_data_mining_query.json"),
                 Some("/knn_data_mining.json"),
                 "ws://woken:8087/mining/job")

  }

  "Woken" should "respond to a mining query with empty algorithm using websocket" in {

    executeQuery(Some("/knn_data_mining_empty_alg_query.json"),
                 Some("/knn_data_mining_empty_alg.json"),
                 "ws://woken:8087/mining/job")

  }

  "Woken" should "respond to an experiment query using websocket" in {

    executeQuery(Some("/knn_experiment_query.json"),
                 Some("/knn_experiment.json"),
                 "ws://woken:8087/mining/experiment")

  }

  private def assertionAsSink(
      expectedResponse: Option[String]): Sink[Message, Future[Done]] = {
    Sink.foreach {<commit_msg>Ignore ws test<commit_after>/*
 * Copyright 2017 Human Brain Project MIP by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hbp.mip.woken.test

import java.util.concurrent.TimeUnit

import akka.{Done, NotUsed}
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.model.ws.{Message, TextMessage, WebSocketRequest}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Keep, Sink, Source}
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.PatienceConfiguration.Timeout
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Minutes, Span}
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
import spray.json._

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.collection.immutable.Seq

class WokenWebSocketAPITest
    extends FlatSpec
    with Matchers
    with Queries
    with ScalaFutures
    with BeforeAndAfterAll {

  val configuration = ConfigFactory.load()
  implicit val system = ActorSystem("WebSocketAPITest")
  implicit val materializer = ActorMaterializer()

  implicit val executionContext = system.dispatcher

  override def afterAll = {
    system.terminate().onComplete { result =>
      println("Actor system shutdown: " + result)
    }
  }

  "Woken" should "respond to a query for the list of methods using websocket" in {

    executeQuery(None, None, "ws://woken:8087/mining/methods")

  }

  "Woken" should "respond to a mining query using websocket" in {

    executeQuery(Some("/knn_data_mining_query.json"),
                 Some("/knn_data_mining.json"),
                 "ws://woken:8087/mining/job")

  }

  "Woken" should "respond to a mining query with empty algorithm using websocket" in {

    executeQuery(Some("/knn_data_mining_empty_alg_query.json"),
                 Some("/knn_data_mining_empty_alg.json"),
                 "ws://woken:8087/mining/job")

  }

  ignore should "respond to an experiment query using websocket" in {

    executeQuery(Some("/knn_experiment_query.json"),
                 Some("/knn_experiment.json"),
                 "ws://woken:8087/mining/experiment")

  }

  private def assertionAsSink(
      expectedResponse: Option[String]): Sink[Message, Future[Done]] = {
    Sink.foreach {
{'fraction_non_alphanumeric': 0.09502724795640327, 'fraction_numerical': 0.008174386920980926, 'mean_word_length': 3.8021259198691744, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '683001', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1703, 'n_words': 478}