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
07a68c53
There was an error fetching the commit references. Please try again later.
Commit
07a68c53
authored
2 years ago
by
Martin Gargalovič
Browse files
Options
Downloads
Patches
Plain Diff
added tests to exercise
parent
4a131daf
No related branches found
No related tags found
3 merge requests
!31
M2
,
!28
M2 user
,
!27
Draft: M2 user
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java
+110
-23
110 additions, 23 deletions
...java/org/fuseri/moduleexercise/exercise/ExerciseTest.java
with
110 additions
and
23 deletions
application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java
+
110
−
23
View file @
07a68c53
package
org.fuseri.moduleexercise.exercise
;
package
org.fuseri.moduleexercise.exercise
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
jakarta.persistence.EntityNotFoundException
;
import
org.fuseri.model.dto.common.Result
;
import
org.fuseri.model.dto.exercise.ExerciseCreateDto
;
import
org.fuseri.model.dto.exercise.ExerciseCreateDto
;
import
org.fuseri.model.dto.exercise.ExerciseDto
;
import
org.fuseri.model.dto.exercise.ExerciseDto
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MockMvc
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
java.util.Map
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
//import static org.assertj.core.api.ClassBasedNavigableIterableAssert.assertThat;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
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
.
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
.
result
.
MockMvcResultMatchers
.
status
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
jsonPath
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
jsonPath
;
@SpringBootTest
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureMockMvc
public
class
ExerciseTest
{
public
class
ExerciseTest
{
...
@@ -42,47 +39,83 @@ public class ExerciseTest {
...
@@ -42,47 +39,83 @@ public class ExerciseTest {
}
}
@Test
@Test
void
getExercise
()
{
void
getExercise
()
{
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
"0"
);
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
"0"
);
String
id
=
""
;
String
id
=
""
;
try
{
try
{
var
dis
=
mockMvc
.
perform
(
post
(
"/exercises"
)
var
dis
=
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise
))
.
content
(
asJsonString
(
postExercise
))
.
contentType
(
MediaType
.
APPLICATION_JSON
));
.
contentType
(
MediaType
.
APPLICATION_JSON
));
var
ok
=
dis
.
andReturn
().
getResponse
().
getContentAsString
();
var
ok
=
dis
.
andReturn
().
getResponse
().
getContentAsString
();
var
ll
=
objectMapper
.
readValue
(
ok
,
ExerciseDto
.
class
);
var
ll
=
objectMapper
.
readValue
(
ok
,
ExerciseDto
.
class
);
id
=
ll
.
getId
();
id
=
ll
.
getId
();
}
}
catch
(
Exception
e
)
{
catch
(
Exception
e
)
{
assert
(
false
);
assert
(
false
);
}
}
// no underlying database
try
{
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
smth
=
mockMvc
.
perform
(
get
(
theId
));
var
smth
=
mockMvc
.
perform
(
get
(
theId
));
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
//
throw new RuntimeException(e);
//do absolutely nothing
}
}
}
@Test
void
getFiltered
()
{
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
0
,
"0"
);
var
postExercise1
=
new
ExerciseCreateDto
(
"idioms1"
,
"exercise on basic idioms"
,
0
,
"0"
);
var
postExercise2
=
new
ExerciseCreateDto
(
"idioms2"
,
"exercise on basic idioms"
,
1
,
"0"
);
try
{
var
exercise1
=
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise
))
.
contentType
(
MediaType
.
APPLICATION_JSON
));
var
exercise2
=
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise1
))
.
contentType
(
MediaType
.
APPLICATION_JSON
));
var
exercise3
=
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise2
))
.
contentType
(
MediaType
.
APPLICATION_JSON
));
}
catch
(
Exception
e
)
{
//do absolutly nothing
}
Map
<
String
,
String
>
params
;
// params.put("page","0")
try
{
var
filtered
=
mockMvc
.
perform
(
get
(
"/exercises/filter"
)
.
param
(
"page"
,
"0"
)
.
param
(
"courseId"
,
"0"
)
.
param
(
"difficulty"
,
"0"
));
var
content
=
filtered
.
andReturn
().
getResponse
().
getContentAsString
();
var
res
=
objectMapper
.
readValue
(
content
,
new
TypeReference
<
Result
<
ExerciseDto
>>()
{
});
assert
(
res
.
getTotal
()
==
2
);
}
catch
(
Exception
e
)
{
assert
(
false
);
}
}
}
@Test
@Test
void
testCreateExercise
()
throws
Exception
{
void
testCreateExercise
()
throws
Exception
{
var
expectedResponse
=
new
ExerciseDto
();
var
expectedResponse
=
new
ExerciseDto
();
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
"0"
);
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
"0"
);
mockMvc
.
perform
(
post
(
"/exercises"
)
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise
))
.
content
(
asJsonString
(
postExercise
))
...
@@ -95,4 +128,58 @@ public class ExerciseTest {
...
@@ -95,4 +128,58 @@ public class ExerciseTest {
.
andReturn
().
getResponse
().
getContentAsString
();
.
andReturn
().
getResponse
().
getContentAsString
();
}
}
@Test
void
TestUpdate
()
{
var
postExercise
=
new
ExerciseCreateDto
(
"idioms"
,
"exercise on basic idioms"
,
2
,
"0"
);
String
id
=
""
;
try
{
var
dis
=
mockMvc
.
perform
(
post
(
"/exercises"
)
.
content
(
asJsonString
(
postExercise
))
.
contentType
(
MediaType
.
APPLICATION_JSON
));
var
ok
=
dis
.
andReturn
().
getResponse
().
getContentAsString
();
var
ll
=
objectMapper
.
readValue
(
ok
,
ExerciseDto
.
class
);
id
=
ll
.
getId
();
}
catch
(
Exception
e
)
{
assert
(
false
);
}
var
expectedExercise
=
new
ExerciseDto
();
expectedExercise
.
setId
(
id
);
expectedExercise
.
setName
(
"idioms"
);
expectedExercise
.
setDifficulty
(
2
);
expectedExercise
.
setCourseId
(
"idioms"
);
expectedExercise
.
setDescription
(
"exercise on basic idioms"
);
var
content
=
"""
{
"name": "idioms",
"description": "exercise on basic idioms",
"difficulty": 2,
"courseId": "idioms"
}
"""
;
try
{
var
theId
=
String
.
format
(
"/exercises/%s"
,
id
);
var
dis
=
mockMvc
.
perform
(
put
(
theId
).
content
(
content
).
contentType
(
MediaType
.
APPLICATION_JSON
));
var
str
=
dis
.
andReturn
().
getResponse
().
getContentAsString
();
var
res
=
objectMapper
.
readValue
(
str
,
ExerciseDto
.
class
);
var
eq
=
res
.
equals
(
expectedExercise
);
assert
res
.
equals
(
expectedExercise
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
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