From 5d784921fff818ebd11ca082939aa821c0b6655d Mon Sep 17 00:00:00 2001 From: kaede10 Date: Mon, 2 Dec 2024 16:35:57 +0800 Subject: [PATCH] update unit test --- .github/linters/checkstyle.xml | 209 ------------------ .github/script/codearts_check.sh | 79 +++++++ .github/workflows/check-label-owner.yml | 40 ++++ .github/workflows/gate-check.yml | 38 ++++ .github/workflows/label-check.yml | 19 ++ .github/workflows/linter.yml | 35 --- 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 +- .../java/com/datastat/constant/Constant.java | 2 +- .../datastat/controller/QueryController.java | 13 ++ src/main/java/com/datastat/dao/QueryDao.java | 84 ++++++- .../com/datastat/dao/metric/MetricDao.java | 4 +- .../interceptor/oneid/OneidInterceptor.java | 8 +- .../model/CustomPropertiesConfig.java | 2 + .../com/datastat/model/dto/RequestParams.java | 2 +- .../com/datastat/service/QueryService.java | 27 ++- .../com/datastat/service/VersionService.java | 3 +- .../com/datastat/util/EsAsyncHttpUtil.java | 4 +- src/main/java/com/datastat/util/RSAUtil.java | 2 +- .../com/datastat/ds/DsApplicationTests.java | 103 --------- .../java/com/datastat/ds/QueryDaoTests.java | 85 ------- .../datastat/ds/unit/ServiceUnitTests.java | 90 ++++++++ 28 files changed, 439 insertions(+), 454 deletions(-) delete mode 100644 .github/linters/checkstyle.xml create mode 100644 .github/script/codearts_check.sh create mode 100644 .github/workflows/check-label-owner.yml create mode 100644 .github/workflows/gate-check.yml create mode 100644 .github/workflows/label-check.yml delete mode 100644 .github/workflows/linter.yml delete mode 100644 src/test/java/com/datastat/ds/DsApplicationTests.java delete mode 100644 src/test/java/com/datastat/ds/QueryDaoTests.java create mode 100644 src/test/java/com/datastat/ds/unit/ServiceUnitTests.java diff --git a/.github/linters/checkstyle.xml b/.github/linters/checkstyle.xml deleted file mode 100644 index fb7ba1b..0000000 --- a/.github/linters/checkstyle.xml +++ /dev/null @@ -1,209 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/.github/script/codearts_check.sh b/.github/script/codearts_check.sh new file mode 100644 index 0000000..b0b84e2 --- /dev/null +++ b/.github/script/codearts_check.sh @@ -0,0 +1,79 @@ + +IAM_DATA=$(cat < label.name); + if (!labels.includes(requiredLabel)) { + throw new Error(`PR 必须包含标签: ${requiredLabel}`); + } diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml deleted file mode 100644 index 0c13a16..0000000 --- a/.github/workflows/linter.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Lint Code Base -on: - push: - branches: [master, main, test, develop_test] - pull_request: - branches: [master, main, test, develop_test] -jobs: - build: - name: Lint - runs-on: ubuntu-latest - - permissions: - contents: read - packages: read - # To report GitHub Actions status checks - statuses: write - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - # super-linter needs the full git history to get the - # list of files that changed across commits - fetch-depth: 0 - - - name: Super-linter - uses: super-linter/super-linter@v7.1.0 # x-release-please-version - env: - # To report GitHub Actions status checks - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - VALIDATE_JAVA: true - FILTER_REGEX_INCLUDE: .*src/.*/ # Only lint files in src/ - LINTER_RULES_PATH: .github/linters - JAVA_FILE_NAME: checkstyle.xml - 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/main/java/com/datastat/constant/Constant.java b/src/main/java/com/datastat/constant/Constant.java index 2f9ee4a..42f2e4b 100644 --- a/src/main/java/com/datastat/constant/Constant.java +++ b/src/main/java/com/datastat/constant/Constant.java @@ -19,7 +19,7 @@ private Constant() { /** * VALID_APPROVAL_REG used to match input string. */ - public static final String VALID_APPROVAL_REG = "model|dataset|space"; + public static final String VALID_APPROVAL_REG = "model|dataset|space|^\\*$"; /** * VALID_DATE_REG used to match input string. diff --git a/src/main/java/com/datastat/controller/QueryController.java b/src/main/java/com/datastat/controller/QueryController.java index 27a150f..aa9a61a 100644 --- a/src/main/java/com/datastat/controller/QueryController.java +++ b/src/main/java/com/datastat/controller/QueryController.java @@ -791,4 +791,17 @@ public String saveFrontendEvents(HttpServletRequest request, public String queryEventCount(HttpServletRequest request, @Valid final RequestParams condition) { return queryService.queryEventCount(request, condition); } + + /** + * Handles HTTP requests to the "/monthdowncount/openmind" endpoint to retrieve the monthly download count for a specified repository. + * + * @param request The HTTP request object containing details of the request. + * @param repoID The unique identifier of the repository, passed as a request parameter. + * @return A string containing the monthly download count information for the repository. + */ + @RequestMapping(value = "/monthdowncount/openmind") + public String monthDownCount(HttpServletRequest request, + @RequestParam(value = "repo_id") String repoID ) { + return queryService.getCommunityMonthDowncount(request, "foundry", repoID); + } } \ No newline at end of file diff --git a/src/main/java/com/datastat/dao/QueryDao.java b/src/main/java/com/datastat/dao/QueryDao.java index 5933d3a..d8560ea 100644 --- a/src/main/java/com/datastat/dao/QueryDao.java +++ b/src/main/java/com/datastat/dao/QueryDao.java @@ -76,6 +76,7 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; @@ -129,8 +130,8 @@ public class QueryDao { public void init() { esUrl = String.format("%s://%s:%s/", env.getProperty("es.scheme"), env.getProperty("es.host"), env.getProperty("es.port")); esQueryUtils = new EsQueryUtils(); - robotUsers = Arrays.asList(Objects.requireNonNull(env.getProperty("skip.robot.user")).split(",")); - domain_ids = Arrays.asList(Objects.requireNonNull(env.getProperty("qa.domain.ids")).split(",")); + robotUsers = Arrays.asList(Objects.requireNonNull(env.getProperty("skip.robot.user", "robot")).split(",")); + domain_ids = Arrays.asList(Objects.requireNonNull(env.getProperty("qa.domain.ids", "qa.domain.ids")).split(",")); } @SneakyThrows @@ -3783,4 +3784,83 @@ public int getStarCount(JsonNode events) { } return Math.max(like - dislike, 0); } + + /** + * Retrieves the monthly download count statistics for a specified community and repository. + * + * @param queryConf Custom configuration properties containing necessary query configurations. + * @param community The name of the community for which to retrieve the statistics. + * @param repoID The unique identifier of the repository. + * @return A JSON string containing the monthly download count statistics. + * @throws Exception If an error occurs during the query process. + */ + @SneakyThrows + public String getCommunityMonthDowncount(CustomPropertiesConfig queryConf, String community, String repoID) { + String query = String.format(queryConf.getOpenmindRepoQueryStr(), repoID); + ListenableFuture future = esAsyncHttpUtil.executeSearch(esUrl, queryConf.getOpenmindRepoIndex(), query); + + Response response = future.get(); + + int statusCode = response.getStatusCode(); + String statusText = response.getStatusText(); + String responseBody = response.getResponseBody(UTF_8); + + var dataObject = objectMapper.createObjectNode(); + var jsonArray = objectMapper.createArrayNode(); + + LocalDate today = LocalDate.now(); + DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + try { + JsonNode dataNode = objectMapper.readTree(responseBody); + JsonNode hits = dataNode.get("hits").get("hits").get(0); + JsonNode source = hits.get("_source"); + JsonNode repoName = source.get("repo_name"); + JsonNode repoType = source.get("repo_type"); + JsonNode aggregations = dataNode.get("aggregations"); + JsonNode dataPerDay = aggregations.get("data_per_day"); + JsonNode buckets = dataPerDay.get("buckets"); + + for(JsonNode b : buckets) { + var jsonObject = objectMapper.createObjectNode(); + jsonObject.set("date",b.get("key_as_string")); + jsonObject.set("count",b.get("doc_count")); + jsonArray.add(jsonObject); + } + + int bLen = buckets.size(); + int j = 0; + var resJsonArray = objectMapper.createArrayNode(); + + for(int i = 29; i >= 0; i--) { + LocalDate date = today.minusDays(i); + String formattedDate = date.format(df); + + if(j < bLen && jsonArray.get(j).get("date").asText().equals(formattedDate)) { + var jsonObject = objectMapper.createObjectNode(); + jsonObject.put("date", formattedDate); + jsonObject.put("count", jsonArray.get(j).get("count").asInt()); + resJsonArray.add(jsonObject); + j++; + }else{ + var jsonObject = objectMapper.createObjectNode(); + jsonObject.put("date", formattedDate); + jsonObject.put("count", 0); + resJsonArray.add(jsonObject); + } + } + + dataObject.put("repo_id", repoID); + dataObject.set("repo_name", repoName); + dataObject.set("repo_type", repoType); + dataObject.set("daily_down_count", resJsonArray); + + return ResultUtil.resultJsonStr(statusCode, objectMapper.valueToTree(dataObject), statusText); + + } catch (Exception e) { + logger.error("query/user/owner/repos get error", e.getMessage()); + + return ResultUtil.resultJsonStr(statusCode, dataObject, "No data found for the given repo_id"); + } + } } diff --git a/src/main/java/com/datastat/dao/metric/MetricDao.java b/src/main/java/com/datastat/dao/metric/MetricDao.java index 3484063..26f4990 100644 --- a/src/main/java/com/datastat/dao/metric/MetricDao.java +++ b/src/main/java/com/datastat/dao/metric/MetricDao.java @@ -47,10 +47,10 @@ public abstract class MetricDao { @Autowired EsAsyncHttpUtil esAsyncHttpUtil; - @Value("${company.query}") + @Value("${company.query:companyQuery}") String companyQueryStr; - @Value("${user.query}") + @Value("${user.query:userQuery}") String userQueryStr; protected static String esUrl; diff --git a/src/main/java/com/datastat/interceptor/oneid/OneidInterceptor.java b/src/main/java/com/datastat/interceptor/oneid/OneidInterceptor.java index 718a263..ed6bbb1 100644 --- a/src/main/java/com/datastat/interceptor/oneid/OneidInterceptor.java +++ b/src/main/java/com/datastat/interceptor/oneid/OneidInterceptor.java @@ -56,16 +56,16 @@ public class OneidInterceptor implements HandlerInterceptor { @Autowired RedisDao redisDao; - @Value("${cookie.token.name}") + @Value("${cookie.token.name:default}") private String cookieTokenName; - @Value("${cookie.token.domains}") + @Value("${cookie.token.domains:default}") private String allowDomains; - @Value("${cookie.token.secures}") + @Value("${cookie.token.secures:default}") private String cookieSecures; - @Value("${oneid.token.base.password}") + @Value("${oneid.token.base.password:default}") private String oneidTokenBasePassword; private static HashMap domain2secure; diff --git a/src/main/java/com/datastat/model/CustomPropertiesConfig.java b/src/main/java/com/datastat/model/CustomPropertiesConfig.java index d8d3f6d..6e20a27 100644 --- a/src/main/java/com/datastat/model/CustomPropertiesConfig.java +++ b/src/main/java/com/datastat/model/CustomPropertiesConfig.java @@ -110,6 +110,7 @@ public class CustomPropertiesConfig { private String spaceEventIndex; private String modelEventIndex; private String datasetEventIndex; + private String openmindRepoIndex; // -- query str -- private String extOsQueryStr; @@ -216,6 +217,7 @@ public class CustomPropertiesConfig { private String repoIssueField; private String userOwnerReposQuery; private String starCountQueryStr; + private String openmindRepoQueryStr; protected static final Map contributeTypeMap = new HashMap<>(); protected static final Map groupFieldMap = new HashMap<>(); diff --git a/src/main/java/com/datastat/model/dto/RequestParams.java b/src/main/java/com/datastat/model/dto/RequestParams.java index b37e24f..78f1e98 100644 --- a/src/main/java/com/datastat/model/dto/RequestParams.java +++ b/src/main/java/com/datastat/model/dto/RequestParams.java @@ -26,7 +26,7 @@ public class RequestParams { */ @Size(max = 20, message = "the length can not exceed 20") @Pattern(regexp = Constant.VALID_APPROVAL_REG, message = "Input repo type error") - private String repoType = "model"; + private String repoType = "*"; /** * This parameter is used to indicate which environment the data comes from. diff --git a/src/main/java/com/datastat/service/QueryService.java b/src/main/java/com/datastat/service/QueryService.java index 0fb2c9c..081f48d 100644 --- a/src/main/java/com/datastat/service/QueryService.java +++ b/src/main/java/com/datastat/service/QueryService.java @@ -89,7 +89,7 @@ public class QueryService { @PostConstruct public void init() { redisDefaultExpire = Long.parseLong(env.getProperty("redis.keyExpire", "60")); - communityList = Arrays.asList(env.getProperty("communitys").split(",")); + communityList = Arrays.asList(env.getProperty("communitys", "").split(",")); } public Boolean checkCommunity(String community) { @@ -1638,4 +1638,29 @@ public String queryEventCount(HttpServletRequest request, RequestParams conditio } return result; } + + /** + * Retrieves the monthly download count statistics for a specified community and repository. + * This method first checks if the community exists, then queries the data either from a cache + * or the data source if not available in the cache. + * + * @param request The HTTP request object containing details of the request. + * @param community The name of the community for which to retrieve the statistics. + * @param repoID The unique identifier of the repository. + * @return A JSON string containing the monthly download count statistics. + */ + public String getCommunityMonthDowncount(HttpServletRequest request, String community, String repoID) { + QueryDao queryDao = getQueryDao(request); + if (!checkCommunity(community)) { + return ResultUtil.resultJsonStr(404, "error", "community not found"); + } + CustomPropertiesConfig queryConf = getQueryConf("foundry"); + String key = "get_community_month_downcount_" + community + repoID; + String result = (String) redisDao.get(key); + if (result == null) { + result = queryDao.getCommunityMonthDowncount(queryConf, community, repoID); + redisDao.set(key, result, redisDefaultExpire); + } + return result; + } } diff --git a/src/main/java/com/datastat/service/VersionService.java b/src/main/java/com/datastat/service/VersionService.java index c57f6d1..cef7070 100644 --- a/src/main/java/com/datastat/service/VersionService.java +++ b/src/main/java/com/datastat/service/VersionService.java @@ -63,7 +63,8 @@ public class VersionService { @PostConstruct public void init() { - esUrl = String.format("%s://%s:%s/", env.getProperty("es.scheme"), env.getProperty("es.host"), env.getProperty("es.port")); + esUrl = String.format("%s://%s:%s/", env.getProperty("es.scheme", "https"), + env.getProperty("es.host", "0.0.0.0"), env.getProperty("es.port", "9200")); } diff --git a/src/main/java/com/datastat/util/EsAsyncHttpUtil.java b/src/main/java/com/datastat/util/EsAsyncHttpUtil.java index b1f18b9..3e7c743 100644 --- a/src/main/java/com/datastat/util/EsAsyncHttpUtil.java +++ b/src/main/java/com/datastat/util/EsAsyncHttpUtil.java @@ -33,10 +33,10 @@ @Service public class EsAsyncHttpUtil { - @Value("${es.user}") + @Value("${es.user:user}") String esUser; - @Value("${es.password}") + @Value("${es.password:pwd}") String esPassword; static volatile AsyncHttpClient asyncHttpClient = null; diff --git a/src/main/java/com/datastat/util/RSAUtil.java b/src/main/java/com/datastat/util/RSAUtil.java index 2fe99c9..15dacc0 100644 --- a/src/main/java/com/datastat/util/RSAUtil.java +++ b/src/main/java/com/datastat/util/RSAUtil.java @@ -38,7 +38,7 @@ public class RSAUtil implements Serializable { @Value("${rsa.key.algorithm:RSA}") public void setKeyAlgorithm(String keyAlgorithm) { RSAUtil.KEY_ALGORITHM = keyAlgorithm; } - @Value("${rsa.authing.algorithm}") + @Value("${rsa.authing.algorithm:RSA}") public void setRsaAlgorithm(String rsaAlgorithm) { RSAUtil.RSA_ALGORITHM = rsaAlgorithm; } /** diff --git a/src/test/java/com/datastat/ds/DsApplicationTests.java b/src/test/java/com/datastat/ds/DsApplicationTests.java deleted file mode 100644 index 5fbe915..0000000 --- a/src/test/java/com/datastat/ds/DsApplicationTests.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.datastat.ds; - -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.context.WebApplicationContext; - -import com.datastat.ds.common.CommonUtil; -import com.datastat.ds.common.ReadCase; -import com.datastat.util.ObjectMapperUtil; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -@SpringBootTest -@AutoConfigureMockMvc -class DsApplicationTests { - - @Test - void contextLoads() { - } - - @Autowired - private WebApplicationContext webApplicationContext; - - private ObjectMapper mapper = new ObjectMapper(); - - private MockMvc mockMvc; - - @BeforeEach - public void setUp() throws Exception { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test - void testNps() throws Exception { - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - paramMap.add("community", "openeuler"); - - JsonNode cases = ReadCase.readFile("src/test/java/com/datastat/ds/case/TestCase.json"); - JsonNode testCases = cases.get("nps"); - for (JsonNode testCase : testCases) { - String body = mapper.writeValueAsString(testCase); - String res = CommonUtil.executePost(mockMvc, "/query/nps", paramMap, body); - CommonUtil.assertOk(res); - } - - } - - @Test - void testModelFoundryDownload() throws Exception { - JsonNode cases = ReadCase.readFile("src/test/java/com/datastat/ds/case/TestCase.json"); - JsonNode testCases = cases.get("modelfoundry_download_count"); - for (JsonNode testCase : testCases) { - Map caseMap = ObjectMapperUtil.jsonToMap(testCase); - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - for (Map.Entry entry : caseMap.entrySet()) { - paramMap.add(entry.getKey(), entry.getValue()); - } - String res = CommonUtil.executeGet(mockMvc, "/query/modelfoundry/download/count", paramMap); - CommonUtil.assertOk(res); - } - } - - @Test - void testModelFoundryView() throws Exception { - JsonNode cases = ReadCase.readFile("src/test/java/com/datastat/ds/case/TestCase.json"); - JsonNode testCases = cases.get("modelfoundry_view_count"); - for (JsonNode testCase : testCases) { - Map caseMap = ObjectMapperUtil.jsonToMap(testCase); - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - for (Map.Entry entry : caseMap.entrySet()) { - paramMap.add(entry.getKey(), entry.getValue()); - } - String res = CommonUtil.executeGet(mockMvc, "/query/modelfoundry/view/count", paramMap); - CommonUtil.assertOk(res); - } - } - - @Test - void testModelFoundryStar() throws Exception { - JsonNode cases = ReadCase.readFile("src/test/java/com/datastat/ds/case/TestCase.json"); - JsonNode testCases = cases.get("modelfoundry_star_count"); - for (JsonNode testCase : testCases) { - Map caseMap = ObjectMapperUtil.jsonToMap(testCase); - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - for (Map.Entry entry : caseMap.entrySet()) { - paramMap.add(entry.getKey(), entry.getValue()); - } - String res = CommonUtil.executeGet(mockMvc, "/query/modelfoundry/star/count", paramMap); - CommonUtil.assertOk(res); - } - } -} diff --git a/src/test/java/com/datastat/ds/QueryDaoTests.java b/src/test/java/com/datastat/ds/QueryDaoTests.java deleted file mode 100644 index 82fe5d5..0000000 --- a/src/test/java/com/datastat/ds/QueryDaoTests.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.datastat.ds; - -import static org.mockito.Mockito.when; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.context.WebApplicationContext; - -import com.datastat.dao.UserIdDao; -import com.datastat.ds.common.CommonUtil; -import com.datastat.ds.common.ReadCase; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import jakarta.servlet.http.Cookie; - -/** - * QueryDaoTests - */ -@SpringBootTest -@AutoConfigureMockMvc -public class QueryDaoTests { - @Test - void contextLoads() { - } - - @Autowired - private WebApplicationContext webApplicationContext; - - @MockBean - private UserIdDao userIdDao; - - private ObjectMapper mapper = new ObjectMapper(); - - private MockMvc mockMvc; - - @BeforeEach - public void setUp() throws Exception { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test - void test_get_nps() throws Exception{ - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - paramMap.add("community", "openeuler"); - paramMap.add("repo", "easy-software"); - paramMap.add("page", "1"); - paramMap.add("pageSize", "1000"); - paramMap.add("sort", "desc"); - - String token = "abc123"; - Cookie cookie = new Cookie("_Y_G_", token); - - when(userIdDao.getUserId(token)).thenReturn("50216"); - String res = CommonUtil.executeGetWithCookie(mockMvc, "/query/get/nps", paramMap, cookie); - CommonUtil.assertOk(res); - } - - @Test - void test_globalnps_issue() throws Exception{ - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - paramMap.add("community", "software"); - - String token = "abc123"; - Cookie cookie = new Cookie("_Y_G_", token); - - when(userIdDao.getUserId(token)).thenReturn("50216"); - JsonNode cases = ReadCase.readFile("src/test/java/com/datastat/ds/case/TestCase.json"); - JsonNode testCases = cases.get("nps"); - for (JsonNode testCase : testCases) { - String body = mapper.writeValueAsString(testCase); - String res = CommonUtil.executePostWithCookie(mockMvc, "/query/globalnps/issue", paramMap, body, cookie); - CommonUtil.assertOk(res); - } - } - -} \ No newline at end of file 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..28a4c71 --- /dev/null +++ b/src/test/java/com/datastat/ds/unit/ServiceUnitTests.java @@ -0,0 +1,90 @@ +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 org.springframework.boot.test.mock.mockito.MockBean; + +import com.datastat.config.FoundryConfig; +import com.datastat.config.context.QueryConfContext; +import com.datastat.dao.FoundryDao; +import com.datastat.dao.KafkaDao; +import com.datastat.dao.ObsDao; +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; + + @MockBean + KafkaDao kafkaDao; + + @MockBean + ObsDao obsDao; + + @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); + } +}