org.scalatest.mockito.MockitoSugar Scala Examples

The following examples show how to use org.scalatest.mockito.MockitoSugar. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: OffsetLoaderTest.scala    From toketi-iothubreact   with MIT License 6 votes vote down vote up
package com.microsoft.azure.iot.iothubreact.checkpointing

import com.microsoft.azure.iot.iothubreact.config.{IConfiguration, IConnectConfiguration}
import org.scalatest.FunSuite
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito.when
import org.scalatest.Matchers._

class OffsetLoaderTest extends FunSuite with MockitoSugar {

  test("test GetSavedOffsets handles None appropriately") {

    val config = mock[IConfiguration]
    val cnConfig = mock[IConnectConfiguration]
    when(config.connect) thenReturn(cnConfig)
    when(cnConfig.iotHubPartitions) thenReturn(10)
    val loader = StubbedLoader(config)
    loader.GetSavedOffsets should be(Map(0 → "Offset 0", 1 → "Offset 1", 3 → "Offset 3"))
  }

  case class StubbedLoader(config: IConfiguration) extends OffsetLoader(config) {

    override private[iothubreact] def GetSavedOffset(partition: Int) = {
      partition match {
        case 0 ⇒ Some("Offset 0")
        case 1 ⇒ Some("Offset 1")
        case 3 ⇒ Some("Offset 3")
        case _ ⇒ None
      }
    }
  }

} 
Example 2
Source File: ThreadFactoryBuilderTest.scala    From gfc-concurrent   with Apache License 2.0 5 votes vote down vote up
package com.gilt.gfc.concurrent

import java.lang.Thread.UncaughtExceptionHandler
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, FunSuite}

class ThreadFactoryBuilderTest extends FunSuite with Matchers with MockitoSugar {
  test("name and groupname") {
    val f = ThreadFactoryBuilder("groupname", "name").withDaemonFlag(false).build()

    val t = f.newThread(new Runnable {
      override def run(): Unit = {}
    })
    t.getName should fullyMatch regex "name\\-\\d+"
    t.getThreadGroup.getName shouldBe "groupname"
  }

  test("All defaults") {
    val f = ThreadFactoryBuilder().build()

    val t = f.newThread(new Runnable {
      override def run(): Unit = {}
    })
    t.getName should fullyMatch regex "Thread\\-\\d+"
    t.isDaemon shouldBe true
    t.getPriority shouldBe Thread.NORM_PRIORITY
    t.getThreadGroup should be theSameInstanceAs Thread.currentThread.getThreadGroup
    t.getUncaughtExceptionHandler shouldBe Thread.currentThread.getThreadGroup
  }

  test("with...") {
    val group = new ThreadGroup("foo")
    val exh = mock[UncaughtExceptionHandler]
    val f = ThreadFactoryBuilder().
      withNameFormat("bar-%d").
      withDaemonFlag(false).
      withPriority(Thread.MIN_PRIORITY).
      withThreadGroup(group).
      withUncaughtExceptionHandler(exh).
      build

    val t = f.newThread(new Runnable {
      override def run(): Unit = {}
    })
    t.getName shouldBe "bar-0"
    t.isDaemon shouldBe false
    t.getPriority shouldBe Thread.MIN_PRIORITY
    t.getThreadGroup should be theSameInstanceAs group
    t.getUncaughtExceptionHandler shouldBe exh
  }
} 
Example 3
Source File: InMemoryLedgerReaderWriterSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.on.memory

import com.codahale.metrics.MetricRegistry
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.daml.ledger.participant.state.kvutils.api.CommitMetadata
import com.daml.ledger.participant.state.v1.{ParticipantId, SubmissionResult}
import com.daml.ledger.validator.{BatchedValidatingCommitter, LedgerStateOperations}
import com.daml.lf.data.Ref
import com.daml.metrics.Metrics
import com.daml.platform.akkastreams.dispatcher.Dispatcher
import com.google.protobuf.ByteString
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito.{times, verify, when}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Matchers}

import scala.concurrent.{ExecutionContext, Future}

class InMemoryLedgerReaderWriterSpec
    extends AsyncWordSpec
    with AkkaBeforeAndAfterAll
    with Matchers
    with MockitoSugar {
  "commit" should {
    "not signal new head in case of failure" in {
      val mockDispatcher = mock[Dispatcher[Index]]
      val mockCommitter = mock[BatchedValidatingCommitter[Index]]
      when(
        mockCommitter.commit(
          anyString(),
          any[ByteString](),
          any[ParticipantId](),
          any[LedgerStateOperations[Index]])(any[ExecutionContext]()))
        .thenReturn(
          Future.successful(SubmissionResult.InternalError("Validation failed with an exception")))
      val instance = new InMemoryLedgerReaderWriter(
        Ref.ParticipantId.assertFromString("participant ID"),
        "ledger ID",
        mockDispatcher,
        InMemoryState.empty,
        mockCommitter,
        new Metrics(new MetricRegistry)
      )

      instance
        .commit("correlation ID", ByteString.copyFromUtf8("some bytes"), CommitMetadata.Empty)
        .map { actual =>
          verify(mockDispatcher, times(0)).signalNewHead(anyInt())
          actual should be(a[SubmissionResult.InternalError])
        }
    }
  }
} 
Example 4
Source File: StoreBackedCommandExecutorSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.apiserver.execution

import com.daml.ledger.api.domain.Commands
import com.daml.ledger.participant.state.index.v2.{ContractStore, IndexPackagesService}
import com.daml.lf.crypto.Hash
import com.daml.lf.data.Ref.ParticipantId
import com.daml.lf.data.{ImmArray, Ref, Time}
import com.daml.lf.engine.{Engine, ResultDone}
import com.daml.lf.transaction.Transaction
import com.daml.lf.transaction.test.TransactionBuilder
import com.daml.logging.LoggingContext
import com.daml.metrics.Metrics
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Matchers}

class StoreBackedCommandExecutorSpec extends AsyncWordSpec with MockitoSugar with Matchers {
  private val emptyTransaction =
    Transaction.SubmittedTransaction(TransactionBuilder.Empty)
  private val emptyTransactionMetadata = Transaction.Metadata(
    submissionSeed = None,
    submissionTime = Time.Timestamp.now(),
    usedPackages = Set.empty,
    dependsOnTime = false,
    nodeSeeds = ImmArray.empty,
    byKeyNodes = ImmArray.empty)

  "execute" should {
    "add interpretation time to result" in {
      val mockEngine = mock[Engine]
      when(mockEngine.submit(any[com.daml.lf.command.Commands], any[ParticipantId], any[Hash]))
        .thenReturn(
          ResultDone[(Transaction.SubmittedTransaction, Transaction.Metadata)](
            (emptyTransaction, emptyTransactionMetadata)
          )
        )
      val instance = new StoreBackedCommandExecutor(
        mockEngine,
        Ref.ParticipantId.assertFromString("anId"),
        mock[IndexPackagesService],
        mock[ContractStore],
        mock[Metrics])
      val mockDomainCommands = mock[Commands]
      val mockLfCommands = mock[com.daml.lf.command.Commands]
      when(mockLfCommands.ledgerEffectiveTime).thenReturn(Time.Timestamp.now())
      when(mockDomainCommands.workflowId).thenReturn(None)
      when(mockDomainCommands.commands).thenReturn(mockLfCommands)

      LoggingContext.newLoggingContext { implicit context =>
        instance.execute(mockDomainCommands, Hash.hashPrivateKey("a key")).map { actual =>
          actual.right.foreach { actualResult =>
            actualResult.interpretationTimeNanos should be > 0L
          }
          succeed
        }
      }
    }
  }
} 
Example 5
Source File: CachingCommitStrategySpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator.caching

import com.daml.caching.{Cache, WeightedCache}
import com.daml.ledger.participant.state.kvutils.DamlKvutils.{
  DamlLogEntry,
  DamlLogEntryId,
  DamlStateKey,
  DamlStateValue
}
import com.daml.ledger.participant.state.kvutils.caching.`Message Weight`
import com.daml.ledger.participant.state.v1.ParticipantId
import com.daml.ledger.validator.CommitStrategy
import com.daml.ledger.validator.TestHelper._
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Matchers}

import scala.concurrent.Future

class CachingCommitStrategySpec extends AsyncWordSpec with Matchers with MockitoSugar {
  "commit" should {
    "update cache with output state upon commit if policy allows" in {
      val cache = newCache()
      val instance = createInstance(cache, shouldCache = true)
      val expectedKey = DamlStateKey.newBuilder.setContractId("a contract ID").build
      val outputState = Map(expectedKey -> DamlStateValue.getDefaultInstance)

      instance
        .commit(aParticipantId, "correlation ID", aLogEntryId(), aLogEntry, Map.empty, outputState)
        .map { _ =>
          cache.getIfPresent(expectedKey) shouldBe defined
        }
    }

    "not update cache with output state upon commit if policy does not allow" in {
      val cache = newCache()
      val instance = createInstance(cache, shouldCache = false)
      val expectedKey = DamlStateKey.newBuilder.setContractId("a contract ID").build
      val outputState = Map(expectedKey -> DamlStateValue.getDefaultInstance)

      instance
        .commit(aParticipantId, "correlation ID", aLogEntryId(), aLogEntry, Map.empty, outputState)
        .map { _ =>
          cache.getIfPresent(expectedKey) should not be defined
        }
    }
  }

  private def newCache(): Cache[DamlStateKey, DamlStateValue] =
    WeightedCache.from[DamlStateKey, DamlStateValue](WeightedCache.Configuration(1024))

  private def createInstance(
      cache: Cache[DamlStateKey, DamlStateValue],
      shouldCache: Boolean): CachingCommitStrategy[Unit] = {
    val mockCommitStrategy = mock[CommitStrategy[Unit]]
    when(
      mockCommitStrategy.commit(
        any[ParticipantId](),
        anyString(),
        any[DamlLogEntryId](),
        any[DamlLogEntry](),
        any[Map[DamlStateKey, Option[DamlStateValue]]](),
        any[Map[DamlStateKey, DamlStateValue]]()
      ))
      .thenReturn(Future.unit)
    new CachingCommitStrategy[Unit](cache, _ => shouldCache, mockCommitStrategy)
  }
} 
Example 6
Source File: CachingDamlLedgerStateReaderSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator.caching

import com.daml.caching.WeightedCache
import com.daml.ledger.participant.state.kvutils.DamlKvutils.{DamlStateKey, DamlStateValue}
import com.daml.ledger.participant.state.kvutils.caching.`Message Weight`
import com.daml.ledger.validator.{DamlLedgerStateReader, DefaultStateKeySerializationStrategy}
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Inside, Matchers}

import scala.concurrent.{ExecutionContext, Future}

class CachingDamlLedgerStateReaderSpec
    extends AsyncWordSpec
    with Matchers
    with Inside
    with MockitoSugar {

  private val keySerializationStrategy = DefaultStateKeySerializationStrategy

  "readState" should {
    "record read keys" in {
      val mockReader = mock[DamlLedgerStateReader]
      when(mockReader.readState(argThat((keys: Seq[DamlStateKey]) => keys.size == 1)))
        .thenReturn(Future.successful(Seq(Some(aDamlStateValue()))))
      val instance = newInstance(mockReader, shouldCache = false)

      instance.readState(Seq(aDamlStateKey)).map { actual =>
        actual should have size 1
        instance.getReadSet should be(
          Set(keySerializationStrategy.serializeStateKey(aDamlStateKey)))
      }
    }

    "update cache upon read if policy allows" in {
      val mockReader = mock[DamlLedgerStateReader]
      when(mockReader.readState(argThat((keys: Seq[DamlStateKey]) => keys.size == 1)))
        .thenReturn(Future.successful(Seq(Some(aDamlStateValue()))))
      val instance = newInstance(mockReader, shouldCache = true)

      instance.readState(Seq(aDamlStateKey)).map { _ =>
        instance.cache.getIfPresent(aDamlStateKey) shouldBe defined
      }
    }

    "do not update cache upon read if policy does not allow" in {
      val mockReader = mock[DamlLedgerStateReader]
      when(mockReader.readState(argThat((keys: Seq[DamlStateKey]) => keys.size == 1)))
        .thenReturn(Future.successful(Seq(Some(aDamlStateValue()))))
      val instance = newInstance(mockReader, shouldCache = false)

      instance.readState(Seq(aDamlStateKey)).map { _ =>
        instance.cache.getIfPresent(aDamlStateKey) should not be defined
      }
    }

    "serve request from cache for seen key (if policy allows)" in {
      val mockReader = mock[DamlLedgerStateReader]
      when(mockReader.readState(any[Seq[DamlStateKey]]())).thenReturn(Future.successful(Seq(None)))
      val instance = newInstance(mockReader, shouldCache = true)

      for {
        originalReadState <- instance.readState(Seq(aDamlStateKey))
        readAgain <- instance.readState(Seq(aDamlStateKey))
      } yield {
        verify(mockReader, times(1)).readState(_)
        readAgain shouldEqual originalReadState
      }
    }
  }

  private lazy val aDamlStateKey = DamlStateKey.newBuilder
    .setContractId("aContractId")
    .build

  private def aDamlStateValue(): DamlStateValue = DamlStateValue.getDefaultInstance

  private def newInstance(damlLedgerStateReader: DamlLedgerStateReader, shouldCache: Boolean)(
      implicit executionContext: ExecutionContext): CachingDamlLedgerStateReader = {
    val cache = WeightedCache.from[DamlStateKey, DamlStateValue](WeightedCache.Configuration(1024))
    new CachingDamlLedgerStateReader(
      cache,
      _ => shouldCache,
      keySerializationStrategy,
      damlLedgerStateReader)
  }
} 
Example 7
Source File: BatchedValidatingCommitterSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator

import java.time.Instant

import akka.stream.Materializer
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.daml.ledger.participant.state.v1.{ParticipantId, SubmissionResult}
import com.daml.ledger.validator.TestHelper.aParticipantId
import com.daml.ledger.validator.batch.BatchedSubmissionValidator
import com.google.protobuf.ByteString
import org.mockito.ArgumentMatchers.{any, anyString}
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Matchers}

import scala.concurrent.{ExecutionContext, Future}

class BatchedValidatingCommitterSpec
    extends AsyncWordSpec
    with AkkaBeforeAndAfterAll
    with Matchers
    with MockitoSugar {
  "commit" should {
    "return Acknowledged in case of success" in {
      val mockValidator = mock[BatchedSubmissionValidator[Unit]]
      when(
        mockValidator.validateAndCommit(
          any[ByteString](),
          anyString(),
          any[Instant](),
          any[ParticipantId](),
          any[DamlLedgerStateReader](),
          any[CommitStrategy[Unit]]())(any[Materializer](), any[ExecutionContext]()))
        .thenReturn(Future.unit)
      val instance =
        BatchedValidatingCommitter[Unit](() => Instant.now(), mockValidator)

      instance
        .commit("", ByteString.EMPTY, aParticipantId, mock[LedgerStateOperations[Unit]])
        .map { actual =>
          actual shouldBe SubmissionResult.Acknowledged
        }
    }

    "return InternalError in case of an exception" in {
      val mockValidator = mock[BatchedSubmissionValidator[Unit]]
      when(
        mockValidator.validateAndCommit(
          any[ByteString](),
          anyString(),
          any[Instant](),
          any[ParticipantId](),
          any[DamlLedgerStateReader](),
          any[CommitStrategy[Unit]]())(any[Materializer](), any[ExecutionContext]()))
        .thenReturn(Future.failed(new IllegalArgumentException("Validation failure")))
      val instance = BatchedValidatingCommitter[Unit](() => Instant.now(), mockValidator)

      instance
        .commit("", ByteString.EMPTY, aParticipantId, mock[LedgerStateOperations[Unit]])
        .map { actual =>
          actual shouldBe SubmissionResult.InternalError("Validation failure")
        }
    }
  }
} 
Example 8
Source File: RawToDamlLedgerStateReaderAdapterSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator

import com.daml.ledger.participant.state.kvutils.DamlKvutils.{
  DamlPartyAllocation,
  DamlStateKey,
  DamlStateValue
}
import com.daml.ledger.participant.state.kvutils.Envelope
import com.daml.ledger.validator.TestHelper.{anInvalidEnvelope, makePartySubmission}
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalatest.{AsyncWordSpec, Matchers}
import org.scalatest.mockito.MockitoSugar

import scala.concurrent.Future

class RawToDamlLedgerStateReaderAdapterSpec extends AsyncWordSpec with Matchers with MockitoSugar {
  "readState" should {
    "read the right key and deserialize it" in {
      val expectedKey = DefaultStateKeySerializationStrategy.serializeStateKey(aDamlStateKey())
      val expectedValue = DamlStateValue.newBuilder
        .setParty(DamlPartyAllocation.newBuilder.setDisplayName("aParty"))
        .build
      val mockReader = mock[LedgerStateReader]
      when(mockReader.read(any[Seq[LedgerStateOperations.Key]]()))
        .thenReturn(Future.successful(Seq(Some(Envelope.enclose(expectedValue)))))
      val instance =
        new RawToDamlLedgerStateReaderAdapter(mockReader, DefaultStateKeySerializationStrategy)

      instance.readState(Seq(aDamlStateKey())).map { actual =>
        verify(mockReader, times(1)).read(Seq(expectedKey))
        actual shouldBe Seq(Some(expectedValue))
      }
    }

    "throw in case of an invalid envelope returned from underlying reader" in {
      val mockReader = mock[LedgerStateReader]
      when(mockReader.read(any[Seq[LedgerStateOperations.Key]]()))
        .thenReturn(Future.successful(Seq(Some(anInvalidEnvelope))))
      val instance =
        new RawToDamlLedgerStateReaderAdapter(mockReader, DefaultStateKeySerializationStrategy)

      instance.readState(Seq(aDamlStateKey())).failed.map { actual =>
        actual shouldBe a[RuntimeException]
        actual.getLocalizedMessage should include("Opening enveloped")
      }
    }

    "throw in case an enveloped value other than a DamlStateValue is returned from underlying reader" in {
      val notADamlStateValue = makePartySubmission("aParty")
      val mockReader = mock[LedgerStateReader]
      when(mockReader.read(any[Seq[LedgerStateOperations.Key]]()))
        .thenReturn(Future.successful(Seq(Some(Envelope.enclose(notADamlStateValue)))))
      val instance =
        new RawToDamlLedgerStateReaderAdapter(mockReader, DefaultStateKeySerializationStrategy)

      instance.readState(Seq(aDamlStateKey())).failed.map { actual =>
        actual shouldBe a[RuntimeException]
        actual.getLocalizedMessage should include("Opening enveloped")
      }
    }
  }

  private def aDamlStateKey(): DamlStateKey =
    DamlStateKey.newBuilder
      .setContractId("aContractId")
      .build
} 
Example 9
Source File: IndexedSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator.batch

import org.scalatest.{AsyncWordSpec, Inside, Matchers}
import org.scalatest.mockito.MockitoSugar

import scala.concurrent.Future

class IndexedSpec extends AsyncWordSpec with Matchers with Inside with MockitoSugar {

  "indexed" should {

    "map over value" in {
      val indexed = Indexed(50, 0L)
      val indexed2 = indexed.map(_ + 1)
      indexed2.index should be(0L)
      indexed2.value should be(51)
    }

    "mapFuture over value" in {
      val indexed = Indexed(50, 0L)
      indexed
        .mapFuture(v => Future { v + 1 })
        .map { newIndexed =>
          newIndexed.index should be(0L)
          newIndexed.value should be(51)
        }
    }

    "fromSeq works" in {
      val seq = Seq(1, 2, 3)
      val indexedSeq = Indexed.fromSeq(seq)
      indexedSeq should have size (3)
      seq.zipWithIndex.foreach {
        case (x, i) =>
          indexedSeq(i).value should be(x)
          indexedSeq(i).index should be(i)
      }
      succeed
    }
  }
} 
Example 10
Source File: LogAppendingCommitStrategySpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.validator

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{AsyncWordSpec, Matchers}
import TestHelper._
import com.daml.ledger.participant.state.kvutils.DamlKvutils.{DamlStateKey, DamlStateValue}
import com.daml.ledger.participant.state.kvutils.Envelope
import com.daml.ledger.validator.LedgerStateOperations.{Key, Value}
import com.google.protobuf.ByteString
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito.{times, verify, when}

import scala.concurrent.Future

class LogAppendingCommitStrategySpec extends AsyncWordSpec with Matchers with MockitoSugar {
  "commit" should {
    "return index from appendToLog" in {
      val mockLedgerStateOperations = mock[LedgerStateOperations[Long]]
      val expectedIndex = 1234L
      when(mockLedgerStateOperations.appendToLog(any[Key](), any[Value]()))
        .thenReturn(Future.successful(expectedIndex))
      val instance =
        new LogAppendingCommitStrategy[Long](
          mockLedgerStateOperations,
          DefaultStateKeySerializationStrategy)

      instance
        .commit(aParticipantId, "a correlation ID", aLogEntryId(), aLogEntry, Map.empty, Map.empty)
        .map { actualIndex =>
          verify(mockLedgerStateOperations, times(1)).appendToLog(any[Key](), any[Value]())
          verify(mockLedgerStateOperations, times(0)).writeState(any[Seq[(Key, Value)]]())
          actualIndex should be(expectedIndex)
        }
    }

    "write keys serialized according to strategy" in {
      val mockLedgerStateOperations = mock[LedgerStateOperations[Long]]
      val actualOutputStateBytesCaptor = ArgumentCaptor
        .forClass(classOf[Seq[(Key, Value)]])
        .asInstanceOf[ArgumentCaptor[Seq[(Key, Value)]]]
      when(mockLedgerStateOperations.writeState(actualOutputStateBytesCaptor.capture()))
        .thenReturn(Future.unit)
      when(mockLedgerStateOperations.appendToLog(any[Key](), any[Value]()))
        .thenReturn(Future.successful(0L))
      val mockStateKeySerializationStrategy = mock[StateKeySerializationStrategy]
      val expectedStateKey = ByteString.copyFromUtf8("some key")
      when(mockStateKeySerializationStrategy.serializeStateKey(any[DamlStateKey]()))
        .thenReturn(expectedStateKey)
      val expectedOutputStateBytes = Seq((expectedStateKey, Envelope.enclose(aStateValue)))
      val instance =
        new LogAppendingCommitStrategy[Long](
          mockLedgerStateOperations,
          mockStateKeySerializationStrategy)

      instance
        .commit(
          aParticipantId,
          "a correlation ID",
          aLogEntryId(),
          aLogEntry,
          Map.empty,
          Map(aStateKey -> aStateValue))
        .map { _: Long =>
          verify(mockStateKeySerializationStrategy, times(1)).serializeStateKey(aStateKey)
          verify(mockLedgerStateOperations, times(1)).writeState(any[Seq[(Key, Value)]]())
          actualOutputStateBytesCaptor.getValue should be(expectedOutputStateBytes)
        }
    }
  }

  private val aStateKey: DamlStateKey = DamlStateKey
    .newBuilder()
    .setContractId(1.toString)
    .build

  private val aStateValue: DamlStateValue = DamlStateValue.getDefaultInstance
} 
Example 11
Source File: FileBasedLedgerDataExportSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.participant.state.kvutils.export

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, DataOutputStream}
import java.time.Instant

import com.daml.ledger.participant.state.v1
import com.google.protobuf.ByteString
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

class FileBasedLedgerDataExportSpec extends WordSpec with Matchers with MockitoSugar {
  // XXX SC remove in Scala 2.13; see notes in ConfSpec
  import scala.collection.GenTraversable, org.scalatest.enablers.Containing
  private[this] implicit def `fixed sig containingNatureOfGenTraversable`[
      E: org.scalactic.Equality,
      TRAV]: Containing[TRAV with GenTraversable[E]] =
    Containing.containingNatureOfGenTraversable[E, GenTraversable]

  "addParentChild" should {
    "add entry to correlation ID mapping" in {
      val instance = new FileBasedLedgerDataExporter(mock[DataOutputStream])
      instance.addParentChild("parent", "child")

      instance.correlationIdMapping should contain("child" -> "parent")
    }
  }

  "addToWriteSet" should {
    "append to existing data" in {
      val instance = new FileBasedLedgerDataExporter(mock[DataOutputStream])
      instance.addParentChild("parent", "child")
      instance.addToWriteSet("child", Seq(keyValuePairOf("a", "b")))
      instance.addToWriteSet("child", Seq(keyValuePairOf("c", "d")))

      instance.bufferedKeyValueDataPerCorrelationId should contain(
        "parent" ->
          Seq(keyValuePairOf("a", "b"), keyValuePairOf("c", "d")))
    }
  }

  "finishedProcessing" should {
    "remove all data such as submission info, write-set and child correlation IDs" in {
      val dataOutputStream = new DataOutputStream(new ByteArrayOutputStream())
      val instance = new FileBasedLedgerDataExporter(dataOutputStream)
      instance.addSubmission(
        ByteString.copyFromUtf8("an envelope"),
        "parent",
        Instant.now(),
        v1.ParticipantId.assertFromString("id"))
      instance.addParentChild("parent", "parent")
      instance.addToWriteSet("parent", Seq(keyValuePairOf("a", "b")))

      instance.finishedProcessing("parent")

      instance.inProgressSubmissions shouldBe empty
      instance.bufferedKeyValueDataPerCorrelationId shouldBe empty
      instance.correlationIdMapping shouldBe empty
    }
  }

  "serialized submission" should {
    "be readable back" in {
      val baos = new ByteArrayOutputStream()
      val dataOutputStream = new DataOutputStream(baos)
      val instance = new FileBasedLedgerDataExporter(dataOutputStream)
      val expectedRecordTimeInstant = Instant.now()
      val expectedParticipantId = v1.ParticipantId.assertFromString("id")
      instance.addSubmission(
        ByteString.copyFromUtf8("an envelope"),
        "parent",
        expectedRecordTimeInstant,
        v1.ParticipantId.assertFromString("id"))
      instance.addParentChild("parent", "parent")
      instance.addToWriteSet("parent", Seq(keyValuePairOf("a", "b")))

      instance.finishedProcessing("parent")

      val dataInputStream = new DataInputStream(new ByteArrayInputStream(baos.toByteArray))
      val (actualSubmissionInfo, actualWriteSet) = Serialization.readEntry(dataInputStream)
      actualSubmissionInfo.submissionEnvelope should be(ByteString.copyFromUtf8("an envelope"))
      actualSubmissionInfo.correlationId should be("parent")
      actualSubmissionInfo.recordTimeInstant should be(expectedRecordTimeInstant)
      actualSubmissionInfo.participantId should be(expectedParticipantId)
      actualWriteSet should be(Seq(keyValuePairOf("a", "b")))
    }
  }

  private def keyValuePairOf(key: String, value: String): (ByteString, ByteString) =
    ByteString.copyFromUtf8(key) -> ByteString.copyFromUtf8(value)
} 
Example 12
Source File: AuthActionSelectorSpec.scala    From nisp-frontend   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.nisp.controllers.auth

import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.OneAppPerSuite
import uk.gov.hmrc.nisp.config.ApplicationConfig
import uk.gov.hmrc.play.test.UnitSpec

class AuthActionSelectorSpec extends UnitSpec with OneAppPerSuite with MockitoSugar {

  "The decide method" should {

    "use VerifyAuthActionImpl when IV disabled" in {
      val applicationConfig: ApplicationConfig = mock[ApplicationConfig]

      when(applicationConfig.identityVerification).thenReturn(false)
      AuthActionSelector.decide(applicationConfig) shouldBe a[VerifyAuthActionImpl]
    }

    "use AuthActionImpl when IV enabled" in {
      val applicationConfig: ApplicationConfig = mock[ApplicationConfig]

      when(applicationConfig.identityVerification).thenReturn(true)
      AuthActionSelector.decide(applicationConfig) shouldBe an[AuthActionImpl]
    }
  }
} 
Example 13
Source File: KafkaConfiguratorAppSpec.scala    From kafka-configurator   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.sky.kafka.configurator

import java.io.{File, FileReader}

import com.sky.kafka.configurator.error.{ConfiguratorFailure, TopicNotFound}
import common.BaseSpec
import io.circe.generic.AutoDerivation
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import scala.util.{Failure, Success}

class KafkaConfiguratorAppSpec extends BaseSpec with MockitoSugar with AutoDerivation {

  val topicConfigurator = mock[TopicConfigurator]
  val kafkaConfiguratorApp = KafkaConfiguratorApp(topicConfigurator)

  it should "provide logs and errors when file has been parsed successfully" in {
    val file = new File(getClass.getResource("/topic-configuration-with-error.yml").getPath)
    val topics = TopicConfigurationParser(new FileReader(file)).right.value

    val error = TopicNotFound(topics(1).name)

    when(topicConfigurator.configure(topics.head))
      .thenReturn(Success(()).withLog("foo"))
    when(topicConfigurator.configure(topics(1)))
      .thenReturn(Failure[Unit](error).asWriter)
    when(topicConfigurator.configure(topics(2)))
      .thenReturn(Success(()).withLog("bar"))

    kafkaConfiguratorApp.configureTopicsFrom(List(file)) shouldBe Success((
      List(ConfiguratorFailure(topics.tail.head.name, error)),
      List("foo", "bar")
    ))
  }

  it should "succeed when given empty configuration file" in {
    val invalidFile = File.createTempFile("empty", "yml")
    invalidFile.deleteOnExit()
    kafkaConfiguratorApp.configureTopicsFrom(List(invalidFile)) shouldBe a[Success[_]]
  }

