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
Merge requests
!125
Added userrepositorytest
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Added userrepositorytest
testUserRepository
into
dev
Overview
2
Commits
2
Pipelines
2
Changes
1
All threads resolved!
Hide all comments
Merged
Mads Lundegaard
requested to merge
testUserRepository
into
dev
5 years ago
Overview
2
Commits
2
Pipelines
2
Changes
1
All threads resolved!
Hide all comments
Expand
Added some tests for user repository
0
0
Merge request reports
Compare
dev
version 1
b83ce08b
5 years ago
dev (base)
and
latest version
latest version
87651a2c
2 commits,
5 years ago
version 1
b83ce08b
1 commit,
5 years ago
1 file
+
116
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/test/java/NTNU/IDATT1002/repository/UserRepositoryTest.java
0 → 100644
+
116
−
0
Options
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
Loading