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
fbd27377
Commit
fbd27377
authored
1 year ago
by
Anders Austlid
Browse files
Options
Downloads
Patches
Plain Diff
Added more unit tests for ProductController
parent
87b08335
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/product/ProductControllerTest.java
+121
-25
121 additions, 25 deletions
...33/SmartMat/controller/product/ProductControllerTest.java
with
121 additions
and
25 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductControllerTest.java
+
121
−
25
View file @
fbd27377
...
@@ -7,6 +7,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
...
@@ -7,6 +7,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import
ntnu.idatt2016.v233.SmartMat.service.AllergyService
;
import
ntnu.idatt2016.v233.SmartMat.service.AllergyService
;
import
ntnu.idatt2016.v233.SmartMat.service.product.CategoryService
;
import
ntnu.idatt2016.v233.SmartMat.service.product.CategoryService
;
import
ntnu.idatt2016.v233.SmartMat.service.product.ProductService
;
import
ntnu.idatt2016.v233.SmartMat.service.product.ProductService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.InjectMocks
;
...
@@ -15,11 +16,11 @@ import org.mockito.junit.jupiter.MockitoExtension;
...
@@ -15,11 +16,11 @@ import org.mockito.junit.jupiter.MockitoExtension;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
@@ -38,17 +39,43 @@ public class ProductControllerTest {
...
@@ -38,17 +39,43 @@ public class ProductControllerTest {
@InjectMocks
@InjectMocks
private
ProductController
productController
;
private
ProductController
productController
;
@Test
private
Product
product
;
public
void
createProduct_productDoesNotExist_returnsCreatedProduct
()
{
// Arrange
private
ProductRequest
productRequest
;
ProductRequest
productRequest
=
ProductRequest
.
builder
()
@BeforeEach
public
void
setUp
()
{
product
=
new
Product
();
product
.
setEan
(
123L
);
product
.
setName
(
"Test Product"
);
product
.
setDescription
(
"A test kylling"
);
product
.
setUnit
(
"kg"
);
product
.
setAmount
(
1.0
);
productRequest
=
ProductRequest
.
builder
()
.
ean
(
123L
)
.
ean
(
123L
)
.
name
(
"Test Product"
)
.
name
(
"Test Product"
)
.
description
(
"A test product kylling"
)
.
description
(
"A test product kylling"
)
.
image
(
"http://test.com/image.jpg"
)
.
image
(
"http://test.com/image.jpg"
)
.
allergies
(
List
.
of
(
"Gluten"
,
"Melk"
))
.
build
();
.
build
();
List
<
Allergy
>
allergies
=
Arrays
.
asList
(
Allergy
.
builder
().
name
(
"test-allergy"
).
build
(),
Allergy
.
builder
().
name
(
"test-allergy2"
).
build
()
);
List
<
Product
>
allergyProducts
=
Arrays
.
asList
(
Product
.
builder
().
ean
(
123L
).
name
(
"Test Product"
).
build
(),
Product
.
builder
().
ean
(
1234L
).
name
(
"Test Product 2"
).
build
()
);
product
.
setAllergies
(
allergies
);
allergies
.
forEach
(
allergy
->
allergy
.
setProducts
(
allergyProducts
));
}
@Test
public
void
createProduct_productDoesNotExist_returnsCreatedProduct
()
{
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
empty
());
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
empty
());
when
(
productService
.
getProductVolume
(
123L
)).
thenReturn
(
Optional
.
of
(
List
.
of
(
"1"
,
"kg"
)));
when
(
productService
.
getProductVolume
(
123L
)).
thenReturn
(
Optional
.
of
(
List
.
of
(
"1"
,
"kg"
)));
when
(
categoryService
.
getCategoryByName
(
anyString
())).
thenReturn
(
Optional
.
of
(
Category
.
builder
()
when
(
categoryService
.
getCategoryByName
(
anyString
())).
thenReturn
(
Optional
.
of
(
Category
.
builder
()
...
@@ -59,27 +86,24 @@ public class ProductControllerTest {
...
@@ -59,27 +86,24 @@ public class ProductControllerTest {
// Act
// Act
ResponseEntity
<
Product
>
response
=
productController
.
createProduct
(
productRequest
);
ResponseEntity
<
Product
>
response
=
productController
.
createProduct
(
productRequest
);
Product
product
=
response
.
getBody
();
Product
productResponse
=
response
.
getBody
();
// Assert
// Assert
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
123L
,
product
.
getEan
());
assert
productResponse
!=
null
;
assertEquals
(
"Test Product"
,
product
.
getName
());
assertEquals
(
123L
,
productResponse
.
getEan
());
assertEquals
(
"A test product kylling"
,
product
.
getDescription
());
assertEquals
(
"Test Product"
,
productResponse
.
getName
());
assertEquals
(
"http://test.com/image.jpg"
,
product
.
getUrl
());
assertEquals
(
"A test product kylling"
,
productResponse
.
getDescription
());
assertEquals
(
"kg"
,
product
.
getUnit
());
assertEquals
(
"http://test.com/image.jpg"
,
productResponse
.
getUrl
());
assertEquals
(
1.0
,
product
.
getAmount
());
assertEquals
(
"kg"
,
productResponse
.
getUnit
());
assertEquals
(
1.0
,
productResponse
.
getAmount
());
}
}
@Test
@Test
public
void
createProduct_productExists_returnsConflict
()
{
public
void
createProduct_productExists_returnsConflict
()
{
// Arrange
// Arrange
ProductRequest
productRequest
=
ProductRequest
.
builder
()
.
ean
(
123L
)
.
name
(
"Test Product"
)
.
description
(
"A test product kylling"
)
.
image
(
"http://test.com/image.jpg"
)
.
build
();
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
of
(
new
Product
()));
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
of
(
new
Product
()));
when
(
categoryService
.
getCategoryByName
(
anyString
())).
thenReturn
(
Optional
.
of
(
Category
.
builder
()
when
(
categoryService
.
getCategoryByName
(
anyString
())).
thenReturn
(
Optional
.
of
(
Category
.
builder
()
...
@@ -96,11 +120,7 @@ public class ProductControllerTest {
...
@@ -96,11 +120,7 @@ public class ProductControllerTest {
@Test
@Test
public
void
getProduct_productExists_returnsProduct
()
{
public
void
getProduct_productExists_returnsProduct
()
{
// Arrange
Product
product
=
new
Product
();
product
.
setEan
(
123L
);
product
.
setName
(
"Test Product"
);
product
.
setDescription
(
"A test kylling"
);
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
of
(
product
));
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
of
(
product
));
...
@@ -110,6 +130,7 @@ public class ProductControllerTest {
...
@@ -110,6 +130,7 @@ public class ProductControllerTest {
// Assert
// Assert
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assert
result
!=
null
;
assertEquals
(
123L
,
result
.
getEan
());
assertEquals
(
123L
,
result
.
getEan
());
assertEquals
(
"Test Product"
,
result
.
getName
());
assertEquals
(
"Test Product"
,
result
.
getName
());
}
}
...
@@ -126,4 +147,79 @@ public class ProductControllerTest {
...
@@ -126,4 +147,79 @@ public class ProductControllerTest {
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
assertFalse
(
response
.
hasBody
());
assertFalse
(
response
.
hasBody
());
}
}
@Test
public
void
getAllProducts_returnsAllProducts
()
{
// Arrange
List
<
Product
>
products
=
Arrays
.
asList
(
new
Product
(),
new
Product
(),
new
Product
()
);
when
(
productService
.
getAllProducts
()).
thenReturn
(
products
);
// Act
ResponseEntity
<
List
<
Product
>>
response
=
productController
.
getAllProducts
();
// Assert
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertNotNull
(
response
.
getBody
());
assertEquals
(
3
,
response
.
getBody
().
size
());
}
@Test
public
void
deleteProduct_productDoesNotExist_returnsNotFound
()
{
// Arrange
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
empty
());
// Act
ResponseEntity
<
String
>
response
=
productController
.
deleteProduct
(
123L
);
// Assert
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
assertEquals
(
"Product not found"
,
response
.
getBody
());
}
@Test
public
void
updateProduct_productDoesNotExist_returnsNotFound
()
{
// Arrange
when
(
productService
.
getProductById
(
123L
)).
thenReturn
(
Optional
.
empty
());
// Act
ResponseEntity
<
Product
>
response
=
productController
.
updateProduct
(
123L
,
productRequest
);
// Assert
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
}
@Test
public
void
getProductByName_productExists_returnsProduct
()
{
// Arrange
when
(
productService
.
getProductByName
(
"Test Product"
)).
thenReturn
(
Optional
.
of
(
product
));
// Act
ResponseEntity
<
Product
>
response
=
productController
.
getProductByName
(
"Test Product"
);
Product
result
=
response
.
getBody
();
// Assert
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assert
result
!=
null
;
assertEquals
(
123L
,
result
.
getEan
());
assertEquals
(
"Test Product"
,
result
.
getName
());
}
@Test
public
void
getProductByName_productDoesNotExist_returnsNotFound
()
{
// Arrange
when
(
productService
.
getProductByName
(
"Nonexistent Product"
)).
thenReturn
(
Optional
.
empty
());
// Act
ResponseEntity
<
Product
>
response
=
productController
.
getProductByName
(
"Nonexistent Product"
);
// Assert
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
assertFalse
(
response
.
hasBody
());
}
}
}
\ 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