ProfileStepdefs.kt 3.59 KB
Newer Older
1 2 3 4 5 6 7 8
package org.craftsrecords.talkadvisor.recommendation.stepdefs

import cucumber.api.java.en.Given
import cucumber.api.java.en.Then
import cucumber.api.java.en.When
import org.assertj.core.api.Assertions.assertThat
import org.craftsrecords.talkadvisor.recommendation.api.CreateProfile
import org.craftsrecords.talkadvisor.recommendation.assertions.that
Julien Topçu's avatar
Julien Topçu committed
9
import org.craftsrecords.talkadvisor.recommendation.criteria.createPreferences
10 11
import org.craftsrecords.talkadvisor.recommendation.preferences.Preferences
import org.craftsrecords.talkadvisor.recommendation.profile.Profile
Julien Topçu's avatar
Julien Topçu committed
12 13
import org.craftsrecords.talkadvisor.recommendation.profile.ProfileAlreadyExistsException
import org.craftsrecords.talkadvisor.recommendation.profile.ProfileNotFoundException
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import org.craftsrecords.talkadvisor.recommendation.spi.Profiles

class ProfileStepdefs(private val testContext: TestContext,
                      private val createProfile: CreateProfile,
                      private val profiles: Profiles) {

    @Given("^a user$")
    fun `a user`() {
        testContext.userId = "frequentUser"
    }

    @Given("^a user with no profile$")
    fun `a user with no profile`() {
        testContext.userId = "noProfileUser"
    }

Julien Topçu's avatar
Julien Topçu committed
30 31 32 33 34 35
    @Given("^he already has a profile$")
    fun `he already has a profile`() {
        val profile = Profile(testContext.userId, createPreferences())
        profiles.save(profile)
    }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    @Given("^he has stored his preferences in his profile$")
    fun `he has stored his preferences in his profile`() {
        val profile = createProfile(testContext.userId)
        profiles.save(profile)

        testContext.createdProfile = profile
    }

    @When("^he creates his profile$")
    fun `he creates his profile`() {
        val preferences = preferencesFromContext()

        val profile = createProfile.forUserWithPreferences(testContext.userId, preferences)

        testContext.requestedPreferences = preferences
        testContext.createdProfile = profile
    }

Julien Topçu's avatar
Julien Topçu committed
54 55 56 57 58 59 60 61 62
    @When("^he tries to create again his profile$")
    fun `he tries to create again his profile`() {
        try {
            createProfile.forUserWithPreferences(testContext.userId, createPreferences())
        } catch (e: Exception) {
            testContext.error = e
        }
    }

63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @Then("^his preferences are stored within$")
    fun `his preferences are stored within`() {
        val profile = testContext.createdProfile
        val frequentUser = testContext.userId
        val hisPreferences = testContext.requestedPreferences

        profile.that `corresponds to user` frequentUser
        profile.that `has preferences` hisPreferences
    }

    @Then("^he is notified that his profile cannot be found$")
    fun `he is notified that his profile cannot be found`() {
        assertThat(testContext.error)
                .isNotNull()
Julien Topçu's avatar
Julien Topçu committed
77 78 79 80 81 82 83 84 85 86
                .isInstanceOf(ProfileNotFoundException::class.java)
                .hasMessage(String.format("No profile found for the user %s", testContext.userId))
    }

    @Then("^he is notified that his profile already exists$")
    fun `he is notified that his profile already exists`() {
        assertThat(testContext.error)
                .isNotNull()
                .isInstanceOf(ProfileAlreadyExistsException::class.java)
                .hasMessage(String.format("A profile already exists for the user %s", testContext.userId))
87 88 89 90 91 92 93 94 95 96
    }

    private fun createProfile(userId: String): Profile {
        return Profile(userId, createPreferences())
    }

    private fun preferencesFromContext(): Preferences {
        return Preferences(testContext.requestedTopics, testContext.requestedTalksFormats)
    }
}