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
9fb47f87
Commit
9fb47f87
authored
2 years ago
by
Birk Øvstetun Narvhus
Browse files
Options
Downloads
Patches
Plain Diff
added more tests on the fridge controller
parent
b237a985
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/group/FridgeControllerTest.java
+216
-49
216 additions, 49 deletions
.../v233/SmartMat/controller/group/FridgeControllerTest.java
with
216 additions
and
49 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeControllerTest.java
+
216
−
49
View file @
9fb47f87
...
...
@@ -7,7 +7,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso;
import
ntnu.idatt2016.v233.SmartMat.entity.group.Fridge
;
import
ntnu.idatt2016.v233.SmartMat.entity.product.Product
;
import
ntnu.idatt2016.v233.SmartMat.service.group.FridgeService
;
import
ntnu.idatt2016.v233.SmartMat.service.group.GroupService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Nested
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.InjectMocks
;
...
...
@@ -27,8 +29,7 @@ import java.util.Optional;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.*;
@ExtendWith
(
MockitoExtension
.
class
)
...
...
@@ -43,6 +44,9 @@ public class FridgeControllerTest {
@Mock
private
FridgeService
fridgeService
;
@Mock
private
GroupService
groupService
;
private
Fridge
fridge
;
private
Product
product
;
...
...
@@ -220,87 +224,250 @@ public class FridgeControllerTest {
}
@Test
public
void
addProductToFridgeAsUser
()
throws
Exception
{
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
@Nested
class
addProduct
{
@Test
public
void
addProductToFridgeAsUserNotAutorized
()
throws
Exception
{
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
false
);
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
addProductToFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
).
isUserInFridge
(
"t
est
"
,
0L
);
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
addProductToFridge
(
fridgeProductRequ
est
,
regularUser
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
verify
(
fridgeService
).
isUserInFridge
(
"test"
,
0L
);
}
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
@Test
public
void
addProductToFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
addProductToFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
empty
(
));
}
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
@Test
public
void
addProductToFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
addProductToFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
empty
(
));
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
addProductToFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
).
isUserInFridge
(
"t
est
"
,
0L
);
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
addProductToFridge
(
fridgeProductRequ
est
,
regularUser
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
}
verify
(
fridgeService
).
isUserInFridge
(
"test"
,
0L
);
@Test
public
void
updateProductInFridge
()
throws
Exception
{
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
of
(
fridgeProductAsso
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
}
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridge
Controller
.
updateProductInFridge
(
fridgeProductRequest
,
adminUser
);
@Test
public
void
addProductTOFridgeAsAdmin
(){
when
(
fridge
Service
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
)
);
when
(
fridgeService
.
addProductToFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
of
(
product
));
verify
(
fridgeService
).
update
Product
In
Fridge
(
any
(
F
ridgeProductRequest
.
class
)
);
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
add
Product
To
Fridge
(
f
ridgeProductRequest
,
adminUser
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
}
@Test
public
void
updateProductInFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
empty
());
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
adminUser
);
verify
(
fridgeService
).
addProductToFridge
(
any
(
FridgeProductRequest
.
class
));
}
@Test
public
void
addProductTOFridgeAsRegUserAllowed
(){
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
when
(
fridgeService
.
addProductToFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
of
(
product
));
ResponseEntity
<
Product
>
responseEntity
=
fridgeController
.
addProductToFridge
(
fridgeProductRequest
,
regularUser
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
verify
(
fridgeService
).
addProductToFridge
(
any
(
FridgeProductRequest
.
class
));
}
verify
(
fridgeService
).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
}
@Test
public
void
removeProductFromFridge_success
()
throws
Exception
{
when
(
fridgeService
.
removeProductFromFridge
(
1L
)).
thenReturn
(
true
);
@Nested
class
updateProduct
{
@Test
public
void
updateProductInFridge
()
throws
Exception
{
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
of
(
fridgeProductAsso
));
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
adminUser
);
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
adminUser
);
verify
(
fridgeService
).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
verify
(
fridgeService
).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
}
@Test
public
void
updateProductInFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
))).
thenReturn
(
Optional
.
empty
());
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
adminUser
);
verify
(
fridgeService
).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
}
@Test
public
void
updateNotAuthorized
()
throws
Exception
{
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
false
);
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
,
times
(
0
)).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
}
@Test
public
void
updateNotAuthorizedRestricted
()
throws
Exception
{
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
when
(
groupService
.
getUserGroupAssoAuthority
(
"test"
,
1L
))
.
thenReturn
(
"restricted"
);
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
,
times
(
0
)).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
}
@Test
public
void
updateAuthorizedWhenNotRestricted
(){
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
when
(
groupService
.
getUserGroupAssoAuthority
(
"test"
,
1L
))
.
thenReturn
(
"USER"
);
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
)))
.
thenReturn
(
Optional
.
of
(
fridgeProductAsso
));
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
,
times
(
1
)).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
HttpStatus
.
OK
,
responseEntity
.
getStatusCode
());
}
@Test
public
void
updateAuthorizedWhenAdmin
(){
when
(
fridgeService
.
isUserInFridge
(
"test"
,
0L
)).
thenReturn
(
true
);
when
(
groupService
.
getUserGroupAssoAuthority
(
"test"
,
1L
))
.
thenReturn
(
"ADMIN"
);
when
(
fridgeService
.
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
)))
.
thenReturn
(
Optional
.
of
(
fridgeProductAsso
));
when
(
fridgeService
.
getFridgeByGroupId
(
1L
)).
thenReturn
(
Optional
.
of
(
fridge
));
ResponseEntity
<
FridgeProductAsso
>
responseEntity
=
fridgeController
.
updateProductInFridge
(
fridgeProductRequest
,
regularUser
);
verify
(
fridgeService
,
times
(
1
)).
updateProductInFridge
(
any
(
FridgeProductRequest
.
class
));
assertEquals
(
HttpStatus
.
OK
,
responseEntity
.
getStatusCode
());
}
}
@Test
public
void
removeProductFromFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
removeProductFromFridge
(
1L
)).
thenReturn
(
false
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
adminUser
);
@Nested
class
removeProductFromFridge
{
@Test
public
void
removeProductFromFridge_success
()
throws
Exception
{
when
(
fridgeService
.
removeProductFromFridge
(
1L
)).
thenReturn
(
true
);
verify
(
fridgeService
).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
adminUser
);
verify
(
fridgeService
).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
}
@Test
public
void
removeProductFromFridge_notFound
()
throws
Exception
{
when
(
fridgeService
.
removeProductFromFridge
(
1L
)).
thenReturn
(
false
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
adminUser
);
verify
(
fridgeService
).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
);
}
@Test
public
void
removeProductNotAuthorizedWhenRestricted
(){
when
(
fridgeService
.
isUserInGroupWithFridgeProduct
(
"test"
,
1L
))
.
thenReturn
(
true
);
when
(
groupService
.
getUserGroupAssoAuthority
(
eq
(
"test"
),
eq
(
1L
)))
.
thenReturn
(
"restricted"
);
when
(
fridgeService
.
getGroupIdFromFridgeProuctId
(
1L
)).
thenReturn
(
1L
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
regularUser
);
verify
(
fridgeService
,
times
(
0
)).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
}
@Test
public
void
removeProductAuthorizedWhenNotRestricted
(){
when
(
fridgeService
.
removeProductFromFridge
(
1L
)).
thenReturn
(
true
);
when
(
fridgeService
.
isUserInGroupWithFridgeProduct
(
"test"
,
1L
))
.
thenReturn
(
true
);
when
(
groupService
.
getUserGroupAssoAuthority
(
eq
(
"test"
),
eq
(
1L
)))
.
thenReturn
(
"USER"
);
when
(
fridgeService
.
getGroupIdFromFridgeProuctId
(
1L
)).
thenReturn
(
1L
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
regularUser
);
verify
(
fridgeService
,
times
(
1
)).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
);
}
@Test
public
void
notRemoveProductWhenNotInGroup
(){
when
(
fridgeService
.
isUserInGroupWithFridgeProduct
(
"test"
,
1L
))
.
thenReturn
(
false
);
ResponseEntity
<
String
>
responseEntity
=
fridgeController
.
removeProductFromFridge
(
1L
,
regularUser
);
verify
(
fridgeService
,
times
(
0
)).
removeProductFromFridge
(
1L
);
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
FORBIDDEN
);
}
}
}
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