Skip to content

Commit

Permalink
fix get the openapi interface that contains namespace information for…
Browse files Browse the repository at this point in the history
… deleted items (#4596)

* fix get the openapi interface that contains namespace information for deleted items

* add includeDeletedItems flag to export namespace config file

* fix ConfigsExportServiceTest

* add unit test and format code

* ServerNamespaceOpenApiService#getNamespaces exclude deleted items

* fix NamespaceServiceTest
  • Loading branch information
CalebZYC authored Oct 9, 2022
1 parent 301d66d commit 25fa47f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -71,7 +71,7 @@ public OpenNamespaceDTO getNamespace(String appId, String env, String clusterNam
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
return OpenApiBeanUtils
.batchTransformFromNamespaceBOs(namespaceService.findNamespaceBOs(appId, Env
.valueOf(env), clusterName));
.valueOf(env), clusterName, false));
}

@Override
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);
(env), clusterName, namespaceName, 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);
List<NamespaceBO> namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, 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) {
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName, 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);
namespaceBO = transformNamespace2BO(env, namespace, includeDeletedItems);
namespaceBOs.add(namespaceBO);
} catch (Exception e) {
LOGGER.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
Expand Down Expand Up @@ -228,6 +228,10 @@ public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterN
.collect(Collectors.toList());
}

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

public List<NamespaceDTO> findNamespaces(String appId, Env env, String clusterName) {
return namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
}
Expand All @@ -246,12 +250,17 @@ public List<NamespaceDTO> 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) {
Expand Down Expand Up @@ -284,7 +293,7 @@ public Map<String, Map<String, Boolean>> 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);

Expand Down Expand Up @@ -322,20 +331,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<ItemBO> deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs);
itemBOs.addAll(deletedItems);
modifiedItemCnt += deletedItems.size();
List<ItemBO> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
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;
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;
Expand Down Expand Up @@ -223,6 +225,52 @@ 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<ItemDTO> 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(2);
itemDTO2.setNamespaceId(2);
itemDTO2.setKey("k2");
itemDTO2.setValue(String.valueOf(2));
itemDTOList.add(itemDTO2);
when(itemService.findItems(any(), any(), any(), any())).thenReturn(itemDTOList);

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

private AppNamespace createAppNamespace(String appId, String name, boolean isPublic) {
AppNamespace instance = new AppNamespace();
Expand Down

0 comments on commit 25fa47f

Please sign in to comment.