Skip to content

Commit

Permalink
fix: 命名空间不存在,仍然可以创建配置
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu committed Nov 23, 2024
1 parent f943e72 commit e4d3cf1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.api.NacosApiException;
import com.alibaba.nacos.api.model.v2.ErrorCode;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.model.RestResultUtils;
Expand Down Expand Up @@ -176,6 +178,7 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res
@RequestParam(value = "schema", required = false) String schema,
@RequestParam(required = false) String encryptedDataKey) throws NacosException {

this.checkNamespaceExists(tenant);
String encryptedDataKeyFinal = null;
if (StringUtils.isNotBlank(encryptedDataKey)) {
encryptedDataKeyFinal = encryptedDataKey;
Expand Down Expand Up @@ -237,6 +240,7 @@ public void getConfig(HttpServletRequest request, HttpServletResponse response,
throws IOException, ServletException, NacosException {
// check tenant
ParamUtils.checkTenant(tenant);
this.checkNamespaceExists(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
// check params
ParamUtils.checkParam(dataId, group, "datumId", "content");
Expand All @@ -259,6 +263,7 @@ public ConfigAllInfo detailConfigInfo(@RequestParam("dataId") String dataId, @Re
throws NacosException {
// check tenant
ParamUtils.checkTenant(tenant);
this.checkNamespaceExists(tenant);
// check params
ParamUtils.checkParam(dataId, group, "datumId", "content");
ConfigAllInfo configAllInfo = configInfoPersistService.findConfigAllInfo(dataId, group, tenant);
Expand Down Expand Up @@ -336,7 +341,8 @@ public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestPar
@Secured(action = ActionTypes.READ, signType = SignType.CONFIG)
public RestResult<ConfigAdvanceInfo> getConfigAdvanceInfo(@RequestParam("dataId") String dataId,
@RequestParam("group") String group,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) {
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) throws NacosApiException {
this.checkNamespaceExists(tenant);
ConfigAdvanceInfo configInfo = configInfoPersistService.findConfigAdvanceInfo(dataId, group, tenant);
return RestResultUtils.success(configInfo);
}
Expand Down Expand Up @@ -398,7 +404,8 @@ public Page<ConfigInfo> searchConfig(@RequestParam("dataId") String dataId, @Req
@RequestParam(value = "appName", required = false) String appName,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "config_tags", required = false) String configTags,
@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) {
@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) throws NacosApiException {
this.checkNamespaceExists(tenant);
Map<String, Object> configAdvanceInfo = new HashMap<>(100);
if (StringUtils.isNotBlank(appName)) {
configAdvanceInfo.put("appName", appName);
Expand Down Expand Up @@ -428,7 +435,8 @@ public Page<ConfigInfo> fuzzySearchConfig(@RequestParam("dataId") String dataId,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "config_tags", required = false) String configTags,
@RequestParam(value = "types", required = false) String types, @RequestParam("pageNo") int pageNo,
@RequestParam("pageSize") int pageSize) {
@RequestParam("pageSize") int pageSize) throws NacosApiException {
this.checkNamespaceExists(tenant);
MetricsMonitor.getFuzzySearchMonitor().incrementAndGet();
Map<String, Object> configAdvanceInfo = new HashMap<>(50);
if (StringUtils.isNotBlank(appName)) {
Expand Down Expand Up @@ -535,8 +543,9 @@ public ResponseEntity<byte[]> exportConfig(@RequestParam(value = "dataId", requi
@RequestParam(value = "group", required = false) String group,
@RequestParam(value = "appName", required = false) String appName,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "ids", required = false) List<Long> ids) {
@RequestParam(value = "ids", required = false) List<Long> ids) throws NacosApiException {
ids.removeAll(Collections.singleton(null));
this.checkNamespaceExists(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = configInfoPersistService.findAllConfigInfo4Export(dataId, group, tenant, appName,
ids);
Expand Down Expand Up @@ -590,8 +599,9 @@ public ResponseEntity<byte[]> exportConfigV2(@RequestParam(value = "dataId", req
@RequestParam(value = "group", required = false) String group,
@RequestParam(value = "appName", required = false) String appName,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "ids", required = false) List<Long> ids) {
@RequestParam(value = "ids", required = false) List<Long> ids) throws NacosApiException {
ids.removeAll(Collections.singleton(null));
this.checkNamespaceExists(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = configInfoPersistService.findAllConfigInfo4Export(dataId, group, tenant, appName,
ids);
Expand Down Expand Up @@ -645,7 +655,7 @@ public RestResult<Map<String, Object>> importAndPublishConfig(HttpServletRequest
if (Objects.isNull(file)) {
return RestResultUtils.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
}

this.checkNamespaceExists(namespace);
namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (StringUtils.isNotBlank(namespace) && namespacePersistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
Expand Down Expand Up @@ -948,4 +958,17 @@ public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request,
return RestResultUtils.success("Clone Completed Successfully", saveResult);
}

/**
* check namespace exists.
* if not exists. throw api exception.
* @param namespaceId namespaceId.
* @throws NacosApiException not exists. throw error exception.
*/
private void checkNamespaceExists(String namespaceId) throws NacosApiException {
int tenantCount = this.namespacePersistService.tenantInfoCountByTenantId(namespaceId);
if (tenantCount == 0) {
throw new NacosApiException(HttpStatus.NOT_FOUND.value(), ErrorCode.NAMESPACE_NOT_EXIST,
"namespaceId [ " + namespaceId + " ] not exist");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -61,6 +62,7 @@
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.NestedServletException;

import javax.servlet.ServletContext;
import java.util.ArrayList;
Expand All @@ -71,6 +73,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
Expand Down Expand Up @@ -129,19 +132,32 @@ void setUp() {
@Test
void testPublishConfig() throws Exception {
when(configOperationService.publishConfig(any(), any(), anyString())).thenReturn(true);
String tenantId = "";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(Constants.CONFIG_CONTROLLER_PATH)
.param("dataId", "test").param("group", "test").param("tenant", "").param("content", "test")
.param("dataId", "test").param("group", "test").param("tenant", tenantId).param("content", "test")
.param("tag", "").param("appName", "").param("src_user", "").param("config_tags", "").param("desc", "")
.param("use", "").param("effect", "").param("type", "").param("schema", "");
// not exists, throw exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
assertEquals("true", actualValue);
}

@Test
void testGetConfig() throws Exception {
String tenantId = "";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH)
.param("dataId", "test").param("group", "test").param("tenant", "").param("tag", "");

.param("dataId", "test").param("group", "test").param("tenant", tenantId).param("tag", "");
// not exists, throw exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getStatus(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);
int actualValue = mockmvc.perform(builder).andReturn().getResponse().getStatus();
assertEquals(200, actualValue);
}
Expand All @@ -154,11 +170,17 @@ void testDetailConfigInfo() throws Exception {
configAllInfo.setCreateIp("localhost");
configAllInfo.setCreateUser("test");

when(configInfoPersistService.findConfigAllInfo("test", "test", "")).thenReturn(configAllInfo);
String tenantId = "";
when(configInfoPersistService.findConfigAllInfo("test", "test", tenantId)).thenReturn(configAllInfo);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH)
.param("show", "all").param("dataId", "test").param("group", "test").param("tenant", "");

// not exists, throw exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();

ConfigAllInfo resConfigAllInfo = JacksonUtils.toObj(actualValue, ConfigAllInfo.class);
Expand Down Expand Up @@ -235,9 +257,17 @@ void testGetConfigAdvanceInfo() throws Exception {

when(configInfoPersistService.findConfigAdvanceInfo("test", "test", "")).thenReturn(configAdvanceInfo);

String tenantId = "";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
Constants.CONFIG_CONTROLLER_PATH + "/catalog").param("dataId", "test").param("group", "test")
.param("tenant", "");
.param("tenant", tenantId);

// not exists, throw servlet exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);

String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
String code = JacksonUtils.toObj(actualValue).get("code").toString();
Expand Down Expand Up @@ -295,10 +325,17 @@ void testSearchConfig() throws Exception {
when(configInfoPersistService.findConfigInfo4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(
page);

String tenantId = "";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH)
.param("search", "accurate").param("dataId", "test").param("group", "test").param("appName", "")
.param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");

.param("tenant", tenantId).param("config_tags", "").param("pageNo", "1").param("pageSize", "10");

// not exists, throw servlet exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();

JsonNode pageItemsNode = JacksonUtils.toObj(actualValue).get("pageItems");
Expand Down Expand Up @@ -328,9 +365,17 @@ void testFuzzySearchConfig() throws Exception {
when(configInfoPersistService.findConfigInfoLike4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(
page);

String tenantId = "";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH)
.param("search", "blur").param("dataId", "test").param("group", "test").param("appName", "")
.param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");
.param("tenant", tenantId).param("config_tags", "").param("pageNo", "1").param("pageSize", "10");

// not exists, throw servlet exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenantId + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenantId)).thenReturn(1);

String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();

Expand Down Expand Up @@ -408,6 +453,13 @@ void testExportConfig() throws Exception {
.param("export", "true").param("dataId", dataId).param("group", group).param("tenant", tenant)
.param("appName", appname).param("ids", "1,2");

// not exists, throw servlet exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenant + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenant)).thenReturn(1);

int actualValue = mockmvc.perform(builder).andReturn().getResponse().getStatus();

assertEquals(200, actualValue);
Expand All @@ -433,6 +485,13 @@ void testExportConfigV2() throws Exception {
.param("exportV2", "true").param("dataId", dataId).param("group", group).param("tenant", tenant)
.param("appName", appname).param("ids", "1,2");

// not exists, throw servlet exception
assertThrowsExactly(NestedServletException.class,
() -> mockmvc.perform(builder).andReturn().getResponse().getContentAsString(),
"namespaceId [ " + tenant + " ] not exist");
// exists, return true
when(namespacePersistService.tenantInfoCountByTenantId(tenant)).thenReturn(1);

int actualValue = mockmvc.perform(builder).andReturn().getResponse().getStatus();

assertEquals(200, actualValue);
Expand Down

0 comments on commit e4d3cf1

Please sign in to comment.