<commit_before>package backend.model.challenges

import backend.model.challenges.ChallengeStatus.*
import backend.model.event.Team
import backend.model.posting.Posting
import backend.model.sponsoring.UnregisteredSponsor
import backend.model.user.Sponsor
import backend.util.euroOf
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito.`when`
import org.powermock.api.mockito.PowerMockito.mock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import kotlin.test.assertFails
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(PowerMockRunner::class)
@PrepareForTest(Sponsor::class, Team::class, Posting::class, UnregisteredSponsor::class)
class ChallengeTest {

    @Test
    fun testSetSponsor() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(sponsor, challenge.sponsor)
        assertNull(challenge.sponsor.unregisteredSponsor)
    }

    @Test
    fun testSetUnregisteredSponsor() {
        val unregistered = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(unregistered, team, euroOf(50), "description")

        assertTrue(challenge.sponsor is UnregisteredSponsor)
        assertNull(challenge.sponsor.registeredSponsor)
    }

    @Test
    fun testAccept() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(PROPOSED, challenge.status)
        challenge.accept()
        assertEquals(ACCEPTED, challenge.status)
    }

    @Test
    fun testMultipleAccept() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(PROPOSED, challenge.status)
        challenge.accept()
        challenge.accept()
        assertEquals(ACCEPTED, challenge.status)
    }

    @Test
    fun testReject() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(PROPOSED, challenge.status)
        challenge.reject()
        assertEquals(REJECTED, challenge.status)
    }

    @Test
    fun testAddProof() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val proof = mock(Posting::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")
        challenge.accept()

        challenge.addProof()
        assertEquals(WITH_PROOF, challenge.status)
    }

    @Test
    fun testAcceptProof() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val proof = mock(Posting::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")
        challenge.accept()
        challenge.addProof()
        challenge.acceptProof()
        assertEquals(PROOF_ACCEPTED, challenge.status)
    }

    @Test
    fun testRejectProof() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val proof = mock(Posting::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")
        challenge.accept()
        challenge.addProof()
        challenge.rejectProof()
        assertEquals(PROOF_REJECTED, challenge.status)
    }

    @Test
    fun testWithdraw() {
        val team = mock(Team::class.java)
        val sponsor = mock(Sponsor::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        challenge.withdraw()

        assertEquals(WITHDRAWN, challenge.status)
        assertFails { challenge.accept() }
        assertFails { challenge.reject() }
    }

    @Test
    fun whenSponsorIsRegistered_thenGetSponsorReturnsSponsor() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.sponsor is Sponsor }
    }

    @Test
    fun whenSponsorIsUnregistered_thenGetSponsorReturnsUnregisteredSponsor() {
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.sponsor is UnregisteredSponsor }
    }

    fun whenSponsorIsUnregistered_thenHasRegisteredSponsorReturnsFalse() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertFalse { challenge.hasRegisteredSponsor() }
    }

    fun whenSponsorIsRegistered_thenHasRegisteredSponsorReturnsTrue() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.hasRegisteredSponsor() }
    }

    @Test
    fun whenChallengeIsFullfilled_thenBillableAmountReturnsAmountOfChallenge() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.addProof()

        assertEquals(euroOf(50), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsProposed_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        assertEquals(euroOf(0.0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsWithdrawn_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.withdraw()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsAccepted_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.accept()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsRejected_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.reject()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeProofIsRejected_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.addProof()
        challenge.rejectProof()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeProofIsAccepted_thenBillableAmountIsAmount() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.addProof()
        challenge.acceptProof()

        assertEquals(euroOf(50), challenge.billableAmount())
    }

    @Test
    fun whenSponsorIsRegistered_thenHasRegisteredSponsorIsTrue() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")
        assertTrue(challenge.hasRegisteredSponsor())
    }

    fun whenUnregisteredSponsorAndUsernameInTeam_thenChallengeCanBeWithdrawn() {
        val username = "<EMAIL>"
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)

        `when`(team.isMember(username)).thenReturn(true)

        val challenge = Challenge(sponsor, team, euroOf(0), "description")

        assertTrue(challenge.checkWithdrawPermissions(username))
    }

    fun whenUnregisteredSponsorAndUsernameNotInTeam_thenChallengeCantBeWithdrawn() {
        val username = "<EMAIL>"
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)
<commit_msg>Fixing tests<commit_after>package backend.model.challenges