  it should "fail-fast when the file does not exist" in {
    kafkaConfiguratorApp.configureTopicsFrom(List(new File("does-not-exist"))) shouldBe a[Failure[_]]
  }

} 
Example 14
Source File: HiveTableFilesFnTest.scala    From eel-sdk   with Apache License 2.0 5 votes vote down vote up
package io.eels.component.hive

import java.nio.file.Paths

import com.sksamuel.exts.Logging
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.hdfs.MiniDFSCluster
import org.apache.hadoop.hive.metastore.IMetaStoreClient
import org.apache.hadoop.hive.metastore.api.Table
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}

class HiveTableFilesFnTest extends FlatSpec with Matchers with Logging with MockitoSugar {

  System.clearProperty(MiniDFSCluster.PROP_TEST_BUILD_DATA)
  val clusterPath = Paths.get("miniclusters", "cluster")
  val conf = new Configuration()
  conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, clusterPath.toAbsolutePath.toString)
  val cluster = new MiniDFSCluster.Builder(conf).build()
  implicit val fs = cluster.getFileSystem

  "HiveTableFilesFn" should "detect all files in root when no partitions" in {

    implicit val client = mock[IMetaStoreClient]
    org.mockito.Mockito.when(client.getTable("default", "mytable")).thenReturn(new Table)

    val root = new Path("tab1")
    fs.mkdirs(root)

    // table scanner will skip 0 length files
    val a = fs.create(new Path(root, "a"))
    a.write(1)
    a.close()

    val b = fs.create(new Path(root, "b"))
    b.write(1)
    b.close()

    HiveTableFilesFn("default", "mytable", fs.resolvePath(root), Nil).values.flatten.map(_.getPath.getName).toSet shouldBe Set("a", "b")
  }

  it should "ignore hidden files in root when no partitions" in {
    implicit val client = mock[IMetaStoreClient]
    org.mockito.Mockito.when(client.getTable("default", "mytable")).thenReturn(new Table)

    val root = new Path("tab2")
    fs.mkdirs(root)

    // table scanner will skip 0 length files
    val a = fs.create(new Path(root, "a"))
    a.write(1)
    a.close()

    val b = fs.create(new Path(root, "_b"))
    b.write(1)
    b.close()

    HiveTableFilesFn("default", "mytable", fs.resolvePath(root), Nil).values.flatten.map(_.getPath.getName).toSet shouldBe Set("a")
  }
} 
Example 15
Source File: MockedSidechainNodeViewHolderFixture.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.fixtures

import akka.actor.{ActorRef, ActorSystem, Props}
import com.horizen._
import org.mockito.Mockito
import org.scalatest.mockito.MockitoSugar
import scorex.core.settings.{NetworkSettings, ScorexSettings}

class MockedSidechainNodeViewHolder(sidechainSettings: SidechainSettings,
                                    history: SidechainHistory,
                                    state: SidechainState,
                                    wallet: SidechainWallet,
                                    mempool: SidechainMemoryPool)
  extends SidechainNodeViewHolder(sidechainSettings, null, null, null, null, null, null, null, null, null, null, null, null) {

  override def restoreState(): Option[(HIS, MS, VL, MP)] = {
    Some(history, state, wallet, mempool)
  }
}


trait MockedSidechainNodeViewHolderFixture extends MockitoSugar {
  def getMockedSidechainNodeViewHolderRef(history: SidechainHistory, state: SidechainState, wallet: SidechainWallet, mempool: SidechainMemoryPool)
                                         (implicit actorSystem: ActorSystem): ActorRef = {
    val sidechainSettings = mock[SidechainSettings]
    val scorexSettings = mock[ScorexSettings]
    val networkSettings = mock[NetworkSettings]
    Mockito.when(sidechainSettings.scorexSettings)
      .thenAnswer(answer => {
        scorexSettings
      })
    Mockito.when(scorexSettings.network)
      .thenAnswer(answer => {
      networkSettings
    })
    Mockito.when(networkSettings.maxModifiersCacheSize)
      .thenAnswer(answer => {
      10
    })

    actorSystem.actorOf(Props(new MockedSidechainNodeViewHolder(sidechainSettings, history, state, wallet, mempool)))
  }
} 
Example 16
Source File: KinesisProducerIntegrationSpec.scala    From reactive-kinesis   with Apache License 2.0 5 votes vote down vote up
package com.weightwatchers.reactive.kinesis

import java.io.File

import com.amazonaws.services.kinesis.producer.{KinesisProducer => AWSKinesisProducer}
import com.typesafe.config.ConfigFactory
import com.weightwatchers.reactive.kinesis.common.{
  KinesisSuite,
  KinesisTestConsumer,
  TestCredentials
}
import com.weightwatchers.reactive.kinesis.consumer.KinesisConsumer.ConsumerConf
import com.weightwatchers.reactive.kinesis.models.ProducerEvent
import com.weightwatchers.reactive.kinesis.producer.{KinesisProducer, ProducerConf}
import org.scalatest.concurrent.Eventually
import org.scalatest.mockito.MockitoSugar
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers}

import scala.concurrent.duration._
import scala.language.postfixOps
import scala.util.Random

//scalastyle:off magic.number
class KinesisProducerIntegrationSpec
    extends FreeSpec
    with Matchers
    with MockitoSugar
    with BeforeAndAfterAll
    with Eventually
    with KinesisSuite {

  implicit val ece = scala.concurrent.ExecutionContext.global

  val TestStreamNrOfMessagesPerShard: Long = 0

  implicit override val patienceConfig: PatienceConfig =
    PatienceConfig(timeout = Span(5, Seconds), interval = Span(100, Millis))

  "The KinesisProducer" - {

    "Should publish a message to a stream" in new withKinesisConfForApp(
      "int-test-stream-producer-1"
    ) {

      val conf     = producerConf()
      val producer = KinesisProducer(conf)

      val existingRecordCount = testConsumer.retrieveRecords(conf.streamName, 10).size

      val event = ProducerEvent("1234", Random.alphanumeric.take(10).mkString)
      producer.addUserRecord(event)

      eventually {
        val records: Seq[String] = testConsumer.retrieveRecords(conf.streamName, 10)
        records.size shouldBe (existingRecordCount + 1)
        records should contain(
          new String(event.payload.array(), java.nio.charset.StandardCharsets.UTF_8)
        )
      }
    }
  }
}

//scalastyle:on 
Example 17
Source File: TypesafeConfigExtensionsSpec.scala    From reactive-kinesis   with Apache License 2.0 5 votes vote down vote up
package com.weightwatchers.reactive.kinesis.utils

import com.typesafe.config.ConfigFactory
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers}


class TypesafeConfigExtensionsSpec
    extends FreeSpec
    with Matchers
    with MockitoSugar
    with BeforeAndAfterAll {

  val kplConfig = ConfigFactory.parseString("""
      |kpl {
      |   AggregationEnabled = true
      |   AggregationMaxCount = 4294967295
      |   AggregationMaxSize = 51200
      |   CollectionMaxCount = 500
      |}
      |
    """.stripMargin).getConfig("kpl")

  //scalastyle:off magic.number
  "The RichConfig" - {

    "Should convert typesafe config key values into Java Properties" in {

      import TypesafeConfigExtensions._

      val javaProperties = kplConfig.toProperties

      javaProperties.size() should equal(4)
      javaProperties.getProperty("AggregationEnabled") should equal("true")
      javaProperties.getProperty("AggregationMaxCount") should equal("4294967295")
      javaProperties.getProperty("AggregationMaxSize") should equal("51200")
      javaProperties.getProperty("CollectionMaxCount") should equal("500")

    }
  }
  //scalastyle:on
} 
Example 18
Source File: HealthControllerSpec.scala    From smui   with Apache License 2.0 5 votes vote down vote up
package controllers

import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play._
import play.api.http.ContentTypes
import play.api.mvc.Result
import play.api.test.Helpers._
import play.api.test.{FakeRequest, Helpers}

import scala.concurrent.Future


class HealthControllerSpec extends PlaySpec with MockitoSugar {

  "The HealthController" must {
    "provide an health status json" in {
      val stubControllerComponents = Helpers.stubControllerComponents()
      val controller = new HealthController(stubControllerComponents)
      val result: Future[Result] = controller.health.apply(FakeRequest())
      val bodyText: String = contentAsString(result)
      contentType(result) mustBe Some(ContentTypes.JSON)
      (contentAsJson(result) \ "name").asOpt[String] mustBe Some("search-management-ui")
    }
  }
} 
Example 19
Source File: OAuth2Spec.scala    From finch-oauth2   with Apache License 2.0 5 votes vote down vote up
package io.finch.oauth2

import cats.effect.IO
import com.twitter.finagle.http.Status
import com.twitter.finagle.oauth2._
import com.twitter.util.Future
import io.finch._
import io.finch.catsEffect._
import org.mockito.Mockito._
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.prop.Checkers

class OAuth2Spec extends FlatSpec with Matchers with Checkers with MockitoSugar {

  behavior of "OAuth2"

  it should "authorize the requests" in {
    val at: AccessToken = AccessToken("foo", None, None, None, null)
    val dh: DataHandler[Int] = mock[DataHandler[Int]]
    val ai: AuthInfo[Int] = new AuthInfo(42, "foo", None, None)

    when(dh.findAccessToken("bar")).thenReturn(Future.value(Some(at)))
    when(dh.isAccessTokenExpired(at)).thenReturn(false)
    when(dh.findAuthInfoByAccessToken(at)).thenReturn(Future.value(Some(ai)))

    val authInfo: Endpoint[IO, AuthInfo[Int]] = authorize(dh)
    val e: Endpoint[IO, Int] = get("user" :: authInfo) { ai: AuthInfo[Int] =>
      Ok(ai.user)
    }

    val i1 = Input.get("/user", "access_token" -> "bar")
    val i2 = Input.get("/user")

    e(i1).awaitOutputUnsafe() shouldBe Some(Ok(42))
    val Some(error) = e(i2).awaitOutputUnsafe()
    error.status shouldBe Status.BadRequest
    error.headers should contain key "WWW-Authenticate"
  }

  it should "issue the access token" in {
    val dh: DataHandler[Int] = mock[DataHandler[Int]]
    val at: AccessToken = AccessToken("foobar", None, None, None, null)

    when(dh.validateClient("id", "", "password")).thenReturn(Future.value(true))
    when(dh.findUser("u", "p")).thenReturn(Future.value(Some(42)))
    when(dh.getStoredAccessToken(AuthInfo(42, "id", None, None))).thenReturn(Future.value(Some(at)))
    when(dh.isAccessTokenExpired(at)).thenReturn(false)

    val grandHandlerResult: Endpoint[IO, GrantResult] = issueAccessToken(dh)
    val e: Endpoint[IO, String] = get("token" :: grandHandlerResult) { ghr: GrantResult =>
      Ok(ghr.accessToken)
    }

    val i1 = Input.get("/token",
      "grant_type" -> "password", "username" -> "u", "password" -> "p", "client_id" -> "id"
    )

    val i2 = Input.get("/token")

    e(i1).awaitOutputUnsafe() shouldBe Some(Ok("foobar"))
    val Some(error) = e(i2).awaitOutputUnsafe()
    error.status shouldBe Status.BadRequest
    error.headers should contain key "WWW-Authenticate"
  }
} 
Example 20
Source File: ServiceTest.scala    From Aton   with GNU General Public License v3.0 5 votes vote down vote up
package test

import org.scalatest.BeforeAndAfterAll
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.PlaySpec
import play.api.mvc.Result
import play.test.WithApplication
import services.state.ActionState

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._


  override def afterAll() {
    application.stopPlay()
  }

  def assertState(future: Future[ActionState], actionState: ActionState) = {
    val result = Await.result(future, 20 seconds)
    assert(result === actionState)
  }

  def waitFor[T](future: Future[T]): T = {
    Await.result(future, 20 seconds)
  }
} 
Example 21
Source File: ControllerTest.scala    From Aton   with GNU General Public License v3.0 5 votes vote down vote up
package test

import model.json.ResultMessage
import org.scalatest.{Assertion, BeforeAndAfterAll}
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.PlaySpec
import play.api.mvc.Result
import play.api.test.Helpers._
import play.test.WithApplication

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._


  override def afterAll() {
    application.stopPlay()
  }

  def assertFutureResultStatus(future: Future[Result], status: Int) = {
    val result: Result = Await.result(future, 20 seconds)
    if(result.header.status != status){
      play.Logger.error(contentAsString(future))
    }
    assert(result.header.status == status)
  }

  def assertBodyJsonMessage(future: Future[Result], message: String, emptyExtras: Boolean): Assertion = {
    //val result: Result = Await.result(future,20 seconds)
    val bodyJson = contentAsJson(future)
    play.Logger.debug(s"BodyJson: $bodyJson")
    val jsResult = bodyJson.validate[ResultMessage]
    assert(jsResult.isSuccess)
    if(!emptyExtras) {
      assert(jsResult.get.extras.nonEmpty)
      if(jsResult.get.extras.isEmpty){
        play.Logger.debug(jsResult.toString)
      }
    }
    assert(jsResult.get.result === message)
  }

  def assertBodyJsonMessage(future: Future[Result], message: String): Assertion = {
    assertBodyJsonMessage(future, message, emptyExtras = true)
  }
} 
Example 22
Source File: AuthMacroTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime

import akka.stream.Materializer
import org.coursera.naptime.access.HeaderAccessControl
import org.coursera.naptime.model.KeyFormat
import org.coursera.naptime.resources.TopLevelCollectionResource
import org.coursera.naptime.router2._
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar
import play.api.libs.json.OFormat
import play.api.mvc.RequestHeader

import scala.concurrent.ExecutionContext

case class CustomAuth()

object CustomAuthorizer extends HeaderAccessControl[CustomAuth] {
  override def run(requestHeader: RequestHeader)(implicit executionContext: ExecutionContext) = ???
  override private[naptime] def check(authInfo: CustomAuth) = ???
}

class AuthorizedResource(
    implicit val executionContext: ExecutionContext,
    val materializer: Materializer)
    extends TopLevelCollectionResource[String, Item] {

  override def keyFormat: KeyFormat[String] = KeyFormat.stringKeyFormat

  override implicit def resourceFormat: OFormat[Item] = Item.jsonFormat

  override def resourceName: String = "items"

  implicit val fields = Fields.withDefaultFields("name")

  def get(id: String) =
    Nap
      .auth(CustomAuthorizer)
      .get { ctx =>
        ???
      }

}

object AuthorizedResource {
  val routerBuilder = Router.build[AuthorizedResource]
}

class AuthMacroTest extends AssertionsForJUnit with MockitoSugar with ResourceTestImplicits {

  val schema = AuthorizedResource.routerBuilder.schema

  @Test
  def get(): Unit = {
    val handler = schema.handlers.find(_.name === "get").get
    assert(handler.authType === Some("org.coursera.naptime.CustomAuth"))
  }

} 
Example 23
Source File: FilterTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.ari.graphql.controllers.filters

import org.coursera.naptime.ari.graphql.GraphqlSchemaProvider
import org.coursera.naptime.ari.graphql.Models
import org.coursera.naptime.ari.graphql.SangriaGraphQlContext
import org.coursera.naptime.ari.graphql.SangriaGraphQlSchemaBuilder
import org.coursera.naptime.ari.graphql.models.MergedCourse
import org.coursera.naptime.ari.graphql.models.MergedInstructor
import org.coursera.naptime.ari.graphql.models.MergedPartner
import org.mockito.Mockito.when
import org.scalatest.concurrent.IntegrationPatience
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar
import play.api.libs.json.Json
import play.api.test.FakeRequest
import sangria.parser.QueryParser
import sangria.schema.Schema

import scala.concurrent.Future

trait FilterTest
    extends AssertionsForJUnit
    with MockitoSugar
    with ScalaFutures
    with IntegrationPatience {

  val baseOutgoingQuery = OutgoingQuery(Json.obj(), None)

  def noopFilter(incomingQuery: IncomingQuery) = {
    Future.successful(baseOutgoingQuery)
  }

  def exceptionThrowingFilter(incomingQuery: IncomingQuery): Future[OutgoingQuery] = {
    assert(false, "This filter should not be run")
    Future.successful(baseOutgoingQuery)
  }

  val filter: Filter

  val defaultQuery =
    """
      |query {
      |  __schema {
      |    queryType {
      |      name
      |    }
      |  }
      |}
    """.stripMargin

  val graphqlSchemaProvider = mock[GraphqlSchemaProvider]

  val allResources = Set(Models.courseResource, Models.instructorResource, Models.partnersResource)

  val schemaTypes = Map(
    "org.coursera.naptime.ari.graphql.models.MergedCourse" -> MergedCourse.SCHEMA,
    "org.coursera.naptime.ari.graphql.models.MergedPartner" -> MergedPartner.SCHEMA,
    "org.coursera.naptime.ari.graphql.models.MergedInstructor" -> MergedInstructor.SCHEMA)
  val builder = new SangriaGraphQlSchemaBuilder(allResources, schemaTypes)

  val schema = builder.generateSchema().data.asInstanceOf[Schema[SangriaGraphQlContext, Any]]
  when(graphqlSchemaProvider.schema).thenReturn(schema)

  def generateIncomingQuery(query: String = defaultQuery) = {
    val document = QueryParser.parse(query).get
    val header = FakeRequest("POST", s"/graphql").withBody(query)
    val variables = Json.obj()
    val operation = None
    IncomingQuery(document, header, variables, operation, debugMode = false)
  }

  def run(incomingQuery: IncomingQuery): Future[OutgoingQuery] = {
    filter.apply(noopFilter)(incomingQuery)
  }

  def ensureNotPropagated(incomingQuery: IncomingQuery): Future[OutgoingQuery] = {
    filter.apply(exceptionThrowingFilter)(incomingQuery)
  }
} 
Example 24
Source File: NaptimeEnumFieldTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.ari.graphql.schema

import com.linkedin.data.schema.EnumDataSchema
import com.linkedin.data.schema.Name
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar
import sangria.schema.EnumType

import scala.collection.JavaConverters._

class NaptimeEnumFieldTest extends AssertionsForJUnit with MockitoSugar {

  def buildEnumDataSchema(values: List[String]): EnumDataSchema = {
    val enum = new EnumDataSchema(new Name("testEnum"))
    val stringBuilder = new java.lang.StringBuilder()
    enum.setSymbols(values.asJava, stringBuilder)
    enum
  }

  @Test
  def build_RegularEnum(): Unit = {
    val values = List("valueOne", "valueTwo")
    val enum = buildEnumDataSchema(values)
    val field = NaptimeEnumField.build(enum, "myField")
    assert(field.fieldType.asInstanceOf[EnumType[String]].values.map(_.name) === values)
  }

  @Test
  def build_EmptyEnum(): Unit = {
    val values = List()
    val expectedValues = List("UNKNOWN")
    val enum = buildEnumDataSchema(values)
    val field = NaptimeEnumField.build(enum, "myField")
    assert(field.fieldType.asInstanceOf[EnumType[String]].values.map(_.name) === expectedValues)
  }

} 
Example 25
Source File: NaptimePaginatedResourceFieldTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.ari.graphql.schema

import org.coursera.naptime.ResourceName
import org.coursera.naptime.ari.graphql.Models
import org.coursera.naptime.ari.graphql.SangriaGraphQlContext
import org.coursera.naptime.ari.graphql.helpers.ArgumentBuilder
import org.junit.Test
import org.mockito.Mockito.when
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar

import scala.concurrent.ExecutionContext

class NaptimePaginatedResourceFieldTest extends AssertionsForJUnit with MockitoSugar {

  val fieldName = "relatedIds"
  val resourceName = ResourceName("courses", 1)
  val context = SangriaGraphQlContext(null, null, ExecutionContext.global, debugMode = false)

  private[this] val schemaMetadata = mock[SchemaMetadata]
  private[this] val resource = Models.courseResource
  when(schemaMetadata.getResourceOpt(resourceName)).thenReturn(Some(resource))
  when(schemaMetadata.getSchema(resource)).thenReturn(Some(null))

  @Test
  def computeComplexity(): Unit = {
    val field = NaptimePaginatedResourceField.build(
      schemaMetadata,
      resourceName,
      fieldName,
      None,
      None,
      List.empty)

    val argDefinitions = NaptimePaginationField.paginationArguments

    val limitTen = field.right.get.complexity.get
      .apply(context, ArgumentBuilder.buildArgs(argDefinitions, Map("limit" -> Some(10))), 1)
    assert(limitTen === 1 * NaptimePaginatedResourceField.COMPLEXITY_COST * 1)

    val limitFifty = field.right.get.complexity.get
      .apply(context, ArgumentBuilder.buildArgs(argDefinitions, Map("limit" -> Some(50))), 1)
    assert(limitFifty === 5 * NaptimePaginatedResourceField.COMPLEXITY_COST * 1)

    val limitZero = field.right.get.complexity.get
      .apply(context, ArgumentBuilder.buildArgs(argDefinitions, Map("limit" -> Some(1))), 1)
    assert(limitZero === 1 * NaptimePaginatedResourceField.COMPLEXITY_COST * 1)

    val childScoreFive = field.right.get.complexity.get
      .apply(context, ArgumentBuilder.buildArgs(argDefinitions, Map("limit" -> Some(1))), 5)
    assert(childScoreFive === 1 * NaptimePaginatedResourceField.COMPLEXITY_COST * 5)

  }

} 
Example 26
Source File: RestContextTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime

import org.junit.Test
import play.api.i18n.Lang
import play.api.mvc.Request
import org.mockito.Mockito.when
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar

class RestContextTest extends AssertionsForJUnit with MockitoSugar {

  private[this] def makeContext(languagePreferences: Seq[Lang]): RestContext[Unit, Unit] = {
    val mockRequest = mock[Request[Unit]]
    val restContext = new RestContext((), (), mockRequest, null, null, null)
    when(mockRequest.acceptLanguages).thenReturn(languagePreferences)
    restContext
  }

  def test(
      requestLanguages: Seq[Lang],
      availableLanguages: Set[Lang],
      defaultLanguage: Lang,
      expected: Lang): Unit = {
    val restContext = makeContext(requestLanguages)
    assert(restContext.selectLanguage(availableLanguages, defaultLanguage) === expected)
  }

  @Test
  def basicLanguage(): Unit = {
    test(
      requestLanguages = Seq(Lang("en")),
      availableLanguages = Set(Lang("fr"), Lang("en")),
      defaultLanguage = Lang("en"),
      expected = Lang("en"))
  }

  @Test
  def defaultFallback(): Unit = {
    test(
      requestLanguages = Seq(Lang("zh")),
      availableLanguages = Set(Lang("fr"), Lang("en")),
      defaultLanguage = Lang("en"),
      expected = Lang("en"))
  }

  @Test
  def choosePreferred(): Unit = {
    test(
      requestLanguages = Seq(Lang("zh"), Lang("fr"), Lang("en")),
      availableLanguages = Set(Lang("fr"), Lang("en")),
      defaultLanguage = Lang("en"),
      expected = Lang("fr"))
  }
} 
Example 27
Source File: RouterTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.router2

import com.google.inject.Injector
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers._
import play.api.test.FakeRequest

object RouterTest {
  class Resource1
  class Resource2
  class Resource3

  abstract class ResourceRouterBuilder1 extends ResourceRouterBuilder {
    type ResourceType = Resource1
  }
  abstract class ResourceRouterBuilder2 extends ResourceRouterBuilder {
    type ResourceType = Resource2
  }
  abstract class ResourceRouterBuilder3 extends ResourceRouterBuilder {
    type ResourceType = Resource3
  }

  abstract class ResourceRouter1 extends ResourceRouter {
    type ResourceType = Resource1
  }
  abstract class ResourceRouter2 extends ResourceRouter {
    type ResourceType = Resource2
  }
  abstract class ResourceRouter3 extends ResourceRouter {
    type ResourceType = Resource3
  }
}

class RouterTest extends AssertionsForJUnit with MockitoSugar {
  import RouterTest._

  val resourceRouterBuilder1 = mock[ResourceRouterBuilder1]
  val resourceRouterBuilder2 = mock[ResourceRouterBuilder2]
  val resourceRouterBuilder3 = mock[ResourceRouterBuilder3]
  val resourceRouter1 = mock[ResourceRouter1]
  val resourceRouter2 = mock[ResourceRouter2]
  val resourceRouter3 = mock[ResourceRouter3]
  val resources = List(resourceRouterBuilder1, resourceRouterBuilder2, resourceRouterBuilder3)
  val injector = mock[Injector]
  setupStandardMocks()
  val router = new Router(injector, resources)

  private[this] def setupStandardMocks(): Unit = {
    when(resourceRouterBuilder1.build(any())).thenReturn(resourceRouter1)
    when(resourceRouterBuilder2.build(any())).thenReturn(resourceRouter2)
    when(resourceRouterBuilder3.build(any())).thenReturn(resourceRouter3)
  }
  val fakeRequest = FakeRequest("GET", "/api/foo.v1")

  @Test
  def simpleRouting(): Unit = {
    when(resourceRouter1.routeRequest(any(), any())).thenReturn(Some(null))
    val result = router.onRouteRequest(fakeRequest)
    assert(result.isDefined, "Expected result to be defined.")
    verifyZeroInteractions(resourceRouter2, resourceRouter3)
  }

  @Test
  def stopImmediately(): Unit = {
    when(resourceRouter1.routeRequest(any(), any())).thenReturn(None)
    when(resourceRouter2.routeRequest(any(), any())).thenReturn(Some(null))
    val result = router.onRouteRequest(fakeRequest)
    assert(result.isDefined, "Expected result to be defined.")
    verifyZeroInteractions(resourceRouter3)
  }

  @Test
  def handleNoMatchingRequests(): Unit = {
    when(resourceRouter1.routeRequest(any(), any())).thenReturn(None)
    when(resourceRouter2.routeRequest(any(), any())).thenReturn(None)
    when(resourceRouter3.routeRequest(any(), any())).thenReturn(None)
    val result = router.onRouteRequest(fakeRequest)
    assert(result.isEmpty, "Expected result to be empty.")
  }
} 
Example 28
Source File: NaptimePlayRouterTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.router2

import akka.util.ByteString
import com.google.inject.Injector
import org.coursera.naptime.resources.RootResource
import org.coursera.naptime.schema.Handler
import org.coursera.naptime.schema.HandlerKind
import org.coursera.naptime.schema.Parameter
import org.coursera.naptime.schema.Resource
import org.coursera.naptime.schema.ResourceKind
import org.junit.Test
import org.mockito.Mockito.when
import org.mockito.Matchers.any
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar
import play.api.libs.streams.Accumulator
import play.api.mvc.EssentialAction
import play.api.mvc.RequestHeader
import play.api.mvc.RequestTaggingHandler
import play.api.mvc.Result
import play.api.test.FakeRequest

class NaptimePlayRouterTest extends AssertionsForJUnit with MockitoSugar {
  object FakeHandler extends  EssentialAction with RequestTaggingHandler {
    override def tagRequest(request: RequestHeader): RequestHeader = request

    override def apply(v1: RequestHeader): Accumulator[ByteString, Result] = ???
  }

  val resourceSchema = Resource(
    kind = ResourceKind.COLLECTION,
    name = "fakeResource",
    version = Some(1L),
    parentClass = Some(classOf[RootResource].getName),
    keyType = "java.lang.String",
    valueType = "FakeModel",
    mergedType = "FakeResourceModel",
    handlers = List(
      Handler(
        kind = HandlerKind.GET,
        name = "get",
        parameters =
          List(Parameter(name = "id", `type` = "String", attributes = List.empty, default = None)),
        inputBodyType = None,
        customOutputBodyType = None,
        attributes = List.empty)),
    className = "org.coursera.naptime.FakeResource",
    attributes = List.empty)

  val resourceRouter = mock[ResourceRouter]
  val resourceRouterBuilder = mock[ResourceRouterBuilder]
  when(resourceRouterBuilder.build(any())).thenReturn(resourceRouter)
  when(resourceRouterBuilder.schema).thenReturn(resourceSchema)

  val injector = mock[Injector]
  val naptimeRoutes = NaptimeRoutes(injector, Set(resourceRouterBuilder))
  val router = new NaptimePlayRouter(naptimeRoutes)

  @Test
  def simpleRouting(): Unit = {
    when(resourceRouter.routeRequest(any(), any())).thenReturn(Some(FakeHandler))
    val handler = router.handlerFor(FakeRequest())
    assert(handler.isDefined)
  }

  @Test
  def simpleRoutingNothing(): Unit = {
    when(resourceRouter.routeRequest(any(), any())).thenReturn(None)
    val handler = router.handlerFor(FakeRequest())
    assert(handler.isEmpty)
  }

  @Test
  def generateDocumentation(): Unit = {
    val documentation = router.documentation
    assert(1 === documentation.length)
    assert(
      (
        "GET --- GET",
        "/fakeResource.v1/$id",
        "[NAPTIME] org.coursera.naptime.FakeResource.get(id: String)") ===
        documentation.head)
  }
} 
Example 29
Source File: MetricRegistryFactorySpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.metrics

import com.codahale.metrics.MetricRegistry
import com.typesafe.config.Config
import org.mockito.Mockito._
import org.scalatest.Matchers._
import org.scalatest._
import org.scalatest.mockito.MockitoSugar

object MockMetricRegistryFactory extends MetricRegistryFactory with MockitoSugar {
  lazy val mockRegistry = mock[MetricRegistry]

  def metricRegistry(config: Config): MetricRegistry = mockRegistry

}

class MockMetricRegistryFactory extends MetricRegistryFactory {
  def metricRegistry(config: Config): MetricRegistry = MockMetricRegistryFactory.mockRegistry
}

class MetricRegistryFactorySpec extends WordSpecLike with BeforeAndAfter with MockitoSugar {

  private val conf = mock[Config]

