Skip to content

Commit

Permalink
[manger] add manger sql dao mockito unit test (#375)
Browse files Browse the repository at this point in the history
* [manger] add mockito unit test

* [manger] add mockito unit test

* [manger] impl unit test ParamDaoTest #360

* [manager] fix NoticeRuleDaoTest

* [manger] impl unit test ParamDefineDaoTest #361

* [manger] impl unit test TagDaoTest #362
  • Loading branch information
gcdd1993 authored and tomsun28 committed Oct 23, 2022
1 parent b2062b1 commit 1680447
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 12 deletions.
61 changes: 58 additions & 3 deletions manager/src/test/java/com/usthe/manager/dao/NoticeRuleDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
package com.usthe.manager.dao;

import com.usthe.manager.controller.NoticeConfigController;
import com.usthe.common.entity.manager.NoticeRule;
import com.usthe.manager.AbstractSpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link NoticeRuleDao}
*/
class NoticeRuleDaoTest {
@Transactional
class NoticeRuleDaoTest extends AbstractSpringIntegrationTest {

@Resource
private NoticeRuleDao noticeRuleDao;

@BeforeEach
void setUp() {
// insert notice rule with enable = true
NoticeRule enabled = NoticeRule.builder()
.name("mock notice rule")
.enable(true)
.filterAll(true)
.gmtCreate(LocalDateTime.now())
.gmtUpdate(LocalDateTime.now())
.modifier("mock")
.creator("mock")
.priorities(Collections.emptyList())
.receiverId(1L)
.receiverName("mock receiver")
.tags(Collections.emptyList())
.build();
enabled = noticeRuleDao.saveAndFlush(enabled);
assertNotNull(enabled);

// insert notice rule with enable = false
NoticeRule disabled = NoticeRule.builder()
.id(2L)
.name("mock notice rule")
.enable(false)
.filterAll(true)
.gmtCreate(LocalDateTime.now())
.gmtUpdate(LocalDateTime.now())
.modifier("mock")
.creator("mock")
.priorities(Collections.emptyList())
.receiverId(1L)
.receiverName("mock receiver")
.tags(Collections.emptyList())
.build();
disabled = noticeRuleDao.saveAndFlush(disabled);
assertNotNull(disabled);
}

@AfterEach
void tearDown() {
noticeRuleDao.deleteAll();
}

@Test
void findNoticeRulesByEnableTrue() {
List<NoticeRule> enabledList = noticeRuleDao.findNoticeRulesByEnableTrue();
assertNotNull(enabledList);
assertEquals(1, enabledList.size());
}
}
81 changes: 78 additions & 3 deletions manager/src/test/java/com/usthe/manager/dao/ParamDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,104 @@
package com.usthe.manager.dao;

import com.usthe.manager.controller.NoticeConfigController;
import com.usthe.common.entity.manager.Param;
import com.usthe.manager.AbstractSpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link ParamDao}
*/
class ParamDaoTest {
@Transactional
class ParamDaoTest extends AbstractSpringIntegrationTest {

@Resource
private ParamDao paramDao;

@BeforeEach
void setUp() {
Param param = Param.builder()
.field("mock field")
.value("mock value")
.gmtCreate(LocalDateTime.now())
.gmtUpdate(LocalDateTime.now())
.monitorId(1L)
.type((byte) 1)
.build();

param = paramDao.saveAndFlush(param);
assertNotNull(param);
}

@AfterEach
void tearDown() {
paramDao.deleteAll();
}

@Test
void findParamsByMonitorId() {
List<Param> paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(1L, paramList.size());
}

@Test
void deleteParamsByMonitorId() {
// make sure params size is correct
List<Param> paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(1L, paramList.size());

// delete params by monitor id when monitor id is wrong
paramDao.deleteParamsByMonitorId(2L);
paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(1L, paramList.size());

// delete params by monitor id when monitor id is true
paramDao.deleteParamsByMonitorId(1L);
paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(0L, paramList.size());
}

@Test
void deleteParamsByMonitorIdIn() {
// make sure params size is correct
List<Param> paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(1L, paramList.size());

// delete params by monitor id when monitor id is wrong
Set<Long> ids = new HashSet<>();
ids.add(2L);
paramDao.deleteParamsByMonitorIdIn(ids);
paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(1L, paramList.size());

// delete params by monitor id when monitor id is true
ids.add(1L);
paramDao.deleteParamsByMonitorId(1L);
paramList = paramDao.findParamsByMonitorId(1L);
assertNotNull(paramList);

assertEquals(0L, paramList.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
package com.usthe.manager.dao;

import com.usthe.manager.controller.NoticeConfigController;
import com.usthe.common.entity.manager.ParamDefine;
import com.usthe.manager.AbstractSpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link ParamDefineDao}
*/
class ParamDefineDaoTest {
@Transactional
class ParamDefineDaoTest extends AbstractSpringIntegrationTest {

@Resource
private ParamDefineDao paramDefineDao;

@BeforeEach
void setUp() {
ParamDefine paramDefine = ParamDefine.builder()
.app("mock app")
.field("mock field")
.defaultValue("mock default value")
.limit((short) 1)
.keyAlias("mock key alias")
.valueAlias("mock value alias")
.options(Collections.emptyList())
.range("mock range")
.name(new HashMap<>())
.hide(true)
.required(true)
.type("mock type")
.placeholder("mock placeholder")
.creator("mock creator")
.modifier("mock modifier")
.gmtCreate(LocalDateTime.now())
.gmtUpdate(LocalDateTime.now())
.build();

paramDefine = paramDefineDao.saveAndFlush(paramDefine);
assertNotNull(paramDefine);
}

@AfterEach
void tearDown() {
paramDefineDao.deleteAll();
}

@Test
void findParamDefinesByApp() {
List<ParamDefine> paramDefineList = paramDefineDao.findParamDefinesByApp("mock app");
assertNotNull(paramDefineList);
assertEquals(1, paramDefineList.size());
}
}
51 changes: 48 additions & 3 deletions manager/src/test/java/com/usthe/manager/dao/TagDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
package com.usthe.manager.dao;

import com.usthe.manager.controller.NoticeConfigController;
import com.usthe.common.entity.manager.Tag;
import com.usthe.manager.AbstractSpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test case for {@link TagDao}
*/
class TagDaoTest {
@Transactional
class TagDaoTest extends AbstractSpringIntegrationTest {

@Resource
private TagDao tagDao;

@BeforeEach
void setUp() {
Tag tag = Tag.builder()
.name("mock tag")
.value("mock value")
.color("mock color")
.type((byte) 1)
.creator("mock creator")
.modifier("mock modifier")
.gmtCreate(LocalDateTime.now())
.gmtUpdate(LocalDateTime.now())
.build();

tag = tagDao.saveAndFlush(tag);
assertNotNull(tag);
}

@AfterEach
void tearDown() {
tagDao.deleteAll();
}

@Test
void deleteTagsByIdIn() {
List<Tag> tagList = tagDao.findAll();

assertNotNull(tagList);
assertFalse(tagList.isEmpty());

Set<Long> ids = tagList.stream().map(Tag::getId).collect(Collectors.toSet());
assertDoesNotThrow(() -> tagDao.deleteTagsByIdIn(ids));

tagList = tagDao.findAll();
assertNotNull(tagList);
assertTrue(tagList.isEmpty());
}
}

0 comments on commit 1680447

Please sign in to comment.