import backend.model.challenges.ChallengeStatus.*
import backend.model.event.Team
import backend.model.posting.Posting
import backend.model.sponsoring.UnregisteredSponsor
import backend.model.user.Sponsor
import backend.util.euroOf
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito.`when`
import org.powermock.api.mockito.PowerMockito.mock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import kotlin.test.assertFails
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(PowerMockRunner::class)
@PrepareForTest(Sponsor::class, Team::class, Posting::class, UnregisteredSponsor::class)
class ChallengeTest {

    @Test
    fun testSetSponsor() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(sponsor, challenge.sponsor)
        assertNull(challenge.sponsor.unregisteredSponsor)
    }

    @Test
    fun testSetUnregisteredSponsor() {
        val unregistered = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(unregistered, team, euroOf(50), "description")

        assertTrue(challenge.sponsor is UnregisteredSponsor)
        assertNull(challenge.sponsor.registeredSponsor)
    }

    @Test
    fun testReject() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertEquals(PROPOSED, challenge.status)
        challenge.reject()
        assertEquals(REJECTED, challenge.status)
    }

    @Test
    fun testAddProof() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val proof = mock(Posting::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")
        challenge.maximumCount = 2

        challenge.addProof()
        challenge.addProof()
        assertEquals(WITH_PROOF, challenge.status)
        assertEquals(2, challenge.fulfilledCount)

        assertFails { challenge.addProof() }
    }

    @Test
    fun testTakeBack() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val proof = mock(Posting::class.java)

        val challenge = Challenge(sponsor, team, euroOf(50), "description")
        challenge.addProof()
        challenge.takeBack()

        assertEquals(PROPOSED, challenge.status)
        assertEquals(0, challenge.fulfilledCount)
    }

    @Test
    fun testWithdraw() {
        val team = mock(Team::class.java)
        val sponsor = mock(Sponsor::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        challenge.withdraw()

        assertEquals(WITHDRAWN, challenge.status)
        assertFails { challenge.reject() }
    }

    @Test
    fun whenSponsorIsRegistered_thenGetSponsorReturnsSponsor() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.sponsor is Sponsor }
    }

    @Test
    fun whenSponsorIsUnregistered_thenGetSponsorReturnsUnregisteredSponsor() {
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.sponsor is UnregisteredSponsor }
    }

    fun whenSponsorIsUnregistered_thenHasRegisteredSponsorReturnsFalse() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertFalse { challenge.hasRegisteredSponsor() }
    }

    fun whenSponsorIsRegistered_thenHasRegisteredSponsorReturnsTrue() {
        val sponsor = mock(Sponsor::class.java)
        val team = mock(Team::class.java)
        val challenge = Challenge(sponsor, team, euroOf(50), "description")

        assertTrue { challenge.hasRegisteredSponsor() }
    }

    @Test
    fun whenChallengeIsFullfilled_thenBillableAmountReturnsAmountOfChallenge() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.addProof()

        assertEquals(euroOf(50), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsFullfilledMultipleTimes_thenBillableAmountReturnsAmountOfChallenge() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")
        challenge.maximumCount = null

        challenge.addProof()
        challenge.addProof()

        assertEquals(euroOf(100), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsProposed_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        assertEquals(euroOf(0.0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsWithdrawn_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.withdraw()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenChallengeIsRejected_thenBillableAmountIsZero() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")

        challenge.reject()

        assertEquals(euroOf(0), challenge.billableAmount())
    }

    @Test
    fun whenSponsorIsRegistered_thenHasRegisteredSponsorIsTrue() {
        val challenge = Challenge(mock(Sponsor::class.java), mock(Team::class.java), euroOf(50), "description")
        assertTrue(challenge.hasRegisteredSponsor())
    }

    fun whenUnregisteredSponsorAndUsernameInTeam_thenChallengeCanBeWithdrawn() {
        val username = "<EMAIL>"
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)

        `when`(team.isMember(username)).thenReturn(true)

        val challenge = Challenge(sponsor, team, euroOf(0), "description")

        assertTrue(challenge.checkWithdrawPermissions(username))
    }

    fun whenUnregisteredSponsorAndUsernameNotInTeam_thenChallengeCantBeWithdrawn() {
        val username = "<EMAIL>"
        val sponsor = mock(UnregisteredSponsor::class.java)
        val team = mock(Team::class.java)

{'fraction_non_alphanumeric': 0.10942269409422695, 'fraction_numerical': 0.006635700066357001, 'mean_word_length': 3.4774212715389186, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7388123', 'n_tokens_mistral': 4673, 'n_tokens_neox': 4334, 'n_words': 771}