Skip to content

Commit

Permalink
Merge branch 'master' into fix/align-database-field-lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam authored Nov 2, 2024
2 parents 3206532 + fe0ac3d commit 0dc3da4
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Apollo 2.4.0
* [Feature: Add ConfigService cache record stats function](https://github.com/apolloconfig/apollo/pull/5247)
* [RefreshAdminServerAddressTask supports dynamic configuration of time interval](https://github.com/apolloconfig/apollo/pull/5248)
* [Refactor: Configuration files uniformly use Kebab style](https://github.com/apolloconfig/apollo/pull/5262)
* [Feature: openapi query namespace support not fill item](https://github.com/apolloconfig/apollo/pull/5249)
* [Refactor: align database ClusterName and NamespaceName fields lengths](https://github.com/apolloconfig/apollo/pull/5263)

------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ public ServerNamespaceOpenApiService(

@Override
public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName,
String namespaceName) {
String namespaceName, boolean fillItemDetail) {
NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf
(env), clusterName, namespaceName, false);
(env), clusterName, namespaceName, fillItemDetail, false);
if (namespaceBO == null) {
return null;
}
return OpenApiBeanUtils.transformFromNamespaceBO(namespaceBO);
}

@Override
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName, boolean fillItemDetail) {
return OpenApiBeanUtils
.batchTransformFromNamespaceBOs(namespaceService.findNamespaceBOs(appId, Env
.valueOf(env), clusterName, false));
.valueOf(env), clusterName, fillItemDetail, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -81,21 +82,22 @@ public OpenAppNamespaceDTO createNamespace(@PathVariable String appId,

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces")
public List<OpenNamespaceDTO> findNamespaces(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName) {
return this.namespaceOpenApiService.getNamespaces(appId, env, clusterName);
@PathVariable String clusterName,
@RequestParam(defaultValue = "true") boolean fillItemDetail) {
return this.namespaceOpenApiService.getNamespaces(appId, env, clusterName, fillItemDetail);
}

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}")
public OpenNamespaceDTO loadNamespace(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String
namespaceName) {
return this.namespaceOpenApiService.getNamespace(appId, env, clusterName, namespaceName);
@PathVariable String clusterName, @PathVariable String namespaceName,
@RequestParam(defaultValue = "true") boolean fillItemDetail) {
return this.namespaceOpenApiService.getNamespace(appId, env, clusterName, namespaceName, fillItemDetail);
}

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock")
public OpenNamespaceLockDTO getNamespaceLock(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable
String namespaceName) {
String namespaceName) {
return this.namespaceOpenApiService.getNamespaceLock(appId, env, clusterName, namespaceName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void exportItems(@PathVariable String appId, @PathVariable String env,
}

NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf
(env), clusterName, namespaceName, false);
(env), clusterName, namespaceName, true, false);

//generate a file.
res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void exportNamespaces(final Env env, final App exportApp, final ClusterD
ZipOutputStream zipOutputStream) {
String clusterName = exportCluster.getName();

List<NamespaceBO> namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, false);
List<NamespaceBO> namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, true, false);

if (CollectionUtils.isEmpty(namespaceBOS)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterN
/**
* load cluster all namespace info with items
*/
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName, boolean includeDeletedItems) {
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName, boolean fillItemDetail, boolean includeDeletedItems) {

List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
if (namespaces == null || namespaces.size() == 0) {
Expand All @@ -200,7 +200,7 @@ public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterN
executorService.submit(() -> {
NamespaceBO namespaceBO;
try {
namespaceBO = transformNamespace2BO(env, namespace, includeDeletedItems);
namespaceBO = transformNamespace2BO(env, namespace, fillItemDetail, includeDeletedItems);
namespaceBOs.add(namespaceBO);
} catch (Exception e) {
LOGGER.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
Expand Down Expand Up @@ -229,7 +229,7 @@ public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterN
}

public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName) {
return findNamespaceBOs(appId, env, clusterName, true);
return findNamespaceBOs(appId, env, clusterName, true, true);
}

public List<NamespaceDTO> findNamespaces(String appId, Env env, String clusterName) {
Expand All @@ -250,17 +250,17 @@ public List<NamespaceDTO> getPublicAppNamespaceAllNamespaces(Env env, String pub
}

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
String namespaceName, boolean includeDeletedItems) {
String namespaceName, boolean fillItemDetail, boolean includeDeletedItems) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}
return transformNamespace2BO(env, namespace, includeDeletedItems);
return transformNamespace2BO(env, namespace, fillItemDetail, includeDeletedItems);
}

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
String namespaceName) {
return loadNamespaceBO(appId, env, clusterName, namespaceName, true);
return loadNamespaceBO(appId, env, clusterName, namespaceName, true, true);
}

public boolean publicAppNamespaceHasAssociatedNamespace(String publicNamespaceName, Env env) {
Expand Down Expand Up @@ -293,7 +293,7 @@ public Map<String, Map<String, Boolean>> getNamespacesPublishInfo(String appId)
return result;
}

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean includeDeletedItems) {
private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean fillItemDetail, boolean includeDeletedItems) {
NamespaceBO namespaceBO = new NamespaceBO();
namespaceBO.setBaseInfo(namespace);

Expand All @@ -306,6 +306,10 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole
List<ItemBO> itemBOs = new LinkedList<>();
namespaceBO.setItems(itemBOs);

if (!fillItemDetail) {
return namespaceBO;
}

//latest Release
ReleaseDTO latestRelease;
Map<String, String> releaseItems = new HashMap<>();
Expand Down Expand Up @@ -347,7 +351,7 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole
}

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) {
return transformNamespace2BO(env, namespace, true);
return transformNamespace2BO(env, namespace, true, true);
}

