Skip to content

Commit f8b7eee

Browse files
committed
unitTesting feature:
change architecture of entity and change mapper with some basic config for map
1 parent 13174d5 commit f8b7eee

9 files changed

Lines changed: 187 additions & 43 deletions

File tree

pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133
</dependency>
134134

135135
<!-- test -->
136-
137136
<dependency>
138137
<groupId>org.spockframework</groupId>
139138
<artifactId>spock-core</artifactId>
@@ -181,14 +180,14 @@
181180
<executions>
182181
<execution>
183182
<goals>
184-
<goal>addSources</goal>
185-
<goal>addTestSources</goal>
186-
<goal>generateStubs</goal>
183+
<!-- <goal>addSources</goal>-->
184+
<!-- <goal>addTestSources</goal>-->
185+
<!-- <goal>generateStubs</goal>-->
187186
<goal>compile</goal>
188-
<goal>generateTestStubs</goal>
187+
<!-- <goal>generateTestStubs</goal>-->
189188
<goal>compileTests</goal>
190-
<goal>removeStubs</goal>
191-
<goal>removeTestStubs</goal>
189+
<!-- <goal>removeStubs</goal>-->
190+
<!-- <goal>removeTestStubs</goal>-->
192191
</goals>
193192
</execution>
194193
</executions>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ir.bigz.springbootreal.commons.util;
2+
3+
import java.sql.Timestamp;
4+
import java.time.LocalDateTime;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
9+
public class Utils {
10+
11+
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd - HH:mm:ss z");
12+
static final DateTimeFormatter SIMPLE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");
13+
14+
public static String getTimeNow(){
15+
return ZonedDateTime.now(ZoneId.of("Asia/Tehran")).format(FORMATTER);
16+
}
17+
18+
public static String convertTimestamp(Timestamp timestamp){
19+
LocalDateTime localDateTime = timestamp.toLocalDateTime();
20+
return localDateTime.format(FORMATTER);
21+
}
22+
23+
public static Timestamp convertTimeString(String time){
24+
LocalDateTime from = LocalDateTime.from(FORMATTER.parse(time));
25+
return Timestamp.valueOf(from);
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ir.bigz.springbootreal.dao;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
import javax.persistence.*;
9+
import java.sql.Timestamp;
10+
import java.time.LocalDateTime;
11+
12+
@MappedSuperclass
13+
@Getter
14+
@Setter
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class BaseEntity {
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.AUTO)
21+
private Long id;
22+
23+
@Version
24+
private Integer version;
25+
26+
@Column(name = "insert_date")
27+
private Timestamp insertDate = Timestamp.valueOf(LocalDateTime.now());
28+
29+
@Column(name = "update_date")
30+
private Timestamp updateDate;
31+
32+
@Column(name = "active_status")
33+
public boolean activeStatus;
34+
}

src/main/java/ir/bigz/springbootreal/dao/User.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
@Entity
88
@Table(name ="users")
99
@Access(AccessType.FIELD)
10-
public class User implements Serializable {
11-
12-
@Id
13-
@GeneratedValue(strategy = GenerationType.AUTO)
14-
@Column(name = "user_id")
15-
private long id;
10+
public class User extends BaseEntity implements Serializable {
1611

1712
@Column(name = "first_name", nullable = false)
1813
private String firstName;
@@ -51,8 +46,7 @@ public int hashCode() {
5146
@Override
5247
public String toString() {
5348
return "User{" +
54-
"id=" + id +
55-
", firstName='" + firstName + '\'' +
49+
"firstName='" + firstName + '\'' +
5650
", lastName='" + lastName + '\'' +
5751
", userName='" + userName + '\'' +
5852
", nationalCode='" + nationalCode + '\'' +
@@ -62,14 +56,6 @@ public String toString() {
6256
'}';
6357
}
6458

65-
public long getId() {
66-
return id;
67-
}
68-
69-
public void setId(long id) {
70-
this.id = id;
71-
}
72-
7359
public String getFirstName() {
7460
return firstName;
7561
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
package ir.bigz.springbootreal.dao.mapper;
22

3+
import ir.bigz.springbootreal.commons.util.Utils;
34
import ir.bigz.springbootreal.dao.User;
45
import ir.bigz.springbootreal.viewmodel.UserModel;
56
import org.mapstruct.Mapper;
7+
import org.mapstruct.Mapping;
8+
import org.mapstruct.Mappings;
9+
import org.mapstruct.Named;
10+
11+
import java.sql.Timestamp;
12+
import java.util.Objects;
613

714
@Mapper(componentModel = "spring")
815
public interface UserMapper {
916

17+
@Mappings({
18+
@Mapping(source = "insertDate", target = "insertDate", qualifiedByName = "timestampToStringMapper"),
19+
@Mapping(source = "updateDate", target = "updateDate", qualifiedByName = "timestampToStringMapper")
20+
})
1021
UserModel userToUserModel(User user);
22+
23+
@Mappings({
24+
@Mapping(source = "insertDate", target = "insertDate", qualifiedByName = "timeStringToTimestampMapper"),
25+
@Mapping(source = "updateDate", target = "updateDate", qualifiedByName = "timeStringToTimestampMapper")
26+
})
1127
User userModelToUser(UserModel userModel);
28+
29+
@Named("timestampToStringMapper")
30+
static String timestampToStringMapper(Timestamp timestamp){
31+
if(Objects.nonNull(timestamp) && !timestamp.toString().isBlank())
32+
return Utils.convertTimestamp(timestamp);
33+
return "";
34+
}
35+
36+
@Named("timeStringToTimestampMapper")
37+
static Timestamp timeStringToTimestampMapper(String time){
38+
if(Objects.nonNull(time) && !time.isBlank())
39+
return Utils.convertTimeString(time);
40+
return null;
41+
}
1242
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ir.bigz.springbootreal.viewmodel;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class BaseModel {
13+
private Long id;
14+
private Integer version;
15+
private String insertDate;
16+
private String updateDate;
17+
public boolean activeStatus;
18+
}

src/main/java/ir/bigz/springbootreal/viewmodel/UserModel.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import javax.validation.constraints.NotBlank;
66
import java.io.Serializable;
77

8-
public class UserModel implements Serializable {
9-
10-
private long id;
8+
public class UserModel extends BaseModel implements Serializable {
119

1210
@NotBlank(message = "firstName must not blank")
1311
private String firstName;
@@ -37,14 +35,35 @@ public class UserModel implements Serializable {
3735
public UserModel() {
3836
}
3937

38+
public UserModel(Long id,
39+
Integer version,
40+
String insertDate,
41+
String updateDate,
42+
boolean activeStatus,
43+
@NotBlank(message = "firstName must not blank") String firstName,
44+
@NotBlank(message = "lastName must not blank") String lastName,
45+
@NotBlank(message = "userName must not blank") String userName,
46+
@Validator(ValidationType.NATIONAL_CODE) String nationalCode,
47+
@Validator(ValidationType.MOBILE) String mobile,
48+
@Validator(ValidationType.EMAIL) String email,
49+
@Validator(ValidationType.GENDER) String gender) {
50+
super(id, version, insertDate, updateDate, activeStatus);
51+
this.firstName = firstName;
52+
this.lastName = lastName;
53+
this.userName = userName;
54+
this.nationalCode = nationalCode;
55+
this.mobile = mobile;
56+
this.email = email;
57+
this.gender = gender;
58+
}
59+
4060
public UserModel(@NotBlank(message = "firstName must not blank") String firstName,
4161
@NotBlank(message = "lastName must not blank") String lastName,
4262
@NotBlank(message = "userName must not blank") String userName,
4363
@Validator(ValidationType.NATIONAL_CODE) String nationalCode,
4464
@Validator(ValidationType.MOBILE) String mobile,
4565
@Validator(ValidationType.EMAIL) String email,
4666
@Validator(ValidationType.GENDER) String gender) {
47-
this.id = id;
4867
this.firstName = firstName;
4968
this.lastName = lastName;
5069
this.userName = userName;
@@ -57,8 +76,7 @@ public UserModel(@NotBlank(message = "firstName must not blank") String firstNam
5776
@Override
5877
public String toString() {
5978
return "UserModel{" +
60-
"id=" + id +
61-
", firstName='" + firstName + '\'' +
79+
"firstName='" + firstName + '\'' +
6280
", lastName='" + lastName + '\'' +
6381
", userName='" + userName + '\'' +
6482
", nationalCode='" + nationalCode + '\'' +
@@ -68,14 +86,6 @@ public String toString() {
6886
'}';
6987
}
7088

71-
public long getId() {
72-
return id;
73-
}
74-
75-
public void setId(long id) {
76-
this.id = id;
77-
}
78-
7989
public String getFirstName() {
8090
return firstName;
8191
}

src/test/groovy/ir/bigz/springbootreal/web/SampleApiTest.groovy

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ import ir.bigz.springbootreal.dao.User
55
import ir.bigz.springbootreal.service.UserService
66
import ir.bigz.springbootreal.service.UserServiceImpl
77
import ir.bigz.springbootreal.viewmodel.UserModel
8+
import org.junit.jupiter.api.Test
89
import org.spockframework.spring.SpringBean
910
import org.springframework.beans.factory.annotation.Autowired
11+
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
12+
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient
13+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
14+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc
15+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
16+
import org.springframework.boot.test.context.SpringBootTest
1017
import org.springframework.boot.test.web.client.TestRestTemplate
1118
import org.springframework.context.ApplicationContext
19+
import org.springframework.http.HttpMethod
1220
import org.springframework.http.HttpStatus
1321
import org.springframework.http.ResponseEntity
1422
import org.springframework.test.context.ActiveProfiles
@@ -18,13 +26,16 @@ import spock.lang.Specification
1826
import spock.lang.Subject
1927
import spock.lang.Title
2028

21-
@ContextConfiguration(classes = [SampleController.class, UserService.class, UserServiceImpl.class, UserModel.class, User.class])
29+
@ContextConfiguration(classes = [SampleController.class, UserServiceImpl.class, UserModel.class, User.class])
2230
@Title("sample spock test for testing project")
2331
@ActiveProfiles("test")
32+
@AutoConfigureWebClient
33+
@AutoConfigureWebMvc
34+
@WebMvcTest(excludeAutoConfiguration = [SecurityAutoConfiguration.class])
2435
class SampleApiTest extends Specification{
2536

26-
// @Autowired
27-
// ApplicationContext applicationContext
37+
@Autowired
38+
ApplicationContext applicationContext
2839

2940
@Autowired
3041
@Subject
@@ -33,10 +44,11 @@ class SampleApiTest extends Specification{
3344
@SpringBean
3445
UserService userService = Stub(UserService.class)
3546

36-
private TestRestTemplate template
47+
private TestRestTemplate template = new TestRestTemplate()
3748

3849
void setup(){}
3950

51+
@Test
4052
def "if find user by id is ok"(){
4153

4254
given:"create mock userModel"
@@ -46,16 +58,17 @@ class SampleApiTest extends Specification{
4658
userService.getUser(_) >> user
4759

4860
when:"call endpoint"
49-
def code = template.getForEntity("http://localhost:9090/api/v1//user/10", ResponseEntity<UserModel>.class as Class<Object>).statusCode
61+
def code = template.exchange("http://localhost:9090/api/v1/user/10", HttpMethod.GET, null, ResponseEntity<?>.class).statusCode
5062

5163
then:"expected return result"
5264
code.value() == "200"
5365
}
5466

5567

5668
private static UserModel generateUser() {
69+
Random random = new Random()
5770
new UserModel(
58-
id: UUID.randomUUID(),
71+
id: random.nextInt(),
5972
userName: "sample",
6073
firstName: "first",
6174
lastName: "last",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ir.bigz.springbootreal.web
2+
3+
import ir.bigz.springbootreal.commons.util.Utils
4+
import org.springframework.test.context.ContextConfiguration
5+
import spock.lang.Specification
6+
import spock.lang.Title
7+
8+
import java.sql.Time
9+
import java.sql.Timestamp
10+
import java.time.LocalDateTime
11+
import java.time.ZoneId
12+
import java.time.ZonedDateTime
13+
14+
@ContextConfiguration(classes = [Utils.class])
15+
@Title("test tools")
16+
class ToolsTest extends Specification{
17+
18+
//this method just for show BDD test with spock
19+
def "create date base on zone"(){
20+
21+
when: "call utils return current dateTime base on specific format"
22+
def now = Utils.getTimeNow()
23+
24+
then: "expected printed time is current time"
25+
ZonedDateTime.now(ZoneId.of("Asia/Tehran")).format(Utils.FORMATTER) == now
26+
}
27+
}

0 commit comments

Comments
 (0)