-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
适配openubmc接口,增加DT测试用例 #278
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* This project is licensed under the Mulan PSL v2. | ||
You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
You may obtain a copy of Mulan PSL v2 at: | ||
http://license.coscl.org.cn/MulanPSL2 | ||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR | ||
PURPOSE. | ||
See the Mulan PSL v2 for more details. | ||
Create: 2024 | ||
*/ | ||
package com.datastat.dao; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import org.asynchttpclient.ListenableFuture; | ||
import org.asynchttpclient.Response; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.datastat.model.CustomPropertiesConfig; | ||
import com.datastat.util.ResultUtil; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
@Repository("openubmcDao") | ||
public class OpenUbmcQueryDao extends QueryDao { | ||
|
||
/** | ||
* Search ownertype based on username. | ||
* | ||
* @param queryConf query config. | ||
* @param userName user name. | ||
* @return Response string. | ||
*/ | ||
@Override | ||
@SneakyThrows | ||
public String queryUserOwnerType(CustomPropertiesConfig queryConf, String userName) { | ||
String index = queryConf.getSigIndex(); | ||
String queryStr = queryConf.getAllUserOwnerTypeQueryStr(); | ||
ListenableFuture<Response> future = this.esAsyncHttpUtil.executeElasticSearch(queryConf.getEsBaseUrl(), | ||
queryConf.getEsAuth(), index, queryStr); | ||
|
||
String responseBody = future.get().getResponseBody(UTF_8); | ||
HashMap<String, ArrayList<Object>> userData = parseOwnerInfo(responseBody, userName); | ||
|
||
ArrayList<Object> ownerInfo = userData.get(userName.toLowerCase()); | ||
return ResultUtil.resultJsonStr(200, objectMapper.valueToTree(ownerInfo), "success"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.datastat.ds.unit; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.asynchttpclient.ListenableFuture; | ||
import org.asynchttpclient.Response; | ||
import org.junit.jupiter.api.BeforeEach; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import引入排序建议按照规则进行排序 |
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
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.util.ReflectionTestUtils; | ||
|
||
import com.datastat.config.QueryConfig; | ||
import com.datastat.config.context.QueryConfContext; | ||
import com.datastat.dao.KafkaDao; | ||
import com.datastat.dao.ObsDao; | ||
import com.datastat.dao.QueryDao; | ||
import com.datastat.dao.RedisDao; | ||
import com.datastat.dao.context.QueryDaoContext; | ||
import com.datastat.ds.common.CommonUtil; | ||
import com.datastat.util.EsAsyncHttpUtil; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class DaoUnitTests { | ||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
@Mock | ||
QueryDaoContext queryDaoContext; | ||
|
||
@Mock | ||
QueryConfContext queryConfContext; | ||
|
||
@Mock | ||
ListenableFuture<Response> mockFuture; | ||
|
||
@Mock | ||
Response mockResponse; | ||
|
||
@MockBean | ||
KafkaDao kafkaDao; | ||
|
||
@MockBean | ||
ObsDao obsDao; | ||
|
||
@Mock | ||
private RedisDao redisDao; | ||
|
||
@Mock | ||
private QueryConfig queryConfig; | ||
|
||
@InjectMocks | ||
private QueryDao queryDao; | ||
|
||
@Mock | ||
private EsAsyncHttpUtil esAsyncHttpUtil; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.openMocks(this); | ||
ReflectionTestUtils.setField(queryDao, "objectMapper", objectMapper); | ||
} | ||
|
||
@Test() | ||
void testUserOwnerTypeDao() throws Exception { | ||
String respBody = "{\"aggregations\":{\"group_field\":{\"buckets\":[{\"key\":\"sig-python-modules\"," | ||
+ "\"user\":{\"buckets\":[{\"key\":\"myeuler\",\"doc_count\":1609," | ||
+ "\"type\":{\"buckets\":[{\"key\":\"maintainers\",\"doc_count\":1609}]}}," | ||
+ "{\"key\":\"shinwell_hu\",\"type\":{\"buckets\":[{\"key\":\"maintainers\",\"doc_count\":1609}]}}]}}]}}}"; | ||
|
||
when(esAsyncHttpUtil.executeSearch(anyString(), isNull(), isNull())).thenReturn(mockFuture); | ||
when(mockFuture.get()).thenReturn(mockResponse); | ||
when(mockResponse.getStatusCode()).thenReturn(200); | ||
when(mockResponse.getStatusText()).thenReturn("OK"); | ||
when(mockResponse.getResponseBody(StandardCharsets.UTF_8)).thenReturn(respBody); | ||
String community = "openubmc"; | ||
String user = "user"; | ||
when(queryDaoContext.getQueryDao(community)).thenReturn(queryDao); | ||
when(queryConfContext.getQueryConfig(community)).thenReturn(queryConfig); | ||
String res = queryDao.queryUserOwnerType(queryConfig, user); | ||
CommonUtil.assertOk(res); | ||
|
||
community = "openeuler"; | ||
when(queryDaoContext.getQueryDao(community)).thenReturn(queryDao); | ||
when(queryConfContext.getQueryConfig(community)).thenReturn(queryConfig); | ||
res = queryDao.queryUserOwnerType(queryConfig, user); | ||
CommonUtil.assertOk(res); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import引入归类 |
||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
|
@@ -11,12 +13,14 @@ | |
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.util.ReflectionTestUtils; | ||
|
||
import com.datastat.config.FoundryConfig; | ||
import com.datastat.config.QueryConfig; | ||
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.QueryDao; | ||
import com.datastat.dao.RedisDao; | ||
import com.datastat.dao.context.QueryDaoContext; | ||
import com.datastat.ds.common.CommonUtil; | ||
|
@@ -50,14 +54,18 @@ void contextLoads() { | |
private QueryService queryService; | ||
|
||
@Mock | ||
private FoundryConfig queryConfig; | ||
private QueryConfig queryConfig; | ||
|
||
@Mock | ||
private FoundryDao foundryDao; | ||
|
||
@Mock | ||
private FoundryDao queryDao; | ||
private QueryDao queryDao; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.openMocks(this); | ||
ReflectionTestUtils.setField(queryService, "communityList", Arrays.asList("openeuler")); | ||
} | ||
|
||
@Test() | ||
|
@@ -66,7 +74,6 @@ void testDownloadCountService() throws Exception { | |
RequestParams params = new RequestParams(); | ||
params.setRepoType("model"); | ||
|
||
|
||
StringBuilder sb = new StringBuilder("modelfoundrycownload_repo_count_"); | ||
sb.append(params.getPath()) | ||
.append(params.getRepoType()) | ||
|
@@ -80,11 +87,31 @@ void testDownloadCountService() throws Exception { | |
CommonUtil.assertOk(serviceRes); | ||
|
||
when(redisDao.get(key)).thenReturn(null); | ||
when(queryDaoContext.getQueryDao("queryDao")).thenReturn(queryDao); | ||
when(queryDaoContext.getQueryDao("queryDao")).thenReturn(foundryDao); | ||
when(queryConfContext.getQueryConfig("foundryConf")).thenReturn(queryConfig); | ||
when(queryDao.queryModelFoundryCountPath(queryConfig, params)).thenReturn(result); | ||
when(foundryDao.queryModelFoundryCountPath(queryConfig, params)).thenReturn(result); | ||
when(redisDao.set(key, result, 1l)).thenReturn(true); | ||
String res = queryService.queryModelFoundryCountPath(request, params); | ||
CommonUtil.assertOk(res); | ||
} | ||
|
||
@Test() | ||
void testUserOwnerTypeService() throws Exception { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
String user = "user"; | ||
String community = "openeuler"; | ||
String key = community.toLowerCase() + user + "ownertype"; | ||
String result = "{\"code\":200,\"data\":[{\"sig\":\"infrastructrue\",\"type\":[\"committers\"]}],\"msg\":\"success\"}"; | ||
when(redisDao.get(key)).thenReturn(result); | ||
String serviceRes = queryService.queryUserOwnerType(request, community, user); | ||
CommonUtil.assertOk(serviceRes); | ||
|
||
when(redisDao.get(key)).thenReturn(null); | ||
when(queryDaoContext.getQueryDao("queryDao")).thenReturn(queryDao); | ||
when(queryConfContext.getQueryConfig("queryConf")).thenReturn(queryConfig); | ||
when(queryDao.queryUserOwnerType(queryConfig, user)).thenReturn(result); | ||
when(redisDao.set(key, result, 1l)).thenReturn(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reuslt 判断 11具体是什么意思呢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redis打桩,设置缓存成功(key-value-time) |
||
String res = queryService.queryUserOwnerType(request, community, user); | ||
CommonUtil.assertOk(res); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请加上copy right