All Downloads are FREE. Search and download functionalities are using the official Maven repository.

est-base.21.3.5.source-code.BotBusAsserts.kt Maven / Gradle / Ivy

/*
 * Copyright (C) 2017/2021 e-voyageurs technologies
 *
 * 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 ai.tock.bot.test

import ai.tock.bot.connector.ConnectorMessage
import ai.tock.bot.definition.ParameterKey
import ai.tock.bot.engine.action.SendAttachment
import ai.tock.bot.engine.action.SendChoice
import ai.tock.bot.engine.action.SendSentence
import ai.tock.bot.engine.message.Attachment
import ai.tock.bot.engine.message.Choice
import ai.tock.bot.engine.message.GenericElement
import ai.tock.bot.engine.message.GenericMessage
import ch.tutteli.atrium.api.cc.en_GB.any
import ch.tutteli.atrium.api.cc.en_GB.contains
import ch.tutteli.atrium.api.cc.en_GB.containsExactly
import ch.tutteli.atrium.api.cc.en_GB.containsNot
import ch.tutteli.atrium.api.cc.en_GB.notToBeNull
import ch.tutteli.atrium.api.cc.en_GB.property
import ch.tutteli.atrium.api.cc.en_GB.returnValueOf
import ch.tutteli.atrium.api.cc.en_GB.toBe
import ch.tutteli.atrium.creating.Assert
import ch.tutteli.atrium.domain.builders.AssertImpl
import ch.tutteli.atrium.domain.creating.any.typetransformation.AnyTypeTransformation
import ch.tutteli.atrium.reporting.RawString
import ch.tutteli.atrium.reporting.translating.Untranslatable
import ch.tutteli.atrium.verbs.expect

fun Assert.toBeSimpleTextMessage(expectedText: String) =
    returnValueOf(BotBusMockLog::text).toBe(expectedText)

fun Assert.asGenericMessage(assertionCreator: Assert.() -> Unit) {
    val parameterObject = AnyTypeTransformation.ParameterObject(
        Untranslatable("is a"),
        RawString.create(GenericMessage::class.simpleName!!),
        this,
        assertionCreator,
        Untranslatable("cannot be converted to generic message : is not a send sentence")
    )
    AssertImpl.any.typeTransformation.transform(
        parameterObject, { it.action is SendSentence && it.genericMessage() != null }, { it.genericMessage()!! },
        AssertImpl.any.typeTransformation.failureHandlers.newExplanatory()
    )
}

fun Assert.toHaveGlobalText(expectedText: String, textName: String = "text") =
    property(GenericMessage::texts).addAssertionsCreatedBy {
        returnValueOf(Map::get, textName).toBe(expectedText)
    }

fun Assert.toHaveGlobalChoices(expectedChoice: String, vararg otherExpectedChoices: String) =
    property(GenericMessage::choices).addAssertionsCreatedBy {
        expect(
            subject.map { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).contains(expectedChoice, *otherExpectedChoices)
    }

fun Assert.toHaveNotGlobalChoices(unexpectedChoice: String, vararg otherUnexpectedChoices: String) =
    property(GenericMessage::choices).addAssertionsCreatedBy {
        expect(
            subject.map { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).containsNot(unexpectedChoice, *otherUnexpectedChoices)
    }

fun Assert.toHaveExactlyGlobalChoices(expectedChoice: String, vararg otherExpectedChoices: String) =
    property(GenericMessage::choices).addAssertionsCreatedBy {
        expect(
            subject.map { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).containsExactly(expectedChoice, *otherExpectedChoices)
    }

fun Assert.toHaveElement(index: Int, assertionCreator: Assert.() -> Unit) =
    property(GenericMessage::subElements).addAssertionsCreatedBy {
        returnValueOf(List::get, index) {
            addAssertionsCreatedBy(assertionCreator)
        }
    }

fun Assert.toHaveText(expectedText: String, textName: String) =
    property(GenericElement::texts).addAssertionsCreatedBy {
        returnValueOf(Map::get, textName).toBe(expectedText)
    }

fun Assert.toHaveTitle(expectedTitle: String) =
    toHaveText(expectedTitle, "title")

fun Assert.toHaveSubtitle(expectedSubtitle: String) =
    toHaveText(expectedSubtitle, "subtitle")

fun Assert.toHaveChoices(expectedChoice: String, vararg otherExpectedChoices: String) =
    property(GenericElement::choices).addAssertionsCreatedBy {
        expect(
            subject.mapNotNull { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).contains(expectedChoice, *otherExpectedChoices)
    }

fun Assert.toHaveNotChoices(unexpectedChoice: String, vararg otherUnexpectedChoices: String) =
    property(GenericElement::choices).addAssertionsCreatedBy {
        expect(
            subject.mapNotNull { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).containsNot(unexpectedChoice, *otherUnexpectedChoices)
    }

fun Assert.toHaveExactlyChoices(expectedChoice: String, vararg otherExpectedChoices: String) =
    property(GenericElement::choices).addAssertionsCreatedBy {
        expect(
            subject.mapNotNull { choice ->
                choice.parameters[SendChoice.TITLE_PARAMETER]
            }
        ).containsExactly(expectedChoice, *otherExpectedChoices)
    }

fun ConnectorMessage.asGenericMessage(assertionCreator: Assert.() -> Unit) {
    expect(toGenericMessage()).notToBeNull(assertionCreator)
}

fun Assert.toHaveAttachment(assertionCreator: Assert.() -> Unit) =
    property(GenericElement::attachments).addAssertionsCreatedBy {
        any {
            addAssertionsCreatedBy(assertionCreator)
        }
    }

fun Assert.toHaveUrl(url: String) = property(Attachment::url).toBe(url)

fun Assert.toBeImage() = property(Attachment::type).toBe(SendAttachment.AttachmentType.image)

fun Assert.toHaveChoice(title: String, assertionCreator: Assert.() -> Unit) =
    property(GenericMessage::choices).addAssertionsCreatedBy {
        any {
            toHaveTitle(title)
            addAssertionsCreatedBy(assertionCreator)
        }
    }

fun Assert.toHaveTitle(title: String) {
    toHaveParameter(SendChoice.TITLE_PARAMETER, title)
}

fun Assert.toHaveIntent(intentName: String) {
    property(Choice::intentName).toBe(intentName)
}

fun Assert.toHaveParameter(key: ParameterKey, value: String) {
    property(Choice::parameters).addAssertionsCreatedBy {
        returnValueOf(Map::get, key.name).toBe(value)
    }
}

fun Assert.toHaveParameter(key: String, value: String) {
    property(Choice::parameters).addAssertionsCreatedBy {
        returnValueOf(Map::get, key).toBe(value)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy