Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
idatt2106-v23-03
backend
Commits
623be50a
Commit
623be50a
authored
1 year ago
by
Birk Øvstetun Narvhus
Browse files
Options
Downloads
Patches
Plain Diff
added test for add / remove products from shoppinglist
parent
d46aa377
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListControllerTest.java
+123
-2
123 additions, 2 deletions
.../v233/SmartMat/controller/ShoppingListControllerTest.java
with
123 additions
and
2 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListControllerTest.java
+
123
−
2
View file @
623be50a
...
...
@@ -6,9 +6,12 @@ import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import
ntnu.idatt2016.v233.SmartMat.entity.group.Group
;
import
ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso
;
import
ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId
;
import
ntnu.idatt2016.v233.SmartMat.entity.product.Product
;
import
ntnu.idatt2016.v233.SmartMat.entity.user.User
;
import
ntnu.idatt2016.v233.SmartMat.service.ShoppingListService
;
import
ntnu.idatt2016.v233.SmartMat.service.group.GroupService
;
import
ntnu.idatt2016.v233.SmartMat.service.product.ProductService
;
import
ntnu.idatt2016.v233.SmartMat.service.user.UserService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
...
...
@@ -21,12 +24,13 @@ import org.springframework.security.core.Authentication;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
*
;
@ExtendWith
(
MockitoExtension
.
class
)
public
class
ShoppingListControllerTest
{
...
...
@@ -40,6 +44,13 @@ public class ShoppingListControllerTest {
@Mock
private
GroupService
groupService
;
@Mock
private
ProductService
productService
;
@Mock
private
UserService
userService
;
private
ShoppingList
shoppingList
;
private
final
Authentication
regularUser
=
new
Authentication
()
{
...
...
@@ -199,7 +210,7 @@ public class ShoppingListControllerTest {
}
@Test
public
void
getAllShoppingListsByGroupId_notFoundReg
()
{
void
getAllShoppingListsByGroupId_notFoundReg
()
{
long
groupId
=
1
;
when
(
shoppingListService
.
getShoppingListByGroupId
(
groupId
)).
thenReturn
(
Optional
.
empty
());
when
(
groupService
.
isUserAssociatedWithGroup
(
regularUser
.
getName
(),
groupId
)).
thenReturn
(
true
);
...
...
@@ -209,4 +220,114 @@ public class ShoppingListControllerTest {
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
}
@Test
void
addToShoppinglist
(){
long
groupId
=
1
;
long
shoppingListId
=
1
;
long
ean
=
100000000007L
;
Product
product
=
Product
.
builder
()
.
ean
(
ean
)
.
build
();
User
user
=
User
.
builder
()
.
username
(
regularUser
.
getName
())
.
build
();
Group
group
=
Group
.
builder
()
.
groupId
(
groupId
)
.
build
();
UserGroupAsso
userGroupAsso
=
UserGroupAsso
.
builder
()
.
id
(
new
UserGroupId
(
user
.
getUsername
(),
group
.
getGroupId
()))
.
user
(
user
)
.
group
(
group
)
.
groupAuthority
(
"USER"
)
.
build
();
group
.
addUser
(
userGroupAsso
);
user
.
addGroup
(
userGroupAsso
);
ShoppingList
.
ShoppingListBuilder
builder
=
ShoppingList
.
builder
();
builder
.
shoppingListID
(
shoppingListId
);
builder
.
products
(
new
ArrayList
<>());
builder
.
group
(
group
);
ShoppingList
shoppingList
=
builder
.
build
();
when
(
shoppingListService
.
getShoppingListById
(
shoppingListId
)).
thenReturn
(
Optional
.
of
(
shoppingList
));
when
(
productService
.
getProductById
(
ean
)).
thenReturn
(
Optional
.
of
(
product
));
when
(
shoppingListService
.
isUserInShoppinglist
(
shoppingListId
,
regularUser
.
getName
())).
thenReturn
(
true
);
when
(
shoppingListService
.
addProductToShoppingList
(
ean
,
shoppingListId
))
.
thenReturn
(
Optional
.
of
(
shoppingList
));
when
(
userService
.
getUserFromUsername
(
regularUser
.
getName
())).
thenReturn
(
Optional
.
of
(
user
));
ResponseEntity
<?>
response
=
shoppingListController
.
addItemToShoppingList
(
shoppingListId
,
String
.
valueOf
(
ean
),
regularUser
);
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
shoppingList
.
getProducts
(),
response
.
getBody
());
}
@Test
void
removeFromShoppinglist
(){
long
groupId
=
1
;
long
shoppingListId
=
1
;
long
ean
=
100000000007L
;
Product
product
=
Product
.
builder
()
.
ean
(
ean
)
.
build
();
User
user
=
User
.
builder
()
.
username
(
regularUser
.
getName
())
.
build
();
Group
group
=
Group
.
builder
()
.
groupId
(
groupId
)
.
build
();
UserGroupAsso
userGroupAsso
=
UserGroupAsso
.
builder
()
.
id
(
new
UserGroupId
(
user
.
getUsername
(),
group
.
getGroupId
()))
.
user
(
user
)
.
group
(
group
)
.
groupAuthority
(
"USER"
)
.
build
();
group
.
addUser
(
userGroupAsso
);
user
.
addGroup
(
userGroupAsso
);
ShoppingList
.
ShoppingListBuilder
builder
=
ShoppingList
.
builder
();
builder
.
shoppingListID
(
shoppingListId
);
builder
.
products
(
List
.
of
(
product
));
builder
.
group
(
group
);
ShoppingList
shoppingList
=
builder
.
build
();
when
(
shoppingListService
.
getShoppingListById
(
shoppingListId
)).
thenReturn
(
Optional
.
of
(
shoppingList
));
when
(
shoppingListService
.
isUserInShoppinglist
(
shoppingListId
,
regularUser
.
getName
())).
thenReturn
(
true
);
when
(
shoppingListService
.
removeProductFromShoppingList
(
ean
,
shoppingListId
))
.
thenReturn
(
Optional
.
of
(
shoppingList
));
ResponseEntity
<?>
response
=
shoppingListController
.
removeProductFromShoppingList
(
String
.
valueOf
(
shoppingListId
),
String
.
valueOf
(
ean
),
regularUser
);
System
.
out
.
println
(
response
.
getBody
());
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
shoppingList
,
response
.
getBody
());
verify
(
shoppingListService
,
times
(
1
)).
removeProductFromShoppingList
(
ean
,
shoppingListId
);
}
}
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