<commit_before>/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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.
 */

import java.util.concurrent.locks.Lock
import javax.transaction.TransactionManager

import akka.actor.{ActorSystem, actorRef2Scala}
import com.stratio.connectors.MockConnectorActor
import com.stratio.meta.common.exceptions.ExecutionException
import com.stratio.meta.common.executionplan.{ExecutionType, QueryWorkflow, ResultType}
import com.stratio.meta.common.logicalplan.LogicalWorkflow
import com.stratio.meta.server.config.{ActorReceiveUtils, ServerConfig}
import com.stratio.meta2.common.data.CatalogName
import com.stratio.meta2.core.coordinator.Coordinator
import com.stratio.meta2.core.execution.ExecutionManager
import com.stratio.meta2.core.grid.Grid
import com.stratio.meta2.core.query._
import com.stratio.meta2.core.statements.SelectStatement
import com.stratio.meta2.server.actors.CoordinatorActor
import com.stratio.meta2.server.mocks.MockConnectorManagerActor
import org.apache.log4j.Logger
import org.scalamock.scalatest.MockFactory
import org.scalatest.{FunSuiteLike, Suite}

import scala.concurrent.duration.DurationInt

//class CoordinatorActorTest extends ActorReceiveUtils with FunSuiteLike with MockFactory  with ServerConfig{
class CoordinatorActorTest extends ActorReceiveUtils with FunSuiteLike with MockFactory with ServerConfig {
  this: Suite =>


  override lazy val logger = Logger.getLogger(classOf[CoordinatorActorTest])
  lazy val system1 = ActorSystem(clusterName, config)

  //val connectorManagerActor = system1.actorOf(ConnectorManagerActor.props(null), "ConnectorManagerActor")
  val connectorManagerActor = system1.actorOf(MockConnectorManagerActor.props(), "ConnectorManagerActor")
  val coordinatorActor = system1.actorOf(CoordinatorActor.props(connectorManagerActor, new Coordinator), "CoordinatorActor")
  val connectorActor = system1.actorOf(MockConnectorActor.props(), "ConnectorActor")

  val queryId = "query_id-2384234-1341234-23434"
  val catalogName = "testCatalog"
  val selectStatement:SelectStatement=null
  val selectParsedQuery = new SelectParsedQuery(new BaseQuery(queryId, "", new CatalogName(catalogName)),
    selectStatement)
  val selectValidatedQuery = new SelectValidatedQuery(selectParsedQuery);
  val selectPlannedQuery = new SelectPlannedQuery(selectValidatedQuery, new QueryWorkflow(queryId, connectorActor,
    ExecutionType.SELECT, ResultType.RESULTS, new LogicalWorkflow(null)));

  def initialize()={
    var grid=Grid.initializer.withContactPoint("127.0.0.1").withPort(7800)
                .withListenAddress("127.0.0.1")
                .withMinInitialMembers(1)
                .withJoinTimeoutInMs(5000)
                .withPersistencePath("/tmp/borrar").init()
    val executionMap = grid.map("myExecutionData").asInstanceOf[java.util.Map[String,java.io.Serializable]]
    val lockExecution:Lock  = grid.lock("myExecutionData")
    val tmExecution:TransactionManager = grid.transactionManager("myExecutionData")
    ExecutionManager.MANAGER.init(executionMap, lockExecution, tmExecution)
  }

