Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
Orders
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Vladimir Trubachoff
Orders
Commits
9aba07ea
Commit
9aba07ea
authored
Jul 25, 2024
by
Vladimir Trubachoff
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'add-tests' into develop
parents
7c7f55d1
ec405098
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
260 additions
and
126 deletions
+260
-126
Order.java
src/main/java/com/example/testj/domain/Order.java
+4
-2
OrderLine.java
src/main/java/com/example/testj/domain/OrderLine.java
+3
-3
ApiError.java
src/main/java/com/example/testj/exception/ApiError.java
+39
-0
RestResponseEntityExceptionHandler.java
...e/testj/exception/RestResponseEntityExceptionHandler.java
+55
-0
OrderDto.java
src/main/java/com/example/testj/service/dto/OrderDto.java
+10
-3
data.sql
src/main/resources/data.sql
+108
-100
OrderRepositoryTest.java
...ava/com/example/testj/repository/OrderRepositoryTest.java
+3
-2
OrderLineServiceTest.java
.../java/com/example/testj/service/OrderLineServiceTest.java
+2
-1
OrderServiceTest.java
...test/java/com/example/testj/service/OrderServiceTest.java
+7
-6
GoodsControllerTest.java
.../java/com/example/testj/web/rest/GoodsControllerTest.java
+4
-1
OrderControllerTest.java
.../java/com/example/testj/web/rest/OrderControllerTest.java
+25
-8
No files found.
src/main/java/com/example/testj/domain/Order.java
View file @
9aba07ea
...
...
@@ -2,12 +2,14 @@ package com.example.testj.domain;
import
jakarta.persistence.*
;
import
jakarta.validation.constraints.NotBlank
;
import
jakarta.validation.constraints.NotNull
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -27,10 +29,10 @@ public class Order {
@Column
(
name
=
"client"
)
private
String
client
;
@Not
Blank
@Not
Null
@DateTimeFormat
(
iso
=
DateTimeFormat
.
ISO
.
DATE
)
@Column
(
name
=
"date"
)
private
String
date
;
private
LocalDate
date
;
@NotBlank
@Column
(
name
=
"address"
)
...
...
src/main/java/com/example/testj/domain/OrderLine.java
View file @
9aba07ea
...
...
@@ -22,19 +22,19 @@ public class OrderLine {
@Column
(
name
=
"id"
)
private
Long
id
;
@NotNull
@JsonIgnore
@ManyToOne
@JoinColumn
(
name
=
"order_id"
)
@NotNull
private
Order
order
;
@NotNull
@ManyToOne
@JoinColumn
(
name
=
"goods_id"
)
@NotNull
private
Goods
goods
;
@Column
(
name
=
"count"
)
@NumberFormat
@Min
(
1
)
@Column
(
name
=
"count"
)
private
int
count
;
}
src/main/java/com/example/testj/exception/ApiError.java
0 → 100644
View file @
9aba07ea
package
com
.
example
.
testj
.
exception
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.springframework.http.HttpStatus
;
import
java.util.*
;
@Getter
@Setter
public
class
ApiError
{
private
final
Date
timestamp
;
private
HttpStatus
status
;
private
String
message
;
private
List
<
Map
<
String
,
String
>>
errors
;
public
ApiError
()
{
timestamp
=
new
Date
();
}
public
ApiError
(
HttpStatus
status
,
String
message
,
List
<
Map
<
String
,
String
>>
errors
)
{
super
();
timestamp
=
new
Date
();
this
.
status
=
status
;
this
.
message
=
message
;
this
.
errors
=
errors
;
}
public
ApiError
(
HttpStatus
status
,
String
message
,
String
error
)
{
super
();
timestamp
=
new
Date
();
this
.
status
=
status
;
this
.
message
=
message
;
errors
=
new
ArrayList
<>((
Collection
)
Map
.
of
(
"message"
,
error
));
}
}
\ No newline at end of file
src/main/java/com/example/testj/exception/RestResponseEntityExceptionHandler.java
0 → 100644
View file @
9aba07ea
package
com
.
example
.
testj
.
exception
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatusCode
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
java.time.format.DateTimeParseException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
@ControllerAdvice
public
class
RestResponseEntityExceptionHandler
extends
ResponseEntityExceptionHandler
{
@ExceptionHandler
({
DateTimeParseException
.
class
})
public
ResponseEntity
<
Object
>
handleDateTimeParseException
(
Exception
ex
,
WebRequest
request
)
{
List
<
Map
<
String
,
String
>>
errors
=
List
.
of
(
Map
.
of
(
"field"
,
"date"
,
"message"
,
ex
.
getMessage
()));
ApiError
body
=
new
ApiError
(
HttpStatus
.
BAD_REQUEST
,
"Date parse error"
,
errors
);
return
new
ResponseEntity
<>(
body
,
new
HttpHeaders
(),
HttpStatus
.
BAD_REQUEST
);
}
// 500
@ExceptionHandler
({
Exception
.
class
})
public
ResponseEntity
<
Object
>
handleAll
(
Exception
ex
,
WebRequest
request
)
{
final
ApiError
apiError
=
new
ApiError
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
ex
.
getLocalizedMessage
(),
"error occurred"
);
return
new
ResponseEntity
<>(
apiError
,
new
HttpHeaders
(),
apiError
.
getStatus
());
}
protected
ResponseEntity
<
Object
>
handleMethodArgumentNotValid
(
MethodArgumentNotValidException
ex
,
HttpHeaders
headers
,
HttpStatusCode
status
,
WebRequest
request
)
{
//Get all fields errors
List
<
Map
<
String
,
String
>>
errors
=
ex
.
getBindingResult
()
.
getFieldErrors
()
.
stream
()
.
map
(
err
->
Map
.
of
(
"field"
,
err
.
getField
(),
"message"
,
err
.
getDefaultMessage
()))
.
collect
(
Collectors
.
toList
());
ApiError
body
=
new
ApiError
(
HttpStatus
.
BAD_REQUEST
,
ex
.
getBody
().
getDetail
(),
errors
);
return
handleExceptionInternal
(
ex
,
body
,
headers
,
status
,
request
);
}
protected
ResponseEntity
<
Object
>
handleHttpMessageNotReadable
(
HttpMessageNotReadableException
ex
,
HttpHeaders
headers
,
HttpStatusCode
status
,
WebRequest
request
)
{
ApiError
body
=
new
ApiError
(
HttpStatus
.
BAD_REQUEST
,
ex
.
getMessage
(),
new
ArrayList
<>());
return
handleExceptionInternal
(
ex
,
body
,
headers
,
status
,
request
);
}
}
src/main/java/com/example/testj/service/dto/OrderDto.java
View file @
9aba07ea
package
com
.
example
.
testj
.
service
.
dto
;
import
jakarta.validation.constraints.NotBlank
;
import
lombok.*
;
import
jakarta.validation.constraints.NotNull
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
java.io.Serializable
;
import
java.time.LocalDate
;
import
java.util.List
;
/**
...
...
@@ -19,8 +25,9 @@ public class OrderDto implements Serializable {
@NotBlank
private
String
client
;
@NotBlank
private
String
date
;
@NotNull
@DateTimeFormat
(
iso
=
DateTimeFormat
.
ISO
.
DATE
)
private
LocalDate
date
;
@NotBlank
private
String
address
;
...
...
src/main/resources/data.sql
View file @
9aba07ea
...
...
@@ -10,581 +10,589 @@ values
(
1
,
'Griz Soppit'
,
'
1/8/2023
'
,
'
2023-08-01
'
,
'4 Fieldstone Pass'
),
(
2
,
'Lorain Kroin'
,
'2/23/2024'
,
'3 Boyd Hill'
),
(
2
,
'Lorain Kroin'
,
'2024-03-02'
,
'3 Boyd Hill'
),
(
3
,
'Benedikta Earry'
,
'
12/2/2023
'
,
'
2023-02-12
'
,
'9 Ridge Oak Road'
),
(
4
,
'Viola Airdrie'
,
'
7/6/2023
'
,
'
2023-06-07
'
,
'587 Sloan Drive'
),
(
5
,
'Tedmund Yukhin'
,
'
1/9/2024
'
,
'
2024-09-01
'
,
'1 Northport Trail'
),
(
6
,
'Niki Thibodeaux'
,
'
1/5/2024
'
,
'
2024-05-01
'
,
'734 Vera Point'
),
(
7
,
'Sheri Habershaw'
,
'
5/5/2024
'
,
'
2024-05-05
'
,
'7689 Melrose Center'
),
(
8
,
'Coral Dellatorre'
,
'
7/8/2023
'
,
'
2023-08-07
'
,
'95 Onsgard Junction'
),
(
9
,
'Leisha Barnicott'
,
'
3/5/2024
'
,
'
2024-05-03
'
,
'945 Hanover Park'
),
(
10
,
'Dukie Yushin'
,
'
4/11/202
4'
,
'
2024-11-0
4'
,
'9 Tennyson Junction'
),
(
11
,
'Toni Lenchenko'
,
'2
/6/2024
'
,
'2
024-06-02
'
,
'83 Jenifer Lane'
),
(
12
,
'Ynez Moncreiff'
,
'
5/2/2024
'
,
'
2024-02-05
'
,
'4182 Sutteridge Parkway'
),
(
13
,
'Forrest Schiersch'
,
'
10/10/2023
'
,
'
2023-10-10
'
,
'3 Anderson Center'
),
(
14
,
'Franky Ply'
,
'
3/6/2024
'
,
'
2024-06-03
'
,
'037 Heffernan Drive'
),
(
15
,
'Shel Aykroyd'
,
'
6/8/2023
'
,
'
2023-08-06
'
,
'53 Del Mar Terrace'
),
(
16
,
'Peyter Colegate'
,
'2
/8/2024
'
,
'2
024-08-02
'
,
'55889 Haas Point'
),
(
17
,
'Garald Blunsden'
,
'
9/3/2023
'
,
'
2023-03-09
'
,
'7 Acker Drive'
),
(
18
,
'Carree Yakunkin'
,
'
4/9/202
4'
,
'
2024-09-0
4'
,
'40 Transport Court'
),
(
19
,
'Even Axleby'
,
'
9/12/2023
'
,
'
2023-12-09
'
,
'087 Bonner Hill'
),
(
20
,
'Birch Cobelli'
,
'
9/6/2023
'
,
'
2023-06-09
'
,
'74707 Haas Circle'
),
(
21
,
'Lu Malshinger'
,
'
3/6/2024
'
,
'
2024-06-03
'
,
'667 Tony Junction'
),
(
22
,
'Hebert Canfer'
,
'
9/4/2023
'
,
'
2023-04-09
'
,
'829 Rusk Trail'
),
(
23
,
'Rickard MacQuarrie'
,
'
3/7/2024
'
,
'
2024-07-03
'
,
'123 Service Junction'
),
(
24
,
'Alyse Triplow'
,
'
12/12/2023
'
,
'
2023-12-12
'
,
'51 Jana Junction'
),
(
25
,
'Cissiee De Angelis'
,
'
1/8/2024
'
,
'
2024-08-01
'
,
'660 Talisman Place'
),
(
26
,
'Rurik Killshaw'
,
'
3/4/2024
'
,
'
2024-04-03
'
,
'3 Dapin Park'
),
(
27
,
'Carmelle Philler'
,
'
4/4/202
4'
,
'
2024-04-0
4'
,
'355 Morning Road'
),
(
28
,
'Sara-ann Pook'
,
'
3/1/2024
'
,
'
2024-01-03
'
,
'7967 Welch Plaza'
),
(
29
,
'Piper Libero'
,
'
1/3/2024
'
,
'3 Prentice Way'
),
(
29
,
'Piper Libero'
,
'
2024-03-01
'
,
'3 Prentice Way'
),
(
30
,
'Shermy Simonnin'
,
'
11/9/2023
'
,
'
2023-09-11
'
,
'44910 Debra Court'
),
(
31
,
'Ilene Flores'
,
'2
/8/2024
'
,
'2
024-08-02
'
,
'201 Brickson Park Center'
),
(
32
,
'Vonnie Packer'
,
'
9/11/2023
'
,
'
2023-11-09
'
,
'7 Park Meadow Street'
),
(
33
,
'Armstrong Jessard'
,
'
5/3/2024
'
,
'
2024-03-05
'
,
'1 Center Park'
),
(
34
,
'Binnie Pearde'
,
'
5/3/2024
'
,
'
2024-03-05
'
,
'72808 Johnson Place'
),
(
35
,
'Rena Gontier'
,
'
6/5/2023
'
,
'
2023-05-06
'
,
'277 Anzinger Terrace'
),
(
36
,
'Calli Cockshoot'
,
'
11/3/2023
'
,
'
2023-03-11
'
,
'23 Glendale Circle'
),
(
37
,
'Mariana Kift'
,
'
9/1/2023
'
,
'94 Linden Hill'
),
(
37
,
'Mariana Kift'
,
'
2023-01-09
'
,
'94 Linden Hill'
),
(
38
,
'Celestia Grewe'
,
'
4/6/202
4'
,
'
2024-06-0
4'
,
'0 Forster Way'
),
(
39
,
'Westbrook Thomsson'
,
'
5/3/2024
'
,
'
2024-03-05
'
,
'097 Mcbride Pass'
),
(
40
,
'Charley Westrip'
,
'
11/8/2023
'
,
'
2023-08-11
'
,
'30327 Moland Alley'
),
(
41
,
'Currie Bartosek'
,
'
1/8/2024
'
,
'
2024-08-01
'
,
'34503 Rockefeller Trail'
),
(
42
,
'Quincy Breese'
,
'
11/9/2023
'
,
'
2023-09-11
'
,
'03 Springs Road'
),
(
43
,
'Blaine Giff'
,
'
12/6/2023
'
,
'
2023-06-12
'
,
'0718 Amoth Way'
),
(
44
,
'Lemmy Stidworthy'
,
'
12/2/2023
'
,
'
2023-02-12
'
,
'6258 Cottonwood Crossing'
),
(
45
,
'Feliza Erett'
,
'
3/11/2024
'
,
'
2024-11-03
'
,
'2588 Brentwood Avenue'
),
(
46
,
'Massimiliano Bogays'
,
'
10/4/2023
'
,
'
2023-04-10
'
,
'578 Northridge Parkway'
),
(
47
,
'Frannie Droogan'
,
'
5/11/2024
'
,
'
2024-11-05
'
,
'444 4th Park'
),
(
48
,
'Ichabod Goulbourne'
,
'
9/8/2023
'
,
'
2023-08-09
'
,
'325 Comanche Park'
),
(
49
,
'Rab Marishenko'
,
'
9/6/2023
'
,
'
2023-06-09
'
,
'310 Stuart Terrace'
),
(
50
,
'Elinore Healings'
,
'
10/2/2023
'
,
'
2023-02-10
'
,
'4 Clyde Gallagher Terrace'
),
(
51
,
'Dorita Batchelder'
,
'
5/4/2024
'
,
'
2024-04-05
'
,
'241 Lien Center'
),
(
52
,
'Amandy Ridolfo'
,
'
6/3/2023
'
,
'
2023-03-06
'
,
'8 Muir Court'
),
(
53
,
'Daveta Mantha'
,
'
8/9/2023
'
,
'
2023-09-08
'
,
'0809 Coolidge Place'
),
(
54
,
'Adolf Ricoald'
,
'2
/5/2024
'
,
'2
024-05-02
'
,
'90865 Lawn Park'
),
(
55
,
'Ilene Hardey'
,
'
3/2/2024
'
,
'
2024-02-03
'
,
'72 Redwing Hill'
),
(
56
,
'Hortense Settle'
,
'
12/4/2023
'
,
'
2023-04-12
'
,
'488 Lakeland Alley'
),
(
57
,
'Rakel Gunter'
,
'
12/8/2023
'
,
'
2023-08-12
'
,
'33 Melvin Street'
),
(
58
,
'Marielle Semour'
,
'
3/1/2024
'
,
'
2024-01-03
'
,
'804 Ohio Place'
),
(
59
,
'Ciro Paulillo'
,
'
9/6/2023
'
,
'
2023-06-09
'
,
'637 Spohn Plaza'
),
(
60
,
'Danyette Parmenter'
,
'
7/5/2023
'
,
'
2023-05-07
'
,
'735 Jenna Terrace'
),
(
61
,
'Brooks Licciardi'
,
'
7/8/2023
'
,
'
2023-08-07
'
,
'01404 Waxwing Lane'
),
(
62
,
'Hunt Trusler'
,
'
6/2/2023
'
,
'
2023-02-06
'
,
'172 Hudson Crossing'
),
(
63
,
'Euphemia Raywood'
,
'
1/2/2024
'
,
'
2024-02-01
'
,
'7 Moland Park'
),
(
64
,
'Barn Poyntz'
,
'
10/9/2023
'
,
'
2023-09-10
'
,
'610 Lakewood Center'
),
(
65
,
'Ethe Croston'
,
'
11/11/2023
'
,
'
2023-11-11
'
,
'49 Kennedy Alley'
),
(
66
,
'Farr Bruhnicke'
,
'
3/6/2024
'
,
'
2024-06-03
'
,
'05 Arrowood Alley'
),
(
67
,
'Ruth Blankau'
,
'2
/4/2024
'
,
'2
024-04-02
'
,
'8683 Oakridge Pass'
),
(
68
,
'Pearline Tubbles'
,
'
11/7/2023
'
,
'
2023-07-11
'
,
'01122 Vidon Drive'
),
(
69
,
'Des Penddreth'
,
'
5/3/2024
'
,
'
2024-03-05
'
,
'87034 Melody Center'
),
(
70
,
'Abram Paddeley'
,
'
12/12/2023
'
,
'
2023-12-12
'
,
'97 Magdeline Avenue'
),
(
71
,
'Corly Mc Meekin'
,
'
10/3/2023
'
,
'
2023-03-10
'
,
'0 Forest Drive'
),
(
72
,
'Arvie Daen'
,
'
11/3/2023
'
,
'
2023-03-11
'
,
'063 Thierer Pass'
),
(
73
,
'Karla Waryk'
,
'
3/4/2024
'
,
'
2024-04-03
'
,
'072 Lindbergh Circle'
),
(
74
,
'Axel Syson'
,
'
9/6/2023
'
,
'
2023-06-09
'
,
'9880 Butternut Trail'
),
(
75
,
'Elliot Beswick'
,
'
11/3/2023
'
,
'
2023-03-11
'
,
'81686 Lakewood Way'
),
(
76
,
'Stepha Jan'
,
'
4/4/202
4'
,
'
2024-04-0
4'
,
'3 Weeping Birch Place'
),
(
77
,
'Ignace Rockey'
,
'
11/5/2023
'
,
'
2023-05-11
'
,
'82131 Rieder Parkway'
),
(
78
,
'Zelig Oughton'
,
'
3/22/2024
'
,
'
2024-02-03
'
,
'5 School Avenue'
),
(
79
,
'Madeleine Loudiane'
,
'
7/9/2023
'
,
'
2023-09-07
'
,
'4 Vahlen Court'
),
(
80
,
'Courtney Roke'
,
'
1/6/2024
'
,
'
2024-06-01
'
,
'1 Donald Circle'
),
(
81
,
'Cyrillus Jordon'
,
'
11/1/2023
'
,
'
2023-01-11
'
,
'4229 Longview Court'
),
(
82
,
'Matthaeus Batterham'
,
'
11/5/2023
'
,
'
2023-05-11
'
,
'928 Kings Drive'
),
(
83
,
'Rock Kellington'
,
'
8/2/2023
'
,
'
2023-02-08
'
,
'5403 2nd Street'
),
(
84
,
'Zolly Screase'
,
'
7/4/2023
'
,
'
2023-04-07
'
,
'09057 Bayside Street'
),
(
85
,
'Walden Sholl'
,
'
10/10/2023
'
,
'
2023-10-10
'
,
'3082 Tomscot Terrace'
),
(
86
,
'Julienne Corballis'
,
'
3/4/2024
'
,
'
2024-04-03
'
,
'5 Johnson Alley'
),
(
87
,
'Flemming Mecco'
,
'
6/2/2023
'
,
'
2023-02-06
'
,
'99211 Killdeer Pass'
),
(
88
,
'Aarika Garcia'
,
'2
/2/2024
'
,
'2
024-02-02
'
,
'001 Kinsman Drive'
),
(
89
,
'Marjory Traske'
,
'
7/5/2023
'
,
'
2023-05-07
'
,
'37049 Manufacturers Center'
),
(
90
,
'Ida Newlands'
,
'
1/9/2024
'
,
'
2024-09-01
'
,
'00 Thierer Alley'
),
(
91
,
'Letti Merrisson'
,
'
7/7/2023
'
,
'
2023-07-07
'
,
'36 Green Plaza'
),
(
92
,
'Fran Bullene'
,
'7/2/2023'
,
'5 Bowman Plaza'
),
(
92
,
'Fran Bullene'
,
'2023-02-07'
,
'5 Bowman Plaza'
),
(
93
,
'Natala Harteley'
,
'
1/2/2024
'
,
'
2024-02-01
'
,
'20 High Crossing Junction'
),
(
94
,
'Golda Stout'
,
'
1/7/2024
'
,
'
2024-07-01
'
,
'7 Bunker Hill Alley'
),
(
95
,
'Stinky Le Grove'
,
'
12/3/2023
'
,
'
2023-03-12
'
,
'415 Haas Plaza'
),
(
96
,
'Nalani Ravenshear'
,
'
3/4/2024
'
,
'
2024-04-03
'
,
'56132 Vera Street'
),
(
97
,
'Georgia Mungham'
,
'
1/1/2024
'
,
'
2024-01-01
'
,
'680 Acker Circle'
),
(
98
,
'Elsworth Dearell'
,
'
12/4/2023
'
,
'
2023-04-12
'
,
'0875 Springview Circle'
),
(
99
,
'Philippe Madelin'
,
'
11/2/2023
'
,
'
2023-02-11
'
,
'312 Morningstar Street'
),
(
100
,
'Bing Everwin'
,
'
6/2/2023
'
,
'
2023-02-06
'
,
'07218 Mitchell Park'
);
...
...
src/test/java/com/example/testj/repository/OrderRepositoryTest.java
View file @
9aba07ea
...
...
@@ -7,6 +7,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import
org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
;
import
org.springframework.test.context.ActiveProfiles
;
import
java.time.LocalDate
;
import
java.util.List
;
import
java.util.stream.StreamSupport
;
...
...
@@ -27,7 +28,7 @@ public class OrderRepositoryTest {
Order
order
=
new
Order
();
order
.
setClient
(
"Client 1"
);
order
.
setAddress
(
"Address 1"
);
order
.
setDate
(
"2000-01-01"
);
order
.
setDate
(
LocalDate
.
of
(
2000
,
1
,
1
)
);
// when
orderRepository
.
save
(
order
);
// then
...
...
@@ -61,7 +62,7 @@ public class OrderRepositoryTest {
Order
order
=
entityManager
.
find
(
Order
.
class
,
1L
);
order
.
setAddress
(
"Address 2"
);
order
.
setClient
(
"Client 2"
);
order
.
setDate
(
"2000-01-01"
);
order
.
setDate
(
LocalDate
.
of
(
2002
,
2
,
2
)
);
// when
Order
saved
=
orderRepository
.
save
(
order
);
// then
...
...
src/test/java/com/example/testj/service/OrderLineServiceTest.java
View file @
9aba07ea
...
...
@@ -18,6 +18,7 @@ import org.mockito.Mock;
import
org.mockito.Spy
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
java.time.LocalDate
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
...
...
@@ -52,7 +53,7 @@ public class OrderLineServiceTest {
@BeforeAll
static
void
init
()
{
orderDto
=
new
OrderDto
(
1L
,
"client 1"
,
"20-20-2020"
,
"address 1"
,
null
);
orderDto
=
new
OrderDto
(
1L
,
"client 1"
,
LocalDate
.
of
(
2020
,
1
,
20
)
,
"address 1"
,
null
);
goodsDto
=
new
GoodsDto
(
1L
,
"product 1"
,
10.99
);
orderLine
=
new
OrderLine
(
1L
,
...
...
src/test/java/com/example/testj/service/OrderServiceTest.java
View file @
9aba07ea
...
...
@@ -19,6 +19,7 @@ import org.springframework.data.domain.PageImpl;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.web.PagedModel
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
...
...
@@ -46,13 +47,13 @@ public class OrderServiceTest {
order
.
setId
(
1L
);
order
.
setClient
(
"client 1"
);
order
.
setAddress
(
"address 1"
);
order
.
setDate
(
"20-20-2020"
);
order
.
setDate
(
LocalDate
.
of
(
2020
,
1
,
1
)
);
Order
order1
=
new
Order
();
order1
.
setId
(
2L
);
order1
.
setClient
(
"client 2"
);
order1
.
setAddress
(
"address 2"
);
order1
.
setDate
(
"10-10-2021"
);
order1
.
setDate
(
LocalDate
.
of
(
2020
,
2
,
20
)
);
ordersList
=
Stream
.
of
(
order
,
...
...
@@ -63,7 +64,7 @@ public class OrderServiceTest {
@Test
void
testCreateOrUpdateOrder
()
{
// given
Order
order
=
new
Order
(
null
,
"client new"
,
"01-01-2021"
,
"address new"
,
null
);
Order
order
=
new
Order
(
null
,
"client new"
,
LocalDate
.
of
(
2021
,
1
,
1
)
,
"address new"
,
null
);
Order
result
=
new
Order
(
ordersList
.
getLast
().
getId
()
+
1
,
...
...
@@ -84,7 +85,7 @@ public class OrderServiceTest {
@Disabled
void
testSaveOrderWithEmptyClient
()
{
// given
Order
order
=
new
Order
(
null
,
""
,
"01-01-2020"
,
"address new"
,
null
);
Order
order
=
new
Order
(
null
,
""
,
LocalDate
.
of
(
2020
,
1
,
1
)
,
"address new"
,
null
);
// when
assertThrows
(
ConstraintViolationException
.
class
,
()
->
orderService
.
createOrUpdateOrder
(
order
));
// then
...
...
@@ -95,7 +96,7 @@ public class OrderServiceTest {
@Disabled
void
testSaveOrderWithNullName
()
{
// given
Order
order
=
new
Order
(
null
,
null
,
"01-01-2020"
,
"address"
,
null
);
Order
order
=
new
Order
(
null
,
null
,
LocalDate
.
of
(
2020
,
1
,
1
)
,
"address"
,
null
);
// when
assertThrows
(
ConstraintViolationException
.
class
,
()
->
orderService
.
createOrUpdateOrder
(
order
));
// then
...
...
@@ -133,7 +134,7 @@ public class OrderServiceTest {
Order
order
=
ordersList
.
getFirst
();
order
.
setClient
(
"client new"
);
order
.
setAddress
(
"address new"
);
order
.
setDate
(
"02-02-2020"
);
order
.
setDate
(
LocalDate
.
of
(
2020
,
2
,
2
)
);
// when
when
(
orderRepository
.
save
(
order
)).
thenReturn
(
order
);
OrderDto
updated
=
orderService
.
createOrUpdateOrder
(
order
);
...
...
src/test/java/com/example/testj/web/rest/GoodsControllerTest.java
View file @
9aba07ea
...
...
@@ -5,6 +5,7 @@ import com.example.testj.service.GoodsService;
import
com.example.testj.service.dto.GoodsDto
;
import
com.example.testj.service.mapper.GoodsMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.hamcrest.Matchers
;
import
org.json.JSONObject
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
...
...
@@ -109,7 +110,9 @@ public class GoodsControllerTest {
JSONObject
bodyJson
=
new
JSONObject
(
"{'name': null, 'price': 0}"
);
mvc
.
perform
(
post
(
"/api/goods"
).
contentType
(
MediaType
.
APPLICATION_JSON
).
content
(
bodyJson
.
toString
()))
.
andExpect
(
status
().
isBadRequest
());
.
andExpect
(
status
().
isBadRequest
())
.
andExpect
(
jsonPath
(
"$.errors[*].field"
).
value
(
Matchers
.
containsInAnyOrder
(
"name"
,
"price"
)));
}
@Test
...
...
src/test/java/com/example/testj/web/rest/OrderControllerTest.java
View file @
9aba07ea
...
...
@@ -4,6 +4,8 @@ import com.example.testj.service.OrderService;
import
com.example.testj.service.dto.OrderDto
;
import
com.example.testj.service.mapper.OrderMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.hamcrest.Matchers
;
import
org.json.JSONObject
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
...
...
@@ -18,6 +20,7 @@ import org.springframework.data.web.PagedModel;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.stream.Stream
;
...
...
@@ -34,6 +37,9 @@ public class OrderControllerTest {
@Autowired
private
MockMvc
mvc
;
@Autowired
private
ObjectMapper
objectMapper
;
@MockBean
private
OrderService
orderService
;
...
...
@@ -46,10 +52,10 @@ public class OrderControllerTest {
static
void
init
()
{
ordersList
=
Stream
.
of
(
new
OrderDto
(
1L
,
"client 1"
,
"20-20-2020"
,
"address 1"
,
null
1L
,
"client 1"
,
LocalDate
.
of
(
2020
,
1
,
20
)
,
"address 1"
,
null
),
new
OrderDto
(
2L
,
"client 2"
,
"20-20-2020"
,
"address 2"
,
null
2L
,
"client 2"
,
LocalDate
.
of
(
2020
,
1
,
21
)
,
"address 2"
,
null
)
).
toList
();
}
...
...
@@ -83,9 +89,9 @@ public class OrderControllerTest {
@DisplayName
(
"POST /api/orders [201 Created]"
)
void
testCreateOrder
()
throws
Exception
{
OrderDto
orderDto
=
new
OrderDto
(
null
,
"client new"
,
"21-21-2021"
,
"address new"
,
null
null
,
"client new"
,
LocalDate
.
of
(
2021
,
1
,
21
)
,
"address new"
,
null
);
String
body
=
new
ObjectMapper
()
.
writeValueAsString
(
orderDto
);
String
body
=
objectMapper
.
writeValueAsString
(
orderDto
);
Long
nextId
=
(
ordersList
.
getLast
().
getId
()
+
1
);
orderDto
.
setId
(
nextId
);
...
...
@@ -96,7 +102,18 @@ public class OrderControllerTest {
.
andExpect
(
jsonPath
(
"$.id"
).
isNumber
())
.
andExpect
(
jsonPath
(
"$.client"
).
value
(
orderDto
.
getClient
()))
.
andExpect
(
jsonPath
(
"$.address"
).
value
(
orderDto
.
getAddress
()))
.
andExpect
(
jsonPath
(
"$.date"
).
value
(
orderDto
.
getDate
())).
andReturn
();
.
andExpect
(
jsonPath
(
"$.date"
).
value
(
orderDto
.
getDate
().
toString
())).
andReturn
();
}
@Test
@DisplayName
(
"POST /api/orders [400 Bad Request]"
)
void
testCreateOrderBadRequest
()
throws
Exception
{
JSONObject
bodyJson
=
new
JSONObject
(
"{'client': null, 'address': '', 'date': ''}"
);
mvc
.
perform
(
post
(
"/api/orders"
).
contentType
(
MediaType
.
APPLICATION_JSON
).
content
(
bodyJson
.
toString
()))
.
andExpect
(
status
().
isBadRequest
())
.
andExpect
(
jsonPath
(
"$.errors[*].field"
).
value
(
Matchers
.
containsInAnyOrder
(
"client"
,
"address"
,
"date"
)));
}
@Test
...
...
@@ -104,9 +121,9 @@ public class OrderControllerTest {
void
testUpdateOrder
()
throws
Exception
{
Long
id
=
1L
;
OrderDto
orderDto
=
new
OrderDto
(
id
,
"client new"
,
"21-21-2021"
,
"address new"
,
null
id
,
"client new"
,
LocalDate
.
of
(
2021
,
1
,
21
)
,
"address new"
,
null
);
String
body
=
new
ObjectMapper
()
.
writeValueAsString
(
orderDto
);
String
body
=
objectMapper
.
writeValueAsString
(
orderDto
);
when
(
orderService
.
getOrder
(
id
)).
thenReturn
(
ordersList
.
getFirst
());
when
(
orderService
.
createOrUpdateOrder
(
orderMapper
.
toEntity
(
orderDto
))).
thenReturn
(
orderDto
);
...
...
@@ -117,7 +134,7 @@ public class OrderControllerTest {
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.client"
).
value
(
orderDto
.
getClient
()))
.
andExpect
(
jsonPath
(
"$.address"
).
value
(
orderDto
.
getAddress
()))
.
andExpect
(
jsonPath
(
"$.date"
).
value
(
orderDto
.
getDate
()));
.
andExpect
(
jsonPath
(
"$.date"
).
value
(
orderDto
.
getDate
()
.
toString
()
));
}
@Test
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment