Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sprachschulsystem
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
Jan Pokorný
Sprachschulsystem
Commits
09b2b0cd
There was an error fetching the commit references. Please try again later.
Commit
09b2b0cd
authored
1 year ago
by
Dominika Zemanovičová
Browse files
Options
Downloads
Patches
Plain Diff
Fix ExerciseControllerTest
parent
01e399a5
No related branches found
No related tags found
2 merge requests
!31
M2
,
!30
M2 exercise
Pipeline
#
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseControllerTest.java
+31
-103
31 additions, 103 deletions
...useri/moduleexercise/exercise/ExerciseControllerTest.java
with
31 additions
and
103 deletions
application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java
→
application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/Exercise
Controller
Test.java
+
31
−
103
View file @
09b2b0cd
package
org.fuseri.moduleexercise.exercise
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
//import org.fuseri.model.dto.common.Result;
import
jakarta.persistence.EntityNotFoundException
;
import
org.fuseri.model.dto.exercise.ExerciseCreateDto
;
import
org.fuseri.model.dto.exercise.ExerciseDto
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentMatchers
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
delete
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
put
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
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
;
@SpringBootTest
@AutoConfigureMockMvc
public
class
ExerciseTest
{
public
class
ExerciseControllerTest
{
@Autowired
ObjectMapper
objectMapper
;
@Autowired
private
MockMvc
mockMvc
;
@MockBean
ExerciseFacade
facade
;
...
...
@@ -52,7 +46,6 @@ public class ExerciseTest {
}
}
// private ExerciseCreateDto exercise;
private
ExerciseDto
exerciseDto
;
private
ExerciseDto
exerciseDto1
;
private
ExerciseDto
exerciseDto2
;
...
...
@@ -84,92 +77,41 @@ public class ExerciseTest {
exerciseCreateDto
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
0
);
exerciseCreateDto1
=
new
ExerciseCreateDto
(
"idioms1"
,
"exercise on intermediate idioms"
,
2
,
0
);
exerciseCreateDto2
=
new
ExerciseCreateDto
(
"idioms2"
,
"exercise on basic idioms"
,
1
,
0L
);
try
{
mockMvc
.
perform
(
post
(
"/exercises"
).
content
(
asJsonString
(
exerciseCreateDto
)).
contentType
(
MediaType
.
APPLICATION_JSON
));
mockMvc
.
perform
(
post
(
"/exercises"
).
content
(
asJsonString
(
exerciseCreateDto1
)).
contentType
(
MediaType
.
APPLICATION_JSON
));
mockMvc
.
perform
(
post
(
"/exercises"
).
content
(
asJsonString
(
exerciseCreateDto2
)).
contentType
(
MediaType
.
APPLICATION_JSON
));
}
catch
(
Exception
e
)
{
assert
(
false
);
}
}
@AfterEach
void
cleanup
()
{
try
{
mockMvc
.
perform
(
delete
(
"/questions/reset"
));
mockMvc
.
perform
(
delete
(
"/exercises/reset"
));
mockMvc
.
perform
(
delete
(
"/answers/reset"
));
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
@Test
void
getExercise
()
{
void
getExercise
()
throws
Exception
{
long
id
=
1L
;
when
(
facade
.
find
(
id
)).
thenReturn
(
exerciseDto
);
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
smth
=
mockMvc
.
perform
(
get
(
theId
));
smth
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.name"
,
is
(
exerciseCreateDto
.
getName
())))
.
andExpect
(
jsonPath
(
"$.description"
,
is
(
exerciseCreateDto
.
getDescription
())))
.
andExpect
(
jsonPath
(
"$.courseId"
,
is
((
int
)
exerciseCreateDto
.
getCourseId
())))
.
andExpect
(
jsonPath
(
"$.difficulty"
,
is
(
exerciseCreateDto
.
getDifficulty
())));
}
catch
(
Exception
e
)
{
//do absolutely nothing
}
}
@Test
void
deleteExercise
()
{
long
id
=
1L
;
// when(facade.delete(id));
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
smth
=
mockMvc
.
perform
(
delete
(
theId
));
smth
.
andExpect
(
status
().
isNoContent
());
}
catch
(
Exception
e
)
{
//do absolutely nothing
}
mockMvc
.
perform
(
get
(
"/exercises/{id}"
,
id
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.name"
,
is
(
exerciseCreateDto
.
getName
())))
.
andExpect
(
jsonPath
(
"$.description"
,
is
(
exerciseCreateDto
.
getDescription
())))
.
andExpect
(
jsonPath
(
"$.courseId"
,
is
((
int
)
exerciseCreateDto
.
getCourseId
())))
.
andExpect
(
jsonPath
(
"$.difficulty"
,
is
(
exerciseCreateDto
.
getDifficulty
())));
}
@Test
void
deleteExercise_notFound
()
{
long
id
=
999999L
;
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
smth
=
mockMvc
.
perform
(
delete
(
theId
));
smth
.
andExpect
(
status
().
isNoContent
());
}
catch
(
Exception
e
)
{
//do absolutely nothing
}
void
testDelete
()
throws
Exception
{
mockMvc
.
perform
(
delete
(
"/exercises/1"
))
.
andExpect
(
status
().
is2xxSuccessful
());
}
@Test
void
getExercise_notFound
()
{
long
id
=
999999
L
;
void
getExercise_notFound
()
throws
Exception
{
long
id
=
1
L
;
when
(
facade
.
find
(
id
)).
thenThrow
(
new
EntityNotFoundException
(
""
));
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
smth
=
mockMvc
.
perform
(
get
(
theId
));
smth
.
andExpect
(
status
().
isNotFound
());
}
catch
(
Exception
e
)
{
//do absolutely nothing
}
mockMvc
.
perform
(
get
(
"/exercises/{id}"
,
id
))
.
andExpect
(
status
().
isNotFound
());
}
@Test
void
FindAll
()
{
when
(
facade
.
findAll
(
0
)).
thenReturn
(
new
PageImpl
<>(
new
ArrayList
<>()));
try
{
var
response
=
mockMvc
.
perform
(
get
(
"/exercises"
).
param
(
"page"
,
"0"
));
var
dis
=
response
.
andExpect
(
status
().
isOk
())
var
dis
=
response
.
andExpect
(
status
().
isOk
())
.
andExpect
(
status
().
is2xxSuccessful
()).
andReturn
().
getResponse
().
getContentAsString
();
assertTrue
(
dis
.
contains
(
"\"content\":[]"
));
}
catch
(
Exception
e
)
{
...
...
@@ -181,7 +123,7 @@ public class ExerciseTest {
@Test
void
getFiltered
()
{
when
(
facade
.
findByCourseIdAndDifficulty
(
0
,
2
,
0
)).
thenReturn
(
new
PageImpl
<>(
new
ArrayList
<>()));
when
(
facade
.
findByCourseIdAndDifficulty
(
0
,
2
,
0
)).
thenReturn
(
new
PageImpl
<>(
new
ArrayList
<>()));
try
{
var
filtered
=
mockMvc
.
perform
(
get
(
"/exercises/filter"
).
param
(
"page"
,
"0"
).
param
(
"courseId"
,
"0"
).
param
(
"difficulty"
,
"2"
));
var
content
=
filtered
.
andReturn
().
getResponse
().
getContentAsString
();
...
...
@@ -194,7 +136,7 @@ public class ExerciseTest {
@Test
void
testCreateExercise
()
throws
Exception
{
when
(
facade
.
create
(
e
xerciseCreateDto
)).
thenReturn
(
exerciseDto
);
when
(
facade
.
create
(
ArgumentMatchers
.
isA
(
E
xerciseCreateDto
.
class
)
)).
thenReturn
(
exerciseDto
);
mockMvc
.
perform
(
post
(
"/exercises"
).
content
(
asJsonString
(
exerciseCreateDto
)).
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isCreated
())
.
andExpect
(
jsonPath
(
"$.name"
).
value
(
"idioms"
))
...
...
@@ -269,36 +211,22 @@ public class ExerciseTest {
@Test
void
testUpdate
()
{
void
testUpdate
()
throws
Exception
{
long
id
=
1L
;
when
(
facade
.
update
(
id
,
exerciseCreateDto
)).
thenReturn
(
exerciseDto
);
try
{
var
theId
=
String
.
format
(
"/exercises/%d"
,
id
);
var
dis
=
mockMvc
.
perform
(
put
(
theId
).
content
(
asJsonString
(
exerciseCreateDto
)).
contentType
(
MediaType
.
APPLICATION_JSON
));
when
(
facade
.
update
(
ArgumentMatchers
.
eq
(
id
),
ArgumentMatchers
.
isA
(
ExerciseCreateDto
.
class
))).
thenReturn
(
exerciseDto
);
var
theId
=
String
.
format
(
"/exercises/%d"
,
id
);
var
dis
=
mockMvc
.
perform
(
put
(
theId
).
content
(
asJsonString
(
exerciseCreateDto
)).
contentType
(
MediaType
.
APPLICATION_JSON
));
dis
.
andExpect
(
status
().
isOk
()).
andExpect
(
jsonPath
(
"$.name"
,
is
(
exerciseDto
.
getName
())))
.
andExpect
(
jsonPath
(
"$.difficulty"
,
is
(
exerciseDto
.
getDifficulty
())))
.
andExpect
(
jsonPath
(
"$.description"
,
is
(
exerciseDto
.
getDescription
())))
.
andExpect
(
jsonPath
(
"$.courseId"
,
is
(
exerciseDto
.
getCourseId
())));
//
// var str = dis.andReturn().getResponse().getContentAsString();
//
// var res = objectMapper.readValue(str, ExerciseDto.class);
//
// assert res.equals(exerciseDto);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
dis
.
andExpect
(
status
().
isOk
()).
andExpect
(
jsonPath
(
"$.name"
,
is
(
exerciseDto
.
getName
())))
.
andExpect
(
jsonPath
(
"$.difficulty"
,
is
(
exerciseDto
.
getDifficulty
())))
.
andExpect
(
jsonPath
(
"$.description"
,
is
(
exerciseDto
.
getDescription
())));
}
@Test
void
testUpdateNotFound
()
{
long
id
=
9999L
;
when
(
facade
.
update
(
id
,
e
xerciseCreateDto
)).
thenThrow
(
new
EntityNotFoundException
());
when
(
facade
.
update
(
ArgumentMatchers
.
eq
(
id
),
ArgumentMatchers
.
isA
(
E
xerciseCreateDto
.
class
)
)).
thenThrow
(
new
EntityNotFoundException
());
try
{
var
theId
=
String
.
format
(
"/exercises/%d"
,
id
);
...
...
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