in.specmatic.core.pattern.URLPattern.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
Deprecation Notice for group ID "in.specmatic"
******************************************************************************************************
Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic".
Please update your dependencies to use the new group ID "io.specmatic".
******************************************************************************************************
The newest version!
package `in`.specmatic.core.pattern
import `in`.specmatic.core.Resolver
import `in`.specmatic.core.Result
import `in`.specmatic.core.pattern.config.NegativePatternConfiguration
import `in`.specmatic.core.value.JSONArrayValue
import `in`.specmatic.core.value.StringValue
import `in`.specmatic.core.value.Value
import java.net.URI
data class URLPattern(val scheme: URLScheme = URLScheme.HTTPS, override val typeAlias: String? = null): Pattern {
override val pattern: String = "(url)"
override fun matches(sampleData: Value?, resolver: Resolver): Result {
return when (sampleData) {
is StringValue -> {
if(scheme.matches(parse(sampleData.string, resolver))) {
Result.Success()
} else Result.Failure("Expected ${sampleData.string} to be ${scheme.type}")
}
else -> Result.Failure("URLs can only be held in strings.")
}
}
override fun generate(resolver: Resolver): StringValue =
StringValue("${scheme.prefix}${randomString().lowercase()}.com/${randomString().lowercase()}")
override fun newBasedOn(row: Row, resolver: Resolver): Sequence> = sequenceOf(HasValue(this))
override fun newBasedOn(resolver: Resolver): Sequence = sequenceOf(this)
override fun negativeBasedOn(row: Row, resolver: Resolver, config: NegativePatternConfiguration): Sequence> {
return newBasedOn(row, resolver)
}
override fun parse(value: String, resolver: Resolver): StringValue = StringValue(URI.create(value).toString())
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
return when(otherPattern) {
this -> Result.Success()
is URLPattern -> Result.Failure("Expected ${scheme.type}, got ${otherPattern.scheme.type}")
else -> Result.Failure("Expected $typeName, got ${otherPattern.typeName}")
}
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return JSONArrayValue(valueList)
}
override val typeName: String = "url"
}