Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Smart Energy Management System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Marek Skácelík
Smart Energy Management System
Commits
5728302d
There was an error fetching the commit references. Please try again later.
Commit
5728302d
authored
1 year ago
by
Miroslav Rouča
Browse files
Options
Downloads
Patches
Plain Diff
Added tests for MetricsService
parent
7aee70d8
No related branches found
No related tags found
2 merge requests
!79
Final merge to main
,
!48
Added tests for MetricsService
Pipeline
#
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/src/test/java/cz/muni/fi/pa165/core/metrics/MetricsServiceTest.java
+417
-15
417 additions, 15 deletions
...ava/cz/muni/fi/pa165/core/metrics/MetricsServiceTest.java
with
417 additions
and
15 deletions
core/src/test/java/cz/muni/fi/pa165/core/metrics/MetricsServiceTest.java
+
417
−
15
View file @
5728302d
package
cz.muni.fi.pa165.core.metrics
;
import
cz.muni.fi.pa165.core.device.DeviceRepository
;
import
cz.muni.fi.pa165.core.device.DeviceService
;
import
org.junit.jupiter.api.AfterEach
;
import
cz.muni.fi.pa165.core.company.Company
;
import
cz.muni.fi.pa165.core.device.Device
;
import
cz.muni.fi.pa165.core.house.House
;
import
cz.muni.fi.pa165.core.smartmeter.SmartMeter
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.data.domain.PageRequest
;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
java.util.Optional
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
MockitoAnnotations
.
openMocks
;
class
MetricsServiceTest
{
@Test
void
create
()
{
@InjectMocks
private
MetricsService
metricsService
;
@Mock
private
MetricsRepository
metricsRepository
;
@BeforeEach
void
init
()
{
openMocks
(
this
);
}
@Test
void
findAllPageable
()
{
void
shouldCreate
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
when
(
metricsRepository
.
save
(
metrics
)).
thenReturn
(
metrics
);
Metrics
createdMetrics
=
metricsService
.
create
(
metrics
);
verify
(
metricsRepository
).
save
(
metrics
);
assertThat
(
createdMetrics
).
isEqualTo
(
metrics
);
}
@Test
void
findAll
()
{
void
shouldFindAllPageable
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
metrics2
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
2
,
20
,
0
))
.
build
();
Page
<
Metrics
>
page
=
new
PageImpl
<>(
List
.
of
(
metrics1
,
metrics2
));
int
pageNumber
=
0
;
int
pageSize
=
10
;
when
(
metricsRepository
.
findAll
(
PageRequest
.
of
(
pageNumber
,
pageSize
))).
thenReturn
(
page
);
Page
<
Metrics
>
result
=
metricsService
.
findAllPageable
(
PageRequest
.
of
(
pageNumber
,
pageSize
));
verify
(
metricsRepository
).
findAll
(
PageRequest
.
of
(
pageNumber
,
pageSize
));
assertThat
(
result
).
containsExactlyInAnyOrder
(
metrics1
,
metrics2
);
}
@Test
void
findAllPageableInt
()
{
void
shouldFindAll
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
metrics2
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
2
,
20
,
0
))
.
build
();
var
data
=
List
.
of
(
metrics1
,
metrics2
);
when
(
metricsRepository
.
findAll
()).
thenReturn
(
data
);
List
<
Metrics
>
result
=
metricsService
.
findAll
();
verify
(
metricsRepository
).
findAll
();
assertEquals
(
data
,
result
);
}
@Test
void
findById
()
{
void
shouldFindAllPageableInt
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
metrics2
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
2
,
20
,
0
))
.
build
();
Page
<
Metrics
>
page
=
new
PageImpl
<>(
List
.
of
(
metrics1
,
metrics2
));
int
pageNumber
=
0
;
int
pageSize
=
10
;
when
(
metricsRepository
.
findAll
(
PageRequest
.
of
(
pageNumber
,
pageSize
))).
thenReturn
(
page
);
Page
<
Metrics
>
result
=
metricsService
.
findAllPageableInt
(
pageNumber
);
verify
(
metricsRepository
).
findAll
(
PageRequest
.
of
(
pageNumber
,
pageSize
));
assertThat
(
result
).
containsExactlyInAnyOrder
(
metrics1
,
metrics2
);
}
@Test
void
deleteAll
()
{
void
shouldFindById
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
when
(
metricsRepository
.
findById
(
metrics
.
getId
())).
thenReturn
(
Optional
.
of
(
metrics
));
Metrics
result
=
metricsService
.
findById
(
metrics
.
getId
());
verify
(
metricsRepository
).
findById
(
metrics
.
getId
());
assertEquals
(
metrics
,
result
);
}
@Test
void
deleteAllById
()
{
void
shouldDeleteAll
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
metrics2
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
2
,
20
,
0
))
.
build
();
var
data
=
List
.
of
(
metrics1
,
metrics2
);
when
(
metricsRepository
.
findAll
()).
thenReturn
(
data
);
metricsService
.
deleteAll
();
verify
(
metricsRepository
).
findAll
();
verify
(
metricsRepository
).
saveAll
(
data
);
}
@Test
void
deleteById
()
{
void
shouldDeleteAllById
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
metrics2
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
2
,
20
,
0
))
.
build
();
String
[]
ids
=
{
metrics1
.
getId
(),
metrics2
.
getId
()};
when
(
metricsRepository
.
findById
(
ids
[
0
])).
thenReturn
(
Optional
.
of
(
metrics1
));
when
(
metricsRepository
.
findById
(
ids
[
1
])).
thenReturn
(
Optional
.
of
(
metrics2
));
metricsService
.
deleteAllById
(
ids
);
verify
(
metricsRepository
).
save
(
metrics1
);
verify
(
metricsRepository
).
save
(
metrics2
);
}
@Test
void
update
()
{
void
shouldDeleteById
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
metrics1
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
when
(
metricsRepository
.
findById
(
metrics1
.
getId
())).
thenReturn
(
Optional
.
of
(
metrics1
));
Metrics
deletedMetrics
=
metricsService
.
deleteById
(
metrics1
.
getId
());
verify
(
metricsRepository
).
findById
(
metrics1
.
getId
());
verify
(
metricsRepository
).
save
(
metrics1
);
// soft delete
assertNotNull
(
deletedMetrics
.
getDeletedDateTime
());
assertEquals
(
metrics1
,
deletedMetrics
);
}
@Test
void
hardDeleteAll
()
{
void
shouldUpdate
()
{
Company
company
=
Company
.
builder
().
name
(
"Apple"
).
build
();
House
house
=
House
.
builder
()
.
state
(
"CR"
)
.
address
(
"lsakdj"
)
.
city
(
"sdfa"
)
.
build
();
Device
device
=
Device
.
builder
()
.
company
(
company
)
.
name
(
"device-1"
)
.
build
();
SmartMeter
smartMeter
=
SmartMeter
.
builder
()
.
name
(
"meter-1"
)
.
device
(
device
)
.
house
(
house
)
.
build
();
Metrics
originalMetrics
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
100.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
Metrics
updatedMetrics
=
Metrics
.
builder
()
.
smartMeter
(
smartMeter
)
.
consumptionKWH
(
300.0
)
.
timeStamp
(
LocalDateTime
.
of
(
2023
,
1
,
1
,
20
,
0
))
.
build
();
when
(
metricsRepository
.
findById
(
originalMetrics
.
getId
())).
thenReturn
(
Optional
.
of
(
originalMetrics
));
when
(
metricsRepository
.
save
(
updatedMetrics
)).
thenReturn
(
updatedMetrics
);
Metrics
result
=
metricsService
.
update
(
updatedMetrics
,
originalMetrics
.
getId
());
verify
(
metricsRepository
).
findById
(
originalMetrics
.
getId
());
verify
(
metricsRepository
).
save
(
updatedMetrics
);
assertEquals
(
originalMetrics
.
getId
(),
result
.
getId
());
assertEquals
(
updatedMetrics
.
getConsumptionKWH
(),
result
.
getConsumptionKWH
());
assertEquals
(
updatedMetrics
.
getSmartMeter
().
getName
(),
result
.
getSmartMeter
().
getName
());
}
@Test
void
getRepository
()
{
void
shouldHardDeleteAll
()
{
metricsService
.
hardDeleteAll
();
verify
(
metricsRepository
).
deleteAll
();
}
...
...
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