  "The MetricRegistry" when {
    "use the DefaultMetricRegistryFactory" should {
      "creating MetricRegistries" in {

        doReturn("com.comcast.money.metrics.DefaultMetricRegistryFactory").when(conf).getString("metrics-registry.class-name")

        val registry = MetricRegistryFactory.metricRegistry(conf)

        registry shouldNot be(null)
      }
    }
  }

  "fall back to the DefaultMetricRegistryFactory" should {
    "when the config is broken" in {

      doReturn(true).when(conf).hasPath("metrics-registry.class-name")
      doReturn("lorem ipsum").when(conf).getString("metrics-registry.class-name")

      intercept[ClassNotFoundException] {
        val registry = MetricRegistryFactory.metricRegistry(conf)
      }
    }
  }

  "use the MockMetricRegistryFactory" should {
    "when configured so" in {

      doReturn(true).when(conf).hasPath("metrics-registry.class-name")
      doReturn("com.comcast.money.core.metrics.MockMetricRegistryFactory").when(conf).getString("metrics-registry.class-name")

      val registry1 = MetricRegistryFactory.metricRegistry(conf)
      val registry2 = MetricRegistryFactory.metricRegistry(conf)

      registry1 should be theSameInstanceAs registry2
    }
  }
} 
Example 30
Source File: CoreSpanFactorySpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core

import com.comcast.money.api.{ Note, SpanHandler, SpanId }
import com.comcast.money.core.handlers.TestData
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }

class CoreSpanFactorySpec extends WordSpec with Matchers with MockitoSugar with TestData {

  val handler = mock[SpanHandler]
  val underTest = new CoreSpanFactory(handler)

  "CoreSpanFactory" should {
    "create a new span" in {
      val result = underTest.newSpan("foo").asInstanceOf[CoreSpan]

      result.info.name shouldBe "foo"
      result.handler shouldBe handler
    }

    "create a new span given an existing span id" in {
      val existingId = new SpanId()
      val result = underTest.newSpan(existingId, "foo").asInstanceOf[CoreSpan]

      result.id shouldBe existingId
    }

    "create a child span whos id descends from an existing span" in {
      val result = underTest.childSpan("child", testSpan)

      val parent = testSpan.info
      val child = result.info

      child.id.traceId shouldBe parent.id.traceId
      child.id.parentId shouldBe parent.id.selfId
      child.id.selfId == parent.id.selfId shouldBe false
    }

    "propagate sticky notes to a child span" in {

      val parentSpan = underTest.newSpan("parent")
      val stickyNote = Note.of("foo", "bar", true)
      val nonStickyNote = Note.of("other", "one", false)
      parentSpan.record(stickyNote)
      parentSpan.record(nonStickyNote)

      val childSpan = underTest.childSpan("child", parentSpan, true)
      val childInfo = childSpan.info

      childInfo.notes should contain value stickyNote
      childInfo.notes shouldNot contain value nonStickyNote
    }

    "create a child span from a well-formed x-moneytrace header" in {
      val parentSpan = underTest.newSpan("parent")

      Formatters.toHttpHeaders(parentSpan.info.id, (headerName, headerValue) => headerName match {
        case Formatters.MoneyTraceHeader => {
          val childSpan = underTest.newSpanFromHeader("child", _ => headerValue)

          childSpan.info.id.traceId shouldBe parentSpan.info.id.traceId
          childSpan.info.id.parentId shouldBe parentSpan.info.id.selfId
          childSpan.info.id.selfId == parentSpan.info.id.selfId shouldBe false
        }
        case _ =>
      })
    }

    "create a root span from a malformed x-moneytrace header" in {
      val parentSpan = underTest.newSpan("parent")
      val traceContextHeader = "mangled header value"
      val childSpan = underTest.newSpanFromHeader("child", headerName => traceContextHeader)

      childSpan.info.id.traceId == parentSpan.info.id.traceId shouldBe false
      childSpan.info.id.parentId == parentSpan.info.id.selfId shouldBe false
      childSpan.info.id.selfId == parentSpan.info.id.selfId shouldBe false
      childSpan.info.id.selfId shouldBe childSpan.info.id.parentId
    }
  }
} 
Example 31
Source File: TraceLoggingSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.logging

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, OneInstancePerTest, WordSpec }
import org.slf4j.Logger

class TraceLoggingSpec extends WordSpec with Matchers with MockitoSugar with OneInstancePerTest {

  val mockLogger = mock[Logger]

  "TraceLogging" should {
    "capture exceptions into a log" in {
      val testTraceLogging = new TraceLogging {
        override lazy val shouldLogExceptions: Boolean = true
        override val logger: Logger = mockLogger
      }

      val t = mock[Throwable]
      testTraceLogging.logException(t)
      verify(mockLogger).error("Tracing exception", t)
    }
    "not capture exceptions if log exceptions is not enabled" in {
      val testTraceLogging = new TraceLogging {
        override lazy val shouldLogExceptions: Boolean = false
        override val logger: Logger = mockLogger
      }
      val t = mock[Throwable]
      testTraceLogging.logException(t)
      verifyZeroInteractions(mockLogger)
    }
  }
} 
Example 32
Source File: CoreSpanSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core

import com.comcast.money.api.{ SpanInfo, SpanHandler, SpanId }
import com.comcast.money.core.handlers.TestData
import org.mockito.ArgumentCaptor
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }

class CoreSpanSpec extends WordSpec with Matchers with TestData with MockitoSugar {

  "CoreSpan" should {
    "set the startTimeMillis and startTimeMicros when started" in {
      val underTest = CoreSpan(new SpanId(), "test", null)
      underTest.start()

      val state = underTest.info

      state.startTimeMicros.toInt should not be 0
      state.startTimeMillis.toInt should not be 0
    }

    "record a timer" in {
      val underTest = CoreSpan(new SpanId(), "test", null)

      underTest.startTimer("foo")
      underTest.stopTimer("foo")

      underTest.info.notes should contain key "foo"
    }

    "record a note" in {
      val underTest = CoreSpan(new SpanId(), "test", null)

      underTest.record(testLongNote)

      underTest.info.notes should contain value testLongNote
    }

    "set the endTimeMillis and endTimeMicros when stopped" in {
      val handler = mock[SpanHandler]
      val underTest = CoreSpan(new SpanId(), "test", handler)

      underTest.stop(true)

      val state = underTest.info

      state.endTimeMicros.toInt should not be 0
      state.endTimeMillis.toInt should not be 0
    }

    "invoke the span handler when stopped" in {
      val handler = mock[SpanHandler]
      val handleCaptor = ArgumentCaptor.forClass(classOf[SpanInfo])
      val underTest = CoreSpan(new SpanId(), "test", handler)

      underTest.start()
      underTest.record(testLongNote)
      underTest.stop(true)

      verify(handler).handle(handleCaptor.capture())

      val handledInfo = handleCaptor.getValue

      handledInfo.id shouldBe underTest.id
      handledInfo.startTimeMicros.toInt should not be 0
      handledInfo.startTimeMillis.toInt should not be 0
      handledInfo.endTimeMicros.toInt should not be 0
      handledInfo.endTimeMillis.toInt should not be 0
      handledInfo.notes should contain value testLongNote
    }
  }
} 
Example 33
Source File: SpanLocalSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.internal

import com.comcast.money.api.SpanId
import com.comcast.money.core.handlers.TestData
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ OneInstancePerTest, BeforeAndAfterEach, Matchers, WordSpec }
import org.slf4j.MDC

class SpanLocalSpec extends WordSpec
  with Matchers with OneInstancePerTest with BeforeAndAfterEach with MockitoSugar with TestData {

  override def afterEach() = {
    SpanLocal.clear()
  }

  "SpanLocal" when {
    "an item exists in span local" should {
      "return the span local value" in {
        SpanLocal.push(testSpan)
        SpanLocal.current shouldEqual Some(testSpan)
      }
      "clear the stored value" in {
        SpanLocal.push(testSpan)

        SpanLocal.clear()
        SpanLocal.current shouldEqual None
      }
      "do nothing if trying to push a null value" in {
        SpanLocal.push(testSpan)
        SpanLocal.push(null)
        SpanLocal.current shouldEqual Some(testSpan)
      }
      "add to the existing call stack" in {
        val nested = testSpan.copy(new SpanId())

        SpanLocal.push(testSpan)
        SpanLocal.push(nested)
        SpanLocal.current shouldEqual Some(nested)
      }
      "pop the last added item from the call stack" in {
        val nested = testSpan.copy(new SpanId())
        SpanLocal.push(testSpan)
        SpanLocal.push(nested)

        val popped = SpanLocal.pop()
        popped shouldEqual Some(nested)
        SpanLocal.current shouldEqual Some(testSpan)
      }
      "set the MDC value on push" in {
        SpanLocal.push(testSpan)

        MDC.get("moneyTrace") shouldEqual MDCSupport.format(testSpan.id)
        MDC.get("spanName") shouldEqual testSpan.name
      }
      "remove the MDC value on pop" in {
        SpanLocal.push(testSpan)
        SpanLocal.pop()

        MDC.get("moneyTrace") shouldBe null
        MDC.get("spanName") shouldBe null
      }
      "reset the MDC value on pop" in {
        SpanLocal.push(testSpan)
        SpanLocal.push(childSpan)

        MDC.get("moneyTrace") shouldEqual MDCSupport.format(childSpan.id)
        MDC.get("spanName") shouldEqual childSpan.name

        SpanLocal.pop()

        MDC.get("moneyTrace") shouldEqual MDCSupport.format(testSpan.id)
        MDC.get("spanName") shouldEqual testSpan.name
      }
      "remove the MDC value on clear" in {
        SpanLocal.push(testSpan)

        MDC.get("moneyTrace") shouldEqual MDCSupport.format(testSpan.id)
        MDC.get("spanName") shouldEqual testSpan.name
        SpanLocal.clear()

        MDC.get("moneyTrace") shouldBe null
        MDC.get("spanName") shouldBe null
      }
    }
  }
} 
Example 34
Source File: TraceFriendlyThreadPoolExecutorSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.concurrent

import java.util.concurrent.{ Callable, ExecutorService }

import com.comcast.money.api.SpanId
import com.comcast.money.core.SpecHelpers
import com.comcast.money.core.internal.SpanLocal
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, OneInstancePerTest, WordSpecLike }
import org.slf4j.MDC

class TraceFriendlyThreadPoolExecutorSpec
  extends WordSpecLike
  with MockitoSugar with Matchers with ConcurrentSupport with OneInstancePerTest with SpecHelpers {

  val executor: ExecutorService = TraceFriendlyThreadPoolExecutor.newCachedThreadPool

  "TraceFriendlyThreadPoolExecutor cachedThreadPool" should {
    "propagate the current span local value" in {
      val traceId = new SpanId("1", 2L, 3L)
      SpanLocal.push(testSpan(traceId))

      val future = executor.submit(testCallable)

      future.get shouldEqual Some(traceId)
      SpanLocal.clear()
    }
    "propagate no span value if none is present" in {
      SpanLocal.clear()

      val future = executor.submit(testCallable)

      future.get shouldEqual None
      SpanLocal.current shouldEqual None
    }
    "propagate only the current span id value" in {
      val traceId1 = new SpanId()
      val traceId2 = new SpanId()
      SpanLocal.push(testSpan(traceId1))
      SpanLocal.push(testSpan(traceId2))

      val future = executor.submit(testCallable)
      future.get shouldEqual Some(traceId2)
    }
    "propagate MDC" in {
      val traceId = new SpanId("1", 2L, 3L)
      SpanLocal.push(testSpan(traceId))
      MDC.put("foo", "bar")

      val mdcCallable = new Callable[String] {
        override def call(): String = MDC.get("foo")
      }

      val future = executor.submit(mdcCallable)

      future.get shouldEqual "bar"
      SpanLocal.clear()
    }
  }
  "TraceFriendlyThreadPoolExecutor fixedThreadPool" should {
    val threadPool: TraceFriendlyThreadPoolExecutor = TraceFriendlyThreadPoolExecutor.newFixedThreadPool(1)
      .asInstanceOf[TraceFriendlyThreadPoolExecutor]

    "created the pool with the specified number of threads" in {
      threadPool.getCorePoolSize shouldEqual 1
    }
  }
} 
Example 35
Source File: TraceFriendlyExecutionContextExecutorSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.concurrent

import com.comcast.money.api.SpanId
import com.comcast.money.core.SpecHelpers
import com.comcast.money.core.internal.SpanLocal
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ BeforeAndAfterEach, Matchers, OneInstancePerTest, WordSpec }
import org.slf4j.MDC

import scala.concurrent.duration._
import scala.concurrent.{ Await, ExecutionContext, Future }

class TraceFriendlyExecutionContextExecutorSpec extends WordSpec
  with Matchers
  with MockitoSugar
  with OneInstancePerTest
  with ConcurrentSupport
  with SpecHelpers
  with BeforeAndAfterEach {

  import com.comcast.money.core.concurrent.TraceFriendlyExecutionContextExecutor.Implicits.global

  override def beforeEach() = {
    SpanLocal.clear()
    MDC.clear()
  }

  // brings in the implicit executor

  "TraceFriendlyExecutionContext" should {
    "propagate the current trace local value" in {
      val originalSpanId = new SpanId("1", 2L, 3L)
      val originalSpan = testSpan(originalSpanId)
      SpanLocal.push(originalSpan)

      val future = Future {
        SpanLocal.current.get.info.id
      }

      val futureResult = Await.result(future, 100 millis)
      futureResult shouldEqual originalSpanId
    }
    "propagate no span value if none is present" in {
      SpanLocal.clear()

      val future = Future {
        SpanLocal.current
      }

      val futureResult = Await.result(future, 100 millis)
      futureResult shouldEqual None
    }
    "propagate only the latest span id value" in {
      val spanId1 = new SpanId()
      val spanId2 = new SpanId()
      SpanLocal.push(testSpan(spanId1))
      SpanLocal.push(testSpan(spanId2))

      val future = Future {
        SpanLocal.current.get.info.id
      }

      val futureResult = Await.result(future, 100 millis)
      futureResult shouldEqual spanId2
    }
    "delegate reportFailure to the wrapped executor" in {
      val mockExecutionContext = mock[ExecutionContext]
      val traceFriendly = TraceFriendlyExecutionContextExecutor(mockExecutionContext)
      val failure = new IllegalArgumentException()

      traceFriendly.reportFailure(failure)
      verify(mockExecutionContext).reportFailure(failure)
    }
    "propogate MDC data" in {
      MDC.put("FINGERPRINT", "print")
      val future = Future {
        MDC.get("FINGERPRINT")
      }
      MDC.get("FINGERPRINT") shouldEqual "print"
      Await.result(future, 100 millis) shouldEqual "print"
    }

    "Child MDC should not escape to parent " in {
      val future = Future {
        MDC.put("FINGERPRINT", "print")
        MDC.get("FINGERPRINT")
      }
      MDC.get("FINGERPRINT") shouldBe null
      Await.result(future, 100 millis) shouldEqual "print"
    }
  }
} 
Example 36
Source File: MetricsHandlerSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.handlers

import com.codahale.metrics.{ Meter, Histogram, MetricRegistry }
import com.typesafe.config.Config
import org.mockito.Mockito._
import org.mockito.Matchers._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ OneInstancePerTest, Matchers, WordSpec }

class MetricsHandlerSpec extends WordSpec with Matchers with MockitoSugar with TestData with OneInstancePerTest {

  val conf = mock[Config]
  doReturn(true).when(conf).hasPath("metrics-registry.class-name")
  doReturn("com.comcast.money.core.metrics.MockMetricRegistryFactory").when(conf).getString("metrics-registry.class-name")

  "MetricsSpanHandler" should {
    "configure the metrics registry" in {
      val underTest = new MetricsSpanHandler()
      underTest.configure(conf)

      underTest.metricRegistry shouldBe a[MetricRegistry]
    }

    "save latency metric" in {
      val underTest = new MetricsSpanHandler()
      underTest.configure(conf)

      val latencyMetric = mock[Histogram]
      val errorMetric = mock[Meter]
      doReturn(latencyMetric).when(underTest.metricRegistry).histogram(anyString())
      doReturn(errorMetric).when(underTest.metricRegistry).meter(anyString())

      underTest.handle(testSpanInfo)

      verify(latencyMetric).update(testSpanInfo.durationMicros)
      verifyZeroInteractions(errorMetric)
    }

    "update the error metric" in {
      val underTest = new MetricsSpanHandler()
      underTest.configure(conf)

      val latencyMetric = mock[Histogram]
      val errorMetric = mock[Meter]
      doReturn(latencyMetric).when(underTest.metricRegistry).histogram(anyString())
      doReturn(errorMetric).when(underTest.metricRegistry).meter(anyString())

      underTest.handle(testSpanInfo.copy(success = false))

      verify(latencyMetric).update(testSpanInfo.durationMicros)
      verify(errorMetric).mark()
    }
  }
} 
Example 37
Source File: HandlerChainSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.handlers

import com.comcast.money.api.SpanHandler
import com.typesafe.config.ConfigFactory
import org.mockito.Mockito
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }

class HandlerChainSpec extends WordSpec with Matchers with MockitoSugar with TestData {

  "HandlerChain" should {

    "invoke all handlers in the chain in order" in {
      val handler1 = mock[SpanHandler]
      val handler2 = mock[SpanHandler]
      val handler3 = mock[SpanHandler]

      val ordered = Mockito.inOrder(handler1, handler2, handler3)

      val underTest = HandlerChain(Seq(handler1, handler2, handler3))

      underTest.handle(testSpanInfo)

      ordered.verify(handler1).handle(testSpanInfo)
      ordered.verify(handler2).handle(testSpanInfo)
      ordered.verify(handler3).handle(testSpanInfo)
    }

    "continues invocation of chain if one of the handlers throws an exception" in {

      val handler1 = mock[SpanHandler]
      val handler2 = mock[SpanHandler]
      val handler3 = mock[SpanHandler]

      doThrow(classOf[RuntimeException]).when(handler1).handle(testSpanInfo)
      val ordered = Mockito.inOrder(handler1, handler2, handler3)

      val underTest = HandlerChain(Seq(handler1, handler2, handler3))

      underTest.handle(testSpanInfo)

      ordered.verify(handler1).handle(testSpanInfo)
      ordered.verify(handler2).handle(testSpanInfo)
      ordered.verify(handler3).handle(testSpanInfo)
    }

    "create a sequence of handlers" in {
      val config = ConfigFactory.parseString(
        """
          |{
          | async = false
          | handlers = [
          |   {
          |     class = "com.comcast.money.core.handlers.ConfiguredHandler"
          |   },
          |   {
          |     class = "com.comcast.money.core.handlers.ConfiguredHandler"
          |   },
          |   {
          |     class = "com.comcast.money.core.handlers.NonConfiguredHandler"
          |   }
          | ]
          |}
        """.stripMargin)

      val result = HandlerChain(config)

      result shouldBe a[HandlerChain]
      result.asInstanceOf[HandlerChain].handlers should have size 3
    }

    "wrap the handler chain in an async handler if async is set to true" in {
      val config = ConfigFactory.parseString(
        """
          |{
          | async = true
          | handlers = [
          |   {
          |     class = "com.comcast.money.core.handlers.ConfiguredHandler"
          |   },
          |   {
          |     class = "com.comcast.money.core.handlers.ConfiguredHandler"
          |   },
          |   {
          |     class = "com.comcast.money.core.handlers.NonConfiguredHandler"
          |   }
          | ]
          |}
        """.stripMargin)

      val result = HandlerChain(config)

      result shouldBe an[AsyncSpanHandler]
      result.asInstanceOf[AsyncSpanHandler]
        .wrapped.asInstanceOf[HandlerChain]
        .handlers should have size 3
    }
  }
} 
Example 38
Source File: AsyncSpanHandlerSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.handlers

import com.comcast.money.api.{ SpanHandler, SpanInfo }
import com.comcast.money.core.SpecHelpers
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }

class AsyncSpanHandlerSpec extends WordSpec with Matchers with MockitoSugar with TestData with SpecHelpers {

  class Wrapped extends SpanHandler {
    var called = false
    override def handle(span: SpanInfo): Unit = called = true
  }

  "AsyncSpanHandler" should {
    "asynchronously invoke the span handler" in {
      val spanHandler = new Wrapped()
      val underTest = new AsyncSpanHandler(scala.concurrent.ExecutionContext.global, spanHandler)

      underTest.handle(testSpanInfo)

      awaitCond(spanHandler.called)
    }
  }
} 
Example 39
Source File: DirectExecutionContextSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.core.async

import com.comcast.money.core.SpecHelpers
import com.comcast.money.core.concurrent.ConcurrentSupport
import org.scalatest.{ Matchers, OneInstancePerTest, WordSpecLike }
import org.scalatest.mockito.MockitoSugar

class DirectExecutionContextSpec
  extends WordSpecLike
  with MockitoSugar with Matchers with ConcurrentSupport with OneInstancePerTest with SpecHelpers {

  val underTest = new DirectExecutionContext()

  "DirectExecutionContext" should {
    "execute the Runnable on the current thread" in {
      val currentThreadId = Thread.currentThread().getId
      var callbackThreadId: Long = 0

      underTest.execute(new Runnable {
        override def run(): Unit = {
          callbackThreadId = Thread.currentThread().getId
        }
      })

      callbackThreadId shouldEqual currentThreadId
    }
  }
} 
Example 40
Source File: TracedMethodAdvisorSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.spring

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }
import org.springframework.aop.support.StaticMethodMatcherPointcut

class TracedMethodAdvisorSpec extends WordSpec with Matchers with MockitoSugar {

  val springTracer = mock[SpringTracer]
  val interceptor = new TracedMethodInterceptor(springTracer)
  val advisor = new TracedMethodAdvisor(interceptor)

  "Trace Advisor" should {
    "bump up code coverage" in {
      advisor.getPointcut shouldBe a[StaticMethodMatcherPointcut]
      advisor.getAdvice shouldBe interceptor
    }
  }
} 
Example 41
Source File: KafkaSpanHandlerSpec.scala    From money   with Apache License 2.0 5 votes vote down vote up
package com.comcast.money.kafka

import com.comcast.money.api.Note
import com.comcast.money.{ api, core }
import com.typesafe.config.{ Config, ConfigFactory }
import kafka.message.{ CompressionCodec, GZIPCompressionCodec }
import kafka.producer.{ KeyedMessage, Producer }
import org.mockito.ArgumentCaptor
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec }

import scala.collection.JavaConverters._

trait MockProducerMaker extends ProducerMaker {

  val mockProducer = mock(classOf[Producer[Array[Byte], Array[Byte]]])

  def makeProducer(conf: Config): Producer[Array[Byte], Array[Byte]] = mockProducer
}

class TestKafkaSpanHandler extends KafkaSpanHandler {

  var producerWasMade = false
  val mockProducer = mock(classOf[Producer[Array[Byte], Array[Byte]]])

  override def makeProducer(conf: Config): Producer[Array[Byte], Array[Byte]] = {
    producerWasMade = true
    mockProducer
  }
}

class KafkaSpanHandlerSpec extends WordSpec
  with Matchers
  with MockitoSugar
  with BeforeAndAfterAll {

  trait KafkaFixture {
    val testConfig = mock[Config]
    when(testConfig.getString("topic")).thenReturn("test-topic")

    val underTest = new TestKafkaSpanHandler()
    underTest.configure(testConfig)

    val testProducer = underTest.mockProducer
    val sampleData = core.CoreSpanInfo(
      id = new api.SpanId("foo", 1L),
      name = "key",
      appName = "app",
      host = "host",
      startTimeMillis = 1L,
      success = true,
      durationMicros = 35L,
      notes = Map[String, Note[_]]("what" -> api.Note.of("what", 1L), "when" -> api.Note.of("when", 2L), "bob" -> api.Note.of("bob", "craig")).asJava)
  }

  "A KafkaEmitter" should {
    "make a producer in configure" in new KafkaFixture {
      underTest.producerWasMade shouldBe true
    }
    "send a message to the producer for a span" in new KafkaFixture {
      underTest.handle(sampleData)

      val captor = ArgumentCaptor.forClass(classOf[KeyedMessage[Array[Byte], Array[Byte]]])
      verify(testProducer).send(captor.capture())
    }
  }

  "A ConfigDrivenProducerMaker" should {
    "set the properties from the config" in {
      val config = ConfigFactory.parseString(
        """
          | topic = "money"
          | compression.codec = "1"
          | producer.type = "async"
          | batch.num.messages = "1"
          | message.send.max.retries = "3"
          | request.required.acks = "0"
          | metadata.broker.list = "localhost:9092"
        """.stripMargin)
      val testHandler = new KafkaSpanHandler()
      testHandler.configure(config)

      val producerConfig = testHandler.producer.config
      producerConfig.brokerList shouldBe "localhost:9092"
      producerConfig.compressionCodec shouldBe GZIPCompressionCodec
      producerConfig.producerType shouldBe "async"
      producerConfig.batchNumMessages shouldBe 1
      producerConfig.messageSendMaxRetries shouldBe 3
      producerConfig.requestRequiredAcks shouldBe 0
    }
  }
} 
Example 42
Source File: TestEnvironment.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler

import com.ivan.nikolov.scheduler.actors.{ActorFactory, ActorFactoryComponent}
import com.ivan.nikolov.scheduler.config.app.AppConfigComponent
import com.ivan.nikolov.scheduler.dao._
import com.ivan.nikolov.scheduler.io.IOServiceComponent
import com.ivan.nikolov.scheduler.services.JobConfigReaderServiceComponent
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

trait TestEnvironment 
  extends AppConfigComponent
  with IOServiceComponent
  with JobConfigReaderServiceComponent
  with DatabaseServiceComponent
  with MigrationComponent
  with DaoServiceComponent
  with ActorFactoryComponent
  with MockitoSugar {

  // use the test configuration file.
  override val appConfigService: AppConfigService = spy(new AppConfigService)
  // override the path here to use the test resources.
  when(appConfigService.configPath).thenReturn(this.getClass.getResource("/").getPath)
  
  override val ioService: IOService = mock[IOService]
  override val jobConfigReaderService: JobConfigReaderService = mock[JobConfigReaderService]
  override val databaseService: DatabaseService = mock[DatabaseService]
  override val migrationService: MigrationService = mock[MigrationService]
  override val daoService: DaoService = mock[DaoService]
  override val actorFactory: ActorFactory = mock[ActorFactory]
} 
Example 43
Source File: TestEnvironment.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler

import com.ivan.nikolov.scheduler.actors.{ActorFactory, ActorFactoryComponent}
import com.ivan.nikolov.scheduler.config.app.AppConfigComponent
import com.ivan.nikolov.scheduler.dao._
import com.ivan.nikolov.scheduler.io.IOServiceComponent
import com.ivan.nikolov.scheduler.services.JobConfigReaderServiceComponent
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

trait TestEnvironment 
  extends AppConfigComponent
  with IOServiceComponent
  with JobConfigReaderServiceComponent
  with DatabaseServiceComponent
  with MigrationComponent
  with DaoServiceComponent
  with ActorFactoryComponent
  with MockitoSugar {

  // use the test configuration file.
  override val appConfigService: AppConfigService = spy(new AppConfigService)
  // override the path here to use the test resources.
  when(appConfigService.configPath).thenReturn(this.getClass.getResource("/").getPath)
  
  override val ioService: IOService = mock[IOService]
  override val jobConfigReaderService: JobConfigReaderService = mock[JobConfigReaderService]
  override val databaseService: DatabaseService = mock[DatabaseService]
  override val migrationService: MigrationService = mock[MigrationService]
  override val daoService: DaoService = mock[DaoService]
  override val actorFactory: ActorFactory = mock[ActorFactory]
} 
Example 44
Source File: UserComponentTest.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.cake

import com.ivan.nikolov.cake.model.Person
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}

class UserComponentTest extends FlatSpec with Matchers with MockitoSugar with TestEnvironment {
  val className = "A"
  val emptyClassName = "B"
  val people = List(
    Person(1, "a", 10),
    Person(2, "b", 15),
    Person(3, "c", 20)
  )
  
  override val userService = new UserService
  
  when(dao.getPeopleInClass(className)).thenReturn(people)
  when(dao.getPeopleInClass(emptyClassName)).thenReturn(List())
  
  "getAverageAgeOfUsersInClass" should "properly calculate the average of all ages." in {
    userService.getAverageAgeOfUsersInClass(className) should equal(15.0)
  }
  
  it should "properly handle an empty result." in {
    userService.getAverageAgeOfUsersInClass(emptyClassName) should equal(0.0)
  }
} 
Example 45
Source File: TestEnvironment.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.cake

import org.scalatest.mockito.MockitoSugar

trait TestEnvironment 
  extends UserComponent
  with DaoComponent
  with DatabaseComponent
  with MigrationComponent
  with MockitoSugar {

  override val dao: Dao = mock[Dao]
  override val databaseService: DatabaseService = mock[DatabaseService]
  override val migrationService: MigrationService = mock[MigrationService]
  override val userService: UserService = mock[UserService]
} 
Example 46
Source File: UserComponentTest.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.cake

import com.ivan.nikolov.cake.model.Person
import org.junit.runner.RunWith
import org.mockito.Mockito._
import org.scalatest.junit.JUnitRunner
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}

@RunWith(classOf[JUnitRunner])
class UserComponentTest extends FlatSpec with Matchers with MockitoSugar with TestEnvironment {
  val className = "A"
  val emptyClassName = "B"
  val people = List(
    Person(1, "a", 10),
    Person(2, "b", 15),
    Person(3, "c", 20)
  )
  
  override val userService = new UserService
  
