Spock Web Console

subscribe to the feed Subscribe
BigDecimal to ID test (via #spockwebconsole)
tweet this script Tweet

BigDecimal to ID test

Published 3 weeks ago by Anonymous
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

// Hit 'Run Script' below
class FooCompanyLoadServiceTest extends Specification {
    FooCompanyRepository repo = Mock()
    def service = new FooCompanyLoadService(repo: repo)
    Map<String, Object> supplierMap = ['id':1L]

    def processData() {
        given: 'a map w/ parent_id as a BigDecimal'
        supplierMap.put('parent_id', 2G) // G is short for BigDecimal

        when: 'mapping the entity'
        def fooCompany = service.mapEntity(supplierMap)

        then: 'expect to call repo once w/ the BigDecimal converted to long'
        1 * repo.findOne(2L) >> { new FooCompany(id: 2L) }

        fooCompany != null && fooCompany.parentCompany != null
        fooCompany.id == 1L
        fooCompany.parentCompany.id == 2L

        and: 'show failing'
        'this string does not match' == 'this other string'

class FooCompany {
    Long id
    FooCompany parentCompany

class FooCompanyLoadService {
    FooCompanyRepository repo;

    FooCompany mapEntity(Map map) {
        def target = new FooCompany(id:map['id'])
        Long parentId = getId(map, 'parent_id')
        if (parentId) { target.parentCompany = repo.findOne(parentId) }
        return target

    private Long getId(Map map, String dataKey) {
        Number nullableNumber = map[dataKey]
        Long nullableLong = nullableNumber?.longValue()
        return nullableLong

interface FooCompanyRepository {
    FooCompany findOne(Long id)