Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Team 14 - IDATT1002
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Eirik Steira
Team 14 - IDATT1002
Commits
22ff4e9b
Commit
22ff4e9b
authored
5 years ago
by
Mads Lundegaard
Committed by
Stian Fjæran Mogen
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Added userrepositorytest
parent
03083c58
No related branches found
Branches containing commit
No related tags found
1 merge request
!165
Weekly merge to Master
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/NTNU/IDATT1002/repository/UserRepositoryTest.java
+116
-0
116 additions, 0 deletions
...st/java/NTNU/IDATT1002/repository/UserRepositoryTest.java
with
116 additions
and
0 deletions
src/test/java/NTNU/IDATT1002/repository/UserRepositoryTest.java
0 → 100644
+
116
−
0
View file @
22ff4e9b
package
NTNU.IDATT1002.repository
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
NTNU.IDATT1002.models.User
;
import
NTNU.IDATT1002.service.UserService
;
import
java.util.List
;
import
java.util.Optional
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
/**
* Test for {@link UserRepository}
*/
class
UserRepositoryTest
{
private
UserRepository
userRepository
;
private
String
testUsername
;
private
User
testUser
;
/**
* Sets up necessary testdata for testin
*/
@BeforeEach
void
setUp
()
{
EntityManagerFactory
entityManagerFactory
=
Persistence
.
createEntityManagerFactory
(
"ImageApplicationTest"
);
EntityManager
entityManager
=
entityManagerFactory
.
createEntityManager
();
userRepository
=
new
UserRepository
(
entityManager
);
testUser
=
new
User
();
testUsername
=
"Test123"
;
testUser
.
setUsername
(
testUsername
);
}
/**
* Test that save a user and makes sure the user was saved
*/
@Test
void
testSaveReturnsInstance
()
{
Optional
<
User
>
savedUser
=
userRepository
.
save
(
testUser
);
assertTrue
(
savedUser
.
isPresent
());
assertEquals
(
testUsername
,
savedUser
.
get
().
getUsername
());
}
/**
* Test that tries to save user with invalid entity and make sure an empty optional is returned
*/
@Test
void
testSaveInvalidEntityRetrunsEmptyOptional
()
{
Optional
<
User
>
savedUser
=
userRepository
.
save
(
null
);
assertTrue
(
savedUser
.
isEmpty
());
}
/**
* Test that saves two users and makes sure both are saved
*/
@Test
void
testFindAllReturnsAllSavedEntities
()
{
User
testUser2
=
new
User
();
String
testUsername2
=
"Test321"
;
testUser2
.
setUsername
(
testUsername2
);
userRepository
.
save
(
testUser
);
userRepository
.
save
(
testUser2
);
List
<?>
foundUsers
=
userRepository
.
findAll
();
assertEquals
(
2
,
foundUsers
.
size
());
}
/**
* Test that saves a user and makes sure correct user is returned when searching by id
*/
@Test
void
testFindByIdReturnsOptionalWithCorrectEntity
()
{
userRepository
.
save
(
testUser
);
Optional
<
User
>
foundUSer
=
userRepository
.
findById
(
testUsername
);
assertTrue
(
foundUSer
.
isPresent
());
assertEquals
(
testUsername
,
foundUSer
.
get
().
getUsername
());
}
/**
* Test that deletes a saved user by id and makes sure it is deleted
*/
@Test
void
testDeleteByIdRemovesEntitiy
()
{
userRepository
.
save
(
testUser
);
Optional
<
User
>
foundUser
=
userRepository
.
findById
(
testUsername
);
foundUser
.
ifPresent
(
user
->
userRepository
.
deleteById
(
testUsername
));
Optional
<
User
>
deletedUser
=
userRepository
.
findById
(
testUsername
);
assertTrue
(
deletedUser
.
isEmpty
());
}
/**
* Test that deletes a saved user object and makes sure it is deleted
*/
@Test
void
testDeleteRemovesEntity
()
{
userRepository
.
save
(
testUser
);
Optional
<
User
>
foundUser
=
userRepository
.
findById
(
testUsername
);
foundUser
.
ifPresent
(
userRepository:
:
delete
);
Optional
<
User
>
deletedUser
=
userRepository
.
findById
(
testUsername
);
assertTrue
(
deletedUser
.
isEmpty
());
}
}
\ 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