  when(dao.getPeopleInClass(className)).thenReturn(people)
  when(dao.getPeopleInClass(emptyClassName)).thenReturn(List())
  
  "getAverageAgeOfUsersInClass" should "properly calculate the average of all ages." in {
    userService.getAverageAgeOfUsersInClass(className) should equal(15.0)
  }
  
  it should "properly handle an empty result." in {
    userService.getAverageAgeOfUsersInClass(emptyClassName) should equal(0.0)
  }
} 
Example 47
Source File: TestEnvironment.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.cake

import org.scalatest.mockito.MockitoSugar

trait TestEnvironment 
  extends UserComponent
  with DaoComponent
  with DatabaseComponent
  with MigrationComponent
  with MockitoSugar {

  override val dao: Dao = mock[Dao]
  override val databaseService: DatabaseService = mock[DatabaseService]
  override val migrationService: MigrationService = mock[MigrationService]
  override val userService: UserService = mock[UserService]
} 
Example 48
Source File: InstancePersistenceSpec.scala    From sbt-docker-compose   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
import com.tapad.docker.{ RunningInstanceInfo, DockerComposePluginLocal }
import com.tapad.docker.DockerComposeKeys._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ BeforeAndAfter, FunSuite, OneInstancePerTest }

class InstancePersistenceSpec extends FunSuite with BeforeAndAfter with OneInstancePerTest with MockitoSugar {

  test("Validate that only running instances from this sbt session are returned") {
    val instanceMock = spy(new DockerComposePluginLocal)

    val runningInstanceMatch = RunningInstanceInfo("instanceNameMatch", "matchingservice", "composePath", List.empty)
    val runningInstanceNoMatch = RunningInstanceInfo("instanceNameNoMatch", "nomatchingservice", "composePath", List.empty)

    doReturn("matchingservice").when(instanceMock).getSetting(composeServiceName)(null)
    doReturn(Option(List(runningInstanceMatch, runningInstanceNoMatch))).when(instanceMock).getAttribute(runningInstances)(null)

    val instanceIds = instanceMock.getServiceRunningInstanceIds(null)

    assert(instanceIds.size == 1)
    assert(instanceIds.contains("instanceNameMatch"))
  }

  test("Validate that only matching instance ids are returned") {
    val instanceMock = spy(new DockerComposePluginLocal)

    val runningInstanceMatch = RunningInstanceInfo("instanceNameMatch", "matchingservice", "composePath", List.empty)
    val runningInstanceNoMatch = RunningInstanceInfo("instanceNameNoMatch", "nomatchingservice", "composePath", List.empty)

    doReturn("matchingservice").when(instanceMock).getSetting(composeServiceName)(null)
    doReturn(Option(List(runningInstanceMatch, runningInstanceNoMatch))).when(instanceMock).getAttribute(runningInstances)(null)

    val instance = instanceMock.getMatchingRunningInstance(null, Seq("instanceNameMatch"))

    assert(instance.isDefined)
    assert(instance.get.instanceName == "instanceNameMatch")
  }
} 
Example 49
Source File: CPConfigurationTest.scala    From toketi-iothubreact   with MIT License 5 votes vote down vote up
// Copyright (c) Microsoft. All rights reserved.

package com.microsoft.azure.iot.iothubreact.checkpointing

import com.microsoft.azure.iot.iothubreact.checkpointing.backends.cassandra.lib.Auth
import com.typesafe.config.{Config, ConfigException}
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FeatureSpec, GivenWhenThen}

class CPConfigurationTest extends FeatureSpec with GivenWhenThen with MockitoSugar {

  info("As a configured instance")
  info("I want logic around returned values to be consistent with application expectations")

  val confPath = "iothub-react.checkpointing."
  Feature("Configuration Cassandra authorization") {

    Scenario("Only one of username or password is supplied") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenReturn("username")
      when(cfg.getString(confPath + "storage.cassandra.password")).thenThrow(new ConfigException.Missing("path"))
      assert(new CPConfiguration(cfg).cassandraAuth == None)

      cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenThrow(new ConfigException.Missing("path"))
      when(cfg.getString(confPath + "storage.cassandra.password")).thenReturn("password")
      assert(new CPConfiguration(cfg).cassandraAuth == None)
    }

    Scenario("Both username and password are supplied") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenReturn("username")
      when(cfg.getString(confPath + "storage.cassandra.password")).thenReturn("password")
      assert(new CPConfiguration(cfg).cassandraAuth == Some(Auth("username", "password")))
    }
  }

  Feature("Storage namespace") {

    Scenario("Cassandra has a special namespace value") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.namespace")).thenReturn("")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("anythingbutcassandra")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub-react-checkpoints")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("AZUREBLOB")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub-react-checkpoints")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("CASSANDRA")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub_react_checkpoints")
    }
  }
} 
Example 50
Source File: ConfigurationTest.scala    From toketi-iothubreact   with MIT License 5 votes vote down vote up
package com.microsoft.azure.iot.iothubreact.checkpointing

import com.microsoft.azure.iot.iothubreact.checkpointing.backends.cassandra.lib.Auth
import com.typesafe.config.{Config, ConfigException}
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FeatureSpec, GivenWhenThen}

class ConfigurationTest extends FeatureSpec with GivenWhenThen with MockitoSugar {

  info("As a configured instance")
  info("I want logic around returned values to be consistent with application expectations")

  val confPath = "iothub-react.checkpointing."
  Feature("Configuration Cassandra authorization") {

    Scenario("Only one of username or password is supplied") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenReturn("username")
      when(cfg.getString(confPath + "storage.cassandra.password")).thenThrow(new ConfigException.Missing("path"))
      assert(new CPConfiguration(cfg).cassandraAuth == None)

      cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenThrow(new ConfigException.Missing("path"))
      when(cfg.getString(confPath + "storage.cassandra.password")).thenReturn("password")
      assert(new CPConfiguration(cfg).cassandraAuth == None)
    }

    Scenario("Both username and password are supplied") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.cassandra.username")).thenReturn("username")
      when(cfg.getString(confPath + "storage.cassandra.password")).thenReturn("password")
      assert(new CPConfiguration(cfg).cassandraAuth == Some(Auth("username", "password")))
    }
  }

  Feature("Storage namespace") {

    Scenario("Cassandra has a special namespace value") {
      var cfg = mock[Config]
      when(cfg.getString(confPath + "storage.namespace")).thenReturn("")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("anythingbutcassandra")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub-react-checkpoints")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("AZUREBLOB")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub-react-checkpoints")

      when(cfg.getString(confPath + "storage.backendType")).thenReturn("CASSANDRA")
      assert(new CPConfiguration(cfg).storageNamespace == "iothub_react_checkpoints")
    }
  }
} 
Example 51
Source File: DataServiceTest.scala    From kafka-jdbc-connector   with Apache License 2.0 5 votes vote down vote up
package com.agoda.kafka.connector.jdbc.services

import java.sql.{Connection, PreparedStatement, ResultSet, ResultSetMetaData}

import com.agoda.kafka.connector.jdbc.utils.DataConverter
import org.apache.kafka.connect.data.Schema
import org.apache.kafka.connect.source.SourceRecord
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito._
import org.scalatest.{Matchers, WordSpec}

import scala.concurrent.duration._
import scala.util.Success

class DataServiceTest extends WordSpec with Matchers with MockitoSugar {

  "Data Service" should {

    val spName = "stored-procedure"
    val connection = mock[Connection]
    val converter = mock[DataConverter]
    val sourceRecord1 = mock[SourceRecord]
    val sourceRecord2 = mock[SourceRecord]
    val resultSet = mock[ResultSet]
    val resultSetMetadata = mock[ResultSetMetaData]
    val preparedStatement = mock[PreparedStatement]
    val schema = mock[Schema]

    val dataService = new DataService {

      override def storedProcedureName: String = spName

      override protected def createPreparedStatement(connection: Connection) = Success(preparedStatement)

      override protected def extractRecords(resultSet: ResultSet, schema: Schema) = Success(Seq(sourceRecord1, sourceRecord2))

      override def dataConverter: DataConverter = converter
    }

    "get records" in {
      doNothing().when(preparedStatement).setQueryTimeout(1)
      when(preparedStatement.executeQuery).thenReturn(resultSet)
      when(resultSet.getMetaData).thenReturn(resultSetMetadata)
      when(converter.convertSchema(spName, resultSetMetadata)).thenReturn(Success(schema))

      dataService.getRecords(connection, 1.second) shouldBe Success(Seq(sourceRecord1, sourceRecord2))

      verify(preparedStatement).setQueryTimeout(1)
      verify(preparedStatement).executeQuery
      verify(resultSet).getMetaData
      verify(converter).convertSchema(spName, resultSetMetadata)
    }
  }
} 
Example 52
Source File: CachingES5TranspilerTest.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.transpile

import com.programmaticallyspeaking.ncd.testing.UnitTest
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito._
import org.mockito.ArgumentMatchers._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer

import scala.collection.mutable.ListBuffer

class CachingES5TranspilerTest extends UnitTest with MockitoSugar {

  val transpilerCalledWith = ListBuffer[String]()

  def fakeTranspiler: ES5Transpiler = {
    transpilerCalledWith.clear()
    val t = mock[ES5Transpiler]
    when(t.transpile(any[String])).thenAnswer(new Answer[String] {
      override def answer(invocation: InvocationOnMock): String = {
        val input = invocation.getArgument[String](0)
        transpilerCalledWith += input
        "transpiled:" + input
      }
    })
    t
  }

  def createSut = new CachingES5Transpiler(fakeTranspiler)

  "A caching ES5 transpiler" - {
    "delegates to the real one" in {
      val caching = createSut
      caching.transpile("input") should be ("transpiled:input")
    }

    "delegates for each unknown input" in {
      val caching = createSut
      caching.transpile("input1")
      caching.transpile("input2")
      transpilerCalledWith should be (Seq("input1", "input2"))
    }

    "caches and reuses ouput" in {
      val caching = createSut
      caching.transpile("input1")
      caching.transpile("input1")
      transpilerCalledWith should be (Seq("input1"))
    }
  }

} 
Example 53
Source File: NashornDebuggerHostTest.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.nashorn

import com.programmaticallyspeaking.ncd.testing.UnitTest
import com.sun.jdi.ThreadReference
import org.scalatest.mockito.MockitoSugar
import org.scalatest.prop.TableDrivenPropertyChecks
import org.mockito.Mockito._

class NashornDebuggerHostTest extends UnitTest with TableDrivenPropertyChecks with MockitoSugar {

  val threadNames = Table(
    ("name", "isInfra"),
    ("Finalizer", true),
    ("Reference Handler", true),
    ("Signal Dispatcher", true),
    ("Attach Listener", true),
    ("attach listener", true),
    ("main", false)
  )

  def threadReferenceWithName(name: String) = {
    val t = mock[ThreadReference]
    when(t.name()).thenReturn(name)
    t
  }

  "isInfrastructureThread" - {
    forAll(threadNames) { (name, isInfra) =>
      val verb = if (isInfra) "be" else "not be"
      s"should consider a thread named '$name' to $verb an infrastructure thread" in {
        NashornDebuggerHost.isInfrastructureThread(threadReferenceWithName(name)) should be(isInfra)
      }
    }
  }
} 
Example 54
Source File: TheFlashTweetsConsumerSpec.scala    From KafkaPlayground   with GNU General Public License v3.0 5 votes vote down vote up
package com.github.pedrovgs.kafkaplayground.flash

import com.github.pedrovgs.kafkaplayground.flash.elasticsearch.ElasticClient
import com.github.pedrovgs.kafkaplayground.utils.EmbeddedKafkaServer
import org.mockito.Mockito.verify
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}

object TheFlashTweetsConsumerSpec {
  private val anyTopic        = "topic"
  private val anyContent      = "content"
  private val anyOtherContent = "anyOtherContent"
}

class TheFlashTweetsConsumerSpec
    extends FlatSpec
    with Matchers
    with EmbeddedKafkaServer
    with ScalaFutures
    with MockitoSugar
    with BeforeAndAfterEach {

  import TheFlashTweetsConsumerSpec._

  private var elasticClient: ElasticClient = _

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    elasticClient = mock[ElasticClient]
  }

  "TheFlashTweetsConsumer" should "create a new document for the configured index using the messages polled from the kafka cluster" in {
    produceMessage(anyTopic, anyContent)

    givenAElasticConsumer().poll()

    val expectedId = s"topic_0_0"
    verify(elasticClient).insertOrUpdate(expectedId, anyContent)
  }

  it should "send more than a message to elasticsearch" in {
    produceMessage(anyTopic, anyContent)
    produceMessage(anyTopic, anyOtherContent)

    givenAElasticConsumer().poll()

    verify(elasticClient).insertOrUpdate("topic_0_0", anyContent)
    verify(elasticClient).insertOrUpdate("topic_0_1", anyOtherContent)
  }

  private def givenAElasticConsumer() =
    new TheFlashTweetsConsumer(kafkaServerAddress(), anyTopic, elasticClient)

} 
Example 55
Source File: GraphJsonTestSupport.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowmanager.storage

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
import spray.json.{DefaultJsonProtocol, JsObject}

import ai.deepsense.deeplang.DOperation
import ai.deepsense.graph.Endpoint

trait GraphJsonTestSupport
  extends WordSpec
  with MockitoSugar
  with DefaultJsonProtocol
  with Matchers {

  def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = {
    assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString)
    assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex)
  }

  def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = {
    edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString &&
    edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex
  }

  def mockOperation(
      inArity: Int,
      outArity: Int,
      id: DOperation.Id,
      name: String): DOperation = {
    val dOperation = mock[DOperation]
    when(dOperation.inArity).thenReturn(inArity)
    when(dOperation.outArity).thenReturn(outArity)
    when(dOperation.id).thenReturn(id)
    when(dOperation.name).thenReturn(name)
    when(dOperation.paramValuesToJson).thenReturn(JsObject())
    dOperation
  }
} 
Example 56
Source File: NotebookDaoImplIntegSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowmanager.storage.impl

import scala.concurrent.{Await, Future}

import org.apache.commons.lang3.RandomStringUtils
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfter, Matchers}

import ai.deepsense.commons.StandardSpec
import ai.deepsense.commons.utils.Logging
import ai.deepsense.graph.Node
import ai.deepsense.models.workflows.Workflow
import ai.deepsense.workflowmanager.storage.GraphJsonTestSupport

class NotebookDaoImplIntegSpec
  extends StandardSpec
  with ScalaFutures
  with MockitoSugar
  with Matchers
  with BeforeAndAfter
  with GraphJsonTestSupport
  with SlickTestSupport
  with Logging {

  var notebooksDao: NotebookDaoImpl = _

  val n1@(notebook1Id, node1Id, notebook1) = createNotebook()
  val n2@(notebook2Id, node2Id, notebook2) = createNotebook()
  val n3@(notebook3Id, node3Id, notebook3) = createNotebook()
  val n4 = (notebook1Id, node2Id, notebook2)

  val storedNotebooks = Set(n1, n2, n4)

  before {
    notebooksDao = new NotebookDaoImpl(db, driver)
  }

  "NotebooksDao" should {

    "find notebook by id" in withStoredNotebooks(storedNotebooks) {
      whenReady(notebooksDao.get(notebook1Id, node1Id)) { notebook =>
        notebook shouldBe Some(notebook1)
      }
    }

    "get all notebooks for workflow" in withStoredNotebooks(storedNotebooks) {
      whenReady(notebooksDao.getAll(notebook1Id)) { notebooks =>
        notebooks.size shouldBe 2
        notebooks.get(node1Id) shouldBe Some(notebook1)
        notebooks.get(node2Id) shouldBe Some(notebook2)
      }
    }

    "return None if notebook does not exist" in withStoredNotebooks(storedNotebooks) {
      whenReady(notebooksDao.get(notebook3Id, node3Id)) { notebook =>
        notebook shouldBe None
      }
    }

    "create notebook" in withStoredNotebooks(storedNotebooks) {
      whenReady(notebooksDao.save(notebook3Id, node3Id, notebook3)) { _ =>
        whenReady(notebooksDao.get(notebook3Id, node3Id)) { notebook =>
          notebook shouldBe Some(notebook3)
        }
      }
    }

    "update notebook" in withStoredNotebooks(storedNotebooks) {
      val modifiedNotebook2 = "modified"
      whenReady(notebooksDao.save(notebook2Id, node2Id, modifiedNotebook2)) { _ =>
        whenReady(notebooksDao.get(notebook2Id, node2Id)) { notebook =>
          notebook shouldBe Some(modifiedNotebook2)
        }
      }
    }
  }

  private def withStoredNotebooks(
      storedNotebooks: Set[(Workflow.Id, Node.Id, String)])(testCode: => Any): Unit = {

    Await.ready(notebooksDao.create(), operationDuration)

    val s = Future.sequence(storedNotebooks.map {
      case (workflowId, nodeId, notebook) => notebooksDao.save(workflowId, nodeId, notebook)
    })
    Await.ready(s, operationDuration)

    try {
      testCode
    } finally {
      Await.ready(notebooksDao.drop(), operationDuration)
    }
  }

  def createNotebook(): (Workflow.Id, Node.Id, String) = {
    (Workflow.Id.randomId, Node.Id.randomId, RandomStringUtils.randomAlphanumeric(16))
  }
} 
Example 57
Source File: ApiSpecSupport.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowmanager.rest

import scala.concurrent.Future

import org.mockito.Matchers._
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.scalatest.mockito.MockitoSugar
import spray.routing.Route

import ai.deepsense.commons.auth.usercontext.{CannotGetUserException, Role, TokenTranslator, UserContext}

trait ApiSpecSupport extends MockitoSugar {

  val authTokens: Map[String, Set[String]]

  val apiPrefix: String

  def createRestComponent(tokenTranslator: TokenTranslator): Route

  protected def testRoute: Route = {
    val tokenTranslator = mock[TokenTranslator]
    when(tokenTranslator.translate(any(classOf[String])))
      .thenAnswer(new Answer[Future[UserContext]] {
      override def answer(invocation: InvocationOnMock): Future[UserContext] = {
        val tokenFromRequest = invocation.getArgumentAt(0, classOf[String])
        if (authTokens.keySet.contains(tokenFromRequest)) {
          val uc = mockUserContext(tokenFromRequest)
          Future.successful(uc)
        } else {
          Future.failed(new CannotGetUserException(tokenFromRequest))
        }
      }
    })
    createRestComponent(tokenTranslator)
  }

  private def mockUserContext(tenantId: String): UserContext = {
    val userContext = mock[UserContext]
    when(userContext.tenantId).thenReturn(tenantId)
    when(userContext.roles).thenReturn(authTokens(tenantId).map(Role(_)))
    userContext
  }
} 
Example 58
Source File: GlobalMQSerializerSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.communication.mq.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json._

import ai.deepsense.commons.StandardSpec
import ai.deepsense.commons.models.Entity
import ai.deepsense.deeplang.DOperable
import ai.deepsense.deeplang.doperables.ColumnsFilterer
import ai.deepsense.graph.Node
import ai.deepsense.models.json.workflow.ExecutionReportJsonProtocol._
import ai.deepsense.models.workflows.{EntitiesMap, ExecutionReport, Workflow}
import ai.deepsense.reportlib.model.factory.ReportContentTestFactory
import ai.deepsense.workflowexecutor.communication.message.global._
import ai.deepsense.workflowexecutor.communication.mq.json.Global.GlobalMQSerializer

class GlobalMQSerializerSpec
  extends StandardSpec
  with MockitoSugar {

    "GlobalMQSerializer" should {
      "serialize ExecutionReport" in {
        val executionReport = ExecutionReport(
          Map(Node.Id.randomId -> ai.deepsense.graph.nodestate.Draft()),
          EntitiesMap(
            Map[Entity.Id, DOperable](
              Entity.Id.randomId -> new ColumnsFilterer),
            Map(Entity.Id.randomId -> ReportContentTestFactory.someReport)),
          None)

        serialize(executionReport) shouldBe asBytes(JsObject(
          "messageType" -> JsString("executionStatus"),
          "messageBody" -> executionReport.toJson))
      }

      "serialize Launch messages" in {
        val workflowId = Workflow.Id.randomId
        val nodesToExecute = Vector(Workflow.Id.randomId, Workflow.Id.randomId, Workflow.Id.randomId)
        val jsNodesToExecute = JsArray(nodesToExecute.map(id => JsString(id.toString)))

        val outMessage = JsObject(
          "messageType" -> JsString("launch"),
          "messageBody" -> JsObject(
            "workflowId" -> JsString(workflowId.toString),
            "nodesToExecute" -> jsNodesToExecute
          )
        )

        val serializedMessage = serialize(Launch(workflowId, nodesToExecute.toSet))
        serializedMessage shouldBe asBytes(outMessage)
      }

      "serialize Heartbeat without SparkUi messages" in {
        val workflowId = "foo-workflow"
        val outMessage = JsObject(
          "messageType" -> JsString("heartbeat"),
          "messageBody" -> JsObject(
            "workflowId" -> JsString(workflowId)))
        serialize(Heartbeat(workflowId, None)) shouldBe asBytes(outMessage)
      }
      "serialize Heartbeat with SparkUi messages" in {
        val workflowId = "foo-workflow"
        val outMessage = JsObject(
          "messageType" -> JsString("heartbeat"),
          "messageBody" -> JsObject(
            "workflowId" -> JsString(workflowId),
            "sparkUiAddress" -> JsString("localhost")))
        serialize(Heartbeat(workflowId, Some("localhost"))) shouldBe asBytes(outMessage)
      }
      "serialize PoisonPill messages" in {
        val outMessage = JsObject(
          "messageType" -> JsString("poisonPill"),
          "messageBody" -> JsObject())
        serialize(PoisonPill()) shouldBe asBytes(outMessage)
      }
      "serialize Ready messages" in {
        val sessionId = "foo-session"
        val outMessage = JsObject(
          "messageType" -> JsString("ready"),
          "messageBody" -> JsObject(
            "sessionId" -> JsString(sessionId)))
        serialize(Ready(sessionId)) shouldBe asBytes(outMessage)
      }
    }

    private def asBytes(jsObject: JsObject): Array[Byte] =
      jsObject.compactPrint.getBytes(StandardCharsets.UTF_8)

    private def serialize(message: Any): Array[Byte] =
      GlobalMQSerializer.serializeMessage(message)
} 
Example 59
Source File: GlobalMQDeserializerSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.communication.mq.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json.{JsArray, JsNull, JsObject, JsString}
import ai.deepsense.commons.StandardSpec
import ai.deepsense.models.workflows.Workflow
import ai.deepsense.workflowexecutor.communication.message.global._
import ai.deepsense.workflowexecutor.communication.mq.json.Global.GlobalMQDeserializer

class GlobalMQDeserializerSpec
  extends StandardSpec
  with MockitoSugar {

  "GlobalMQDeserializer" should {
    "deserialize Launch messages" in {
      val workflowId = Workflow.Id.randomId
      val nodesToExecute = Vector(Workflow.Id.randomId, Workflow.Id.randomId, Workflow.Id.randomId)
      val jsNodesToExecute = JsArray(nodesToExecute.map(id => JsString(id.toString)))

      val rawMessage = JsObject(
        "messageType" -> JsString("launch"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString),
          "nodesToExecute" -> jsNodesToExecute
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage)
      readMessage shouldBe Launch(workflowId, nodesToExecute.toSet)
    }

    "deserialize Heartbeat messages" in {
      val workflowId = "foo-workflow"
      val rawMessage = JsObject(
        "messageType" -> JsString("heartbeat"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId),
          "sparkUiAddress" -> JsNull))
      serializeAndRead(rawMessage) shouldBe Heartbeat(workflowId, None)
    }
    "deserialize PoisonPill messages" in {
      val rawMessage = JsObject(
        "messageType" -> JsString("poisonPill"),
        "messageBody" -> JsObject())
      serializeAndRead(rawMessage) shouldBe PoisonPill()
    }
    "deserialize Ready messages" in {
      val sessionId = "foo-session"
      val rawMessage = JsObject(
        "messageType" -> JsString("ready"),
        "messageBody" -> JsObject(
          "sessionId" -> JsString(sessionId)))
      serializeAndRead(rawMessage) shouldBe Ready(sessionId)
    }
  }

  private def serializeAndRead(
    rawMessage: JsObject): Any = {
    val bytes = rawMessage.compactPrint.getBytes(StandardCharsets.UTF_8)
    GlobalMQDeserializer.deserializeMessage(bytes)
  }
} 
Example 60
Source File: DatasourceListJsonProtocolSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.json.envelope

import org.joda.time.DateTime
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.api.datasourcemanager.model.{AccessLevel, Datasource, DatasourceParams, DatasourceType}
import ai.deepsense.commons.datasource.DatasourceTestData
import ai.deepsense.commons.json.datasources.DatasourceListJsonProtocol

class DatasourceListJsonProtocolSpec
  extends WordSpec
  with MockitoSugar
  with Matchers {

  val uuid = "123e4567-e89b-12d3-a456-426655440000"
  val externalFile = DatasourceType.EXTERNALFILE

  val dsList = List(DatasourceTestData.multicharSeparatorLibraryCsvDatasource)

  "DatasourceJsonProtocolSpec" should {
    "serialize and deserialize single datasource" in {
      val datasourcesJson = DatasourceListJsonProtocol.toString(dsList)
      val asString = datasourcesJson.toString
      val datasources = DatasourceListJsonProtocol.fromString(asString)
      info(s"Datasource: $datasources, json: $asString")
      datasources should contain theSameElementsAs dsList
    }

    "serialize no datasource" in {
      val datasourcesJson = DatasourceListJsonProtocol.toString(List.empty[Datasource])
      datasourcesJson shouldBe "[]"
    }
  }
} 
Example 61
Source File: EntitiesMapSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.models.workflows

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.commons.models.Entity
import ai.deepsense.deeplang.doperables.dataframe.DataFrame
import ai.deepsense.reportlib.model.ReportContent

class EntitiesMapSpec
  extends WordSpec
  with Matchers
  with MockitoSugar {

  "EntitiesMap" should {
    "be correctly created from results and reports" in {

      val entity1Id = Entity.Id.randomId
      val doperable1 = new DataFrame()
      val report1 = mock[ReportContent]

      val entity2Id = Entity.Id.randomId
      val doperable2 = new DataFrame()

      val results = Map(entity1Id -> doperable1, entity2Id -> doperable2)
      val reports = Map(entity1Id -> report1)

      EntitiesMap(results, reports) shouldBe EntitiesMap(Map(
        entity1Id -> EntitiesMap.Entry(
          "ai.deepsense.deeplang.doperables.dataframe.DataFrame", Some(report1)),
        entity2Id -> EntitiesMap.Entry(
          "ai.deepsense.deeplang.doperables.dataframe.DataFrame", None)
      ))
    }
  }
} 
Example 62
Source File: NodeStateWithResultsSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.models.workflows

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.commons.models.Entity
import ai.deepsense.deeplang.exceptions.DeepLangException
import ai.deepsense.deeplang.inference.InferenceWarnings
import ai.deepsense.deeplang.{DKnowledge, DOperable}
import ai.deepsense.graph.NodeInferenceResult
import ai.deepsense.reportlib.model.ReportContent

class NodeStateWithResultsSpec extends WordSpec with Matchers with MockitoSugar {

  "NodeStateWithResults" should {

    "copy knowledge, keep warnings and clear errors for nonempty DOperable list" in {
      val draftNode = draftNodeState
      val (entityIds, operables, reportsMap, operablesMap) = executionResultFixture(2)
      val finished = draftNode.enqueue.start.finish(entityIds, reportsMap, operablesMap)

      finished.nodeState.isCompleted shouldBe true
      finished.knowledge shouldBe Some(NodeInferenceResult(
        operables.map(DKnowledge(_)).toVector,
        draftNode.knowledge.get.warnings,
        Vector()))
    }
    "copy knowledge, keep warnings and clear errors for empty DOperable list" in {
      val draftNode = draftNodeState
      val (entityIds, operables, reportsMap, operablesMap) = executionResultFixture(0)
      val finished = draftNode.enqueue.start.finish(entityIds, reportsMap, operablesMap)

      finished.nodeState.isCompleted shouldBe true
      finished.knowledge shouldBe Some(NodeInferenceResult(
        Vector(),
        draftNode.knowledge.get.warnings,
        Vector()))
    }
  }

  private def draftNodeState = {
    NodeStateWithResults.draft.withKnowledge(
      NodeInferenceResult(
        Vector(DKnowledge(mock[DOperable])),
        mock[InferenceWarnings],
        Vector(mock[DeepLangException])))
  }

  private def executionResultFixture(dOperableCount: Int):
      (Seq[Entity.Id], Seq[DOperable], Map[Entity.Id, ReportContent], Map[Entity.Id, DOperable]) = {
    val entityIds = (1 to dOperableCount).map(_ => Entity.Id.randomId).toList
    val operables = entityIds.map(_ => mock[DOperable])
    val reportsMap = entityIds.map(id => id -> mock[ReportContent]).toMap
    val operablesMap = entityIds.zip(operables).toMap
    (entityIds, operables, reportsMap, operablesMap)
  }
} 
Example 63
Source File: AbstractParamSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.deeplang.params

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
import spray.json.{JsObject, JsValue}

import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader

abstract class AbstractParamSpec[T, U <: Param[T]]
  extends WordSpec
  with Matchers
  with MockitoSugar {

  def className: String

  def paramFixture: (U, JsValue)  // param + its json description

  def valueFixture: (T, JsValue)  // value + its json description

  val defaultValue: T = valueFixture._1

  def graphReader: GraphReader = mock[GraphReader]

  def serializeDefaultValue(default: T): JsValue = paramFixture._1.valueToJson(default)

  className should {
    "serialize itself to JSON" when {
      "default value is not provided" in {
        val (param, expectedJson) = paramFixture
        param.toJson(maybeDefault = None) shouldBe expectedJson
      }
      "default value is provided" in {
        val (param, expectedJson) = paramFixture
        val expectedJsonWithDefault = JsObject(
          expectedJson.asJsObject.fields + ("default" -> serializeDefaultValue(defaultValue))
        )
        param.toJson(maybeDefault = Some(defaultValue)) shouldBe expectedJsonWithDefault
      }
    }
  }

  it should {
    "serialize value to JSON" in {
      val param = paramFixture._1
      val (value, expectedJson) = valueFixture
      param.valueToJson(value) shouldBe expectedJson
    }
  }

  it should {
    "deserialize value from JSON" in {
      val param = paramFixture._1
      val (expectedValue, valueJson) = valueFixture
      val extractedValue = param.valueFromJson(valueJson, graphReader)
      extractedValue shouldBe expectedValue
    }
  }
} 
Example 64
Source File: WrappersDefaultValidationSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.deeplang.params.wrappers.spark

import org.apache.spark.ml
import org.apache.spark.ml.param._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

class WrappersDefaultValidationSpec
  extends WordSpec
  with Matchers
  with MockitoSugar {

  class ExampleSparkParams extends ml.param.Params {
    override val uid: String = "id"
    val intSparkParam = new IntParam("", "name", "description")
    val floatSparkParam = new FloatParam("", "name", "description")
    val doubleSparkParam = new DoubleParam("", "name", "description")

    override def copy(extra: ParamMap): Params = ???
  }

  "IntParamWrapper" should {

    val intParamWrapper = new IntParamWrapper[ExampleSparkParams](
      "name",
      Some("description"),
      _.intSparkParam)

    "validate whole Int range" in {
      intParamWrapper.validate(Int.MinValue + 1) shouldBe empty
      intParamWrapper.validate(Int.MaxValue - 1) shouldBe empty
    }
    "reject fractional values" in {
      intParamWrapper.validate(Int.MinValue + 0.005) should have size 1
      intParamWrapper.validate(Int.MaxValue - 0.005) should have size 1
    }
  }

  "FloatParamWrapper" should {

    val floatParamWrapper = new FloatParamWrapper[ExampleSparkParams](
      "name",
      Some("description"),
      _.floatSparkParam)

    "validate whole Float range" in {
      floatParamWrapper.validate(Float.MinValue + 1) shouldBe empty
      floatParamWrapper.validate(Float.MaxValue - 1) shouldBe empty
    }
    "reject values out of Float range" in {
      floatParamWrapper.validate(Double.MinValue + 1) should have size 1
      floatParamWrapper.validate(Double.MaxValue - 1) should have size 1
    }
  }

  "DoubleParamWrapper" should {
    "validate whole Double range" in {
      val doubleParamWrapper = new DoubleParamWrapper[ExampleSparkParams](
        "name",
        Some("description"),
        _.doubleSparkParam)
      doubleParamWrapper.validate(Double.MinValue + 1) shouldBe empty
      doubleParamWrapper.validate(Double.MinValue - 1) shouldBe empty
    }
  }
} 
Example 65
Source File: ParamsWithSparkWrappersSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.deeplang.params.wrappers.spark

import org.apache.spark.ml
import org.apache.spark.ml.param._
import org.apache.spark.sql.types.StructType
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.deeplang.params.BooleanParam
import ai.deepsense.deeplang.params.choice.{ChoiceParam, Choice}

class ParamsWithSparkWrappersSpec extends WordSpec
  with Matchers
  with MockitoSugar {

  import ParamsWithSparkWrappersSpec._

  "ParamsWithSparkWrappers" should {
    "calculate sparkParamWrappers" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass()
      paramsWithSparkWrappers.sparkParamWrappers shouldBe
        Array(paramsWithSparkWrappers.paramA, paramsWithSparkWrappers.paramB)
    }
    "return parameter values" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass().setParamA("a").setParamB(0.0)
      paramsWithSparkWrappers.sparkParamMap(
        paramsWithSparkWrappers.exampleSparkParams, StructType(Seq())).toSeq.toSet shouldBe
        Set(
          paramsWithSparkWrappers.exampleSparkParams.sparkParamA -> "a",
          paramsWithSparkWrappers.exampleSparkParams.sparkParamB -> 0)
    }
    "return wrappers nested in choice parameter values" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass()
        .setChoice(OneParamChoiceWithWrappers().setParamC("c"))
      paramsWithSparkWrappers.sparkParamMap(
        paramsWithSparkWrappers.exampleSparkParams, StructType(Seq())).toSeq.toSet shouldBe
        Set(
          paramsWithSparkWrappers.exampleSparkParams.sparkParamC -> "c")
    }
  }
}

object ParamsWithSparkWrappersSpec {

  class ExampleSparkParams extends ml.param.Params {
    override val uid: String = "id"
    val sparkParamA = new Param[String]("", "paramA", "descA")
    val sparkParamB = new IntParam("", "paramB", "descB")
    val sparkParamC = new Param[String]("", "paramC", "descC")

    override def copy(extra: ParamMap): Params = ???
  }

  case class ParamsWithSparkWrappersClass() extends ParamsWithSparkWrappers {

    val exampleSparkParams = new ExampleSparkParams

    val paramA = new StringParamWrapper[ExampleSparkParams]("paramA", Some("descA"), _.sparkParamA)
    val paramB = new IntParamWrapper[ExampleSparkParams]("paramB", Some("descB"), _.sparkParamB)
    val choiceWithParamsInValues = new ChoiceParam[ChoiceWithWrappers]("choice", Some("descChoice"))
    val notWrappedParam = BooleanParam("booleanParamName", Some("booleanParamDescription"))

    val params: Array[ai.deepsense.deeplang.params.Param[_]] =
      Array(paramA, paramB, choiceWithParamsInValues, notWrappedParam)

    def setParamA(v: String): this.type = set(paramA, v)
    def setParamB(v: Double): this.type = set(paramB, v)
    def setChoice(v: ChoiceWithWrappers): this.type = set(choiceWithParamsInValues, v)
  }

  sealed trait ChoiceWithWrappers extends Choice with ParamsWithSparkWrappers {
    override val choiceOrder: List[Class[_ <: ChoiceWithWrappers]] = List(
      classOf[OneParamChoiceWithWrappers],
      classOf[EmptyChoiceWithWrappers])
  }

  case class OneParamChoiceWithWrappers() extends ChoiceWithWrappers {
    val paramC = new StringParamWrapper[ExampleSparkParams]("paramC", Some("descC"), _.sparkParamC)
    def setParamC(v: String): this.type = set(paramC, v)

    override val name = "one param"
    val params: Array[ai.deepsense.deeplang.params.Param[_]] = Array(paramC)
  }

  case class EmptyChoiceWithWrappers() extends ChoiceWithWrappers {
    override val name = "no params"
    val params: Array[ai.deepsense.deeplang.params.Param[_]] = Array()
  }
} 
Example 66
Source File: DeeplangTestSupport.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.deeplang

import org.apache.spark.sql
import org.apache.spark.sql.types.StructType
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import ai.deepsense.deeplang.catalogs.doperable.DOperableCatalog
import ai.deepsense.deeplang.doperables.dataframe.DataFrame
import ai.deepsense.deeplang.inference.InferContext

trait DeeplangTestSupport extends MockitoSugar {

  protected def createInferContext(
      dOperableCatalog: DOperableCatalog): InferContext = MockedInferContext(dOperableCatalog)

  protected def createExecutionContext: ExecutionContext = {
    val mockedExecutionContext = mock[ExecutionContext]
    val mockedInferContext = mock[InferContext]
    when(mockedExecutionContext.inferContext).thenReturn(mockedInferContext)
    mockedExecutionContext
  }

  protected def createSchema(fields: Array[String] = Array[String]()): StructType = {
    val schemaMock = mock[StructType]
    when(schemaMock.fieldNames).thenReturn(fields)
    schemaMock
  }

  protected def createSparkDataFrame(schema: StructType = createSchema()) = {
    val sparkDataFrameMock = mock[sql.DataFrame]
    when(sparkDataFrameMock.schema).thenReturn(schema)
    when(sparkDataFrameMock.toDF).thenReturn(sparkDataFrameMock)
    sparkDataFrameMock
  }

  protected def createDataFrame(fields: Array[String] = Array[String]()): DataFrame = {
    val schema = createSchema(fields)
    createDataFrame(schema)
  }

  protected def createDataFrame(schema: StructType): DataFrame = {
    val sparkDataFrameMock = createSparkDataFrame(schema)
    val dataFrameMock = mock[DataFrame]
    when(dataFrameMock.sparkDataFrame).thenReturn(sparkDataFrameMock)
    when(dataFrameMock.schema).thenReturn(Some(schema))
    dataFrameMock
  }
} 
Example 67
Source File: MockedInferContext.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.deeplang

import org.scalatest.mockito.MockitoSugar

import ai.deepsense.commons.rest.client.datasources.{DatasourceClient, DatasourceInMemoryClientFactory}
import ai.deepsense.deeplang.catalogs.DCatalog
import ai.deepsense.deeplang.catalogs.doperable.DOperableCatalog
import ai.deepsense.deeplang.catalogs.doperations.DOperationsCatalog
import ai.deepsense.deeplang.doperables.dataframe.DataFrameBuilder
import ai.deepsense.deeplang.inference.InferContext

object MockedInferContext extends MockitoSugar {
  def apply(
      dOperableCatalog: DOperableCatalog =
      CatalogRecorder.resourcesCatalogRecorder.catalogs.operables,
      dataFrameBuilder: DataFrameBuilder = mock[DataFrameBuilder],
      dOperationsCatalog: DOperationsCatalog = mock[DOperationsCatalog],
      datasourceClient: DatasourceClient = new DatasourceInMemoryClientFactory(List.empty).createClient
  ): InferContext = {
    val catalogPair = DCatalog(dOperableCatalog, dOperationsCatalog)
    InferContext(
      dataFrameBuilder,
      catalogPair,
      datasourceClient
    )
  }
} 
Example 68
Source File: GraphKnowledgeSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.graph

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.deeplang.exceptions.DeepLangException

class GraphKnowledgeSpec
  extends WordSpec
  with MockitoSugar
  with Matchers {

  "GraphKnowledge" should {
    "return proper errors map" in {
      val node1Id = Node.Id.randomId
      val node2Id = Node.Id.randomId
      val inferenceResultsWithErrors = mock[NodeInferenceResult]
      val errors = Vector(mock[DeepLangException], mock[DeepLangException])
      when(inferenceResultsWithErrors.errors).thenReturn(errors)
      val inferenceResultsWithoutErrors = mock[NodeInferenceResult]
      when(inferenceResultsWithoutErrors.errors).thenReturn(Vector.empty)

      val knowledge = GraphKnowledge()
        .addInference(node1Id, inferenceResultsWithErrors)
        .addInference(node2Id, inferenceResultsWithoutErrors)

      knowledge.errors shouldBe Map(node1Id -> errors)
    }
  }
} 
Example 69
Source File: GraphTestSupport.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.graph

import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import ai.deepsense.commons.datetime.DateTimeConverter
import ai.deepsense.commons.exception.FailureDescription
import ai.deepsense.commons.models.Entity
import ai.deepsense.deeplang._
import ai.deepsense.graph.DeeplangGraph.DeeplangNode

trait GraphTestSupport {
  self: MockitoSugar =>

  val op0To1 = {
    val m = mock[DOperation0To1[DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  val op1To1 = createOp1To1

  def createOp1To1: DOperation1To1[DOperable, DOperable] = {
    val m = mock[DOperation1To1[DOperable, DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  val op2To2 = {
    val m = mock[DOperation2To2[DOperable, DOperable, DOperable, DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  

  val nodesSeq = generateNodes(op0To1, op1To1, op1To1, op1To1, op2To2)
  val nodeSet = nodesSeq.map(_._2).toSet
  val idA :: idB :: idC :: idD :: idE :: Nil = nodesSeq.map(_._1).toList
  val nodeA :: nodeB :: nodeC :: nodeD :: nodeE :: Nil = nodesSeq.map(_._2).toList

  val edgeList: List[Edge] = edges(idA, idB, idC, idD, idE)
  val edge1 :: edge2 :: edge3 :: edge4 :: edge5 :: Nil = edgeList
  val edgeSet = edgeList.toSet
  val nodeIds = Seq(idA, idB, idC, idD, idE)

  val results = Map(
    idA -> Seq(mock[Entity.Id]),
    idB -> Seq(mock[Entity.Id]),
    idC -> Seq(mock[Entity.Id]),
    idD -> Seq(mock[Entity.Id]),
    idE -> Seq(mock[Entity.Id], mock[Entity.Id])
  )


  private def edges(
      idA: Node.Id,
      idB: Node.Id,
      idC: Node.Id,
      idD: Node.Id,
      idE: Node.Id): List[Edge] = {
    List(
      Edge(Endpoint(idA, 0), Endpoint(idB, 0)),
      Edge(Endpoint(idB, 0), Endpoint(idC, 0)),
      Edge(Endpoint(idC, 0), Endpoint(idD, 0)),
      Edge(Endpoint(idA, 0), Endpoint(idE, 0)),
      Edge(Endpoint(idB, 0), Endpoint(idE, 1))
    )
  }

  protected def generateNodes(ops: DOperation*): Seq[(Node.Id, DeeplangNode)] = {
    val nodes = ops.map { o => Node(Node.Id.randomId, o)}
    nodes.map(n => n.id -> n)
  }

  protected def nodeRunning: nodestate.Running = nodestate.Running(DateTimeConverter.now)

  protected def nodeFailed: nodestate.Failed =
    nodestate.Running(DateTimeConverter.now).fail(mock[FailureDescription])

  protected def nodeCompleted: nodestate.Completed = {
    val date = DateTimeConverter.now
    nodestate.Completed(date, date.plusMinutes(1), Seq())
  }

  protected def nodeCompletedId(nodeId: Entity.Id): nodestate.Completed = {
    val date = DateTimeConverter.now
    nodestate.Completed(date, date.plusMinutes(1), results(nodeId))
  }
} 
Example 70
Source File: GraphTestObjects.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.graph

import scala.reflect.runtime.{universe => ru}

import org.scalatest.mockito.MockitoSugar

import ai.deepsense.commons.utils.Version
import ai.deepsense.deeplang._
import ai.deepsense.deeplang.doperables.DOperableMock
import ai.deepsense.graph.DeeplangGraph.DeeplangNode

object RandomNodeFactory {
  def randomNode(operation: DOperation): DeeplangNode = Node(Node.Id.randomId, operation)
}

object DClassesForDOperations extends MockitoSugar {
  trait A extends DOperableMock
  case class A1() extends A
  case class A2() extends A
}

object DOperationTestClasses {
  import ai.deepsense.graph.DClassesForDOperations._

  trait DOperationBaseFields extends DOperation {
    // NOTE: id will be different for each instance
    override val id: DOperation.Id = DOperation.Id.randomId

    override val name: String = ""

    override val description: String = ""

    val specificParams: Array[ai.deepsense.deeplang.params.Param[_]] = Array()
  }

  case class DOperationCreateA1() extends DOperation0To1[A1] with DOperationBaseFields {
    override protected def execute()(context: ExecutionContext): A1 = ???

    @transient
    override lazy val tTagTO_0: ru.TypeTag[A1] = ru.typeTag[A1]
  }

  case class DOperationReceiveA1() extends DOperation1To0[A1] with DOperationBaseFields {
    override protected def execute(t0: A1)(context: ExecutionContext): Unit = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
  }

  case class DOperationA1ToA() extends DOperation1To1[A1, A] with DOperationBaseFields {
    override protected def execute(t1: A1)(context: ExecutionContext): A = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
  }

  case class DOperationAToA1A2() extends DOperation1To2[A, A1, A2] with DOperationBaseFields {
    override protected def execute(in: A)(context: ExecutionContext): (A1, A2) = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_1: ru.TypeTag[A2] = ru.typeTag[A2]
  }

  case class DOperationA1A2ToA() extends DOperation2To1[A1, A2, A] with DOperationBaseFields {
    override protected def execute(t1: A1, t2: A2)(context: ExecutionContext): A = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTI_1: ru.TypeTag[A2] = ru.typeTag[A2]
  }

  case class DOperationAToALogging() extends DOperation1To1[A, A] with DOperationBaseFields {
    logger.trace("Initializing logging to test the serialization")
    override protected def execute(t0: A)(context: ExecutionContext): A = ???

    def trace(message: String): Unit = logger.trace(message)

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
  }
} 
Example 71
Source File: SparkRBackendSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor

import org.apache.spark.api.r._
import org.scalatest.concurrent.TimeLimits
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, PrivateMethodTester, WordSpec}

import ai.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class SparkRBackendSpec
  extends WordSpec
  with MockitoSugar
  with Matchers
  with TimeLimits
  with PrivateMethodTester {

  "Spark R Backend" should {
    "return 0 for Entry Point Id" in {
      val sparkRBackend = new SparkRBackend()
      val customCodeEntryPoint = mock[CustomCodeEntryPoint]
      sparkRBackend.start(customCodeEntryPoint)
      sparkRBackend.entryPointId shouldBe "0"
      sparkRBackend.close()
    }
  }
} 
Example 72
Source File: DataFrameStorageSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.session.storage

import org.apache.spark.sql.{DataFrame => SparkDataFrame}
import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar

import ai.deepsense.commons.StandardSpec
import ai.deepsense.commons.models.Id
import ai.deepsense.deeplang.DataFrameStorage
import ai.deepsense.deeplang.doperables.dataframe.DataFrame

class DataFrameStorageSpec
    extends StandardSpec
    with BeforeAndAfter
    with MockitoSugar {

  val workflow1Id = Id.randomId
  val workflow2Id = Id.randomId

  val node1Id = Id.randomId
  val node2Id = Id.randomId

  val dataframe1Id = "dataframe1"
  val dataframe2Id = "dataframe2"
  val dataframe3Id = "dataframe3"

  val dataframe1 = mock[DataFrame]
  val dataframe2 = mock[DataFrame]
  val dataframe3 = mock[DataFrame]

  val sparkDataFrame1 = mock[SparkDataFrame]
  val sparkDataFrame2 = mock[SparkDataFrame]
  val sparkDataFrame3 = mock[SparkDataFrame]

  var storage: DataFrameStorage = _

  before {
    storage = new DataFrameStorageImpl
  }

  "DataFrameStorage" should {
    "register input dataFrames" in {
      storage.setInputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setInputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setInputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)

      storage.getInputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getInputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 0) shouldBe Some(sparkDataFrame2)
      storage.getInputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getInputDataFrame(workflow2Id, node2Id, 2) shouldBe None
    }

    "delete input dataFrames" in {
      storage.setInputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setInputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setInputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)
      storage.setInputDataFrame(workflow2Id, node2Id, 0, sparkDataFrame3)

      storage.removeNodeInputDataFrames(workflow1Id, node2Id, 0)

      storage.getInputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getInputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 0) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getInputDataFrame(workflow2Id, node2Id, 2) shouldBe None
      storage.getInputDataFrame(workflow2Id, node2Id, 0) shouldBe Some(sparkDataFrame3)
    }

    "register output dataFrames" in {
      storage.setOutputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setOutputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setOutputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)

      storage.getOutputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getOutputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getOutputDataFrame(workflow1Id, node2Id, 0) shouldBe Some(sparkDataFrame2)
      storage.getOutputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getOutputDataFrame(workflow2Id, node2Id, 2) shouldBe None
    }

    "delete some output dataFrames" in {
      storage.setOutputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setOutputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setOutputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)
      storage.setOutputDataFrame(workflow2Id, node2Id, 1, sparkDataFrame3)

      storage.removeNodeOutputDataFrames(workflow1Id, node2Id)

      storage.getOutputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getOutputDataFrame(workflow1Id, node2Id, 0) shouldBe None
      storage.getOutputDataFrame(workflow1Id, node2Id, 1) shouldBe None
      storage.getOutputDataFrame(workflow2Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
    }
  }
} 
Example 73
Source File: ProtocolJsonSerializerSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.communication.mq.serialization.json

import java.nio.charset.Charset

import org.scalatest.mockito.MockitoSugar
import spray.json._

import ai.deepsense.commons.StandardSpec
import ai.deepsense.commons.models.Entity
import ai.deepsense.deeplang.DOperable
import ai.deepsense.deeplang.doperables.ColumnsFilterer
import ai.deepsense.graph._
import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader
import ai.deepsense.models.json.workflow.{ExecutionReportJsonProtocol, InferredStateJsonProtocol, WorkflowWithResultsJsonProtocol}
import ai.deepsense.models.workflows._
import ai.deepsense.reportlib.model.factory.ReportContentTestFactory
import ai.deepsense.workflowexecutor.communication.message.global._
import ai.deepsense.workflowexecutor.communication.message.workflow.Synchronize

class ProtocolJsonSerializerSpec
  extends StandardSpec
  with MockitoSugar
  with WorkflowWithResultsJsonProtocol
  with InferredStateJsonProtocol
  with HeartbeatJsonProtocol {

  override val graphReader: GraphReader = mock[GraphReader]

  "ProtocolJsonSerializer" should {
    val protocolJsonSerializer = ProtocolJsonSerializer(graphReader)


    "serialize Synchronize messages" in {
      protocolJsonSerializer.serializeMessage(Synchronize()) shouldBe
        expectedSerializationResult("synchronize", JsObject())
    }

  }

  private def expectedSerializationResult(messageType: String, jsonObject: JsValue): Array[Byte] = {
    JsObject(
      "messageType" -> JsString(messageType),
      "messageBody" -> jsonObject
    ).compactPrint.getBytes(Charset.forName("UTF-8"))
  }

} 
Example 74
Source File: ProtocolJsonDeserializerSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.communication.mq.serialization.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json._

import ai.deepsense.commons.StandardSpec
import ai.deepsense.deeplang.CatalogRecorder
import ai.deepsense.graph.DeeplangGraph
import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader
import ai.deepsense.models.workflows.{Workflow, WorkflowMetadata, WorkflowType}
import ai.deepsense.workflowexecutor.communication.message.workflow.{Abort, Synchronize, UpdateWorkflow}

class ProtocolJsonDeserializerSpec
  extends StandardSpec
  with MockitoSugar {

  "ProtocolJsonDeserializer" should {
    "deserialize Abort messages" in {
      val workflowId = Workflow.Id.randomId

      val rawMessage = JsObject(
        "messageType" -> JsString("abort"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString)
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage)
      readMessage shouldBe Abort(workflowId)
    }
    "deserialize UpdateWorkflow messages" in {
      val dOperationsCatalog = CatalogRecorder.resourcesCatalogRecorder.catalogs.operations
      val graphReader = new GraphReader(dOperationsCatalog)
      val protocolDeserializer = ProtocolJsonDeserializer(graphReader)
      val workflowId = Workflow.Id.randomId

      val rawMessage = JsObject(
        "messageType" -> JsString("updateWorkflow"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString),
          "workflow" -> JsObject(
            "metadata" -> JsObject(
              "type" -> JsString("batch"),
              "apiVersion" -> JsString("1.0.0")
            ),
            "workflow" -> JsObject(
              "nodes" -> JsArray(),
              "connections" -> JsArray()
            ),
            "thirdPartyData" -> JsObject()
          )
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage, protocolDeserializer)
      readMessage shouldBe UpdateWorkflow(
        workflowId,
        Workflow(WorkflowMetadata(WorkflowType.Batch, "1.0.0"), DeeplangGraph(), JsObject()))
    }

    "deserialize Synchronize messages" in {
      val rawMessage = JsObject(
        "messageType" -> JsString("synchronize"),
        "messageBody" -> JsObject())
      serializeAndRead(rawMessage) shouldBe Synchronize()
    }
  }

  private def serializeAndRead(
      rawMessage: JsObject,
      protocolDeserializer: ProtocolJsonDeserializer =
        ProtocolJsonDeserializer(mock[GraphReader])): Any = {
    val bytes = rawMessage.compactPrint.getBytes(StandardCharsets.UTF_8)
    protocolDeserializer.deserializeMessage(bytes)
  }
} 
Example 75
Source File: WorkflowJsonParamsOverriderSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor

import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar
import spray.json._

import ai.deepsense.commons.StandardSpec

class WorkflowJsonParamsOverriderSpec
    extends StandardSpec
    with BeforeAndAfter
    with MockitoSugar
    with DefaultJsonProtocol {

  "WorkflowJsonParamsOverrider" should {
    "override parameters based on passed extra params" in {
      val overrides = Map(
        "node1.param with spaces" -> "new value",
        "node2.nested.parameter.test" -> "changed"
      )

      WorkflowJsonParamsOverrider.overrideParams(originalJson, overrides) shouldBe expectedJson
    }

    "throw when invalid parameters are passed" in {
      val overrides = Map(
        "node1.no such param" -> "no such param",
        "no such node.param" -> "no such node"
      )

      a[RuntimeException] should be thrownBy {
        WorkflowJsonParamsOverrider.overrideParams(originalJson, overrides)
      }
    }
  }

  val originalJson =
    """{
      |  "workflow": {
      |    "nodes": [{
      |      "id": "node1",
      |      "parameters": {
      |        "param with spaces": "value"
      |      }
      |     }, {
      |      "id": "node2",
      |      "parameters": {
      |        "param": "value",
      |        "nested": {
      |          "parameter": {
      |            "test": "nested value"
      |          }
      |        }
      |      }
      |    }]
      |  }
      |}
    """.stripMargin.parseJson

  val expectedJson =
    """{
      |  "workflow": {
      |    "nodes": [{
      |      "id": "node1",
      |      "parameters": {
      |        "param with spaces": "new value"
      |      }
      |     }, {
      |      "id": "node2",
      |      "parameters": {
      |        "param": "value",
      |        "nested": {
      |          "parameter": {
      |            "test": "changed"
      |          }
      |        }
      |      }
      |    }]
      |  }
      |}
    """.stripMargin.parseJson
} 
Example 76
Source File: OperationExecutionDispatcherSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.pythongateway

import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar
import ai.deepsense.commons.StandardSpec
import ai.deepsense.commons.models.Id
import ai.deepsense.deeplang.OperationExecutionDispatcher

class OperationExecutionDispatcherSpec
    extends StandardSpec
    with MockitoSugar
    with BeforeAndAfter {

  val workflowId = Id.randomId
  val nodeId = Id.randomId

  var dispatcher: OperationExecutionDispatcher = _

  before {
    dispatcher = new OperationExecutionDispatcher
  }

  "OperationExecutionDispatcher" should {

    "execute operation and finish" when {

      "notified of success with proper workflow and node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        dispatcher.executionEnded(workflowId, nodeId, Right(()))
        future.isCompleted shouldBe true
      }

      "notified of failure with proper workflow and node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        dispatcher.executionEnded(workflowId, nodeId, Left("A stacktrace"))
        future.isCompleted shouldBe true
        future.value.get.get shouldBe Left("A stacktrace")
      }
    }

    "throw an exception" when {

      "multiple executions of the same node are started" in {
        dispatcher.executionStarted(workflowId, nodeId)

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionStarted(workflowId, nodeId)
        }
      }

      "notified with non-existing workflow id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionEnded(Id.randomId, nodeId, Right(()))
        }
      }

      "notified with non-existing node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionEnded(workflowId, Id.randomId, Right(()))
        }
      }
    }
  }
} 
Example 77
Source File: PythonCustomCodeEntryPointTest.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.pythongateway

import java.util.concurrent.TimeoutException

import scala.concurrent.duration._

import org.apache.spark.SparkContext
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.deeplang.{CustomCodeExecutor, DataFrameStorage, OperationExecutionDispatcher}
import ai.deepsense.sparkutils.SparkSQLSession
import ai.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class PythonCustomCodeEntryPointTest extends WordSpec with MockitoSugar with Matchers {

  "PythonEntryPoint" should {
    "throw on uninitialized code executor" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getCodeExecutor(100.millis)
      }
    }

    "throw on uninitialized callback server port" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getPythonPort(100.millis)
      }
    }

    "return initialized code executor" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]
      entryPoint.registerCodeExecutor(mockExecutor)
      entryPoint.getCodeExecutor(100.millis) shouldBe mockExecutor
    }

    "return initialized callback server port" in {
      val entryPoint = createEntryPoint
      entryPoint.registerCallbackServerPort(4412)
      entryPoint.getPythonPort(100.millis) shouldBe 4412
    }

    "return code executor initialized while waiting on it" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]

      new Thread(new Runnable {
        override def run(): Unit = {
          Thread.sleep(1000)
          entryPoint.registerCodeExecutor(mockExecutor)
        }
      }).start()

      entryPoint.getCodeExecutor(2.seconds) shouldBe mockExecutor
    }
  }

  private def createEntryPoint: CustomCodeEntryPoint =
    new CustomCodeEntryPoint(
      mock[SparkContext],
      mock[SparkSQLSession],
      mock[DataFrameStorage],
      mock[OperationExecutionDispatcher])
} 
Example 78
Source File: DOperationCategoryNodeJsonProtocolSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.models.json.workflow

import scala.collection.immutable.SortedMap
import scala.collection.immutable.ListMap

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import ai.deepsense.deeplang.DOperation
import ai.deepsense.deeplang.catalogs.SortPriority
import ai.deepsense.deeplang.catalogs.doperations.{DOperationCategory, DOperationCategoryNode, DOperationDescriptor}
import ai.deepsense.models.json.workflow.DOperationCategoryNodeJsonProtocol._

object SortPriorityTest

