Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 11

[ 307 ]

account.points == 350

when:
bbPlus.upgrade(account, "PREMIUM")
then: "Should have 550 for PREMIUM and 100 bonus"
account.points == 650
}

def "Upgrade to premium reward"() {
expect:
account.points == 120

when:
bbPlus.upgrade(account, "PREMIUM")
then: "Should have 550 for PREMIUM and 100 bonus"
account.points == 650
}

Running this test case should pass all of the tests. This would verify that all of
the rewards that we have deployed in the DSL are being activated as expected.
However, the final testUpgradeToPremiumReward fails. This reveals a flaw in
our reward logic. The conditions that we have used allow a bonus for upgrading
from BASIC to PLUS. If the subscriber then upgrades to PREMIUM, they keep the
bonus points. However, a subscriber upgrading from BASIC straight to PREMIUM is
disadvantaged by not receiving the bonus, which was not our intention.


onUpgrade = {
reward ("Upgrade to PLUS and get 100 free points") {
anyOf {
condition {
toPlan == "PLUS"
}
allOf {
condition {
toPlan == "PREMIUM"
}
condition {
fromPlan == "BASIC"
}
}
}
grant {
points 100
}
}
}
Free download pdf