  test("Should return a KO message") {
    initialize()<commit_msg>inicializing ExecutionManager and MetadataManager in CoordinatorActorTest<commit_after>/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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.
 */

import java.util.concurrent.locks.Lock
import javax.transaction.TransactionManager

import akka.actor.{ActorSystem, actorRef2Scala}
import akka.pattern.ask
import com.stratio.connectors.MockConnectorActor
import com.stratio.meta.common.exceptions.ExecutionException
import com.stratio.meta.common.executionplan.{ExecutionType, QueryWorkflow, ResultType}
import com.stratio.meta.common.logicalplan.LogicalWorkflow
import com.stratio.meta.communication.{getConnectorName, replyConnectorName}
import com.stratio.meta.server.config.{ActorReceiveUtils, ServerConfig}
import com.stratio.meta2.common.api.PropertyType
import com.stratio.meta2.common.data.{CatalogName, ConnectorName, FirstLevelName}
import com.stratio.meta2.common.metadata.{ConnectorMetadata, IMetadata}
import com.stratio.meta2.core.coordinator.Coordinator
import com.stratio.meta2.core.execution.ExecutionManager
import com.stratio.meta2.core.grid.Grid
import com.stratio.meta2.core.metadata.MetadataManager
import com.stratio.meta2.core.query._
import com.stratio.meta2.core.statements.SelectStatement
import com.stratio.meta2.server.actors.CoordinatorActor
import com.stratio.meta2.server.mocks.MockConnectorManagerActor
import org.apache.log4j.Logger
import org.scalamock.scalatest.MockFactory
import org.scalatest.{FunSuiteLike, Suite}

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt

//class CoordinatorActorTest extends ActorReceiveUtils with FunSuiteLike with MockFactory  with ServerConfig{
class CoordinatorActorTest extends ActorReceiveUtils with FunSuiteLike with MockFactory with ServerConfig {
  this: Suite =>


  override lazy val logger = Logger.getLogger(classOf[CoordinatorActorTest])
  lazy val system1 = ActorSystem(clusterName, config)

  //val connectorManagerActor = system1.actorOf(ConnectorManagerActor.props(null), "ConnectorManagerActor")
  val connectorManagerActor = system1.actorOf(MockConnectorManagerActor.props(), "ConnectorManagerActor")
  val coordinatorActor = system1.actorOf(CoordinatorActor.props(connectorManagerActor, new Coordinator), "CoordinatorActor")
  val connectorActor = system1.actorOf(MockConnectorActor.props(), "ConnectorActor")

  val queryId = "query_id-2384234-1341234-23434"
  val catalogName = "testCatalog"
  val selectStatement:SelectStatement=null
  val selectParsedQuery = new SelectParsedQuery(new BaseQuery(queryId, "", new CatalogName(catalogName)),
    selectStatement)
  val selectValidatedQuery = new SelectValidatedQuery(selectParsedQuery);
  val selectPlannedQuery = new SelectPlannedQuery(selectValidatedQuery, new QueryWorkflow(queryId, connectorActor,
    ExecutionType.SELECT, ResultType.RESULTS, new LogicalWorkflow(null)));

  def initialize()={
    var grid=Grid.initializer.withContactPoint("127.0.0.1").withPort(7800)
                .withListenAddress("127.0.0.1")
                .withMinInitialMembers(1)
                .withJoinTimeoutInMs(5000)
                .withPersistencePath("/tmp/borrar").init()
    val executionMap = grid.map("myExecutionData").asInstanceOf[java.util.Map[String,java.io.Serializable]]
    val lockExecution:Lock  = grid.lock("myExecutionData")
    val tmExecution:TransactionManager = grid.transactionManager("myExecutionData")
    ExecutionManager.MANAGER.init(executionMap, lockExecution, tmExecution)

    val metadataMap = grid.map("myMetadata").asInstanceOf[java.util.Map[FirstLevelName,IMetadata]]
    val lock:Lock  = grid.lock("myMetadata")
    val tm = grid.transactionManager("myMetadata")
    MetadataManager.MANAGER.init(metadataMap,lock,tm)
    val future=connectorActor ? getConnectorName()
    val connectorName= Await.result(future, 3 seconds).asInstanceOf[replyConnectorName]
    val dataStoreRefs = new java.util.ArrayList[String]().asInstanceOf[java.util.List[String]]
    val requiredProperties= new java.util.ArrayList[PropertyType]().asInstanceOf[java.util.List[PropertyType]]
    val optionalProperties= new java.util.ArrayList[PropertyType]().asInstanceOf[java.util.List[PropertyType]]
    val supportedOperations= new java.util.ArrayList[String]()
    val connectorMetadata = new ConnectorMetadata(
        new ConnectorName(connectorName.name),
        "version",
        dataStoreRefs,
        requiredProperties,
        optionalProperties,
        supportedOperations
    )
    MetadataManager.MANAGER.createConnector( connectorMetadata )
    MetadataManager.MANAGER.addConnectorRef(new ConnectorName(connectorName.name),connectorActor)
    //MetadataManager.MANAGER.setConnectorStatus(new ConnectorName(connectorName),Status.ONLINE)
  }

  test("Should return a KO message") {
    initialize()
{'fraction_non_alphanumeric': 0.0888365621320775, 'fraction_numerical': 0.013057904313389703, 'mean_word_length': 5.435261707988981, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '6350671', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2557, 'n_words': 597}