class DOperationCategoryNodeJsonProtocolSpec extends FlatSpec with Matchers with MockitoSugar {

  "DOperationCategoryNode" should "be correctly serialized to json" in {
    val childCategory =
      new DOperationCategory(DOperationCategory.Id.randomId, "mock child name", SortPriority.coreDefault) {}
    val childNode = DOperationCategoryNode(Some(childCategory))

    val operationDescriptor = mock[DOperationDescriptor]
    when(operationDescriptor.id) thenReturn DOperation.Id.randomId
    when(operationDescriptor.name) thenReturn "mock operation descriptor name"
    when(operationDescriptor.description) thenReturn "mock operator descriptor description"

    val node = DOperationCategoryNode(
      None,
      successors = SortedMap(childCategory -> childNode),
      operations = List(operationDescriptor))

    val expectedJson = JsObject(
      "catalog" -> JsArray(
        JsObject(
          "id" -> JsString(childCategory.id.toString),
          "name" -> JsString(childCategory.name),
          "catalog" -> JsArray(),
          "items" -> JsArray())
      ),
      "items" -> JsArray(
        JsObject(
          "id" -> JsString(operationDescriptor.id.toString),
          "name" -> JsString(operationDescriptor.name),
          "description" -> JsString(operationDescriptor.description)
        )
      )
    )

    node.toJson shouldBe expectedJson
  }
} 
Example 79
Source File: GraphJsonTestSupport.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.models.json.graph

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
import spray.json.{DefaultJsonProtocol, JsObject}

import ai.deepsense.deeplang.DOperation
import ai.deepsense.graph.Endpoint

trait GraphJsonTestSupport
  extends WordSpec
  with MockitoSugar
  with DefaultJsonProtocol
  with Matchers {

  def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = {
    assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString)
    assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex)
  }

  def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = {
    edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString &&
    edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex
  }

  def mockOperation(
      inArity: Int,
      outArity: Int,
      id: DOperation.Id,
      name: String): DOperation = {

    val dOperation = mock[DOperation]
    when(dOperation.inArity).thenReturn(inArity)
    when(dOperation.outArity).thenReturn(outArity)
    when(dOperation.id).thenReturn(id)
    when(dOperation.name).thenReturn(name)
    dOperation
  }
} 
Example 80
Source File: SeahorseSparkLauncherSpec.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service.sessionspawner.sparklauncher.clusters

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FunSuite, Matchers}
import ai.deepsense.sessionmanager.service.sessionspawner.sparklauncher.spark.SparkArgumentParser._

class SeahorseSparkLauncherSpec extends FunSuite with Matchers  with MockitoSugar {
  test("Merging Configuration Option") {
    testCases.foreach { testCase =>
      val output = testCase.inputArgs.updateConfOptions(testCase.inputKey, testCase.inputValue)
      output shouldBe testCase.output
    }
  }

  test("Getting configuration option returns key value if it is present") {
    confOptionMap.getConfOption("key") shouldBe Some(Set("5"))
  }

  test("Getting configuration option returns multiple values if it is present") {
    confOptionMap.getConfOption("keyB") shouldBe Some(Set("1", "2"))
  }

  test("Getting configuration option returns None if there is no --conf argument") {
    Map("--con" -> Set("key=5", "keyB=1", "keyB=2")).getConfOption("key") shouldBe None
  }

  test("Getting configuration option returns None if there is no key") {
    confOptionMap.getConfOption("NonePresentKey") shouldBe None
  }

  val confOptionMap = Map("--conf" -> Set("key=5", "keyB=1", "keyB=2"))

  val testCases = Seq(
    TestCase(
      inputKey = "key",
      inputValue = "value",
      inputArgs = Map("--conf" -> Set("otherKey=5"), "--con" -> Set("value=otherValue")),
      output = Map("--conf" -> Set("otherKey=5", "key=value"), "--con" -> Set("value=otherValue"))),
    TestCase(
      inputKey = "key",
      inputValue = "value",
      inputArgs = Map(("--conf" -> Set("key=valueOther", "key2=value2"))),
      output = Map("--conf" -> Set("key=valueOther,value", "key2=value2")))
  )

  case class TestCase(
    inputKey: String,
    inputValue: String,
    inputArgs: Map[String, Set[String]],
    output: Map[String, Set[String]])

} 
Example 81
Source File: YarnSchedulerBackendSuite.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.scheduler.cluster

import scala.language.reflectiveCalls

import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar

import org.apache.spark.{LocalSparkContext, SparkContext, SparkFunSuite}
import org.apache.spark.scheduler.TaskSchedulerImpl
import org.apache.spark.serializer.JavaSerializer

class YarnSchedulerBackendSuite extends SparkFunSuite with MockitoSugar with LocalSparkContext {

  test("RequestExecutors reflects node blacklist and is serializable") {
    sc = new SparkContext("local", "YarnSchedulerBackendSuite")
    val sched = mock[TaskSchedulerImpl]
    when(sched.sc).thenReturn(sc)
    val yarnSchedulerBackend = new YarnSchedulerBackend(sched, sc) {
      def setHostToLocalTaskCount(hostToLocalTaskCount: Map[String, Int]): Unit = {
        this.hostToLocalTaskCount = hostToLocalTaskCount
      }
    }
    val ser = new JavaSerializer(sc.conf).newInstance()
    for {
      blacklist <- IndexedSeq(Set[String](), Set("a", "b", "c"))
      numRequested <- 0 until 10
      hostToLocalCount <- IndexedSeq(
        Map[String, Int](),
        Map("a" -> 1, "b" -> 2)
      )
    } {
      yarnSchedulerBackend.setHostToLocalTaskCount(hostToLocalCount)
      when(sched.nodeBlacklist()).thenReturn(blacklist)
      val req = yarnSchedulerBackend.prepareRequestExecutors(numRequested)
      assert(req.requestedTotal === numRequested)
      assert(req.nodeBlacklist === blacklist)
      assert(req.hostToLocalTaskCount.keySet.intersect(blacklist).isEmpty)
      // Serialize to make sure serialization doesn't throw an error
      ser.serialize(req)
    }
    sc.stop()
  }

} 
Example 82
Source File: NettyRpcEnvSuite.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.rpc.netty

import org.scalatest.mockito.MockitoSugar

import org.apache.spark._
import org.apache.spark.network.client.TransportClient
import org.apache.spark.rpc._

class NettyRpcEnvSuite extends RpcEnvSuite with MockitoSugar {

  override def createRpcEnv(
      conf: SparkConf,
      name: String,
      port: Int,
      clientMode: Boolean = false): RpcEnv = {
    val config = RpcEnvConfig(conf, "test", "localhost", "localhost", port,
      new SecurityManager(conf), 0, clientMode)
    new NettyRpcEnvFactory().create(config)
  }

  test("non-existent endpoint") {
    val uri = RpcEndpointAddress(env.address, "nonexist-endpoint").toString
    val e = intercept[SparkException] {
      env.setupEndpointRef(env.address, "nonexist-endpoint")
    }
    assert(e.getCause.isInstanceOf[RpcEndpointNotFoundException])
    assert(e.getCause.getMessage.contains(uri))
  }

  test("advertise address different from bind address") {
    val sparkConf = new SparkConf()
    val config = RpcEnvConfig(sparkConf, "test", "localhost", "example.com", 0,
      new SecurityManager(sparkConf), 0, false)
    val env = new NettyRpcEnvFactory().create(config)
    try {
      assert(env.address.hostPort.startsWith("example.com:"))
    } finally {
      env.shutdown()
    }
  }

  test("RequestMessage serialization") {
    def assertRequestMessageEquals(expected: RequestMessage, actual: RequestMessage): Unit = {
      assert(expected.senderAddress === actual.senderAddress)
      assert(expected.receiver === actual.receiver)
      assert(expected.content === actual.content)
    }

    val nettyEnv = env.asInstanceOf[NettyRpcEnv]
    val client = mock[TransportClient]
    val senderAddress = RpcAddress("locahost", 12345)
    val receiverAddress = RpcEndpointAddress("localhost", 54321, "test")
    val receiver = new NettyRpcEndpointRef(nettyEnv.conf, receiverAddress, nettyEnv)

    val msg = new RequestMessage(senderAddress, receiver, "foo")
    assertRequestMessageEquals(
      msg,
      RequestMessage(nettyEnv, client, msg.serialize(nettyEnv)))

    val msg2 = new RequestMessage(null, receiver, "foo")
    assertRequestMessageEquals(
      msg2,
      RequestMessage(nettyEnv, client, msg2.serialize(nettyEnv)))

    val msg3 = new RequestMessage(senderAddress, receiver, null)
    assertRequestMessageEquals(
      msg3,
      RequestMessage(nettyEnv, client, msg3.serialize(nettyEnv)))
  }
} 
Example 83
Source File: PartiallyUnrolledIteratorSuite.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.storage

import org.mockito.Matchers
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import org.apache.spark.SparkFunSuite
import org.apache.spark.memory.MemoryMode.ON_HEAP
import org.apache.spark.storage.memory.{MemoryStore, PartiallyUnrolledIterator}

class PartiallyUnrolledIteratorSuite extends SparkFunSuite with MockitoSugar {
  test("join two iterators") {
    val unrollSize = 1000
    val unroll = (0 until unrollSize).iterator
    val restSize = 500
    val rest = (unrollSize until restSize + unrollSize).iterator

    val memoryStore = mock[MemoryStore]
    val joinIterator = new PartiallyUnrolledIterator(memoryStore, ON_HEAP, unrollSize, unroll, rest)

    // Firstly iterate over unrolling memory iterator
    (0 until unrollSize).foreach { value =>
      assert(joinIterator.hasNext)
      assert(joinIterator.hasNext)
      assert(joinIterator.next() == value)
    }

    joinIterator.hasNext
    joinIterator.hasNext
    verify(memoryStore, times(1))
      .releaseUnrollMemoryForThisTask(Matchers.eq(ON_HEAP), Matchers.eq(unrollSize.toLong))

    // Secondly, iterate over rest iterator
    (unrollSize until unrollSize + restSize).foreach { value =>
      assert(joinIterator.hasNext)
      assert(joinIterator.hasNext)
      assert(joinIterator.next() == value)
    }

    joinIterator.close()
    // MemoryMode.releaseUnrollMemoryForThisTask is called only once
    verifyNoMoreInteractions(memoryStore)
  }
} 
Example 84
Source File: PrometheusControllerSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.controllers

import java.util.Collections

import io.prometheus.client.Collector.MetricFamilySamples
import io.prometheus.client.{Collector, CollectorRegistry}
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.PlaySpec
import play.api.mvc.Results
import play.api.test.FakeRequest
import play.api.test.Helpers._


class PrometheusControllerSpec extends PlaySpec with Results with MockitoSugar {

  "Get metrics method" should {
    "Return the prometheus metrics" in {
      val collectorRegistry = mock[CollectorRegistry]
      val metricsFamilySample = new MetricFamilySamples("test", Collector.Type.COUNTER, "help", Collections.emptyList())
      when(collectorRegistry.metricFamilySamples()).thenReturn(new java.util.Vector(Collections.singleton(metricsFamilySample)).elements)

      val client = new PrometheusController(collectorRegistry, stubControllerComponents())

      val request = FakeRequest(GET, "/metrics")

      val result = client.getMetrics.apply(request)
      status(result) mustBe OK
      contentAsString(result) mustBe "# HELP test help\n# TYPE test counter\n"
    }
  }

} 
Example 85
Source File: RouteLatencyFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.metrics.DefaultPlayUnmatchedDefaults
import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import io.prometheus.client.CollectorRegistry
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.verify
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{MustMatchers, WordSpec}
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.libs.typedmap.TypedMap
import play.api.mvc._
import play.api.routing.{HandlerDef, Router}
import play.api.test.Helpers.stubControllerComponents
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class RouteLatencyFilterSpec extends WordSpec with MustMatchers with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite  {

  private implicit val mat = app.materializer
  private val configuration = mock[Configuration]

  "Filter constructor" should {
    "Add a histogram to the prometheus registry" in {
      val collectorRegistry = mock[CollectorRegistry]
      new RouteLatencyFilter(collectorRegistry, configuration)
      verify(collectorRegistry).register(any())
    }
  }

  "Apply method" should {
    "Measure the latency" in {
      val filter = new RouteLatencyFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest().withAttrs( TypedMap(
        Router.Attrs.HandlerDef -> HandlerDef(null, null, null, "test", null, null ,null ,null ,null)
      ))
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(samples.size() - 2)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 1
      countSample.labelValues.get(0) mustBe "test"
    }

    "Measure the latency for an unmatched route" in {
      val filter = new RouteLatencyFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest()
      val action = new MockController(stubControllerComponents()).error

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(samples.size() - 2)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 1
      countSample.labelValues.get(0) mustBe DefaultPlayUnmatchedDefaults.UnmatchedRouteString
    }
  }

} 
Example 86
Source File: MetricFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.metrics.CounterRequestMetrics.CounterRequestMetricBuilder
import com.github.stijndehaes.playprometheusfilters.metrics.{DefaultPlayUnmatchedDefaults, RequestMetric}
import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import com.typesafe.config.ConfigFactory
import io.prometheus.client.CollectorRegistry
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class MetricFilterSpec extends PlaySpec with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite {

  val configuration = Configuration(ConfigFactory.parseString(
    """play-prometheus-filters.exclude.paths = ["/test"]"""
  ))

  "Filter constructor" should {
    "Get exclude paths from configuration" in {
      implicit val mat = app.materializer
      val filter = new MetricsFilter(configuration) {
        override val metrics = List.empty[RequestMetric[_, RequestHeader, Result]]
      }

      filter.excludePaths must have size 1 // only check size since cannot compare Regex's
    }
  }

  "Apply method" should {
    "skip metrics for excluded paths" in {
      implicit val mat = app.materializer
      val collectorRegistry = mock[CollectorRegistry]
      val filter = new MetricsFilter(configuration) {
        override val metrics = List(
          CounterRequestMetricBuilder.build(collectorRegistry, DefaultPlayUnmatchedDefaults)
        )
      }

      val rh = FakeRequest("GET", "/test")
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      samples.size() mustBe 0 // expect no metrics
    }
  }
} 
Example 87
Source File: StatusCounterFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import io.prometheus.client.CollectorRegistry
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.verify
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{MustMatchers, WordSpec}
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.mvc.Results
import play.api.test.Helpers.stubControllerComponents
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class StatusCounterFilterSpec extends WordSpec with MustMatchers with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite {

  private implicit val mat = app.materializer
  private val configuration = mock[Configuration]

  "Filter constructor" should {
    "Add a counter to the prometheus registry" in {
      val collectorRegistry = mock[CollectorRegistry]
      new StatusCounterFilter(collectorRegistry, configuration)
      verify(collectorRegistry).register(any())
    }
  }

  "Apply method" should {
    "Count the requests with status" in {
      val filter = new StatusCounterFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest()
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      samples.get(0).value mustBe 1.0
      samples.get(0).labelValues must have size 1
      samples.get(0).labelValues.get(0) mustBe "200"
    }
  }

} 
Example 88
Source File: StatusAndRouteLatencyFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.metrics.DefaultPlayUnmatchedDefaults
import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import io.prometheus.client.CollectorRegistry
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.verify
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{MustMatchers, WordSpec}
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.libs.typedmap.TypedMap
import play.api.mvc.Results
import play.api.routing.{HandlerDef, Router}
import play.api.test.Helpers.stubControllerComponents
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class StatusAndRouteLatencyFilterSpec extends WordSpec with MustMatchers with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite  {

  private implicit val mat = app.materializer
  private val configuration = mock[Configuration]

  "Filter constructor" should {
    "Add a histogram to the prometheus registry" in {
      val collectorRegistry = mock[CollectorRegistry]
      new StatusAndRouteLatencyFilter(collectorRegistry, configuration)
      verify(collectorRegistry).register(any())
    }
  }

  "Apply method" should {
    "Measure the latency" in {
      val filter = new StatusAndRouteLatencyFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest().withAttrs( TypedMap(
        Router.Attrs.HandlerDef -> HandlerDef(null, null, "testController", "test", null, "GET", "/path", null ,null)
      ))
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(samples.size() - 2)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 5
      countSample.labelValues.get(0) mustBe "test"
      countSample.labelValues.get(1) mustBe "200"
      countSample.labelValues.get(2) mustBe "testController"
      countSample.labelValues.get(3) mustBe "/path"
      countSample.labelValues.get(4) mustBe "GET"
    }

    "Measure the latency for an unmatched route" in {
      val filter = new StatusAndRouteLatencyFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest()
      val action = new MockController(stubControllerComponents()).error

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(samples.size() - 2)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 5
      countSample.labelValues.get(0) mustBe DefaultPlayUnmatchedDefaults.UnmatchedRouteString
      countSample.labelValues.get(1) mustBe "404"
      countSample.labelValues.get(2) mustBe DefaultPlayUnmatchedDefaults.UnmatchedControllerString
      countSample.labelValues.get(3) mustBe DefaultPlayUnmatchedDefaults.UnmatchedPathString
      countSample.labelValues.get(4) mustBe DefaultPlayUnmatchedDefaults.UnmatchedVerbString
    }
  }
} 
Example 89
Source File: LatencyFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import io.prometheus.client.CollectorRegistry
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class LatencyFilterSpec extends PlaySpec with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite {

  val configuration = mock[Configuration]

  "Filter constructor" should {
    "Add a histogram to the prometheus registry" in {
      implicit val mat = app.materializer
      val collectorRegistry = mock[CollectorRegistry]
      new LatencyFilter(collectorRegistry, configuration)
      verify(collectorRegistry).register(any())
    }
  }

  "Apply method" should {
    "Measure the latency" in {
      implicit val mat = app.materializer
      val filter = new LatencyFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest()
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(samples.size() - 2)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 0
    }
  }

} 
Example 90
Source File: StatusAndRouteCounterFilterSpec.scala    From play-prometheus-filters   with MIT License 5 votes vote down vote up
package com.github.stijndehaes.playprometheusfilters.filters

import com.github.stijndehaes.playprometheusfilters.metrics.DefaultPlayUnmatchedDefaults
import com.github.stijndehaes.playprometheusfilters.mocks.MockController
import io.prometheus.client.CollectorRegistry
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.verify
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{MustMatchers, WordSpec}
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import play.api.libs.typedmap.TypedMap
import play.api.mvc.Results
import play.api.routing.{HandlerDef, Router}
import play.api.test.Helpers.stubControllerComponents
import play.api.test.{DefaultAwaitTimeout, FakeRequest, FutureAwaits}

import scala.concurrent.ExecutionContext.Implicits.global

class StatusAndRouteCounterFilterSpec extends WordSpec with MustMatchers with MockitoSugar with Results with DefaultAwaitTimeout with FutureAwaits with GuiceOneAppPerSuite  {

  private implicit val mat = app.materializer
  private val configuration = mock[Configuration]

  "Filter constructor" should {
    "Add a histogram to the prometheus registry" in {
      val collectorRegistry = mock[CollectorRegistry]
      new StatusAndRouteLatencyFilter(collectorRegistry, configuration)
      verify(collectorRegistry).register(any())
    }
  }

  "Apply method" should {
    "Measure the count" in {
      val filter = new StatusAndRouteCounterFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest().withAttrs( TypedMap(
        Router.Attrs.HandlerDef -> HandlerDef(null, null, "testController", "test", null, "GET", "/path", null ,null)
      ))
      val action = new MockController(stubControllerComponents()).ok

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(0)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 5
      countSample.labelValues.get(0) mustBe "test"
      countSample.labelValues.get(1) mustBe "200"
      countSample.labelValues.get(2) mustBe "testController"
      countSample.labelValues.get(3) mustBe "/path"
      countSample.labelValues.get(4) mustBe "GET"
    }

    "Measure the count for an unmatched route" in {
      val filter = new StatusAndRouteCounterFilter(mock[CollectorRegistry], configuration)
      val rh = FakeRequest()
      val action = new MockController(stubControllerComponents()).error

      await(filter(action)(rh).run())

      val metrics = filter.metrics(0).metric.collect()
      metrics must have size 1
      val samples = metrics.get(0).samples
      //this is the count sample
      val countSample = samples.get(0)
      countSample.value mustBe 1.0
      countSample.labelValues must have size 5
      countSample.labelValues.get(0) mustBe DefaultPlayUnmatchedDefaults.UnmatchedRouteString
      countSample.labelValues.get(1) mustBe "404"
      countSample.labelValues.get(2) mustBe DefaultPlayUnmatchedDefaults.UnmatchedControllerString
      countSample.labelValues.get(3) mustBe DefaultPlayUnmatchedDefaults.UnmatchedPathString
      countSample.labelValues.get(4) mustBe DefaultPlayUnmatchedDefaults.UnmatchedVerbString
    }
  }

} 
Example 91
Source File: BoardSpec.scala    From slab   with Apache License 2.0 5 votes vote down vote up
package com.criteo.slab.core

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import org.mockito.Mockito._
import shapeless.HNil

class BoardSpec extends FlatSpec with Matchers with MockitoSugar {
  val box1 = mock[Box[_]]
  when(box1.title) thenReturn "box 1"
  val box2 = mock[Box[_]]
  when(box2.title) thenReturn "box 2"

  "constructor" should "require that boxes and layout are correctly defined" in {
    intercept[IllegalArgumentException] {
      Board(
        "a broken board",
        box1 :: HNil,
        (views, _) => views.values.head,
        Layout(
          Column(
            100,
            Row("row", 10, box2 :: Nil)
          )
        )
      )
    }
    intercept[IllegalArgumentException] {
      Board(
        "a broken board",
        box1 :: box2 :: HNil,
        (views, _) => views.values.head,
        Layout(
          Column(
            100,
            Row("row", 10, List.empty)
          )
        )
      )
    }
  }
} 
Example 92
Source File: OutputXMLMatchesInputXMLSpec.scala    From akka-xml-parser   with Apache License 2.0 5 votes vote down vote up
import akka.stream.scaladsl.{Keep, Source}
import akka.util.ByteString
import org.scalatest
import org.scalatest.concurrent.{Eventually, ScalaFutures}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterEach, Matchers}
import uk.gov.hmrc.akka.xml._
import uk.gov.hmrc.play.test.UnitSpec

import scala.concurrent.ExecutionContext.Implicits.global

class OutputXMLMatchesInputXMLSpec extends UnitSpec with BeforeAndAfterEach with Matchers with ScalaFutures with MockitoSugar with Eventually with XMLParserFixtures {

  val inputXml                        = "<Address xmlns=\"http://www.govtalk.gov.uk/CM/address\"><Line>Line 1</Line><Line>Line 2</Line><PostCode>Tf3 4NT</PostCode></Address>"
  val inputXmlWithSelfClosingElement  = "<Address xmlns=\"http://www.govtalk.gov.uk/CM/address\"><Line>Line 1</Line><Line>Line 2</Line><Line/><PostCode>Tf3 4NT</PostCode></Address>"
  val inputXmlWithBlankElement        = "<Address xmlns=\"http://www.govtalk.gov.uk/CM/address\"><Line>Line 1</Line><Line>Line 2</Line><Line></Line><PostCode>Tf3 4NT</PostCode></Address>"

  val f = fixtures

  def xpathValue(xmlElements: Set[XMLElement], xPath: Seq[String]): Option[String] = xmlElements.collectFirst { case XMLElement(`xPath`, _, Some(xpathValue)) => xpathValue }

  def parseAndCompare(inputXml: String): scalatest.Assertion = {
    val inputXmlSource: Source[ByteString, _] = Source.single(ByteString(inputXml))

    await(
      for {
        parsedXmlElements <- inputXmlSource
          .via(CompleteChunkStage.parser())
          .via(ParsingStage.parser(Seq(XMLExtract(Seq("Address"), Map.empty, true))))
          .via(f.flowXMLElements)
          .toMat(f.collectXMLElements)(Keep.right)
          .run()(f.mat)

        parsedXml = xpathValue(parsedXmlElements, Seq("Address"))
      } yield {

        val outputXml = parsedXml.get

        println(s"INPUT  XML = $inputXml")
        println(s"OUTPUT XML = $outputXml")
        println()

        outputXml shouldBe inputXml
      }
    )
  }


  "The output XML" should {
    "match the input XML" when {
      "blank elements *** ARE *** present"            in parseAndCompare(inputXmlWithBlankElement)
      "self closing elements are *** NOT *** present" in parseAndCompare(inputXml)
      "self closing elements *** ARE *** present"     in parseAndCompare(inputXmlWithSelfClosingElement)
    }
  }


} 
Example 93
Source File: KuduSinkUnitTest.scala    From kafka-examples   with Apache License 2.0 5 votes vote down vote up
package com.cloudera.streaming.refapp.kudu

import org.apache.kudu.spark.kudu.KuduContext
import org.apache.spark.sql.DataFrame
import org.mockito.Mockito._
import org.scalatest._
import org.scalatest.mockito.MockitoSugar

class KuduSinkUnitTest extends FunSuite with MockitoSugar {

  private val frame = mock[DataFrame]

  private def setupKuduContextMock(kuduContext: KuduContext, failTimes: Int): KuduContext = {
    if (failTimes > 0) {
      val stubber = doThrow(new RuntimeException)
      for (_ <- 2 to failTimes) {
        stubber.doThrow(new RuntimeException)
      }
      stubber.doCallRealMethod()
        .when(kuduContext).upsertRows(frame, "table")
    }
    kuduContext
  }

  test("kudu upsert fails, retries once") {
    val helper = new KuduSinkWithMockedContext(setupKuduContextMock(mock[KuduContext], failTimes = 1), 1)

    helper.sink.addBatch(0, frame)
    assert(helper.initialized == 1, "context should be initialized once")
  }

  test("kudu upsert fails twice, retries once, fails") {
    val helper = new KuduSinkWithMockedContext(setupKuduContextMock(mock[KuduContext], failTimes = 2), 1)

    intercept[RuntimeException] {
      helper.sink.addBatch(0, frame)
    }
    assert(helper.initialized == 1, "context should be initialized once")
  }

  test("kudu upsert fails 3 times, retries 3 times") {
    val helper = new KuduSinkWithMockedContext(setupKuduContextMock(mock[KuduContext], failTimes = 3), 3)
    helper.sink.addBatch(0, frame)
    assert(helper.initialized == 3, "context should be initialized three times")
  }

  test("kudu upsert fails 3 times, retries 4 times") {
    val helper = new KuduSinkWithMockedContext(setupKuduContextMock(mock[KuduContext], failTimes = 3), 4)
    helper.sink.addBatch(0, frame)
    assert(helper.initialized == 3, "context should be initialized only three times")
  }

}

class KuduSinkWithMockedContext(kuduContext: KuduContext, retries: Int) {

  // KuduSink constructor inits once
  var initialized = -1

  private def initKuduConext: KuduContext = {
    initialized += 1
    kuduContext
  }

  val sink = new KuduSink(initKuduConext, Map(
    "kudu.table" -> "table",
    "kudu.master" -> "master",
    "retries" -> retries.toString))
} 
Example 94
Source File: CoordinatorSpec.scala    From cave   with MIT License 5 votes vote down vote up
package actors

import akka.actor.{ActorSystem, PoisonPill, Props}
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import com.cave.metrics.data._
import init.AwsWrapper
import init.AwsWrapper.WorkItem
import org.mockito.Mockito._
import org.mockito.Matchers._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
import org.specs2.matcher.ShouldMatchers

import scala.concurrent.{Future, ExecutionContext}
import scala.util.Success

class CoordinatorSpec extends TestKit(ActorSystem()) with WordSpecLike with ShouldMatchers with ImplicitSender with BeforeAndAfterAll with AlertJsonData with MockitoSugar {

  val mockAwsWrapper = mock[AwsWrapper]
  val mockDataManager = mock[CacheDataManager]

  override def afterAll() = {
    system.shutdown()
  }

  "A coordinator" must {

    "create schedulers for all enabled alerts" in {

      val SomeId = "1234"
      val AnotherId = "4321"
      val OtherId = "12345"

      val alerts = List(
        Schedule(OrgName, Some(TeamName), None, NotificationUrl, Alert(Some(SomeId), AlertDescription, AlertEnabled, AlertPeriod, AlertCondition, Some(AlertHandbookUrl), Some(AlertRouting))),
        Schedule(OrgName, Some(TeamName), None, NotificationUrl, Alert(Some(AnotherId), AlertDescription, AlertEnabled, AlertPeriod, AlertCondition, Some(AlertHandbookUrl), Some(AlertRouting))))

      val moreAlerts = List(
        Schedule(TeamName, None, None, NotificationUrl, Alert(Some(OtherId), AlertDescription, AlertEnabled, AlertPeriod, AlertCondition, Some(AlertHandbookUrl), Some(AlertRouting)))
      )

      when(mockDataManager.getEnabledAlerts()).thenReturn(Success(Map(OrgName -> alerts, TeamName -> moreAlerts)))
      when(mockAwsWrapper.receiveMessages()(any[ExecutionContext])).thenReturn(Future.successful(List.empty[WorkItem]))
      val coordinator = TestActorRef(Props(new Coordinator(mockAwsWrapper, mockDataManager) {
        override def createScheduler(schedule: Schedule) = {}
      }))

      coordinator ! Coordinator.StatusRequest

      expectMsgPF() {
        case Coordinator.StatusResponse(cache, schedules) =>
          cache.schedulesByOrganization should haveSize(2)
          val forOrgName = cache.schedulesByOrganization(OrgName)
          forOrgName should haveSize(2)
          val forTeamName = cache.schedulesByOrganization(TeamName)
          forTeamName should haveSize(1)

          schedules should haveSize(3)

        case _ => fail("Unexpected message received.")
      }


      coordinator ! PoisonPill
      watch(coordinator)
      expectTerminated(coordinator)
    }
  }
} 
Example 95
Source File: ConverterFactorySpec.scala    From cave   with MIT License 5 votes vote down vote up
package worker.converter

