|
| 1 | +/** |
| 2 | + * Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.service |
| 19 | + |
| 20 | +import spock.lang.Specification |
| 21 | +import spock.lang.Unroll |
| 22 | + |
| 23 | +import ru.mystamps.web.dao.SeriesSalesDao |
| 24 | +import ru.mystamps.web.dao.dto.AddSeriesSalesDbDto |
| 25 | +import ru.mystamps.web.dao.dto.Currency |
| 26 | +import ru.mystamps.web.model.AddSeriesSalesForm |
| 27 | +import ru.mystamps.web.tests.DateUtils |
| 28 | + |
| 29 | +@SuppressWarnings(['ClassJavadoc', 'MethodName', 'NoDef', 'NoTabCharacter', 'TrailingWhitespace']) |
| 30 | +class SeriesSalesServiceImplTest extends Specification { |
| 31 | + |
| 32 | + private final SeriesSalesDao seriesSalesDao = Mock() |
| 33 | + private final SeriesSalesService service = new SeriesSalesServiceImpl(seriesSalesDao) |
| 34 | + |
| 35 | + private AddSeriesSalesForm form |
| 36 | + |
| 37 | + def setup() { |
| 38 | + form = new AddSeriesSalesForm() |
| 39 | + form.setCurrency(Currency.EUR) |
| 40 | + } |
| 41 | + |
| 42 | + // |
| 43 | + // Tests for add() |
| 44 | + // |
| 45 | + |
| 46 | + def "add() should throw exception when dto is null"() { |
| 47 | + when: |
| 48 | + service.add(null, 123, 456) |
| 49 | + then: |
| 50 | + thrown IllegalArgumentException |
| 51 | + } |
| 52 | + |
| 53 | + def 'add() should throw exception when currency is null'() { |
| 54 | + when: |
| 55 | + service.add(new AddSeriesSalesForm(), 123, 456) |
| 56 | + then: |
| 57 | + thrown IllegalArgumentException |
| 58 | + } |
| 59 | + |
| 60 | + def 'add() should throw exception when series id is null'() { |
| 61 | + when: |
| 62 | + service.add(form, null, 456) |
| 63 | + then: |
| 64 | + thrown IllegalArgumentException |
| 65 | + } |
| 66 | + |
| 67 | + def 'add() should throw exception when user id is null'() { |
| 68 | + when: |
| 69 | + service.add(form, 123, null) |
| 70 | + then: |
| 71 | + thrown IllegalArgumentException |
| 72 | + } |
| 73 | + |
| 74 | + @Unroll |
| 75 | + @SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryObjectReferences', 'UnnecessaryReturnKeyword']) |
| 76 | + def 'add() should create series sales'( |
| 77 | + Date date, |
| 78 | + String url, |
| 79 | + BigDecimal altPrice, Currency altCurrency, |
| 80 | + Integer buyerId) { |
| 81 | + |
| 82 | + given: |
| 83 | + // mandatory fields |
| 84 | + form.setSellerId(444) |
| 85 | + form.setPrice(new BigDecimal('100')) |
| 86 | + form.setCurrency(Currency.EUR) |
| 87 | + // optional fields |
| 88 | + form.setDate(date) |
| 89 | + form.setUrl(url) |
| 90 | + form.setAltPrice(altPrice) |
| 91 | + form.setAltCurrency(altCurrency) |
| 92 | + form.setBuyerId(buyerId) |
| 93 | + and: |
| 94 | + Integer expectedSeriesId = 777 |
| 95 | + Integer expectedUserId = 888 |
| 96 | + when: |
| 97 | + service.add(form, expectedSeriesId, expectedUserId) |
| 98 | + then: |
| 99 | + 1 * seriesSalesDao.add({ AddSeriesSalesDbDto dto -> |
| 100 | + assert dto?.date == form.date |
| 101 | + assert dto?.sellerId == form.sellerId |
| 102 | + assert dto?.url == form.url |
| 103 | + assert dto?.price == form.price |
| 104 | + assert dto?.currency == form.currency?.toString() |
| 105 | + assert dto?.altPrice == form.altPrice |
| 106 | + assert dto?.altCurrency == form.altCurrency?.toString() |
| 107 | + assert dto?.buyerId == form.buyerId |
| 108 | + assert dto?.createdBy == expectedUserId |
| 109 | + assert dto?.seriesId == expectedSeriesId |
| 110 | + assert DateUtils.roughlyEqual(dto?.createdAt, new Date()) |
| 111 | + return true |
| 112 | + }) |
| 113 | + where: |
| 114 | + date | url | altPrice | altCurrency | buyerId |
| 115 | + null | null | null | null | null |
| 116 | + new Date() - 10 | 'example.com' | new BigDecimal('6200') | Currency.RUB | 555 |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments