Skip to content

Commit 0fd5eed

Browse files
committed
Country,Series: added createdBy/updatedBy fields.
Added dependency to PowerMock because I use it in unit tests (for mocking static methods). Also due to issue #398 I downgrade Mockito to 1.9.0 @see http://code.google.com/p/powermock/issues/detail?id=398 More details about PowerMock: - http://code.google.com/p/powermock/wiki/MockitoUsage13 - http://code.google.com/p/powermock/wiki/TestNG_usage Fixed #133
1 parent 6a880dd commit 0fd5eed

File tree

10 files changed

+216
-4
lines changed

10 files changed

+216
-4
lines changed

pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
<javax.validation.version>1.1.0.Alpha1</javax.validation.version>
2525

2626
<testng.version>6.7</testng.version>
27-
<mockito.version>1.9.5-rc1</mockito.version>
27+
<mockito.version>1.9.0</mockito.version>
2828
<fest.assert.version>2.0M6</fest.assert.version>
29+
<powermock.version>1.4.12</powermock.version>
2930

3031
<java.version>1.6</java.version>
3132

@@ -222,6 +223,20 @@
222223
<scope>test</scope>
223224
</dependency>
224225

226+
<dependency>
227+
<groupId>org.powermock</groupId>
228+
<artifactId>powermock-api-mockito</artifactId>
229+
<version>${powermock.version}</version>
230+
<scope>test</scope>
231+
</dependency>
232+
233+
<dependency>
234+
<groupId>org.powermock</groupId>
235+
<artifactId>powermock-module-testng</artifactId>
236+
<version>${powermock.version}</version>
237+
<scope>test</scope>
238+
</dependency>
239+
225240
<dependency>
226241
<groupId>org.springframework</groupId>
227242
<artifactId>spring-test</artifactId>

src/env/dev/WEB-INF/classes/mysql-scheme.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
name varchar(50) not null unique,
99
updated_at datetime not null,
1010
version bigint not null,
11+
created_by integer not null,
12+
updated_by integer not null,
1113
primary key (id)
1214
) ENGINE=InnoDB;
1315

@@ -117,6 +119,18 @@
117119
primary key (id)
118120
) ENGINE=InnoDB;
119121

122+
alter table countries
123+
add index FK509F9AB4E7A879A3 (created_by),
124+
add constraint FK509F9AB4E7A879A3
125+
foreign key (created_by)
126+
references users (id);
127+
128+
alter table countries
129+
add index FK509F9AB4846862F0 (updated_by),
130+
add constraint FK509F9AB4846862F0
131+
foreign key (updated_by)
132+
references users (id);
133+
120134
alter table series
121135
add index FKCA01FE77DF85B0F0 (country_id),
122136
add constraint FKCA01FE77DF85B0F0

src/env/test/WEB-INF/classes/h2-scheme.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
name varchar(50) not null unique,
99
updated_at timestamp not null,
1010
version bigint not null,
11+
created_by integer not null,
12+
updated_by integer not null,
1113
primary key (id)
1214
);
1315

@@ -117,6 +119,16 @@
117119
primary key (id)
118120
);
119121

122+
alter table countries
123+
add constraint FK509F9AB4E7A879A3
124+
foreign key (created_by)
125+
references users;
126+
127+
alter table countries
128+
add constraint FK509F9AB4846862F0
129+
foreign key (updated_by)
130+
references users;
131+
120132
alter table series
121133
add constraint FKCA01FE77DF85B0F0
122134
foreign key (country_id)

src/env/test/WEB-INF/classes/test-data.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
--
22
-- Auto-generated by Maven, based on values from src/env/test/WEB-INF/classes/spring/test-data.properties
33
--
4-
INSERT INTO users(login, name, email, registered_at, activated_at, hash, salt)
4+
INSERT INTO users(id, login, name, email, registered_at, activated_at, hash, salt)
55
VALUES (
6+
1,
67
'@valid_user_login@',
78
'@valid_user_name@',
89
'coder@rock.home',
@@ -26,10 +27,12 @@ VALUES(
2627
NOW()
2728
);
2829

29-
INSERT INTO countries(name, created_at, updated_at, version)
30+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, version)
3031
VALUES(
3132
'@valid_country_name@',
3233
NOW(),
34+
1,
3335
NOW(),
36+
1,
3437
0
3538
);

