From 70fe46581737d51ef5b9cae8673e52dadf1d9d08 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Tue, 4 Oct 2022 20:53:12 +0800 Subject: [PATCH 1/6] fix get the openapi interface that contains namespace information for deleted items --- CHANGES.md | 1 + .../ServerNamespaceOpenApiService.java | 2 +- .../portal/service/NamespaceService.java | 31 +++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c724a90626a..5d536ab5fb9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -31,6 +31,7 @@ Apollo 2.1.0 * [fix create namespace with single dot 500 error](https://github.com/apolloconfig/apollo/pull/4568) * [Add nodejs client sdk and fix doc](https://github.com/apolloconfig/apollo/pull/4590) * [Move apollo-core, apollo-client, apollo-mockserver, apollo-openapi and apollo-client-config-data to apollo-java repo](https://github.com/apolloconfig/apollo/pull/4594) +* [fix get the openapi interface that contains namespace information for deleted items](https://github.com/apolloconfig/apollo/pull/4596) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1) \ No newline at end of file diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java index 0e3b5b22009..01474737b56 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java @@ -60,7 +60,7 @@ public ServerNamespaceOpenApiService( public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName, String namespaceName) { NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf - (env), clusterName, namespaceName); + (env), clusterName, namespaceName, false); if (namespaceBO == null) { return null; } diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java index a9fe506db42..4808ffd0c6d 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java @@ -246,12 +246,17 @@ public List getPublicAppNamespaceAllNamespaces(Env env, String pub } public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName, - String namespaceName) { + String namespaceName, boolean includeDeletedItems) { NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName); if (namespace == null) { throw new BadRequestException("namespaces not exist"); } - return transformNamespace2BO(env, namespace); + return transformNamespace2BO(env, namespace, includeDeletedItems); + } + + public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName, + String namespaceName) { + return loadNamespaceBO(appId, env, clusterName, namespaceName, true); } public boolean publicAppNamespaceHasAssociatedNamespace(String publicNamespaceName, Env env) { @@ -284,7 +289,7 @@ public Map> getNamespacesPublishInfo(String appId) return result; } - private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) { + private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean includeDeletedItems) { NamespaceBO namespaceBO = new NamespaceBO(); namespaceBO.setBaseInfo(namespace); @@ -322,20 +327,26 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) { itemBOs.add(itemBO); } - //deleted items - itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { - deletedItemDTOs.put(item.getKey(),item); - }); + if (includeDeletedItems) { + //deleted items + itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { + deletedItemDTOs.put(item.getKey(), item); + }); - List deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs); - itemBOs.addAll(deletedItems); - modifiedItemCnt += deletedItems.size(); + List deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs); + itemBOs.addAll(deletedItems); + modifiedItemCnt += deletedItems.size(); + } namespaceBO.setItemModifiedCnt(modifiedItemCnt); return namespaceBO; } + private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) { + return transformNamespace2BO(env, namespace, true); + } + private void fillAppNamespaceProperties(NamespaceBO namespace) { final NamespaceDTO namespaceDTO = namespace.getBaseInfo(); From 96e7d94c700f6ed4798d0d62d8cb238646bea734 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Wed, 5 Oct 2022 18:13:33 +0800 Subject: [PATCH 2/6] add includeDeletedItems flag to export namespace config file --- .../apollo/portal/controller/ConfigsExportController.java | 2 +- .../apollo/portal/service/ConfigsExportService.java | 2 +- .../framework/apollo/portal/service/NamespaceService.java | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java index 85ccadc3e2d..237a320de99 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java @@ -93,7 +93,7 @@ public void exportItems(@PathVariable String appId, @PathVariable String env, } NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf - (env), clusterName, namespaceName); + (env), clusterName, namespaceName, false); //generate a file. res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java index e867eaeb6eb..03388a07d8d 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java @@ -235,7 +235,7 @@ private void exportNamespaces(final Env env, final App exportApp, final ClusterD ZipOutputStream zipOutputStream) { String clusterName = exportCluster.getName(); - List namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName); + List namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, false); if (CollectionUtils.isEmpty(namespaceBOS)) { return; diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java index 4808ffd0c6d..b6a8331c1fc 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java @@ -186,7 +186,7 @@ public NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterN /** * load cluster all namespace info with items */ - public List findNamespaceBOs(String appId, Env env, String clusterName) { + public List findNamespaceBOs(String appId, Env env, String clusterName, boolean includeDeletedItems) { List namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName); if (namespaces == null || namespaces.size() == 0) { @@ -200,7 +200,7 @@ public List findNamespaceBOs(String appId, Env env, String clusterN executorService.submit(() -> { NamespaceBO namespaceBO; try { - namespaceBO = transformNamespace2BO(env, namespace); + namespaceBO = transformNamespace2BO(env, namespace, includeDeletedItems); namespaceBOs.add(namespaceBO); } catch (Exception e) { LOGGER.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}", @@ -228,6 +228,10 @@ public List findNamespaceBOs(String appId, Env env, String clusterN .collect(Collectors.toList()); } + public List findNamespaceBOs(String appId, Env env, String clusterName) { + return findNamespaceBOs(appId, env, clusterName, true); + } + public List findNamespaces(String appId, Env env, String clusterName) { return namespaceAPI.findNamespaceByCluster(appId, env, clusterName); } From 9cc2474b6b268aae11bcfc7dde1658b03ec5a0d1 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Wed, 5 Oct 2022 20:48:49 +0800 Subject: [PATCH 3/6] fix ConfigsExportServiceTest --- .../apollo/portal/service/ConfigsExportServiceTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java index 28383239451..71d691386d7 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java @@ -147,10 +147,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)).thenReturn(app1Cluster1Namespace); - when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2)).thenReturn(app1Cluster2Namespace); - when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1)).thenReturn(app2Cluster1Namespace); - when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2)).thenReturn(app2Cluster2Namespace); + 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); FileOutputStream fileOutputStream = new FileOutputStream("/tmp/apollo.zip"); From 7dcbb5ed7da344bfa3ed2f42ef6ca4bdbc689de0 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Thu, 6 Oct 2022 00:04:11 +0800 Subject: [PATCH 4/6] add unit test and format code --- .../portal/service/NamespaceService.java | 2 +- .../portal/service/NamespaceServiceTest.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java index b6a8331c1fc..b8abe7a0a8c 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java @@ -250,7 +250,7 @@ public List getPublicAppNamespaceAllNamespaces(Env env, String pub } public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName, - String namespaceName, boolean includeDeletedItems) { + String namespaceName, boolean includeDeletedItems) { NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName); if (namespace == null) { throw new BadRequestException("namespaces not exist"); diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java index 0ceab7b41d7..e4dfbf42ddf 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java @@ -45,6 +45,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -223,6 +224,43 @@ 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); + + ReleaseDTO releaseDTO = new ReleaseDTO(); + releaseDTO.setConfigurations("{\"k1\":\"k1\",\"k2\":\"k2\", \"k3\":\"\"}"); + when(releaseService.loadLatestRelease(any(), any(), any(), any())).thenReturn(releaseDTO); + + List itemDTOList = Lists.newArrayList(); + ItemDTO itemDTO1 = new ItemDTO(); + itemDTO1.setId(1); + itemDTO1.setNamespaceId(1); + itemDTO1.setKey("k1"); + itemDTO1.setValue(String.valueOf(1)); + itemDTOList.add(itemDTO1); + + ItemDTO itemDTO2 = new ItemDTO(); + itemDTO2.setId(1); + itemDTO2.setNamespaceId(2); + itemDTO2.setKey("k2"); + itemDTO2.setValue(String.valueOf(2)); + itemDTOList.add(itemDTO2); + when(itemService.findItems(any(), any(), any(), any())).thenReturn(itemDTOList); + + List 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 namespaceBO = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName); + assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(3); + } private AppNamespace createAppNamespace(String appId, String name, boolean isPublic) { AppNamespace instance = new AppNamespace(); From d516ca9116cb81a92cd868f71035468858ddf369 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Thu, 6 Oct 2022 18:49:22 +0800 Subject: [PATCH 5/6] ServerNamespaceOpenApiService#getNamespaces exclude deleted items --- .../openapi/server/service/ServerNamespaceOpenApiService.java | 2 +- .../framework/apollo/portal/service/NamespaceServiceTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java index 01474737b56..2a4bdfaea2e 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java @@ -71,7 +71,7 @@ public OpenNamespaceDTO getNamespace(String appId, String env, String clusterNam public List getNamespaces(String appId, String env, String clusterName) { return OpenApiBeanUtils .batchTransformFromNamespaceBOs(namespaceService.findNamespaceBOs(appId, Env - .valueOf(env), clusterName)); + .valueOf(env), clusterName, false)); } @Override diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java index e4dfbf42ddf..6c9118a241d 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java @@ -243,7 +243,7 @@ public void testLoadNamespaceBO() { itemDTOList.add(itemDTO1); ItemDTO itemDTO2 = new ItemDTO(); - itemDTO2.setId(1); + itemDTO2.setId(2); itemDTO2.setNamespaceId(2); itemDTO2.setKey("k2"); itemDTO2.setValue(String.valueOf(2)); From b56e17c78e13aec04527575aab4bc749f1eed8b6 Mon Sep 17 00:00:00 2001 From: CalebZYC Date: Fri, 7 Oct 2022 20:28:14 +0800 Subject: [PATCH 6/6] fix NamespaceServiceTest --- .../portal/service/NamespaceServiceTest.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java index 6c9118a241d..c881f893db3 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java @@ -41,6 +41,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; @@ -258,8 +259,17 @@ public void testLoadNamespaceBO() { deletedItemDTOList.add(deletedItemDTO); when(itemService.findDeletedItems(any(), any(), any(), any())).thenReturn(deletedItemDTOList); - NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName); - assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(3); + NamespaceBO namespaceBO1 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName); + List 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 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")); } private AppNamespace createAppNamespace(String appId, String name, boolean isPublic) {