import com.typesafe.config.Config
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito._
import scala.collection.JavaConverters._

class ConverterFactorySpec extends FlatSpec with MockitoSugar with Matchers {

  val mockConfig = mock[Config]

  "A converter factory" should "build from configuration" in {
    when(mockConfig.getStringList("list")).thenReturn(List("worker.converter.PagerDutyConverter").asJava)
    when(mockConfig.getString("default")).thenReturn("worker.converter.JsonConverter")

    val converterFactory = new ConverterFactory(mockConfig)

    converterFactory.converters.toList.size should be(1)
    converterFactory.converters.head.isInstanceOf[PagerDutyConverter] should be(true)
    converterFactory.default.isInstanceOf[JsonConverter] should be(true)
  }
} 
Example 96
Source File: CoordinatorSpec.scala    From cave   with MIT License 5 votes vote down vote up
package worker

import akka.actor._
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import com.cave.metrics.data._
import init.AwsWrapper
import init.AwsWrapper.WorkItem
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

import scala.collection.mutable
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Success

class CoordinatorSpec extends TestKit(ActorSystem()) with WordSpecLike with Matchers with ImplicitSender with BeforeAndAfterAll with AlertJsonData with MockitoSugar {

  def fakeCoordinator(awsWrapper: AwsWrapper, mockCheckers: mutable.Map[ActorRef, WorkItem]): Props = Props(new Coordinator(awsWrapper, shouldSendHistory = false) {

    override val checkers = mockCheckers
    override def createNotifier(item: WorkItem): Unit = { }
  })

  def fakeChecker(parentCoordinator: ActorRef): Props = Props(new Actor {
    def receive = {
      case "abort" =>
        parentCoordinator ! Checker.Aborted("Boom!")
        context stop self
      case "true" =>
        parentCoordinator ! Checker.Done(Success(true))
        context stop self
      case "false" =>
        parentCoordinator ! Checker.Done(Success(false))
        context stop self
    }
  })

  val mockAwsWrapper = mock[AwsWrapper]
  val mockDataManager = mock[CacheDataManager]

  override def afterAll() = {
    system.shutdown()
  }

  "A coordinator" must {

    "return its status" in {
      when(mockAwsWrapper.receiveMessages()(any[ExecutionContext])).thenReturn(Future.successful(List.empty[WorkItem]))

      val checkers = mutable.Map.empty[ActorRef, WorkItem]
      val mockItem = mock[WorkItem]

      val coordinator = TestActorRef(fakeCoordinator(mockAwsWrapper, checkers))

      val checker1 = TestActorRef(fakeChecker(coordinator))
      val checker2 = TestActorRef(fakeChecker(coordinator))
      val checker3 = TestActorRef(fakeChecker(coordinator))
      val checker4 = TestActorRef(fakeChecker(coordinator))
      val checker5 = TestActorRef(fakeChecker(coordinator))
      val checker6 = TestActorRef(fakeChecker(coordinator))

      checkers ++= mutable.Map(checker1 -> mockItem, checker2 -> mockItem, checker3 -> mockItem,
        checker4 -> mockItem, checker5 -> mockItem, checker6 -> mockItem)

      checker1 ! "abort"
      checker2 ! "abort"
      checker3 ! "false"
      checker4 ! "false"
      checker5 ! "false"
      checker6 ! "true"

      coordinator ! Coordinator.StatusRequest

      expectMsgPF() {
        case Coordinator.StatusResponse(currentlyActive, aborted, totalProcessed, noOfAlarmsTriggered) =>
          currentlyActive should be(0)
          aborted should be(2)
          noOfAlarmsTriggered should be(1)
          totalProcessed should be(4)
        case _ => fail("Unexpected message received.")
      }

      coordinator ! PoisonPill
      watch(coordinator)
      expectTerminated(coordinator)
    }
  }
} 
Example 97
Source File: CheckerSpec.scala    From cave   with MIT License 5 votes vote down vote up
package worker

import akka.actor.{ActorSystem, Props}
import akka.testkit.TestKit
import com.cave.metrics.data.evaluator.DataFetcher
import com.cave.metrics.data.influxdb.InfluxClientFactory
import com.cave.metrics.data.{AlertJsonData, Check}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Try, Success}

class CheckerSpec extends TestKit(ActorSystem()) with WordSpecLike with BeforeAndAfterAll with AlertJsonData with MockitoSugar {

  override def afterAll() = {
    system.shutdown()
  }

  final val SomeReason = "BOOM!"
  val mockClientFactory = mock[InfluxClientFactory]

  def fakeChecker(check: Check): Props = Props(new Checker(check) {
    override def fetcher = new DataFetcher(mockClientFactory)

    override def run(check: Check)(implicit ec: ExecutionContext): Future[Try[Boolean]] = {
      if (check.schedule.alert.description == AlertDescription) Future.successful(Success(true))
      else if (check.schedule.alert.description == AlertFiveDescription) Future.successful(Success(false))
      else Future.failed(new RuntimeException(SomeReason))
    }
  })


  "A checker" must {
    "send Done(true) if an alarm condition has been detected" in {
      val checker = system.actorOf(Props(new StepParent(fakeChecker(InsufficientOrders), testActor)), "alarm")

      expectMsg(Checker.Done(alarm = Success(true)))
      watch(checker)
      expectTerminated(checker)
    }

    "send Done(false) if no alarm condition has been detected" in {
      val checker = system.actorOf(Props(new StepParent(fakeChecker(InsufficientOrdersFive), testActor)), "notAlarm")

      expectMsg(Checker.Done(alarm = Success(false)))
      watch(checker)
      expectTerminated(checker)
    }

    "properly finish in case of error" in {
      val checker = system.actorOf(Props(new StepParent(fakeChecker(OrdersLessThanPredicted), testActor)), "error")

      expectMsg(Checker.Aborted(SomeReason))
      watch(checker)
      expectTerminated(checker)
    }
  }
} 
Example 98
Source File: InfluxMetricSpec.scala    From cave   with MIT License 5 votes vote down vote up
package com.cave.metrics.data.influxdb

import com.cave.metrics.data.Metric
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}

class InfluxMetricSpec extends FlatSpec with Matchers with MockitoSugar {

  private val METRIC_NAME = "orders"
  private val TIMESTAMP = 1L
  private val VALUE = 1.0

  private val ORG1_NAME = "org1"
  private val ORG2_NAME = "org2"

  private def withAccountTag(orgName: String, tags: Map[String, String]) = tags ++ Map(Metric.Organization -> orgName)

  private val NO_TAGS = Map.empty[String, String]

  private val TAG1_KEY = "shipTo"
  private val TAG1_VALUE = "US"
  private val ONE_TAG = Map(TAG1_KEY -> TAG1_VALUE)

  private val TAG2_KEY = "service"
  private val TAG2_VALUE = "svc-important"
  private val TWO_TAGS = Map(TAG1_KEY -> TAG1_VALUE, TAG2_KEY -> TAG2_VALUE)

  "an InfluxMetric" should "be created from a CAVE metric without tags" in {
    val result = InfluxMetric.prepareRequests(Seq(Metric(METRIC_NAME, TIMESTAMP, VALUE, withAccountTag(ORG1_NAME, NO_TAGS))))

    result.size should be(1)
    val org = result.get(ORG1_NAME)
    org should be(Some(s"""[{"name":"$METRIC_NAME","columns":["time","value"],"points":[[$TIMESTAMP,$VALUE]]}]"""))
  }

  it should "be created from a CAVE metric with one tag" in {
    val result = InfluxMetric.prepareRequests(Seq(Metric(METRIC_NAME, TIMESTAMP, VALUE, withAccountTag(ORG1_NAME, ONE_TAG))))

    result.size should be(1)
    val org = result.get(ORG1_NAME)
    org should be(Some(s"""[{"name":"$METRIC_NAME","columns":["time","value","$TAG1_KEY"],"points":[[$TIMESTAMP,$VALUE,"$TAG1_VALUE"]]}]"""))
  }

  it should "be created from a CAVE metric with two tags" in {
    val result = InfluxMetric.prepareRequests(Seq(Metric(METRIC_NAME, TIMESTAMP, VALUE, withAccountTag(ORG1_NAME, TWO_TAGS))))

    result.size should be(1)
    val org = result.get(ORG1_NAME)
    org should be(Some(s"""[{"name":"$METRIC_NAME","columns":["time","value","$TAG1_KEY","$TAG2_KEY"],"points":[[$TIMESTAMP,$VALUE,"$TAG1_VALUE","$TAG2_VALUE"]]}]"""))
  }

  it should "be separated by org name" in {
    val result = InfluxMetric.prepareRequests(Seq(
      Metric(METRIC_NAME, TIMESTAMP, VALUE, withAccountTag(ORG1_NAME, NO_TAGS)),
      Metric(METRIC_NAME, TIMESTAMP, VALUE, withAccountTag(ORG2_NAME, ONE_TAG))
    ))

    result.size should be(2)

    result.get(ORG1_NAME) should be(Some(s"""[{"name":"$METRIC_NAME","columns":["time","value"],"points":[[$TIMESTAMP,$VALUE]]}]"""))
    result.get(ORG2_NAME) should be(Some(s"""[{"name":"$METRIC_NAME","columns":["time","value","$TAG1_KEY"],"points":[[$TIMESTAMP,$VALUE,"$TAG1_VALUE"]]}]"""))
  }

  it should "not be parsed if the Organization tag is missing" in {
    try {
      InfluxMetric.prepareRequests(Seq(Metric(METRIC_NAME, TIMESTAMP, VALUE, ONE_TAG)))
      fail("Expected to throw an exception")
    } catch {
      case e: RuntimeException =>
        e.getMessage should be(InfluxMetric.MissingAccountTagMessage)
    }
  }
} 
Example 99
Source File: GlobalMQSerializerSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.communication.mq.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json._

import io.deepsense.commons.StandardSpec
import io.deepsense.commons.models.Entity
import io.deepsense.deeplang.DOperable
import io.deepsense.deeplang.doperables.ColumnsFilterer
import io.deepsense.graph.Node
import io.deepsense.models.json.workflow.ExecutionReportJsonProtocol._
import io.deepsense.models.workflows.{EntitiesMap, ExecutionReport, Workflow}
import io.deepsense.reportlib.model.factory.ReportContentTestFactory
import io.deepsense.workflowexecutor.communication.message.global._
import io.deepsense.workflowexecutor.communication.mq.json.Global.GlobalMQSerializer

class GlobalMQSerializerSpec
  extends StandardSpec
  with MockitoSugar {

    "GlobalMQSerializer" should {
      "serialize ExecutionReport" in {
        val executionReport = ExecutionReport(
          Map(Node.Id.randomId -> io.deepsense.graph.nodestate.Draft()),
          EntitiesMap(
            Map[Entity.Id, DOperable](
              Entity.Id.randomId -> new ColumnsFilterer),
            Map(Entity.Id.randomId -> ReportContentTestFactory.someReport)),
          None)

        serialize(executionReport) shouldBe asBytes(JsObject(
          "messageType" -> JsString("executionStatus"),
          "messageBody" -> executionReport.toJson))
      }

      "serialize Launch messages" in {
        val workflowId = Workflow.Id.randomId
        val nodesToExecute = Vector(Workflow.Id.randomId, Workflow.Id.randomId, Workflow.Id.randomId)
        val jsNodesToExecute = JsArray(nodesToExecute.map(id => JsString(id.toString)))

        val outMessage = JsObject(
          "messageType" -> JsString("launch"),
          "messageBody" -> JsObject(
            "workflowId" -> JsString(workflowId.toString),
            "nodesToExecute" -> jsNodesToExecute
          )
        )

        val serializedMessage = serialize(Launch(workflowId, nodesToExecute.toSet))
        serializedMessage shouldBe asBytes(outMessage)
      }

      "serialize Heartbeat messages" in {
        val workflowId = "foo-workflow"
        val outMessage = JsObject(
          "messageType" -> JsString("heartbeat"),
          "messageBody" -> JsObject(
            "workflowId" -> JsString(workflowId)))
        serialize(Heartbeat(workflowId)) shouldBe asBytes(outMessage)
      }
      "serialize PoisonPill messages" in {
        val outMessage = JsObject(
          "messageType" -> JsString("poisonPill"),
          "messageBody" -> JsObject())
        serialize(PoisonPill()) shouldBe asBytes(outMessage)
      }
      "serialize Ready messages" in {
        val sessionId = "foo-session"
        val outMessage = JsObject(
          "messageType" -> JsString("ready"),
          "messageBody" -> JsObject(
            "sessionId" -> JsString(sessionId)))
        serialize(Ready(sessionId)) shouldBe asBytes(outMessage)
      }
    }

    private def asBytes(jsObject: JsObject): Array[Byte] =
      jsObject.compactPrint.getBytes(StandardCharsets.UTF_8)

    private def serialize(message: Any): Array[Byte] =
      GlobalMQSerializer.serializeMessage(message)
} 
Example 100
Source File: GlobalMQDeserializerSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.communication.mq.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json.{JsArray, JsObject, JsString}

import io.deepsense.commons.StandardSpec
import io.deepsense.models.workflows.Workflow
import io.deepsense.workflowexecutor.communication.message.global._
import io.deepsense.workflowexecutor.communication.mq.json.Global.GlobalMQDeserializer

class GlobalMQDeserializerSpec
  extends StandardSpec
  with MockitoSugar {

  "GlobalMQDeserializer" should {
    "deserialize Launch messages" in {
      val workflowId = Workflow.Id.randomId
      val nodesToExecute = Vector(Workflow.Id.randomId, Workflow.Id.randomId, Workflow.Id.randomId)
      val jsNodesToExecute = JsArray(nodesToExecute.map(id => JsString(id.toString)))

      val rawMessage = JsObject(
        "messageType" -> JsString("launch"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString),
          "nodesToExecute" -> jsNodesToExecute
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage)
      readMessage shouldBe Launch(workflowId, nodesToExecute.toSet)
    }

    "deserialize Heartbeat messages" in {
      val workflowId = "foo-workflow"
      val rawMessage = JsObject(
        "messageType" -> JsString("heartbeat"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId)))
      serializeAndRead(rawMessage) shouldBe Heartbeat(workflowId)
    }
    "deserialize PoisonPill messages" in {
      val rawMessage = JsObject(
        "messageType" -> JsString("poisonPill"),
        "messageBody" -> JsObject())
      serializeAndRead(rawMessage) shouldBe PoisonPill()
    }
    "deserialize Ready messages" in {
      val sessionId = "foo-session"
      val rawMessage = JsObject(
        "messageType" -> JsString("ready"),
        "messageBody" -> JsObject(
          "sessionId" -> JsString(sessionId)))
      serializeAndRead(rawMessage) shouldBe Ready(sessionId)
    }
  }

  private def serializeAndRead(
    rawMessage: JsObject): Any = {
    val bytes = rawMessage.compactPrint.getBytes(StandardCharsets.UTF_8)
    GlobalMQDeserializer.deserializeMessage(bytes)
  }
} 
Example 101
Source File: DatasourceListJsonProtocolSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.commons.json.envelope

import org.joda.time.DateTime
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.api.datasourcemanager.model.{AccessLevel, Datasource, DatasourceParams, DatasourceType}
import io.deepsense.commons.datasource.DatasourceTestData
import io.deepsense.commons.json.datasources.DatasourceListJsonProtocol

class DatasourceListJsonProtocolSpec
  extends WordSpec
  with MockitoSugar
  with Matchers {

  val uuid = "123e4567-e89b-12d3-a456-426655440000"
  val externalFile = DatasourceType.EXTERNALFILE

  val dsList = List(DatasourceTestData.multicharSeparatorLibraryCsvDatasource)

  "DatasourceJsonProtocolSpec" should {
    "serialize and deserialize single datasource" in {
      val datasourcesJson = DatasourceListJsonProtocol.toString(dsList)
      val asString = datasourcesJson.toString
      val datasources = DatasourceListJsonProtocol.fromString(asString)
      info(s"Datasource: $datasources, json: $asString")
      datasources should contain theSameElementsAs dsList
    }

    "serialize no datasource" in {
      val datasourcesJson = DatasourceListJsonProtocol.toString(List.empty[Datasource])
      datasourcesJson shouldBe "[]"
    }
  }
} 
Example 102
Source File: EntitiesMapSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.models.workflows

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.commons.models.Entity
import io.deepsense.deeplang.doperables.dataframe.DataFrame
import io.deepsense.reportlib.model.ReportContent

class EntitiesMapSpec
  extends WordSpec
  with Matchers
  with MockitoSugar {

  "EntitiesMap" should {
    "be correctly created from results and reports" in {

      val entity1Id = Entity.Id.randomId
      val doperable1 = new DataFrame()
      val report1 = mock[ReportContent]

      val entity2Id = Entity.Id.randomId
      val doperable2 = new DataFrame()

      val results = Map(entity1Id -> doperable1, entity2Id -> doperable2)
      val reports = Map(entity1Id -> report1)

      EntitiesMap(results, reports) shouldBe EntitiesMap(Map(
        entity1Id -> EntitiesMap.Entry(
          "io.deepsense.deeplang.doperables.dataframe.DataFrame", Some(report1)),
        entity2Id -> EntitiesMap.Entry(
          "io.deepsense.deeplang.doperables.dataframe.DataFrame", None)
      ))
    }
  }
} 
Example 103
Source File: NodeStateWithResultsSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.models.workflows

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.commons.models.Entity
import io.deepsense.deeplang.exceptions.DeepLangException
import io.deepsense.deeplang.inference.InferenceWarnings
import io.deepsense.deeplang.{DKnowledge, DOperable}
import io.deepsense.graph.NodeInferenceResult
import io.deepsense.reportlib.model.ReportContent

class NodeStateWithResultsSpec extends WordSpec with Matchers with MockitoSugar {

  "NodeStateWithResults" should {

    "copy knowledge, keep warnings and clear errors for nonempty DOperable list" in {
      val draftNode = draftNodeState
      val (entityIds, operables, reportsMap, operablesMap) = executionResultFixture(2)
      val finished = draftNode.enqueue.start.finish(entityIds, reportsMap, operablesMap)

      finished.nodeState.isCompleted shouldBe true
      finished.knowledge shouldBe Some(NodeInferenceResult(
        operables.map(DKnowledge(_)).toVector,
        draftNode.knowledge.get.warnings,
        Vector()))
    }
    "copy knowledge, keep warnings and clear errors for empty DOperable list" in {
      val draftNode = draftNodeState
      val (entityIds, operables, reportsMap, operablesMap) = executionResultFixture(0)
      val finished = draftNode.enqueue.start.finish(entityIds, reportsMap, operablesMap)

      finished.nodeState.isCompleted shouldBe true
      finished.knowledge shouldBe Some(NodeInferenceResult(
        Vector(),
        draftNode.knowledge.get.warnings,
        Vector()))
    }
  }

  private def draftNodeState = {
    NodeStateWithResults.draft.withKnowledge(
      NodeInferenceResult(
        Vector(DKnowledge(mock[DOperable])),
        mock[InferenceWarnings],
        Vector(mock[DeepLangException])))
  }

  private def executionResultFixture(dOperableCount: Int):
      (Seq[Entity.Id], Seq[DOperable], Map[Entity.Id, ReportContent], Map[Entity.Id, DOperable]) = {
    val entityIds = (1 to dOperableCount).map(_ => Entity.Id.randomId).toList
    val operables = entityIds.map(_ => mock[DOperable])
    val reportsMap = entityIds.map(id => id -> mock[ReportContent]).toMap
    val operablesMap = entityIds.zip(operables).toMap
    (entityIds, operables, reportsMap, operablesMap)
  }
} 
Example 104
Source File: AbstractParamSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.deeplang.params

import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
import spray.json.{JsObject, JsValue}

abstract class AbstractParamSpec[T, U <: Param[T]]
  extends WordSpec
  with Matchers
  with MockitoSugar {

  def className: String

  def paramFixture: (U, JsValue)  // param + its json description

  def valueFixture: (T, JsValue)  // value + its json description

  val defaultValue: T = valueFixture._1

  def serializeDefaultValue(default: T): JsValue = paramFixture._1.valueToJson(default)

  className should {
    "serialize itself to JSON" when {
      "default value is not provided" in {
        val (param, expectedJson) = paramFixture
        param.toJson(maybeDefault = None) shouldBe expectedJson
      }
      "default value is provided" in {
        val (param, expectedJson) = paramFixture
        val expectedJsonWithDefault = JsObject(
          expectedJson.asJsObject.fields + ("default" -> serializeDefaultValue(defaultValue))
        )
        param.toJson(maybeDefault = Some(defaultValue)) shouldBe expectedJsonWithDefault
      }
    }
  }

  it should {
    "serialize value to JSON" in {
      val param = paramFixture._1
      val (value, expectedJson) = valueFixture
      param.valueToJson(value) shouldBe expectedJson
    }
  }

  it should {
    "deserialize value from JSON" in {
      val param = paramFixture._1
      val (expectedValue, valueJson) = valueFixture
      val extractedValue = param.valueFromJson(valueJson)
      extractedValue shouldBe expectedValue
    }
  }
} 
Example 105
Source File: WrappersDefaultValidationSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.deeplang.params.wrappers.spark

import org.apache.spark.ml
import org.apache.spark.ml.param._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

class WrappersDefaultValidationSpec
  extends WordSpec
  with Matchers
  with MockitoSugar {

  class ExampleSparkParams extends ml.param.Params {
    override val uid: String = "id"
    val intSparkParam = new IntParam("", "name", "description")
    val floatSparkParam = new FloatParam("", "name", "description")
    val doubleSparkParam = new DoubleParam("", "name", "description")

    override def copy(extra: ParamMap): Params = ???
  }

  "IntParamWrapper" should {

    val intParamWrapper = new IntParamWrapper[ExampleSparkParams](
      "name",
      Some("description"),
      _.intSparkParam)

    "validate whole Int range" in {
      intParamWrapper.validate(Int.MinValue + 1) shouldBe empty
      intParamWrapper.validate(Int.MaxValue - 1) shouldBe empty
    }
    "reject fractional values" in {
      intParamWrapper.validate(Int.MinValue + 0.005) should have size 1
      intParamWrapper.validate(Int.MaxValue - 0.005) should have size 1
    }
  }

  "FloatParamWrapper" should {

    val floatParamWrapper = new FloatParamWrapper[ExampleSparkParams](
      "name",
      Some("description"),
      _.floatSparkParam)

    "validate whole Float range" in {
      floatParamWrapper.validate(Float.MinValue + 1) shouldBe empty
      floatParamWrapper.validate(Float.MaxValue - 1) shouldBe empty
    }
    "reject values out of Float range" in {
      floatParamWrapper.validate(Double.MinValue + 1) should have size 1
      floatParamWrapper.validate(Double.MaxValue - 1) should have size 1
    }
  }

  "DoubleParamWrapper" should {
    "validate whole Double range" in {
      val doubleParamWrapper = new DoubleParamWrapper[ExampleSparkParams](
        "name",
        Some("description"),
        _.doubleSparkParam)
      doubleParamWrapper.validate(Double.MinValue + 1) shouldBe empty
      doubleParamWrapper.validate(Double.MinValue - 1) shouldBe empty
    }
  }
} 
Example 106
Source File: ParamsWithSparkWrappersSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.deeplang.params.wrappers.spark

import org.apache.spark.ml
import org.apache.spark.ml.param._
import org.apache.spark.sql.types.StructType
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.deeplang.params.BooleanParam
import io.deepsense.deeplang.params.choice.{ChoiceParam, Choice}

class ParamsWithSparkWrappersSpec extends WordSpec
  with Matchers
  with MockitoSugar {

  import ParamsWithSparkWrappersSpec._

  "ParamsWithSparkWrappers" should {
    "calculate sparkParamWrappers" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass()
      paramsWithSparkWrappers.sparkParamWrappers shouldBe
        Array(paramsWithSparkWrappers.paramA, paramsWithSparkWrappers.paramB)
    }
    "return parameter values" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass().setParamA("a").setParamB(0.0)
      paramsWithSparkWrappers.sparkParamMap(
        paramsWithSparkWrappers.exampleSparkParams, StructType(Seq())).toSeq.toSet shouldBe
        Set(
          paramsWithSparkWrappers.exampleSparkParams.sparkParamA -> "a",
          paramsWithSparkWrappers.exampleSparkParams.sparkParamB -> 0)
    }
    "return wrappers nested in choice parameter values" in {
      val paramsWithSparkWrappers = ParamsWithSparkWrappersClass()
        .setChoice(OneParamChoiceWithWrappers().setParamC("c"))
      paramsWithSparkWrappers.sparkParamMap(
        paramsWithSparkWrappers.exampleSparkParams, StructType(Seq())).toSeq.toSet shouldBe
        Set(
          paramsWithSparkWrappers.exampleSparkParams.sparkParamC -> "c")
    }
  }
}

object ParamsWithSparkWrappersSpec {

  class ExampleSparkParams extends ml.param.Params {
    override val uid: String = "id"
    val sparkParamA = new Param[String]("", "paramA", "descA")
    val sparkParamB = new IntParam("", "paramB", "descB")
    val sparkParamC = new Param[String]("", "paramC", "descC")

    override def copy(extra: ParamMap): Params = ???
  }

  case class ParamsWithSparkWrappersClass() extends ParamsWithSparkWrappers {

    val exampleSparkParams = new ExampleSparkParams

    val paramA = new StringParamWrapper[ExampleSparkParams]("paramA", Some("descA"), _.sparkParamA)
    val paramB = new IntParamWrapper[ExampleSparkParams]("paramB", Some("descB"), _.sparkParamB)
    val choiceWithParamsInValues = new ChoiceParam[ChoiceWithWrappers]("choice", Some("descChoice"))
    val notWrappedParam = BooleanParam("booleanParamName", Some("booleanParamDescription"))

    val params: Array[io.deepsense.deeplang.params.Param[_]] =
      Array(paramA, paramB, choiceWithParamsInValues, notWrappedParam)

    def setParamA(v: String): this.type = set(paramA, v)
    def setParamB(v: Double): this.type = set(paramB, v)
    def setChoice(v: ChoiceWithWrappers): this.type = set(choiceWithParamsInValues, v)
  }

  sealed trait ChoiceWithWrappers extends Choice with ParamsWithSparkWrappers {
    override val choiceOrder: List[Class[_ <: ChoiceWithWrappers]] = List(
      classOf[OneParamChoiceWithWrappers],
      classOf[EmptyChoiceWithWrappers])
  }

  case class OneParamChoiceWithWrappers() extends ChoiceWithWrappers {
    val paramC = new StringParamWrapper[ExampleSparkParams]("paramC", Some("descC"), _.sparkParamC)
    def setParamC(v: String): this.type = set(paramC, v)

    override val name = "one param"
    val params: Array[io.deepsense.deeplang.params.Param[_]] = Array(paramC)
  }

  case class EmptyChoiceWithWrappers() extends ChoiceWithWrappers {
    override val name = "no params"
    val params: Array[io.deepsense.deeplang.params.Param[_]] = Array()
  }
} 
Example 107
Source File: DeeplangTestSupport.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.deeplang

import org.apache.spark.sql
import org.apache.spark.sql.types.StructType
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import io.deepsense.deeplang.catalogs.doperable.DOperableCatalog
import io.deepsense.deeplang.doperables.dataframe.DataFrame
import io.deepsense.deeplang.inference.InferContext

trait DeeplangTestSupport extends MockitoSugar {

  protected def createInferContext(
      dOperableCatalog: DOperableCatalog): InferContext = MockedInferContext(dOperableCatalog)

  protected def createSchema(fields: Array[String] = Array[String]()): StructType = {
    val schemaMock = mock[StructType]
    when(schemaMock.fieldNames).thenReturn(fields)
    schemaMock
  }

  protected def createSparkDataFrame(schema: StructType = createSchema()) = {
    val sparkDataFrameMock = mock[sql.DataFrame]
    when(sparkDataFrameMock.schema).thenReturn(schema)
    when(sparkDataFrameMock.toDF).thenReturn(sparkDataFrameMock)
    sparkDataFrameMock
  }

  protected def createDataFrame(fields: Array[String] = Array[String]()): DataFrame = {
    val schema = createSchema(fields)
    createDataFrame(schema)
  }

  protected def createDataFrame(schema: StructType): DataFrame = {
    val sparkDataFrameMock = createSparkDataFrame(schema)
    val dataFrameMock = mock[DataFrame]
    when(dataFrameMock.sparkDataFrame).thenReturn(sparkDataFrameMock)
    when(dataFrameMock.schema).thenReturn(Some(schema))
    dataFrameMock
  }
} 
Example 108
Source File: MockedInferContext.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.deeplang

import org.scalatest.mockito.MockitoSugar

import io.deepsense.commons.rest.client.datasources.{DatasourceClient, DatasourceInMemoryClientFactory}
import io.deepsense.deeplang.catalogs.doperable.DOperableCatalog
import io.deepsense.deeplang.doperables.dataframe.DataFrameBuilder
import io.deepsense.deeplang.inference.InferContext

object MockedInferContext extends MockitoSugar {

  def apply(dOperableCatalog: DOperableCatalog =
              CatalogRecorder.resourcesCatalogRecorder.catalogs.dOperableCatalog,
            dataFrameBuilder: DataFrameBuilder = mock[DataFrameBuilder],
            innerWorkflowParser: InnerWorkflowParser = mock[InnerWorkflowParser],
            datasourceClient: DatasourceClient = new DatasourceInMemoryClientFactory(List.empty).createClient
           ): InferContext = InferContext(
    dataFrameBuilder,
    dOperableCatalog,
    innerWorkflowParser,
    datasourceClient
  )

} 
Example 109
Source File: GraphKnowledgeSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.graph

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.deeplang.exceptions.DeepLangException

