Skip to content

Commit 55b0f34

Browse files
committed
CountryService.add(): change argument from User to Integer.
Addressed to #120 No functional changes.
1 parent d5503ea commit 55b0f34

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

src/main/java/ru/mystamps/web/controller/CountryController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public String processInput(
7676
return null;
7777
}
7878

79-
UrlEntityDto countryUrl = countryService.add(form, currentUser);
79+
UrlEntityDto countryUrl = countryService.add(form, currentUser.getId());
8080

8181
String dstUrl = UriComponentsBuilder.fromUriString(Url.INFO_COUNTRY_PAGE)
8282
.buildAndExpand(countryUrl.getId(), countryUrl.getSlug())

src/main/java/ru/mystamps/web/service/CountryService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import java.util.Map;
2121

22-
import ru.mystamps.web.entity.User;
2322
import ru.mystamps.web.service.dto.AddCountryDto;
2423
import ru.mystamps.web.service.dto.LinkEntityDto;
2524
import ru.mystamps.web.service.dto.SelectEntityDto;
2625
import ru.mystamps.web.service.dto.UrlEntityDto;
2726

2827
public interface CountryService {
29-
UrlEntityDto add(AddCountryDto dto, User user);
28+
UrlEntityDto add(AddCountryDto dto, Integer userId);
3029
Iterable<SelectEntityDto> findAllAsSelectEntities(String lang);
3130
Iterable<LinkEntityDto> findAllAsLinkEntities(String lang);
3231
long countAll();

src/main/java/ru/mystamps/web/service/CountryServiceImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import ru.mystamps.web.dao.JdbcCountryDao;
3535
import ru.mystamps.web.dao.dto.AddCountryDbDto;
36-
import ru.mystamps.web.entity.User;
3736
import ru.mystamps.web.service.dto.AddCountryDto;
3837
import ru.mystamps.web.service.dto.LinkEntityDto;
3938
import ru.mystamps.web.service.dto.SelectEntityDto;
@@ -49,11 +48,11 @@ public class CountryServiceImpl implements CountryService {
4948
@Override
5049
@Transactional
5150
@PreAuthorize("hasAuthority('CREATE_COUNTRY')")
52-
public UrlEntityDto add(AddCountryDto dto, User user) {
51+
public UrlEntityDto add(AddCountryDto dto, Integer userId) {
5352
Validate.isTrue(dto != null, "DTO should be non null");
5453
Validate.isTrue(dto.getName() != null, "Country name on English should be non null");
5554
Validate.isTrue(dto.getNameRu() != null, "Country name on Russian should be non null");
56-
Validate.isTrue(user != null, "Current user must be non null");
55+
Validate.isTrue(userId != null, "User id must be non null");
5756

5857
AddCountryDbDto country = new AddCountryDbDto();
5958
country.setName(dto.getName());
@@ -69,9 +68,9 @@ public UrlEntityDto add(AddCountryDto dto, User user) {
6968
Date now = new Date();
7069

7170
country.setCreatedAt(now);
72-
country.setCreatedBy(user.getId());
71+
country.setCreatedBy(userId);
7372
country.setUpdatedAt(now);
74-
country.setUpdatedBy(user.getId());
73+
country.setUpdatedBy(userId);
7574

7675
Integer id = countryDao.add(country);
7776

src/test/groovy/ru/mystamps/web/service/CountryServiceImplTest.groovy

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import spock.lang.Unroll
2222

2323
import ru.mystamps.web.dao.JdbcCountryDao
2424
import ru.mystamps.web.dao.dto.AddCountryDbDto
25-
import ru.mystamps.web.entity.User
2625
import ru.mystamps.web.model.AddCountryForm
2726
import ru.mystamps.web.service.dto.LinkEntityDto
2827
import ru.mystamps.web.service.dto.SelectEntityDto
@@ -33,7 +32,7 @@ import ru.mystamps.web.util.SlugUtils
3332
class CountryServiceImplTest extends Specification {
3433

3534
private AddCountryForm form
36-
private User user
35+
private Integer userId = 321
3736

3837
private JdbcCountryDao countryDao = Mock()
3938
private CountryService service = new CountryServiceImpl(countryDao)
@@ -42,8 +41,6 @@ class CountryServiceImplTest extends Specification {
4241
form = new AddCountryForm()
4342
form.setName('Any country name')
4443
form.setNameRu('Любое название страны')
45-
46-
user = TestObjects.createUser()
4744
}
4845

4946
//
@@ -52,7 +49,7 @@ class CountryServiceImplTest extends Specification {
5249

5350
def "add() should throw exception when dto is null"() {
5451
when:
55-
service.add(null, user)
52+
service.add(null, userId)
5653
then:
5754
thrown IllegalArgumentException
5855
}
@@ -61,7 +58,7 @@ class CountryServiceImplTest extends Specification {
6158
given:
6259
form.setName(null)
6360
when:
64-
service.add(form, user)
61+
service.add(form, userId)
6562
then:
6663
thrown IllegalArgumentException
6764
}
@@ -70,7 +67,7 @@ class CountryServiceImplTest extends Specification {
7067
given:
7168
form.setNameRu(null)
7269
when:
73-
service.add(form, user)
70+
service.add(form, userId)
7471
then:
7572
thrown IllegalArgumentException
7673
}
@@ -94,7 +91,7 @@ class CountryServiceImplTest extends Specification {
9491
and:
9592
UrlEntityDto expected = new UrlEntityDto(expectedId, expectedSlug)
9693
when:
97-
UrlEntityDto actual = service.add(form, user)
94+
UrlEntityDto actual = service.add(form, userId)
9895
then:
9996
actual == expected
10097
}
@@ -104,7 +101,7 @@ class CountryServiceImplTest extends Specification {
104101
String expectedCountryName = 'Italy'
105102
form.setName(expectedCountryName)
106103
when:
107-
service.add(form, user)
104+
service.add(form, userId)
108105
then:
109106
1 * countryDao.add({ AddCountryDbDto country ->
110107
assert country?.name == expectedCountryName
@@ -117,7 +114,7 @@ class CountryServiceImplTest extends Specification {
117114
String expectedCountryName = 'Италия'
118115
form.setNameRu(expectedCountryName)
119116
when:
120-
service.add(form, user)
117+
service.add(form, userId)
121118
then:
122119
1 * countryDao.add({ AddCountryDbDto country ->
123120
assert country?.nameRu == expectedCountryName
@@ -129,7 +126,7 @@ class CountryServiceImplTest extends Specification {
129126
given:
130127
form.setName(null)
131128
when:
132-
service.add(form, user)
129+
service.add(form, userId)
133130
then:
134131
thrown IllegalArgumentException
135132
}
@@ -142,7 +139,7 @@ class CountryServiceImplTest extends Specification {
142139
and:
143140
form.setName(name)
144141
when:
145-
service.add(form, user)
142+
service.add(form, userId)
146143
then:
147144
1 * countryDao.add({ AddCountryDbDto country ->
148145
assert country?.slug == slug
@@ -152,7 +149,7 @@ class CountryServiceImplTest extends Specification {
152149

153150
def "add() should assign created at to current date"() {
154151
when:
155-
service.add(form, user)
152+
service.add(form, userId)
156153
then:
157154
1 * countryDao.add({ AddCountryDbDto country ->
158155
assert DateUtils.roughlyEqual(country?.createdAt, new Date())
@@ -162,7 +159,7 @@ class CountryServiceImplTest extends Specification {
162159

163160
def "add() should assign updated at to current date"() {
164161
when:
165-
service.add(form, user)
162+
service.add(form, userId)
166163
then:
167164
1 * countryDao.add({ AddCountryDbDto country ->
168165
assert DateUtils.roughlyEqual(country?.updatedAt, new Date())
@@ -171,21 +168,25 @@ class CountryServiceImplTest extends Specification {
171168
}
172169

173170
def "add() should assign created by to user"() {
171+
given:
172+
Integer expectedUserId = 10
174173
when:
175-
service.add(form, user)
174+
service.add(form, expectedUserId)
176175
then:
177176
1 * countryDao.add({ AddCountryDbDto country ->
178-
assert country?.createdBy == user.id
177+
assert country?.createdBy == expectedUserId
179178
return true
180179
}) >> 70
181180
}
182181

183182
def "add() should assign updated by to user"() {
183+
given:
184+
Integer expectedUserId = 10
184185
when:
185-
service.add(form, user)
186+
service.add(form, expectedUserId)
186187
then:
187188
1 * countryDao.add({ AddCountryDbDto country ->
188-
assert country?.updatedBy == user.id
189+
assert country?.updatedBy == expectedUserId
189190
return true
190191
}) >> 80
191192
}

0 commit comments

Comments
 (0)