Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaede10 committed Dec 2, 2024
1 parent 6e2e516 commit def6e0b
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 7 deletions.
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<includes>
<include>com/datastat/ds/unit/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/FoundryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/MindSporeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/OpenEulerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/OpenGaussConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/OpenLookengConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/QueryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastat/config/SoftwareConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
80 changes: 80 additions & 0 deletions src/test/java/com/datastat/ds/unit/ServiceUnitTests.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit def6e0b

Please sign in to comment.