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

import io.gatling.core.config.GatlingConfiguration
import org.mockito.Mockito.when
import org.scalatest.{ FlatSpec, Matchers }
import org.scalatest.mock.MockitoSugar

import io.gatling.core.result._
import io.gatling.core.result.message._
import io.gatling.core.result.reader._
import io.gatling.core.util.StringHelper.RichString

class AssertionValidatorSpec extends FlatSpec with Matchers with MockitoSugar with AssertionSupport {

  implicit val configuration = GatlingConfiguration.loadForTest()
  val assertionValidator = new AssertionValidator

  private type Conditions = List[AssertionWithPathAndTarget => Assertion]
  private type StatsModifiers = List[Stats => Stats]

  private case class Stats(generalStats: GeneralStats,
                           requestName: String = "",
                           groupPath: List[String] = Nil,
                           status: Option[Status] = None) {

    def request = requestName.trimToOption
    def group = if (groupPath.nonEmpty) Some(Group(groupPath)) else None
  }

  private val SetRequestThenGroupModifiers: StatsModifiers =
    List(_.copy(requestName = "foo"), _.copy(groupPath = List("foo")))

  private def mockDataReaderWithStats(metric: AssertionWithPathAndTarget,
                                      conditions: Conditions,
                                      stats: Stats*) = {
      def mockAssertion(dataReader: DataReader) = when(dataReader.assertions) thenReturn conditions.map(_(metric))

      def mockStats(stat: Stats, dataReader: DataReader) =
        when(dataReader.requestGeneralStats(stat.request, stat.group, stat.status)) thenReturn stat.generalStats

      def statsPaths = stats.map(stat => (stat.request, stat.group)).map {
        case (Some(request), group) => RequestStatsPath(request, group)
        case (None, Some(group))    => GroupStatsPath(group)
        case _                      => throw new AssertionError("Can't have neither a request or group stats path")
      }.toList

      def mockStatsPath(dataReader: DataReader) =
        when(dataReader.statsPaths) thenReturn statsPaths
<commit_msg>Fix AssertionValidatorSpec<commit_after>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gatling.core.assertion

import io.gatling.core.config.GatlingConfiguration
import org.mockito.Mockito.when
import org.scalatest.{ FlatSpec, Matchers }
import org.scalatest.mock.MockitoSugar

import io.gatling.core.result._
import io.gatling.core.result.message._
import io.gatling.core.result.reader._
import io.gatling.core.util.StringHelper.RichString

class AssertionValidatorSpec extends FlatSpec with Matchers with MockitoSugar with AssertionSupport {

  implicit val configuration = GatlingConfiguration.loadForTest()
  val assertionValidator = new AssertionValidator

  private type Conditions = List[AssertionWithPathAndTarget => Assertion]
  private type StatsModifiers = List[Stats => Stats]

  private case class Stats(generalStats: GeneralStats,
                           requestName: String = "",
                           groupPath: List[String] = Nil,
                           status: Option[Status] = None) {

    def request = requestName.trimToOption
    def group = if (groupPath.nonEmpty) Some(Group(groupPath)) else None
  }

  private val SetRequestThenGroupModifiers: StatsModifiers =
    List(_.copy(requestName = "foo"), _.copy(groupPath = List("foo")))

  private def mockDataReaderWithStats(metric: AssertionWithPathAndTarget,
                                      conditions: Conditions,
                                      stats: Stats*) = {
      def mockAssertion(dataReader: DataReader) = when(dataReader.assertions) thenReturn conditions.map(_(metric))

      def mockStats(stat: Stats, dataReader: DataReader) = {
        when(dataReader.requestGeneralStats(stat.request, stat.group, stat.status)) thenReturn stat.generalStats
        stat.group.foreach { group =>
          when(dataReader.groupCumulatedResponseTimeGeneralStats(group, stat.status)) thenReturn stat.generalStats
        }
      }

      def statsPaths = stats.map(stat => (stat.request, stat.group)).map {
        case (Some(request), group) => RequestStatsPath(request, group)
        case (None, Some(group))    => GroupStatsPath(group)
        case _                      => throw new AssertionError("Can't have neither a request or group stats path")
      }.toList

      def mockStatsPath(dataReader: DataReader) =
        when(dataReader.statsPaths) thenReturn statsPaths

{'fraction_non_alphanumeric': 0.08551724137931034, 'fraction_numerical': 0.004137931034482759, 'mean_word_length': 3.342065868263473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3526815', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1503, 'n_words': 451}