forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add push dao unit tests (apache#2614)
Co-authored-by: I744506 <[email protected]> Co-authored-by: shown <[email protected]>
- Loading branch information
1 parent
12a53c6
commit 6e430bc
Showing
2 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.push.dao; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.apache.hertzbeat.common.entity.push.PushMetrics; | ||
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; | ||
|
||
/** | ||
* test case for {@link PushMetricsDao} | ||
*/ | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class PushMetricsDaoTest { | ||
@Mock | ||
private PushMetricsDao pushMetricsDao; | ||
|
||
@InjectMocks | ||
private PushMetricsDaoTest pushMetricsDaoTest; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void shallFindFirstByMonitorIdOrderByTimeDesc() { | ||
|
||
PushMetrics expectedMetrics = new PushMetrics(); | ||
expectedMetrics.setMonitorId(1L); | ||
expectedMetrics.setTime(System.currentTimeMillis()); | ||
|
||
when(pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L)).thenReturn(expectedMetrics); | ||
|
||
PushMetrics actualMetrics = pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L); | ||
|
||
assertEquals(expectedMetrics, actualMetrics); | ||
verify(pushMetricsDao, times(1)).findFirstByMonitorIdOrderByTimeDesc(1L); | ||
} | ||
|
||
@Test | ||
void shallDeleteAllByTimeBefore() { | ||
|
||
doNothing().when(pushMetricsDao).deleteAllByTimeBefore(anyLong()); | ||
|
||
pushMetricsDao.deleteAllByTimeBefore(1000L); | ||
|
||
verify(pushMetricsDao, times(1)).deleteAllByTimeBefore(1000L); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
push/src/test/java/org/apache/hertzbeat/push/dao/PushMonitorDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.push.dao; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.hertzbeat.common.entity.manager.Monitor; | ||
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; | ||
|
||
/** | ||
* test case for {@link PushMonitorDao} | ||
*/ | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class PushMonitorDaoTest { | ||
@Mock | ||
private PushMonitorDao pushMonitorDao; | ||
|
||
@InjectMocks | ||
private PushMonitorDaoTest pushMonitorDaoTest; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void shallSaveMonitor() { | ||
|
||
Monitor monitor = new Monitor(); | ||
monitor.setId(1L); | ||
monitor.setName("Test Monitor"); | ||
|
||
when(pushMonitorDao.save(any(Monitor.class))).thenReturn(monitor); | ||
|
||
Monitor savedMonitor = pushMonitorDao.save(monitor); | ||
|
||
assertEquals(monitor, savedMonitor); | ||
verify(pushMonitorDao, times(1)).save(monitor); | ||
} | ||
|
||
@Test | ||
void shallFindById() { | ||
Monitor monitor = new Monitor(); | ||
monitor.setId(1L); | ||
monitor.setName("Test Monitor"); | ||
|
||
when(pushMonitorDao.findById(1L)).thenReturn(Optional.of(monitor)); | ||
|
||
Optional<Monitor> foundMonitor = pushMonitorDao.findById(1L); | ||
|
||
assertTrue(foundMonitor.isPresent()); | ||
assertEquals(monitor, foundMonitor.get()); | ||
verify(pushMonitorDao, times(1)).findById(1L); | ||
} | ||
|
||
@Test | ||
void shallDeleteById() { | ||
doNothing().when(pushMonitorDao).deleteById(1L); | ||
|
||
pushMonitorDao.deleteById(1L); | ||
|
||
verify(pushMonitorDao, times(1)).deleteById(1L); | ||
} | ||
} |