Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pa165-airport-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Petr Kabourek
pa165-airport-manager
Commits
10220d53
There was an error fetching the commit references. Please try again later.
Commit
10220d53
authored
11 months ago
by
xkromm
Browse files
Options
Downloads
Patches
Plain Diff
added employee controller tests
parent
35a1b383
No related branches found
No related tags found
1 merge request
!12
Controller tests
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
airports-hr-service/src/test/java/cz/muni/fi/pa165/hr/rest/EmployeeControllerTest.java
+78
-7
78 additions, 7 deletions
...java/cz/muni/fi/pa165/hr/rest/EmployeeControllerTest.java
with
78 additions
and
7 deletions
airports-hr-service/src/test/java/cz/muni/fi/pa165/hr/rest/EmployeeControllerTest.java
+
78
−
7
View file @
10220d53
package
cz.muni.fi.pa165.hr.rest
;
import
cz.muni.fi.pa165.api.employee.Employee
;
import
cz.muni.fi.pa165.api.employee.requests.EmployeeRequest
;
import
cz.muni.fi.pa165.hr.excdption.EmployeeNotFoundException
;
import
cz.muni.fi.pa165.hr.facade.EmployeeFacade
;
import
cz.muni.fi.pa165.hr.util.ObjectConverter
;
import
cz.muni.fi.pa165.hr.util.TestApiFactory
;
...
...
@@ -14,14 +16,13 @@ import org.springframework.http.MediaType;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.nio.charset.StandardCharsets
;
import
java.time.LocalDate
;
import
java.util.UUID
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
*
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
jsonPath
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
@WebMvcTest
(
controllers
=
{
EmployeeController
.
class
})
class
EmployeeControllerTest
{
...
...
@@ -36,12 +37,10 @@ class EmployeeControllerTest {
private
EmployeeFacade
employeeFacade
;
@Test
void
findById_personFound_returnsPerson
()
throws
Exception
{
// Arrange
void
findById_employeeFound_returnsEmployee
()
throws
Exception
{
UUID
id
=
new
UUID
(
0x1
,
0xf
);
Mockito
.
when
(
employeeFacade
.
get
(
id
)).
thenReturn
(
TestApiFactory
.
getEmployeeEntity
());
// Act
String
responseJson
=
mockMvc
.
perform
(
get
(
"/employee/{id}"
,
id
)
.
accept
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
andExpect
(
status
().
isOk
())
...
...
@@ -51,8 +50,80 @@ class EmployeeControllerTest {
Employee
response
=
ObjectConverter
.
convertJsonToObject
(
responseJson
,
Employee
.
class
);
assertThat
(
responseJson
).
isEqualTo
(
"{\"id\":\"00000000-0000-0001-0000-00000000000f\",\"name\":\"Name\",\"surname\":\"Surname\",\"gender\":true,\"dateOfBirth\":\"1980-05-15\",\"hired\":\"2010-05-15\",\"terminated\":null}"
);
assertThat
(
response
).
isEqualTo
(
TestApiFactory
.
getEmployeeEntity
());
}
@Test
void
findById_employeeNotFound_throws404
()
throws
Exception
{
UUID
id
=
UUID
.
randomUUID
();
Mockito
.
when
(
employeeFacade
.
get
(
id
)).
thenThrow
(
new
EmployeeNotFoundException
());
mockMvc
.
perform
(
get
(
"/employee/{id}"
,
id
)
.
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isNotFound
());
Mockito
.
verify
(
employeeFacade
,
Mockito
.
times
(
1
)).
get
(
id
);
}
@Test
void
updateEmployee_employeeFound_returnEmployee
()
throws
Exception
{
UUID
id
=
new
UUID
(
0x1
,
0xf
);
Employee
updatedEmployee
=
TestApiFactory
.
getEmployeeEntity
();
Mockito
.
when
(
employeeFacade
.
updateEmployee
(
Mockito
.
eq
(
id
),
Mockito
.
any
(
EmployeeRequest
.
class
))).
thenReturn
(
updatedEmployee
);
String
requestJson
=
"{\"id\":\""
+
id
+
"\",\"name\":\"UpdatedName\",\"surname\":\"UpdatedSurname\",\"gender\":false,\"dateOfBirth\":\"1990-01-01\",\"hired\":\"2015-01-01\",\"terminated\":\"2022-01-01\"}"
;
String
responseJson
=
mockMvc
.
perform
(
put
(
"/employee/{id}"
,
id
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
requestJson
))
.
andExpect
(
status
().
isOk
())
.
andReturn
()
.
getResponse
()
.
getContentAsString
(
StandardCharsets
.
UTF_8
);
Employee
response
=
ObjectConverter
.
convertJsonToObject
(
responseJson
,
Employee
.
class
);
assertThat
(
response
).
isEqualTo
(
updatedEmployee
);
Mockito
.
verify
(
employeeFacade
,
Mockito
.
times
(
1
)).
updateEmployee
(
Mockito
.
eq
(
id
),
Mockito
.
any
(
EmployeeRequest
.
class
));
}
@Test
void
updateEmployee_employeeNotFound_throws404
()
throws
Exception
{
UUID
id
=
UUID
.
randomUUID
();
Mockito
.
when
(
employeeFacade
.
updateEmployee
(
Mockito
.
eq
(
id
),
Mockito
.
any
(
EmployeeRequest
.
class
))).
thenThrow
(
new
EmployeeNotFoundException
());
mockMvc
.
perform
(
put
(
"/employee/{id}"
,
id
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
"{\"id\":\""
+
id
.
toString
()
+
"\",\"name\":\"UpdatedName\",\"surname\":\"UpdatedSurname\",\"gender\":false,\"dateOfBirth\":\"1990-01-01\",\"hired\":\"2015-01-01\",\"terminated\":\"2022-01-01\"}"
))
.
andExpect
(
status
().
isNotFound
());
Mockito
.
verify
(
employeeFacade
,
Mockito
.
times
(
1
)).
updateEmployee
(
Mockito
.
eq
(
id
),
Mockito
.
any
(
EmployeeRequest
.
class
));
}
@Test
void
terminateEmployee_employeeFound
()
throws
Exception
{
UUID
id
=
UUID
.
randomUUID
();
Employee
terminatedEmployee
=
TestApiFactory
.
getEmployeeEntity
();
terminatedEmployee
.
setTerminated
(
LocalDate
.
now
());
Mockito
.
when
(
employeeFacade
.
terminate
(
id
)).
thenReturn
(
terminatedEmployee
);
mockMvc
.
perform
(
delete
(
"/employee/{id}"
,
id
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.terminated"
).
exists
())
.
andExpect
(
jsonPath
(
"$.terminated"
).
value
(
LocalDate
.
now
().
toString
()));
Mockito
.
verify
(
employeeFacade
,
Mockito
.
times
(
1
)).
terminate
(
id
);
}
@Test
void
terminateEmployee_employeeNotFound_throwsEmployeeNotFoundException
()
throws
Exception
{
UUID
id
=
UUID
.
randomUUID
();
Mockito
.
when
(
employeeFacade
.
terminate
(
id
)).
thenThrow
(
new
EmployeeNotFoundException
());
mockMvc
.
perform
(
delete
(
"/employee/{id}"
,
id
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isNotFound
());
Mockito
.
verify
(
employeeFacade
,
Mockito
.
times
(
1
)).
terminate
(
id
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment