

package org.mongodb.scala.model

import java.lang.reflect.Modifier._

import scala.util.{Success, Try}

import org.scalatest.prop.TableDrivenPropertyChecks._
import org.scalatest.{FlatSpec, Matchers}

class ValidationActionSpec extends FlatSpec with Matchers {

  "ValidationAction" should "have the same static fields as the wrapped ValidationAction" in {
    val ValidationActionClass: Class[ValidationAction] = classOf[com.mongodb.client.model.ValidationAction]
    val wrappedFields = ValidationActionClass.getDeclaredFields.filter(f => isStatic(f.getModifiers)).map(_.getName).toSet
    val wrappedMethods = ValidationActionClass.getDeclaredMethods.filter(f => isStatic(f.getModifiers)).map(_.getName).toSet
    val exclusions = Set("$VALUES", "valueOf", "values")

    val wrapped = (wrappedFields ++ wrappedMethods) -- exclusions
    val local = ValidationAction.getClass.getDeclaredMethods.map(_.getName).toSet -- Set("apply", "$deserializeLambda$", "$anonfun$fromString$1")

    local should equal(wrapped)
  }

  it should "return the expected ValidationActions" in {
    forAll(validationActions) { (stringValue: String, expectedValue: Try[ValidationAction]) =>
      ValidationAction.fromString(stringValue) should equal(expectedValue)
    }
  }

  it should "handle invalid strings" in {
    forAll(invalidValidationActions) { (stringValue: String) =>
      ValidationAction.fromString(stringValue) should be a 'failure
    }
  }

  val validationActions =
    Table(
      ("stringValue", "JavaValue"),
      ("error", Success(ValidationAction.ERROR)),
      ("ERROR", Success(ValidationAction.ERROR)),
      ("warn", Success(ValidationAction.WARN)),
      ("WARN", Success(ValidationAction.WARN))
    )

  val invalidValidationActions = Table("invalid strings", "all", "none")
}