class GraphKnowledgeSpec
  extends WordSpec
  with MockitoSugar
  with Matchers {

  "GraphKnowledge" should {
    "return proper errors map" in {
      val node1Id = Node.Id.randomId
      val node2Id = Node.Id.randomId
      val inferenceResultsWithErrors = mock[NodeInferenceResult]
      val errors = Vector(mock[DeepLangException], mock[DeepLangException])
      when(inferenceResultsWithErrors.errors).thenReturn(errors)
      val inferenceResultsWithoutErrors = mock[NodeInferenceResult]
      when(inferenceResultsWithoutErrors.errors).thenReturn(Vector.empty)

      val knowledge = GraphKnowledge()
        .addInference(node1Id, inferenceResultsWithErrors)
        .addInference(node2Id, inferenceResultsWithoutErrors)

      knowledge.errors shouldBe Map(node1Id -> errors)
    }
  }
} 
Example 110
Source File: GraphTestSupport.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.graph

import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import io.deepsense.commons.datetime.DateTimeConverter
import io.deepsense.commons.exception.FailureDescription
import io.deepsense.commons.models.Entity
import io.deepsense.deeplang._
import io.deepsense.graph.DeeplangGraph.DeeplangNode

trait GraphTestSupport {
  self: MockitoSugar =>

  val op0To1 = {
    val m = mock[DOperation0To1[DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  val op1To1 = createOp1To1

  def createOp1To1: DOperation1To1[DOperable, DOperable] = {
    val m = mock[DOperation1To1[DOperable, DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  val op2To2 = {
    val m = mock[DOperation2To2[DOperable, DOperable, DOperable, DOperable]]
    when(m.sameAs(any())).thenReturn(true)
    m
  }

  

  val nodesSeq = generateNodes(op0To1, op1To1, op1To1, op1To1, op2To2)
  val nodeSet = nodesSeq.map(_._2).toSet
  val idA :: idB :: idC :: idD :: idE :: Nil = nodesSeq.map(_._1).toList
  val nodeA :: nodeB :: nodeC :: nodeD :: nodeE :: Nil = nodesSeq.map(_._2).toList

  val edgeList: List[Edge] = edges(idA, idB, idC, idD, idE)
  val edge1 :: edge2 :: edge3 :: edge4 :: edge5 :: Nil = edgeList
  val edgeSet = edgeList.toSet
  val nodeIds = Seq(idA, idB, idC, idD, idE)

  val results = Map(
    idA -> Seq(mock[Entity.Id]),
    idB -> Seq(mock[Entity.Id]),
    idC -> Seq(mock[Entity.Id]),
    idD -> Seq(mock[Entity.Id]),
    idE -> Seq(mock[Entity.Id], mock[Entity.Id])
  )


  private def edges(
      idA: Node.Id,
      idB: Node.Id,
      idC: Node.Id,
      idD: Node.Id,
      idE: Node.Id): List[Edge] = {
    List(
      Edge(Endpoint(idA, 0), Endpoint(idB, 0)),
      Edge(Endpoint(idB, 0), Endpoint(idC, 0)),
      Edge(Endpoint(idC, 0), Endpoint(idD, 0)),
      Edge(Endpoint(idA, 0), Endpoint(idE, 0)),
      Edge(Endpoint(idB, 0), Endpoint(idE, 1))
    )
  }

  protected def generateNodes(ops: DOperation*): Seq[(Node.Id, DeeplangNode)] = {
    val nodes = ops.map { o => Node(Node.Id.randomId, o)}
    nodes.map(n => n.id -> n)
  }

  protected def nodeRunning: nodestate.Running = nodestate.Running(DateTimeConverter.now)

  protected def nodeFailed: nodestate.Failed =
    nodestate.Running(DateTimeConverter.now).fail(mock[FailureDescription])

  protected def nodeCompleted: nodestate.Completed = {
    val date = DateTimeConverter.now
    nodestate.Completed(date, date.plusMinutes(1), Seq())
  }

  protected def nodeCompletedId(nodeId: Entity.Id): nodestate.Completed = {
    val date = DateTimeConverter.now
    nodestate.Completed(date, date.plusMinutes(1), results(nodeId))
  }
} 
Example 111
Source File: GraphTestObjects.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.graph

import scala.reflect.runtime.{universe => ru}

import org.scalatest.mockito.MockitoSugar

import io.deepsense.commons.utils.Version
import io.deepsense.deeplang._
import io.deepsense.deeplang.doperables.DOperableMock
import io.deepsense.graph.DeeplangGraph.DeeplangNode

object RandomNodeFactory {
  def randomNode(operation: DOperation): DeeplangNode = Node(Node.Id.randomId, operation)
}

object DClassesForDOperations extends MockitoSugar {
  trait A extends DOperableMock
  case class A1() extends A
  case class A2() extends A
}

object DOperationTestClasses {
  import io.deepsense.graph.DClassesForDOperations._

  trait DOperationBaseFields extends DOperation {
    // NOTE: id will be different for each instance
    override val id: DOperation.Id = DOperation.Id.randomId

    override val name: String = ""

    override val description: String = ""

    val params: Array[io.deepsense.deeplang.params.Param[_]] = Array()
  }

  case class DOperationCreateA1() extends DOperation0To1[A1] with DOperationBaseFields {
    override protected def execute()(context: ExecutionContext): A1 = ???

    @transient
    override lazy val tTagTO_0: ru.TypeTag[A1] = ru.typeTag[A1]
  }

  case class DOperationReceiveA1() extends DOperation1To0[A1] with DOperationBaseFields {
    override protected def execute(t0: A1)(context: ExecutionContext): Unit = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
  }

  case class DOperationA1ToA() extends DOperation1To1[A1, A] with DOperationBaseFields {
    override protected def execute(t1: A1)(context: ExecutionContext): A = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
  }

  case class DOperationAToA1A2() extends DOperation1To2[A, A1, A2] with DOperationBaseFields {
    override protected def execute(in: A)(context: ExecutionContext): (A1, A2) = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_1: ru.TypeTag[A2] = ru.typeTag[A2]
  }

  case class DOperationA1A2ToA() extends DOperation2To1[A1, A2, A] with DOperationBaseFields {
    override protected def execute(t1: A1, t2: A2)(context: ExecutionContext): A = ???

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A1] = ru.typeTag[A1]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTI_1: ru.TypeTag[A2] = ru.typeTag[A2]
  }

  case class DOperationAToALogging() extends DOperation1To1[A, A] with DOperationBaseFields {
    logger.trace("Initializing logging to test the serialization")
    override protected def execute(t0: A)(context: ExecutionContext): A = ???

    def trace(message: String): Unit = logger.trace(message)

    @transient
    override lazy val tTagTI_0: ru.TypeTag[A] = ru.typeTag[A]
    @transient
    override lazy val tTagTO_0: ru.TypeTag[A] = ru.typeTag[A]
  }
} 
Example 112
Source File: SparkRBackendSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor

import org.apache.spark.api.r._
import org.scalatest.concurrent.TimeLimits
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, PrivateMethodTester, WordSpec}

import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class SparkRBackendSpec
  extends WordSpec
  with MockitoSugar
  with Matchers
  with TimeLimits
  with PrivateMethodTester {

  "Spark R Backend" should {
    "return 0 for Entry Point Id" in {
      val sparkRBackend = new SparkRBackend()
      val customCodeEntryPoint = mock[CustomCodeEntryPoint]
      sparkRBackend.start(customCodeEntryPoint)
      sparkRBackend.entryPointId shouldBe "0"
      sparkRBackend.close()
    }
  }
} 
Example 113
Source File: DataFrameStorageSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.session.storage

import org.apache.spark.sql.{DataFrame => SparkDataFrame}
import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar

import io.deepsense.commons.StandardSpec
import io.deepsense.commons.models.Id
import io.deepsense.deeplang.DataFrameStorage
import io.deepsense.deeplang.doperables.dataframe.DataFrame

class DataFrameStorageSpec
    extends StandardSpec
    with BeforeAndAfter
    with MockitoSugar {

  val workflow1Id = Id.randomId
  val workflow2Id = Id.randomId

  val node1Id = Id.randomId
  val node2Id = Id.randomId

  val dataframe1Id = "dataframe1"
  val dataframe2Id = "dataframe2"
  val dataframe3Id = "dataframe3"

  val dataframe1 = mock[DataFrame]
  val dataframe2 = mock[DataFrame]
  val dataframe3 = mock[DataFrame]

  val sparkDataFrame1 = mock[SparkDataFrame]
  val sparkDataFrame2 = mock[SparkDataFrame]
  val sparkDataFrame3 = mock[SparkDataFrame]

  var storage: DataFrameStorage = _

  before {
    storage = new DataFrameStorageImpl
  }

  "DataFrameStorage" should {
    "register input dataFrames" in {
      storage.setInputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setInputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setInputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)

      storage.getInputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getInputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 0) shouldBe Some(sparkDataFrame2)
      storage.getInputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getInputDataFrame(workflow2Id, node2Id, 2) shouldBe None
    }

    "delete input dataFrames" in {
      storage.setInputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setInputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setInputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)
      storage.setInputDataFrame(workflow2Id, node2Id, 0, sparkDataFrame3)

      storage.removeNodeInputDataFrames(workflow1Id, node2Id, 0)

      storage.getInputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getInputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 0) shouldBe None
      storage.getInputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getInputDataFrame(workflow2Id, node2Id, 2) shouldBe None
      storage.getInputDataFrame(workflow2Id, node2Id, 0) shouldBe Some(sparkDataFrame3)
    }

    "register output dataFrames" in {
      storage.setOutputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setOutputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setOutputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)

      storage.getOutputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getOutputDataFrame(workflow1Id, node1Id, 1) shouldBe None
      storage.getOutputDataFrame(workflow1Id, node2Id, 0) shouldBe Some(sparkDataFrame2)
      storage.getOutputDataFrame(workflow1Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
      storage.getOutputDataFrame(workflow2Id, node2Id, 2) shouldBe None
    }

    "delete some output dataFrames" in {
      storage.setOutputDataFrame(workflow1Id, node1Id, 0, sparkDataFrame1)
      storage.setOutputDataFrame(workflow1Id, node2Id, 0, sparkDataFrame2)
      storage.setOutputDataFrame(workflow1Id, node2Id, 1, sparkDataFrame3)
      storage.setOutputDataFrame(workflow2Id, node2Id, 1, sparkDataFrame3)

      storage.removeNodeOutputDataFrames(workflow1Id, node2Id)

      storage.getOutputDataFrame(workflow1Id, node1Id, 0) shouldBe Some(sparkDataFrame1)
      storage.getOutputDataFrame(workflow1Id, node2Id, 0) shouldBe None
      storage.getOutputDataFrame(workflow1Id, node2Id, 1) shouldBe None
      storage.getOutputDataFrame(workflow2Id, node2Id, 1) shouldBe Some(sparkDataFrame3)
    }
  }
} 
Example 114
Source File: ProtocolJsonSerializerSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.communication.mq.serialization.json

import java.nio.charset.Charset

import org.scalatest.mockito.MockitoSugar
import spray.json._

import io.deepsense.commons.StandardSpec
import io.deepsense.commons.models.Entity
import io.deepsense.deeplang.DOperable
import io.deepsense.deeplang.doperables.ColumnsFilterer
import io.deepsense.graph._
import io.deepsense.models.json.graph.GraphJsonProtocol.GraphReader
import io.deepsense.models.json.workflow.{ExecutionReportJsonProtocol, InferredStateJsonProtocol, WorkflowWithResultsJsonProtocol}
import io.deepsense.models.workflows._
import io.deepsense.reportlib.model.factory.ReportContentTestFactory
import io.deepsense.workflowexecutor.communication.message.global._
import io.deepsense.workflowexecutor.communication.message.workflow.Synchronize

class ProtocolJsonSerializerSpec
  extends StandardSpec
  with MockitoSugar
  with WorkflowWithResultsJsonProtocol
  with InferredStateJsonProtocol
  with HeartbeatJsonProtocol {

  override val graphReader: GraphReader = mock[GraphReader]

  "ProtocolJsonSerializer" should {
    val protocolJsonSerializer = ProtocolJsonSerializer(graphReader)


    "serialize Synchronize messages" in {
      protocolJsonSerializer.serializeMessage(Synchronize()) shouldBe
        expectedSerializationResult("synchronize", JsObject())
    }

  }

  private def expectedSerializationResult(messageType: String, jsonObject: JsValue): Array[Byte] = {
    JsObject(
      "messageType" -> JsString(messageType),
      "messageBody" -> jsonObject
    ).compactPrint.getBytes(Charset.forName("UTF-8"))
  }

} 
Example 115
Source File: ProtocolJsonDeserializerSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.communication.mq.serialization.json

import java.nio.charset.StandardCharsets

import org.scalatest.mockito.MockitoSugar
import spray.json._

import io.deepsense.commons.StandardSpec
import io.deepsense.deeplang.CatalogRecorder
import io.deepsense.graph.DeeplangGraph
import io.deepsense.models.json.graph.GraphJsonProtocol.GraphReader
import io.deepsense.models.workflows.{Workflow, WorkflowMetadata, WorkflowType}
import io.deepsense.workflowexecutor.communication.message.workflow.{Abort, Synchronize, UpdateWorkflow}

class ProtocolJsonDeserializerSpec
  extends StandardSpec
  with MockitoSugar {

  "ProtocolJsonDeserializer" should {
    "deserialize Abort messages" in {
      val workflowId = Workflow.Id.randomId

      val rawMessage = JsObject(
        "messageType" -> JsString("abort"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString)
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage)
      readMessage shouldBe Abort(workflowId)
    }
    "deserialize UpdateWorkflow messages" in {
      val dOperationsCatalog = CatalogRecorder.resourcesCatalogRecorder.catalogs.dOperationsCatalog
      val graphReader = new GraphReader(dOperationsCatalog)
      val protocolDeserializer = ProtocolJsonDeserializer(graphReader)
      val workflowId = Workflow.Id.randomId

      val rawMessage = JsObject(
        "messageType" -> JsString("updateWorkflow"),
        "messageBody" -> JsObject(
          "workflowId" -> JsString(workflowId.toString),
          "workflow" -> JsObject(
            "metadata" -> JsObject(
              "type" -> JsString("batch"),
              "apiVersion" -> JsString("1.0.0")
            ),
            "workflow" -> JsObject(
              "nodes" -> JsArray(),
              "connections" -> JsArray()
            ),
            "thirdPartyData" -> JsObject()
          )
        )
      )

      val readMessage: Any = serializeAndRead(rawMessage, protocolDeserializer)
      readMessage shouldBe UpdateWorkflow(
        workflowId,
        Workflow(WorkflowMetadata(WorkflowType.Batch, "1.0.0"), DeeplangGraph(), JsObject()))
    }

    "deserialize Synchronize messages" in {
      val rawMessage = JsObject(
        "messageType" -> JsString("synchronize"),
        "messageBody" -> JsObject())
      serializeAndRead(rawMessage) shouldBe Synchronize()
    }
  }

  private def serializeAndRead(
      rawMessage: JsObject,
      protocolDeserializer: ProtocolJsonDeserializer =
        ProtocolJsonDeserializer(mock[GraphReader])): Any = {
    val bytes = rawMessage.compactPrint.getBytes(StandardCharsets.UTF_8)
    protocolDeserializer.deserializeMessage(bytes)
  }
} 
Example 116
Source File: WorkflowJsonParamsOverriderSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor

import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar
import spray.json._

import io.deepsense.commons.StandardSpec

class WorkflowJsonParamsOverriderSpec
    extends StandardSpec
    with BeforeAndAfter
    with MockitoSugar
    with DefaultJsonProtocol {

  "WorkflowJsonParamsOverrider" should {
    "override parameters based on passed extra params" in {
      val overrides = Map(
        "node1.param with spaces" -> "new value",
        "node2.nested.parameter.test" -> "changed"
      )

      WorkflowJsonParamsOverrider.overrideParams(originalJson, overrides) shouldBe expectedJson
    }

    "throw when invalid parameters are passed" in {
      val overrides = Map(
        "node1.no such param" -> "no such param",
        "no such node.param" -> "no such node"
      )

      a[RuntimeException] should be thrownBy {
        WorkflowJsonParamsOverrider.overrideParams(originalJson, overrides)
      }
    }
  }

  val originalJson =
    """{
      |  "workflow": {
      |    "nodes": [{
      |      "id": "node1",
      |      "parameters": {
      |        "param with spaces": "value"
      |      }
      |     }, {
      |      "id": "node2",
      |      "parameters": {
      |        "param": "value",
      |        "nested": {
      |          "parameter": {
      |            "test": "nested value"
      |          }
      |        }
      |      }
      |    }]
      |  }
      |}
    """.stripMargin.parseJson

  val expectedJson =
    """{
      |  "workflow": {
      |    "nodes": [{
      |      "id": "node1",
      |      "parameters": {
      |        "param with spaces": "new value"
      |      }
      |     }, {
      |      "id": "node2",
      |      "parameters": {
      |        "param": "value",
      |        "nested": {
      |          "parameter": {
      |            "test": "changed"
      |          }
      |        }
      |      }
      |    }]
      |  }
      |}
    """.stripMargin.parseJson
} 
Example 117
Source File: OperationExecutionDispatcherSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.pythongateway

import org.scalatest.BeforeAndAfter
import org.scalatest.mockito.MockitoSugar
import io.deepsense.commons.StandardSpec
import io.deepsense.commons.models.Id
import io.deepsense.deeplang.OperationExecutionDispatcher

class OperationExecutionDispatcherSpec
    extends StandardSpec
    with MockitoSugar
    with BeforeAndAfter {

  val workflowId = Id.randomId
  val nodeId = Id.randomId

  var dispatcher: OperationExecutionDispatcher = _

  before {
    dispatcher = new OperationExecutionDispatcher
  }

  "OperationExecutionDispatcher" should {

    "execute operation and finish" when {

      "notified of success with proper workflow and node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        dispatcher.executionEnded(workflowId, nodeId, Right(()))
        future.isCompleted shouldBe true
      }

      "notified of failure with proper workflow and node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        dispatcher.executionEnded(workflowId, nodeId, Left("A stacktrace"))
        future.isCompleted shouldBe true
        future.value.get.get shouldBe Left("A stacktrace")
      }
    }

    "throw an exception" when {

      "multiple executions of the same node are started" in {
        dispatcher.executionStarted(workflowId, nodeId)

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionStarted(workflowId, nodeId)
        }
      }

      "notified with non-existing workflow id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionEnded(Id.randomId, nodeId, Right(()))
        }
      }

      "notified with non-existing node id" in {
        val future = dispatcher.executionStarted(workflowId, nodeId)
        future.isCompleted shouldBe false

        an[IllegalArgumentException] shouldBe thrownBy {
          dispatcher.executionEnded(workflowId, Id.randomId, Right(()))
        }
      }
    }
  }
} 
Example 118
Source File: PythonCustomCodeEntryPointTest.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.pythongateway

import java.util.concurrent.TimeoutException

import scala.concurrent.duration._

import org.apache.spark.SparkContext
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.deeplang.{CustomCodeExecutor, DataFrameStorage, OperationExecutionDispatcher}
import io.deepsense.sparkutils.SparkSQLSession
import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class PythonCustomCodeEntryPointTest extends WordSpec with MockitoSugar with Matchers {

  "PythonEntryPoint" should {
    "throw on uninitialized code executor" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getCodeExecutor(100.millis)
      }
    }

    "throw on uninitialized callback server port" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getPythonPort(100.millis)
      }
    }

    "return initialized code executor" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]
      entryPoint.registerCodeExecutor(mockExecutor)
      entryPoint.getCodeExecutor(100.millis) shouldBe mockExecutor
    }

    "return initialized callback server port" in {
      val entryPoint = createEntryPoint
      entryPoint.registerCallbackServerPort(4412)
      entryPoint.getPythonPort(100.millis) shouldBe 4412
    }

    "return code executor initialized while waiting on it" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]

      new Thread(new Runnable {
        override def run(): Unit = {
          Thread.sleep(1000)
          entryPoint.registerCodeExecutor(mockExecutor)
        }
      }).start()

      entryPoint.getCodeExecutor(2.seconds) shouldBe mockExecutor
    }
  }

  private def createEntryPoint: CustomCodeEntryPoint =
    new CustomCodeEntryPoint(
      mock[SparkContext],
      mock[SparkSQLSession],
      mock[DataFrameStorage],
      mock[OperationExecutionDispatcher])
} 
Example 119
Source File: DOperationCategoryNodeJsonProtocolSpec.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.models.json.workflow

import scala.collection.immutable.ListMap

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import spray.json._

import io.deepsense.deeplang.DOperation
import io.deepsense.deeplang.catalogs.doperations.{DOperationCategory, DOperationCategoryNode, DOperationDescriptor}
import io.deepsense.models.json.workflow.DOperationCategoryNodeJsonProtocol._

class DOperationCategoryNodeJsonProtocolSpec extends FlatSpec with Matchers with MockitoSugar {

  "DOperationCategoryNode" should "be correctly serialized to json" in {
    val childCategory =
      new DOperationCategory(DOperationCategory.Id.randomId, "mock child name", None) {}
    val childNode = DOperationCategoryNode(Some(childCategory))

    val operationDescriptor = mock[DOperationDescriptor]
    when(operationDescriptor.id) thenReturn DOperation.Id.randomId
    when(operationDescriptor.name) thenReturn "mock operation descriptor name"
    when(operationDescriptor.description) thenReturn "mock operator descriptor description"

    val node = DOperationCategoryNode(
      None,
      successors = ListMap(childCategory -> childNode),
      operations = List(operationDescriptor))

    val expectedJson = JsObject(
      "catalog" -> JsArray(
        JsObject(
          "id" -> JsString(childCategory.id.toString),
          "name" -> JsString(childCategory.name),
          "catalog" -> JsArray(),
          "items" -> JsArray())
      ),
      "items" -> JsArray(
        JsObject(
          "id" -> JsString(operationDescriptor.id.toString),
          "name" -> JsString(operationDescriptor.name),
          "description" -> JsString(operationDescriptor.description)
        )
      )
    )

    node.toJson shouldBe expectedJson
  }
} 
Example 120
Source File: GraphJsonTestSupport.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.models.json.graph

import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
import spray.json.{DefaultJsonProtocol, JsObject}

import io.deepsense.deeplang.DOperation
import io.deepsense.graph.Endpoint

trait GraphJsonTestSupport
  extends WordSpec
  with MockitoSugar
  with DefaultJsonProtocol
  with Matchers {

  def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = {
    assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString)
    assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex)
  }

  def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = {
    edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString &&
    edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex
  }

  def mockOperation(
      inArity: Int,
      outArity: Int,
      id: DOperation.Id,
      name: String): DOperation = {

    val dOperation = mock[DOperation]
    when(dOperation.inArity).thenReturn(inArity)
    when(dOperation.outArity).thenReturn(outArity)
    when(dOperation.id).thenReturn(id)
    when(dOperation.name).thenReturn(name)
    dOperation
  }
} 
Example 121
Source File: FetchWithCacheConfigClientSpec.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package izanami.configs

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.testkit.TestKit
import izanami.Strategy.FetchWithCacheStrategy
import izanami._
import izanami.scaladsl.{Config, Configs, IzanamiClient}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.mockito.MockitoSugar
import play.api.libs.json.Json

import scala.concurrent.duration.DurationInt

class FetchWithCacheConfigClientSpec extends IzanamiSpec with BeforeAndAfterAll with MockitoSugar with ConfigServer {

  implicit val system       = ActorSystem("test")
  implicit val materializer = ActorMaterializer()

  override def afterAll: Unit = {
    TestKit.shutdownActorSystem(system)
  }

  "FetchWithCacheFeatureStrategy" should {
    "List configs" in {
      runServer { ctx =>
        //#config-fetch-cache
        val strategy = IzanamiClient(
          ClientConfig(ctx.host)
        ).configClient(
          strategy = FetchWithCacheStrategy(maxElement = 2, duration = 1.second),
          fallback = Configs(
            "test2" -> Json.obj("value" -> 2)
          )
        )
        //#config-fetch-cache

        val initialConfigs = Seq(
          Config("test", Json.obj("value" -> 1))
        )
        ctx.setValues(initialConfigs)

        val configs: Configs = strategy.configs("*").futureValue

        strategy.configs("*").futureValue
        strategy.configs("*").futureValue
        strategy.configs("*").futureValue

        configs.configs must be(initialConfigs)
        ctx.calls.size must be(1)

        configs.get("test") must be(Json.obj("value"  -> 1))
        configs.get("test2") must be(Json.obj("value" -> 2))
        configs.get("other") must be(Json.obj())
      }
    }

    "Test feature active" in {
      runServer { ctx =>
        val strategy = IzanamiClient(
          ClientConfig(ctx.host)
        ).configClient(
          strategy = FetchWithCacheStrategy(2, 5.second),
          fallback = Configs(
            "test5" -> Json.obj("value" -> 2)
          )
        )

        val initialConfigs = Seq(
          Config("test1", Json.obj("value" -> 1)),
          Config("test2", Json.obj("value" -> 2)),
          Config("test3", Json.obj("value" -> 3)),
          Config("test4", Json.obj("value" -> 4))
        )

        ctx.setValues(initialConfigs)

        strategy.config("test1").futureValue must be(Json.obj("value" -> 1))
        ctx.calls must have size 1
        strategy.config("test2").futureValue must be(Json.obj("value" -> 2))
        ctx.calls must have size 2
        strategy.config("test1").futureValue must be(Json.obj("value" -> 1))
        ctx.calls must have size 2

        strategy.config("test2").futureValue must be(Json.obj("value" -> 2))
        ctx.calls must have size 2

        strategy.config("test3").futureValue must be(Json.obj("value" -> 3))
        ctx.calls must have size 3

      }
    }
  }

} 
Example 122
Source File: KnownBossesObserverSpec.scala    From gbf-raidfinder   with MIT License 4 votes vote down vote up
package walfie.gbf.raidfinder

import java.util.Date
import monix.execution.schedulers.TestScheduler
import monix.reactive.Observer
import monix.reactive.subjects._
import org.mockito.Mockito._
import org.scalatest._
import org.scalatest.concurrent.{Eventually, ScalaFutures}
import org.scalatest.Matchers._
import org.scalatest.mockito.MockitoSugar
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random
import walfie.gbf.raidfinder.domain._

class KnownBossesObserverSpec extends KnownBossesObserverSpecHelpers {
  "Start with initial value" in new ObserverFixture {
    val boss1 = mockRaidInfo("A").boss
    val boss2 = mockRaidInfo("B").boss
    override val initialBosses = Seq(boss1, boss2)

    observer.get shouldBe Map("A" -> boss1, "B" -> boss2)
    cancelable.cancel()
  }

  "Keep last known of each boss" in new ObserverFixture {
    val bosses1 = (1 to 5).map(_ => mockRaidInfo("A"))
    val bosses2 = (1 to 10).map(_ => mockRaidInfo("B"))

    bosses1.foreach(raidInfos.onNext)
    bosses2.foreach(raidInfos.onNext)

    eventually {
      scheduler.tick()
      observer.get shouldBe Map(
        "A" -> bosses1.last.boss,
        "B" -> bosses2.last.boss
      )
    }
    cancelable.cancel()
  }

  "purgeOldBosses" - {
    "remove old bosses" in new ObserverFixture {
      val bosses = (1 to 10).map { i =>
        RaidBoss(name = i.toString, level = i, image = None, lastSeen = new Date(i), language = Language.Japanese)
      }
      override val initialBosses = bosses

      scheduler.tick()
      observer.get shouldBe bosses.map(boss => boss.name -> boss).toMap

      val resultF = observer.purgeOldBosses(minDate = new Date(5), levelThreshold = Some(100))
      scheduler.tick()

      resultF.futureValue shouldBe
        bosses.drop(5).map(boss => boss.name -> boss).toMap
    }

    "keep bosses that are above a certain level" in new ObserverFixture {
      val bosses = Seq(10, 50, 100, 120, 150).map { i =>
        RaidBoss(name = i.toString, level = i, image = None, lastSeen = new Date(0), language = Language.English)
      }
      override val initialBosses = bosses

      scheduler.tick()
      observer.get.values.toSet shouldBe bosses.toSet

      val resultF = observer.purgeOldBosses(minDate = new Date(5), levelThreshold = Some(100))
      scheduler.tick()

      resultF.futureValue.values.toSet shouldBe
        bosses.filter(_.level >= 100).toSet
    }
  }

}

trait KnownBossesObserverSpecHelpers extends FreeSpec
  with MockitoSugar with Eventually with ScalaFutures {

  trait ObserverFixture {
    implicit val scheduler = TestScheduler()
    val initialBosses: Seq[RaidBoss] = Seq.empty
    val raidInfos = ConcurrentSubject.replay[RaidInfo]
    lazy val (observer, cancelable) = KnownBossesObserver
      .fromRaidInfoObservable(raidInfos, initialBosses)
  }

  def mockRaidInfo(bossName: String): RaidInfo = {
    val tweet = mock[RaidTweet]
    when(tweet.bossName) thenReturn bossName
    when(tweet.createdAt) thenReturn (new Date(Random.nextLong.abs * 1000))
    val boss = mock[RaidBoss]
    when(boss.name) thenReturn bossName
    RaidInfo(tweet, boss)
  }
}