Skip to content
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

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/com/datastat/dao/OpenUbmcQueryDao.java
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");
}
}
37 changes: 26 additions & 11 deletions src/main/java/com/datastat/dao/QueryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -1201,16 +1201,35 @@ public String querySigsOfTCOwners(CustomPropertiesConfig queryConf) {
return objectMapper.valueToTree(resMap).toString();
}

/**
* Search ownertype based on username.
*
* @param queryConf query config.
* @param userName user name.
* @return Response string.
*/
@SneakyThrows
public String queryUserOwnerType(CustomPropertiesConfig queryConf, String userName) {
String index = queryConf.getSigIndex();
String queryStr = queryConf.getAllUserOwnerTypeQueryStr();

ListenableFuture<Response> future = esAsyncHttpUtil.executeSearch(esUrl, 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");
}

/**
* parse owner info based on username.
*
* @param responseBody response string based on username.
* @param userName user name.
* @return Response string.
*/
@SneakyThrows
public HashMap<String, ArrayList<Object>> parseOwnerInfo(String responseBody, String userName) {
JsonNode dataNode = objectMapper.readTree(responseBody);
Iterator<JsonNode> buckets = dataNode.get("aggregations").get("group_field").get("buckets").elements();

HashMap<String, ArrayList<Object>> userData = new HashMap<>();
while (buckets.hasNext()) {
JsonNode bucket = buckets.next();
Expand All @@ -1219,7 +1238,8 @@ public String queryUserOwnerType(CustomPropertiesConfig queryConf, String userNa
while (users.hasNext()) {
JsonNode userBucket = users.next();
String user = userBucket.get("key").asText();
if (!user.equalsIgnoreCase(userName)) continue;
if (!user.equalsIgnoreCase(userName))
continue;

Iterator<JsonNode> types = userBucket.get("type").get("buckets").elements();
ArrayList<String> typeList = new ArrayList<>();
Expand All @@ -1240,13 +1260,7 @@ public String queryUserOwnerType(CustomPropertiesConfig queryConf, String userNa
}
}
}

HashMap<String, Object> resMap = new HashMap<>();
resMap.put("code", 200);
resMap.put("data", userData.get(userName.toLowerCase()));
resMap.put("msg", "success");
resMap.put("update_at", (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")).format(new Date()));
return objectMapper.valueToTree(resMap).toString();
return userData;
}

@SneakyThrows
Expand Down Expand Up @@ -1647,7 +1661,6 @@ protected String getCountResult(ListenableFuture<Response> future, String dataFl

protected String getGiteeResNum(String access_token, String community) throws Exception {
AsyncHttpClient client = EsAsyncHttpUtil.getClient();
RequestBuilder builder = esAsyncHttpUtil.getBuilder();
Param access_tokenParam = new Param("access_token", access_token);
Param visibility = new Param("visibility", "public");
Param affiliation = new Param("affiliation", "admin");
Expand All @@ -1665,6 +1678,8 @@ protected String getGiteeResNum(String access_token, String community) throws Ex
params.add(q);
params.add(page);
params.add(per_page);

RequestBuilder builder = new RequestBuilder();
Request request = builder.setUrl(env.getProperty("gitee.user.repos")).setQueryParams(params)
.addHeader("Content-Type", "application/json;charset=UTF-8").setMethod("GET").build();
ListenableFuture<Response> responseListenableFuture = client.executeRequest(request);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/datastat/model/CustomPropertiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class CustomPropertiesConfig {
private String npsIssueFilter;
private String globalNpsIssueFormat;
private String globalNpsIssueTitle;
private String esBaseUrl;
private String esAuth;

// -- index --
private String extOsIndex;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/datastat/util/EsAsyncHttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,39 @@ public ListenableFuture<Response> executeCount(String esUrl, String index, Strin

return client.executeRequest(builder.build());
}

/**
* Get es builder based on auth.
*
* @param auth auth info.
* @return RequestBuilder.
*/
public RequestBuilder getEsBuilder(String auth) {
RequestBuilder builder = new RequestBuilder();
builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
builder.addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((auth).getBytes()))
.setMethod("POST");
return builder;

}

/**
* Execute search based on query.
*
* @param esUrl es url.
* @param auth auth info.
* @param index name of index.
* @param query string.
* @return ListenableFuture<Response>.
*/
public ListenableFuture<Response> executeElasticSearch(String esUrl, String auth, String index,
String query) throws NoSuchAlgorithmException, KeyManagementException {
AsyncHttpClient client = getClient();
RequestBuilder builder = getEsBuilder(auth);

builder.setUrl(esUrl + index + "/_search");
builder.setBody(query);

return client.executeRequest(builder.build());
}
}
105 changes: 105 additions & 0 deletions src/test/java/com/datastat/ds/unit/DaoUnitTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.datastat.ds.unit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请加上copy right


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import引入排序建议按照规则进行排序
1、外部引入
2、自由
3、公共引入

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);
}

}
39 changes: 33 additions & 6 deletions src/test/java/com/datastat/ds/unit/ServiceUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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())
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reuslt 判断 11具体是什么意思呢

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
}
Loading