From def6e0ba0a379ed1a1524737042d88c7de36d294 Mon Sep 17 00:00:00 2001 From: kaede10 Date: Mon, 2 Dec 2024 14:21:33 +0800 Subject: [PATCH] Add unit tests --- pom.xml | 30 +++++++ .../com/datastat/config/FoundryConfig.java | 2 +- .../com/datastat/config/MindSporeConfig.java | 2 +- .../com/datastat/config/OpenEulerConfig.java | 2 +- .../com/datastat/config/OpenGaussConfig.java | 2 +- .../datastat/config/OpenLookengConfig.java | 2 +- .../java/com/datastat/config/QueryConfig.java | 2 +- .../com/datastat/config/SoftwareConfig.java | 2 +- .../datastat/ds/unit/ServiceUnitTests.java | 80 +++++++++++++++++++ 9 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/datastat/ds/unit/ServiceUnitTests.java diff --git a/pom.xml b/pom.xml index a22ffa3..4753d24 100644 --- a/pom.xml +++ b/pom.xml @@ -229,6 +229,36 @@ org.springframework.boot spring-boot-maven-plugin + + org.jacoco + jacoco-maven-plugin + 0.8.7 + + + default-prepare-agent + + prepare-agent + + + + default-report + test + + report + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + com/datastat/ds/unit/*.java + + + diff --git a/src/main/java/com/datastat/config/FoundryConfig.java b/src/main/java/com/datastat/config/FoundryConfig.java index 6db2aec..b96ed04 100644 --- a/src/main/java/com/datastat/config/FoundryConfig.java +++ b/src/main/java/com/datastat/config/FoundryConfig.java @@ -18,7 +18,7 @@ import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix = "foundry") -@PropertySource(value = {"file:${config.path}/foundry.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/foundry.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("foundryConf") @Data public class FoundryConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/MindSporeConfig.java b/src/main/java/com/datastat/config/MindSporeConfig.java index 1e1982e..17ed852 100644 --- a/src/main/java/com/datastat/config/MindSporeConfig.java +++ b/src/main/java/com/datastat/config/MindSporeConfig.java @@ -18,7 +18,7 @@ import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix = "mindspore") -@PropertySource(value = {"file:${config.path}/mindspore.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/mindspore.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("mindsporeConf") @Data public class MindSporeConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/OpenEulerConfig.java b/src/main/java/com/datastat/config/OpenEulerConfig.java index bc07a55..1692a3a 100644 --- a/src/main/java/com/datastat/config/OpenEulerConfig.java +++ b/src/main/java/com/datastat/config/OpenEulerConfig.java @@ -18,7 +18,7 @@ import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix = "openeuler") -@PropertySource(value = {"file:${config.path}/openeuler.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/openeuler.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("openeulerConf") @Data public class OpenEulerConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/OpenGaussConfig.java b/src/main/java/com/datastat/config/OpenGaussConfig.java index 6c903e8..9c55fbc 100644 --- a/src/main/java/com/datastat/config/OpenGaussConfig.java +++ b/src/main/java/com/datastat/config/OpenGaussConfig.java @@ -26,7 +26,7 @@ import org.springframework.core.env.Environment; @ConfigurationProperties(prefix = "opengauss") -@PropertySource(value = {"file:${config.path}/opengauss.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/opengauss.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("opengaussConf") @Data public class OpenGaussConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/OpenLookengConfig.java b/src/main/java/com/datastat/config/OpenLookengConfig.java index 51b7108..c4f1195 100644 --- a/src/main/java/com/datastat/config/OpenLookengConfig.java +++ b/src/main/java/com/datastat/config/OpenLookengConfig.java @@ -18,7 +18,7 @@ import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix = "openlookeng") -@PropertySource(value = {"file:${config.path}/openlookeng.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/openlookeng.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("openlookengConf") @Data public class OpenLookengConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/QueryConfig.java b/src/main/java/com/datastat/config/QueryConfig.java index d29fb10..a83ca7a 100644 --- a/src/main/java/com/datastat/config/QueryConfig.java +++ b/src/main/java/com/datastat/config/QueryConfig.java @@ -18,7 +18,7 @@ import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix = "custom") -@PropertySource(value = {"file:${config.path}/custom.properties"}) +@PropertySource(value = {"file:${config.path}/custom.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("queryConf") @Data public class QueryConfig extends CustomPropertiesConfig { diff --git a/src/main/java/com/datastat/config/SoftwareConfig.java b/src/main/java/com/datastat/config/SoftwareConfig.java index 675ce7d..9f1a09c 100644 --- a/src/main/java/com/datastat/config/SoftwareConfig.java +++ b/src/main/java/com/datastat/config/SoftwareConfig.java @@ -9,7 +9,7 @@ import lombok.Data; @ConfigurationProperties(prefix = "software") -@PropertySource(value = {"file:${config.path}/software.properties"}, encoding = "UTF-8") +@PropertySource(value = {"file:${config.path}/software.properties"}, ignoreResourceNotFound = true, encoding = "UTF-8") @Configuration("softwareConf") @Data public class SoftwareConfig extends CustomPropertiesConfig { } diff --git a/src/test/java/com/datastat/ds/unit/ServiceUnitTests.java b/src/test/java/com/datastat/ds/unit/ServiceUnitTests.java new file mode 100644 index 0000000..fa96c45 --- /dev/null +++ b/src/test/java/com/datastat/ds/unit/ServiceUnitTests.java @@ -0,0 +1,80 @@ +package com.datastat.ds.unit; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import com.datastat.config.FoundryConfig; +import com.datastat.config.context.QueryConfContext; +import com.datastat.dao.FoundryDao; +import com.datastat.dao.RedisDao; +import com.datastat.dao.context.QueryDaoContext; +import com.datastat.ds.common.CommonUtil; +import com.datastat.model.dto.RequestParams; +import com.datastat.service.QueryService; +import jakarta.servlet.http.HttpServletRequest; + +@SpringBootTest +@AutoConfigureMockMvc +public class ServiceUnitTests { + @Test + void contextLoads() { + } + + @Mock + QueryDaoContext queryDaoContext; + + @Mock + QueryConfContext queryConfContext; + + @Mock + private RedisDao redisDao; + + @InjectMocks + private QueryService queryService; + + @Mock + private FoundryConfig queryConfig; + + @Mock + private FoundryDao queryDao; + + @BeforeEach + public void setUp() throws Exception { + MockitoAnnotations.openMocks(this); + } + + @Test() + void testDownloadCountService() throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + RequestParams params = new RequestParams(); + params.setRepoType("model"); + + + StringBuilder sb = new StringBuilder("modelfoundrycownload_repo_count_"); + sb.append(params.getPath()) + .append(params.getRepoType()) + .append(params.getRepoId()) + .append(params.getStart()) + .append(params.getEnd()); + String key = sb.toString(); + String result = "{\"code\":200,\"msg\":\"ok\",\"data\":[{\"repo_id\":\"1313\",\"repo\":\"Qwen\",\"download\":30}]}"; + when(redisDao.get(key)).thenReturn(result); + String serviceRes = queryService.queryModelFoundryCountPath(request, params); + CommonUtil.assertOk(serviceRes); + + when(redisDao.get(key)).thenReturn(null); + when(queryDaoContext.getQueryDao("queryDao")).thenReturn(queryDao); + when(queryConfContext.getQueryConfig("foundryConf")).thenReturn(queryConfig); + when(queryDao.queryModelFoundryCountPath(queryConfig, params)).thenReturn(result); + when(redisDao.set(key, result, 1l)).thenReturn(true); + String res = queryService.queryModelFoundryCountPath(request, params); + CommonUtil.assertOk(res); + } +}