src/main/java/ru/mystamps/web/entity/Country.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import javax.persistence.Entity;
2525
import javax.persistence.GeneratedValue;
2626
import javax.persistence.Id;
27+
import javax.persistence.JoinColumn;
28+
import javax.persistence.ManyToOne;
2729
import javax.persistence.Table;
2830
import javax.persistence.Temporal;
2931
import javax.persistence.TemporalType;
@@ -52,10 +54,18 @@ public class Country {
5254
@Column(name = "created_at", nullable = false)
5355
private Date createdAt;
5456

57+
@ManyToOne(optional = false)
58+
@JoinColumn(name = "created_by", nullable = false)
59+
private User createdBy;
60+
5561
@Temporal(TemporalType.TIMESTAMP)
5662
@Column(name = "updated_at", nullable = false)
5763
private Date updatedAt;
5864

65+
@ManyToOne(optional = false)
66+
@JoinColumn(name = "updated_by", nullable = false)
67+
private User updatedBy;
68+
5969
@Setter(AccessLevel.PROTECTED)
6070
@Version
6171
@Column(nullable = false)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import org.springframework.transaction.annotation.Transactional;
2828

2929
import static com.google.common.base.Preconditions.checkArgument;
30+
import static com.google.common.base.Preconditions.checkState;
3031

3132
import ru.mystamps.web.entity.Country;
33+
import ru.mystamps.web.entity.User;
3234
import ru.mystamps.web.dao.CountryDao;
3335

3436
@Service
@@ -37,6 +39,9 @@ public class CountryService {
3739
@Inject
3840
private CountryDao countryDao;
3941

42+
@Inject
43+
private UserService userService;
44+
4045
@Transactional
4146
@PreAuthorize("hasAuthority('ROLE_USER')")
4247
public Country add(final String countryName) {
@@ -49,6 +54,11 @@ public Country add(final String countryName) {
4954
country.setCreatedAt(now);
5055
country.setUpdatedAt(now);
5156

57+
final User currentUser = userService.getCurrentUser();
58+
checkState(currentUser != null, "Current user must be non null");
59+
country.setCreatedBy(currentUser);
60+
country.setUpdatedBy(currentUser);
61+
5262
return countryDao.save(country);
5363
}
5464

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
import org.springframework.security.authentication.encoding.PasswordEncoder;
31+
import org.springframework.security.core.Authentication;
32+
import org.springframework.security.core.context.SecurityContext;
33+
import org.springframework.security.core.context.SecurityContextHolder;
34+
3135
import org.springframework.stereotype.Service;
3236
import org.springframework.transaction.annotation.Transactional;
3337

@@ -39,6 +43,7 @@
3943
import ru.mystamps.web.entity.UsersActivation;
4044
import ru.mystamps.web.dao.UserDao;
4145
import ru.mystamps.web.dao.UsersActivationDao;
46+
import ru.mystamps.web.support.spring.security.CustomUserDetails;
4247

4348
@Service
4449
public class UserService {
@@ -133,6 +138,30 @@ public User findByLogin(final String login) {
133138
return users.findByLogin(login);
134139
}
135140

141+
User getCurrentUser() {
142+
final SecurityContext ctx = SecurityContextHolder.getContext();
143+
checkState(ctx != null, "Security context must be non null");
144+
145+
final Authentication auth = ctx.getAuthentication();
146+
if (auth == null) {
147+
return null;
148+
}
149+
150+
final Object principal = auth.getPrincipal();
151+
if (principal == null) {
152+
return null;
153+
}
154+
155+
checkState(
156+
principal instanceof CustomUserDetails,
157+
"Principal must be CustomUserDetails type"
158+
);
159+
160+
final CustomUserDetails userDetails = (CustomUserDetails)principal;
161+
162+
return userDetails.getUser();
163+
}
164+
136165
/**
137166
* Generates activation key.
138167
* @return string which contains numbers and letters in lower case

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetails.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public String getSalt() {
5050
return user.getSalt();
5151
}
5252

53+
// used by UserService.getCurrentUser()
54+
public User getUser() {
55+
return user;
56+
}
57+
5358
}

src/test/java/ru/mystamps/web/service/CountryServiceTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import ru.mystamps.web.dao.CountryDao;
4444
import ru.mystamps.web.entity.Country;
45+
import ru.mystamps.web.entity.User;
4546
import ru.mystamps.web.tests.fest.DateAssert;
4647

4748
public class CountryServiceTest {
@@ -52,6 +53,9 @@ public class CountryServiceTest {
5253
@Mock
5354
private CountryDao countryDao;
5455

56+
@Mock
57+
private UserService userService;
58+
5559
@Captor
5660
private ArgumentCaptor<Country> countryCaptor;
5761

@@ -61,6 +65,7 @@ public class CountryServiceTest {
6165
@BeforeMethod
6266
public void setUp() {
6367
MockitoAnnotations.initMocks(this);
68+
when(userService.getCurrentUser()).thenReturn(UserServiceTest.getValidUser());
6469
}
6570

6671
//
@@ -109,6 +114,35 @@ public void addShouldAssignUpdatedAtToCurrentDate() {
109114
DateAssert.assertThat(countryCaptor.getValue().getUpdatedAt()).isCurrentDate();
110115
}
111116

117+
@Test(expectedExceptions = IllegalStateException.class)
118+
public void addShouldThrowExceptionWhenCannotDetermineCurrentUser() {
119+
when(userService.getCurrentUser()).thenReturn(null);
120+
121+
service.add(TEST_COUNTRY_NAME);
122+
}
123+
124+
@Test
125+
public void addShouldAssignCreatedAtToCurrentUser() {
126+
final User expectedUser = UserServiceTest.getValidUser();
127+
when(userService.getCurrentUser()).thenReturn(expectedUser);
128+
129+
service.add(TEST_COUNTRY_NAME);
130+
131+
verify(countryDao).save(countryCaptor.capture());
132+
assertThat(countryCaptor.getValue().getCreatedBy()).isEqualTo(expectedUser);
133+
}
134+
135+
@Test
136+
public void addShouldAssignUpdatedAtToCurrentUser() {
137+
final User expectedUser = UserServiceTest.getValidUser();
138+
when(userService.getCurrentUser()).thenReturn(expectedUser);
139+
140+
service.add(TEST_COUNTRY_NAME);
141+
142+
verify(countryDao).save(countryCaptor.capture());
143+
assertThat(countryCaptor.getValue().getUpdatedBy()).isEqualTo(expectedUser);
144+
}
145+
112146
//
113147
// Tests for findAll()
114148
//

0 commit comments

Comments
 (0)