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
b8ced3a1
Commit
b8ced3a1
authored
1 year ago
by
Anders Austlid
Browse files
Options
Downloads
Patches
Plain Diff
Added WasteService unit tests
parent
bca73d32
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/service/group/WasteServiceTest.java
+191
-39
191 additions, 39 deletions
...att2016/v233/SmartMat/service/group/WasteServiceTest.java
with
191 additions
and
39 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteServiceTest.java
+
191
−
39
View file @
b8ced3a1
package
ntnu.idatt2016.v233.SmartMat.service.group
;
import
ntnu.idatt2016.v233.SmartMat.dto.request.WasteRequest
;
import
ntnu.idatt2016.v233.SmartMat.entity.Waste
;
import
ntnu.idatt2016.v233.SmartMat.entity.group.Group
;
import
ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso
;
import
ntnu.idatt2016.v233.SmartMat.entity.product.Category
;
import
ntnu.idatt2016.v233.SmartMat.entity.product.Product
;
import
ntnu.idatt2016.v233.SmartMat.entity.user.User
;
import
ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository
;
import
ntnu.idatt2016.v233.SmartMat.repository.group.WasteRepository
;
import
ntnu.idatt2016.v233.SmartMat.repository.product.ProductRepository
;
import
ntnu.idatt2016.v233.SmartMat.service.group.WasteService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
java.sql.Timestamp
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
ArgumentMatchers
.*;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
@ExtendWith
(
MockitoExtension
.
class
)
public
class
WasteServiceTest
{
@InjectMocks
private
WasteService
wasteService
;
@Mock
private
WasteRepository
wasteRepository
;
/**
@Mock
private
GroupRepository
groupRepository
;
@Mock
private
ProductRepository
productRepository
;
public
Group
group
;
public
Product
product
;
public
Waste
waste
;
public
WasteRequest
wasteRequest
;
@BeforeEach
public
void
setUp
()
{
MockitoAnnotations.openMocks(this);
wasteService = new WasteService(wasteRepository);
Category
category
=
Category
.
builder
().
categoryName
(
"Test"
).
build
();
group
=
Group
.
builder
().
groupId
(
1
).
build
();
product
=
Product
.
builder
().
ean
(
1
).
amount
(
2.0
).
unit
(
"kg"
).
category
(
category
).
build
();
waste
=
Waste
.
builder
().
wasteId
(
1
).
groupId
(
group
).
ean
(
product
).
amount
(
1
).
unit
(
"kg"
).
timestamp
(
new
Timestamp
(
System
.
currentTimeMillis
())).
build
();
wasteRequest
=
new
WasteRequest
(
1
,
1
,
1
,
"kg"
);
}
@Test
void
createWaste_Success
()
{
when
(
groupRepository
.
findByGroupId
(
wasteRequest
.
groupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
productRepository
.
findById
(
wasteRequest
.
ean
())).
thenReturn
(
Optional
.
of
(
product
));
when
(
wasteRepository
.
save
(
any
(
Waste
.
class
))).
thenReturn
(
waste
);
Optional
<
Waste
>
createdWaste
=
wasteService
.
createWaste
(
wasteRequest
);
assertTrue
(
createdWaste
.
isPresent
());
assertEquals
(
waste
,
createdWaste
.
get
());
}
@Test
void
createWaste_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
wasteRequest
.
groupId
())).
thenReturn
(
Optional
.
empty
());
when
(
productRepository
.
findById
(
wasteRequest
.
ean
())).
thenReturn
(
Optional
.
empty
());
Optional
<
Waste
>
createdWaste
=
wasteService
.
createWaste
(
wasteRequest
);
assertFalse
(
createdWaste
.
isPresent
());
}
@Test
public void testCreateWaste() {
Waste waste = Waste.builder()
.groupId(1L)
.ean(1234567890123L)
.timestamp(new Timestamp(System.currentTimeMillis()))
.amount(1.0)
.unit("kg")
.build();
void
getWasteById_Success
()
{
when
(
wasteRepository
.
findById
(
waste
.
getWasteId
())).
thenReturn
(
Optional
.
of
(
waste
));
Waste createdWaste = Waste.builder()
.wasteId(1L)
.groupId(1L)
.ean(1234567890123L)
.timestamp(waste.getTimestamp())
.amount(1.0)
.unit("kg")
.build();
Optional
<
Waste
>
fetchedWaste
=
wasteService
.
getWasteById
(
waste
.
getWasteId
());
assertTrue
(
fetchedWaste
.
isPresent
());
assertEquals
(
waste
,
fetchedWaste
.
get
());
}
when(wasteRepository.save(waste)).thenReturn(createdWaste);
@Test
void
getWasteById_Failure
()
{
when
(
wasteRepository
.
findById
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Waste result = wasteService.createWaste(waste
);
Optional
<
Waste
>
fetchedWaste
=
wasteService
.
getWasteById
(
999L
);
assert
Equals(createdWaste, result
);
assert
False
(
fetchedWaste
.
isPresent
()
);
}
@Test
public void testGetWasteById() {
Waste waste = Waste.builder()
.wasteId(1L)
.groupId(1L)
.ean(1234567890123L)
.timestamp(new Timestamp(System.currentTimeMillis()))
.amount(1.0)
.unit("kg")
.build();
void
getWasteByGroupId_Success
()
{
when
(
groupRepository
.
findByGroupId
(
group
.
getGroupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
wasteRepository
.
findByGroupId
(
group
)).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
List
<
Waste
>>
fetchedWastes
=
wasteService
.
getWasteByGroupId
(
group
.
getGroupId
());
when(wasteRepository.findById(1L)).thenReturn(Optional.of(waste));
assertTrue
(
fetchedWastes
.
isPresent
());
assertEquals
(
1
,
fetchedWastes
.
get
().
size
());
assertEquals
(
waste
,
fetchedWastes
.
get
().
get
(
0
));
}
@Test
void
getWasteByGroupId_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Optional<Waste>
result
= wasteService.getWasteBy
Id(1
L);
Optional
<
List
<
Waste
>
>
fetchedWastes
=
wasteService
.
getWasteBy
GroupId
(
999
L
);
assertTrue(result.isPresent());
assertEquals(waste, result.get());
assertFalse
(
fetchedWastes
.
isPresent
());
}
*/
}
@Test
void
getWasteOfCategoryByGroupId_Success
()
{
when
(
wasteRepository
.
findAllWasteOfOneCategoryFromGroup
(
group
.
getGroupId
(),
product
.
getCategory
().
getCategoryName
())).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
List
<
Waste
>>
fetchedWastes
=
wasteService
.
getWasteOfCategoryByGroupId
(
group
.
getGroupId
(),
product
.
getCategory
().
getCategoryName
());
assertTrue
(
fetchedWastes
.
isPresent
());
assertEquals
(
1
,
fetchedWastes
.
get
().
size
());
assertEquals
(
waste
,
fetchedWastes
.
get
().
get
(
0
));
}
@Test
void
getWasteOfCategoryByGroupId_Failure
()
{
when
(
wasteRepository
.
findAllWasteOfOneCategoryFromGroup
(
anyLong
(),
anyString
())).
thenReturn
(
Optional
.
empty
());
Optional
<
List
<
Waste
>>
fetchedWastes
=
wasteService
.
getWasteOfCategoryByGroupId
(
999L
,
"Nonexistent Category"
);
assertFalse
(
fetchedWastes
.
isPresent
());
}
@Test
void
getCakeDiagram_Success
()
{
when
(
groupRepository
.
findByGroupId
(
group
.
getGroupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
wasteRepository
.
findByGroupId
(
group
)).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
double
[]>
fetchedCakeDiagram
=
wasteService
.
getCakeDiagram
(
group
.
getGroupId
());
assertTrue
(
fetchedCakeDiagram
.
isPresent
());
}
@Test
void
getCakeDiagram_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Optional
<
double
[]>
fetchedCakeDiagram
=
wasteService
.
getCakeDiagram
(
999L
);
assertFalse
(
fetchedCakeDiagram
.
isPresent
());
}
@Test
void
getLastMonth_Success
()
{
when
(
groupRepository
.
findByGroupId
(
group
.
getGroupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
wasteRepository
.
findByGroupId
(
group
)).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
double
[]>
fetchedLastMonthData
=
wasteService
.
getLastMonth
(
group
.
getGroupId
());
assertTrue
(
fetchedLastMonthData
.
isPresent
());
}
@Test
void
getLastMonth_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Optional
<
double
[]>
fetchedLastMonthData
=
wasteService
.
getLastMonth
(
999L
);
assertFalse
(
fetchedLastMonthData
.
isPresent
());
}
@Test
void
getLostMoney_Success
()
{
when
(
groupRepository
.
findByGroupId
(
group
.
getGroupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
wasteRepository
.
findByGroupId
(
group
)).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
Double
>
fetchedLostMoney
=
wasteService
.
getLostMoney
(
group
.
getGroupId
());
assertTrue
(
fetchedLostMoney
.
isPresent
());
}
@Test
void
getLostMoney_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Optional
<
Double
>
fetchedLostMoney
=
wasteService
.
getLostMoney
(
999L
);
assertFalse
(
fetchedLostMoney
.
isPresent
());
}
@Test
void
getCO2PerPerson_Success
()
{
when
(
groupRepository
.
findByGroupId
(
group
.
getGroupId
())).
thenReturn
(
Optional
.
of
(
group
));
when
(
groupRepository
.
countAllUserInGroup
(
group
.
getGroupId
())).
thenReturn
(
1
);
when
(
wasteRepository
.
findByGroupId
(
group
)).
thenReturn
(
Optional
.
of
(
Collections
.
singletonList
(
waste
)));
Optional
<
Double
>
fetchedCO2PerPerson
=
wasteService
.
getCO2PerPerson
(
group
.
getGroupId
());
assertTrue
(
fetchedCO2PerPerson
.
isPresent
());
}
@Test
void
getCO2PerPerson_Failure
()
{
when
(
groupRepository
.
findByGroupId
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
Optional
<
Double
>
fetchedCO2PerPerson
=
wasteService
.
getCO2PerPerson
(
999L
);
assertFalse
(
fetchedCO2PerPerson
.
isPresent
());
}
@Test
void
isUserAssosiatedWithWaste_Failure
()
{
when
(
wasteRepository
.
findById
(
anyLong
())).
thenReturn
(
Optional
.
empty
());
boolean
isUserAssociated
=
wasteService
.
isUserAssosiatedWithWaste
(
"TestUsername"
,
999L
);
assertFalse
(
isUserAssociated
);
}
}
\ 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