NamespaceService { @Transactional public void deleteNamespace(String appId, Env env, String clusterName, String namespaceName) { AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName); if (namespaceHasInstances(appId, env, clusterName, namespaceName)) { throw new BadRequestException( "Can not delete namespace because namespace has active instances"); } NamespaceDTO childNamespace = branchService .findBranchBaseInfo(appId, env, clusterName, namespaceName); if (childNamespace != null && namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) { throw new BadRequestException( "Can not delete namespace because namespace's branch has active instances"); } if (appNamespace != null && appNamespace.isPublic() && publicAppNamespaceHasAssociatedNamespace( namespaceName, env)) { throw new BadRequestException( "Can not delete public namespace which has associated namespaces"); } String operator = userInfoHolder.getUser().getUserId(); namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator); } NamespaceService(
      final PortalConfig portalConfig,
      final PortalSettings portalSettings,
      final UserInfoHolder userInfoHolder,
      final AdminServiceAPI.NamespaceAPI namespaceAPI,
      final ItemService itemService,
      final ReleaseService releaseService,
      final AppNamespaceService appNamespaceService,
      final InstanceService instanceService,
      final @Lazy NamespaceBranchService branchService,
      final RolePermissionService rolePermissionService); NamespaceDTO createNamespace(Env env, NamespaceDTO namespace); @Transactional void deleteNamespace(String appId, Env env, String clusterName, String namespaceName); NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterName,
      String namespaceName); List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName); List<NamespaceDTO> findNamespaces(String appId, Env env, String clusterName); List<NamespaceDTO> getPublicAppNamespaceAllNamespaces(Env env, String publicNamespaceName,
      int page,
      int size); NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
      String namespaceName); boolean namespaceHasInstances(String appId, Env env, String clusterName,
      String namespaceName); boolean publicAppNamespaceHasAssociatedNamespace(String publicNamespaceName, Env env); NamespaceBO findPublicNamespaceForAssociatedNamespace(Env env, String appId,
      String clusterName, String namespaceName); Map<String, Map<String, Boolean>> getNamespacesPublishInfo(String appId); void assignNamespaceRoleToOperator(String appId, String namespaceName, String operator);  }
@Test public void testDeletePrivateNamespace() { String operator = "user"; AppNamespace privateNamespace = createAppNamespace(testAppId, testNamespaceName, false); when(appNamespaceService.findByAppIdAndName(testAppId, testNamespaceName)).thenReturn(privateNamespace); when(userInfoHolder.getUser()).thenReturn(createUser(operator)); namespaceService.deleteNamespace(testAppId, testEnv, testClusterName, testNamespaceName); verify(namespaceAPI, times(1)).deleteNamespace(testEnv, testAppId, testClusterName, testNamespaceName, operator); }
@Test(expected = BadRequestException.class) public void testDeleteNamespaceHasInstance() { AppNamespace publicNamespace = createAppNamespace(testAppId, testNamespaceName, true); when(appNamespaceService.findByAppIdAndName(testAppId, testNamespaceName)).thenReturn(publicNamespace); when(instanceService.getInstanceCountByNamepsace(testAppId, testEnv, testClusterName, testNamespaceName)) .thenReturn(10); namespaceService.deleteNamespace(testAppId, testEnv, testClusterName, testNamespaceName); }