private void fillAppNamespaceProperties(NamespaceBO namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public class ConfigsExportServiceTest extends AbstractUnitTest {

@Test
public void testNamespaceExportImport() throws FileNotFoundException {
// Test with fillItemDetail = true
testExportImportScenario(true);
}

@Test
public void testNamespaceExportImportWithFillItemDetail() throws FileNotFoundException {
// Test with fillItemDetail = false
testExportImportScenario(false);
}

private void testExportImportScenario(boolean fillItemDetail) throws FileNotFoundException {

File temporaryFolder = Files.newTemporaryFolder();
temporaryFolder.deleteOnExit();
String filePath = temporaryFolder + File.separator + "export.zip";
Expand Down Expand Up @@ -146,10 +158,10 @@ public void testNamespaceExportImport() throws FileNotFoundException {
when(permissionValidator.isAppAdmin(any())).thenReturn(true);
when(clusterService.findClusters(env, appId1)).thenReturn(app1Clusters);
when(clusterService.findClusters(env, appId2)).thenReturn(app2Clusters);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, false)).thenReturn(app1Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, false)).thenReturn(app1Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, false)).thenReturn(app2Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, false)).thenReturn(app2Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app1Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app1Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app2Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app2Cluster2Namespace);

FileOutputStream fileOutputStream = new FileOutputStream(filePath);

Expand Down Expand Up @@ -186,10 +198,12 @@ public void testNamespaceExportImport() throws FileNotFoundException {

verify(clusterService, times(4)).createCluster(any(), any());

verify(namespaceService, times(6)).createNamespace(any(), any());
verify(roleInitializationService,times(6)).initNamespaceRoles(any(), any(), anyString());
verify(roleInitializationService,times(6)).initNamespaceEnvRoles(any(), any(), anyString());
verify(itemService, times(12)).createItem(any(), any(), any(), any(), any());
if(fillItemDetail){
verify(namespaceService, times(6)).createNamespace(any(), any());
verify(roleInitializationService,times(6)).initNamespaceRoles(any(), any(), anyString());
verify(roleInitializationService,times(6)).initNamespaceEnvRoles(any(), any(), anyString());
verify(itemService, times(12)).createItem(any(), any(), any(), any(), any());
}
}

