run.qontract.core.pattern.DateTimePattern.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qontract-core Show documentation
Show all versions of qontract-core Show documentation
A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner
package run.qontract.core.pattern
import run.qontract.core.Resolver
import run.qontract.core.Result
import run.qontract.core.value.JSONArrayValue
import run.qontract.core.value.StringValue
import run.qontract.core.value.Value
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object DateTimePattern : Pattern, ScalarType {
override fun matches(sampleData: Value?, resolver: Resolver): Result = when (sampleData) {
is StringValue -> resultOf {
parse(sampleData.string, resolver)
Result.Success()
}
else -> Result.Failure("DateTime types can only be represented using strings")
}
override fun generate(resolver: Resolver): StringValue =
StringValue(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME))
override fun newBasedOn(row: Row, resolver: Resolver): List = listOf(this)
override fun parse(value: String, resolver: Resolver): StringValue =
attempt {
DateTimeFormatter.ISO_DATE_TIME.parse(value)
StringValue(value)
}
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
return encompasses(this, otherPattern, thisResolver, otherResolver, typeStack)
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return JSONArrayValue(valueList)
}
override val typeAlias: String?
get() = null
override val typeName: String = "datetime"
override val pattern = "(datetime)"
}