private App genApp(String name, String appId, String orgId, String orgName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,60 @@ public void testDeleteEmptyNamespace() {

@Test
public void testLoadNamespaceBO() {
String branchName = "branch";
NamespaceDTO namespaceDTO = createNamespace(testAppId, branchName, testNamespaceName);
when(namespaceAPI.loadNamespace(any(), any(), any(), any())).thenReturn(namespaceDTO);
boolean fillItemDetail = true;
NamespaceBO namespaceBO = loadNamespaceBO(fillItemDetail);

List<String> namespaceKey2 = namespaceBO.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(2);
assertThat(namespaceBO.getItems().size()).isEqualTo(2);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2"));
}

@Test
public void testLoadNamespaceBOWithoutItemDetail() {
boolean fillItemDetail = false;
NamespaceBO namespaceBO = loadNamespaceBO(fillItemDetail);

assertThat(namespaceBO.getItems().size()).isEqualTo(0);
}

private NamespaceBO loadNamespaceBO(boolean fillItemDetail) {
when(namespaceAPI.loadNamespace(any(), any(), any(), any())).thenReturn(createNamespace(testAppId, "branch", testNamespaceName));
when(releaseService.loadLatestRelease(any(), any(), any(), any())).thenReturn(createReleaseDTO());
when(itemService.findItems(any(), any(), any(), any())).thenReturn(createItems());
when(itemService.findDeletedItems(any(), any(), any(), any())).thenReturn(createDeletedItems());

NamespaceBO namespaceBO1 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName);
List<String> namespaceKey1 = namespaceBO1.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO1.getItemModifiedCnt()).isEqualTo(3);
assertThat(namespaceBO1.getItems().size()).isEqualTo(3);
assertThat(namespaceKey1).isEqualTo(Arrays.asList("k1", "k2", "k3"));

return namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName, fillItemDetail, false);
}

@Test
public void testFindPublicNamespaceForAssociatedNamespace() {
when(namespaceAPI.findPublicNamespaceForAssociatedNamespace(any(), any(), any(), any())).thenReturn(createNamespace(testAppId, "branch", testNamespaceName));
when(releaseService.loadLatestRelease(any(), any(), any(), any())).thenReturn(createReleaseDTO());
when(itemService.findItems(any(), any(), any(), any())).thenReturn(createItems());
when(itemService.findDeletedItems(any(), any(), any(), any())).thenReturn(createDeletedItems());

NamespaceBO namespaceBO = namespaceService.findPublicNamespaceForAssociatedNamespace(testEnv, testAppId, testClusterName, testNamespaceName);

List<String> namespaceKey2 = namespaceBO.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(3);
assertThat(namespaceBO.getItems().size()).isEqualTo(3);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2", "k3"));
}

private ReleaseDTO createReleaseDTO() {
ReleaseDTO releaseDTO = new ReleaseDTO();
releaseDTO.setConfigurations("{\"k1\":\"k1\",\"k2\":\"k2\", \"k3\":\"\"}");
when(releaseService.loadLatestRelease(any(), any(), any(), any())).thenReturn(releaseDTO);
return releaseDTO;
}

private List<ItemDTO> createItems() {
List<ItemDTO> itemDTOList = Lists.newArrayList();
ItemDTO itemDTO1 = new ItemDTO();
itemDTO1.setId(1);
Expand All @@ -249,27 +295,18 @@ public void testLoadNamespaceBO() {
itemDTO2.setKey("k2");
itemDTO2.setValue(String.valueOf(2));
itemDTOList.add(itemDTO2);
when(itemService.findItems(any(), any(), any(), any())).thenReturn(itemDTOList);

return itemDTOList;
}

private List<ItemDTO> createDeletedItems() {
List<ItemDTO> deletedItemDTOList = Lists.newArrayList();
ItemDTO deletedItemDTO = new ItemDTO();
deletedItemDTO.setId(3);
deletedItemDTO.setNamespaceId(3);
deletedItemDTO.setKey("k3");
deletedItemDTOList.add(deletedItemDTO);
when(itemService.findDeletedItems(any(), any(), any(), any())).thenReturn(deletedItemDTOList);

NamespaceBO namespaceBO1 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName);
List<String> namespaceKey1 = namespaceBO1.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO1.getItemModifiedCnt()).isEqualTo(3);
assertThat(namespaceBO1.getItems().size()).isEqualTo(3);
assertThat(namespaceKey1).isEqualTo(Arrays.asList("k1", "k2", "k3"));

NamespaceBO namespaceBO2 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName, false);
List<String> namespaceKey2 = namespaceBO2.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO2.getItemModifiedCnt()).isEqualTo(2);
assertThat(namespaceBO2.getItems().size()).isEqualTo(2);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2"));
return deletedItemDTOList;
}

private AppNamespace createAppNamespace(String appId, String name, boolean isPublic) {
Expand Down
Loading

0 comments on commit 0dc3da4

Please sign in to comment.