Commit d521b24f by Yiming Liu

Add unique check for app/cluster/namespace

parent cf7feb30
......@@ -73,4 +73,9 @@ public class AppController {
return BeanUtils.transfrom(AppDTO.class, app);
}
@RequestMapping("/apps/{appId}/unique")
public boolean isAppIdUnique(@PathVariable("appId") String appId) {
return appService.isAppIdUnique(appId);
}
}
package com.ctrip.apollo.adminservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.service.AppNamespaceService;
@RestController
public class AppNamespaceController {
@Autowired
private AppNamespaceService appNamespaceService;
@RequestMapping("/apps/{appId}/appnamespace/{appnamespace}/unique")
public boolean isAppNamespaceUnique(@PathVariable("appId") String appId,
@PathVariable("appnamespace") String appnamespace) {
return appNamespaceService.isAppNamespaceNameUnique(appId, appnamespace);
}
}
......@@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ClusterService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
......@@ -22,9 +21,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public class ClusterController {
@Autowired
private ViewService viewService;
@Autowired
private ClusterService clusterService;
@RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST)
......@@ -56,7 +52,7 @@ public class ClusterController {
@RequestMapping("/apps/{appId}/clusters")
public List<ClusterDTO> find(@PathVariable("appId") String appId) {
List<Cluster> clusters = viewService.findClusters(appId);
List<Cluster> clusters = clusterService.findClusters(appId);
return BeanUtils.batchTransform(ClusterDTO.class, clusters);
}
......@@ -68,4 +64,9 @@ public class ClusterController {
return BeanUtils.transfrom(ClusterDTO.class, cluster);
}
@RequestMapping("/apps/{appId}/cluster/{clusterName}/unique")
public boolean isAppIdUnique(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
return clusterService.isClusterNameUnique(appId, clusterName);
}
}
......@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ItemService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
......@@ -23,9 +22,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public class ItemController {
@Autowired
private ViewService viewService;
@Autowired
private ItemService itemService;
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
......@@ -59,7 +55,7 @@ public class ItemController {
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Item> items = viewService.findItems(appId, clusterName, namespaceName);
List<Item> items = itemService.findItems(appId, clusterName, namespaceName);
List<ItemDTO> itemDTOs = new LinkedList<>();
for (Item item: items){
......
......@@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.NamespaceDTO;
......@@ -22,9 +21,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public class NamespaceController {
@Autowired
private ViewService viewService;
@Autowired
private NamespaceService namespaceService;
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
......@@ -59,7 +55,7 @@ public class NamespaceController {
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
public List<NamespaceDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
List<Namespace> groups = namespaceService.findNamespaces(appId, clusterName);
return BeanUtils.batchTransform(NamespaceDTO.class, groups);
}
......
......@@ -15,7 +15,6 @@ import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
......@@ -25,9 +24,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public class ReleaseController {
@Autowired
private ViewService viewSerivce;
@Autowired
private ReleaseService releaseService;
@Autowired
......@@ -48,7 +44,7 @@ public class ReleaseController {
public List<ReleaseDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Release> releases = viewSerivce.findReleases(appId, clusterName, namespaceName);
List<Release> releases = releaseService.findReleases(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ReleaseDTO.class, releases);
}
......
......@@ -24,6 +24,24 @@ public class AppControllerTest extends AbstractControllerTest {
}
@Test
public void testCheckIfAppIdUnique() {
AppDTO dto = generateSampleDTOData();
ResponseEntity<AppDTO> response =
restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
AppDTO result = response.getBody();
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertEquals(dto.getAppId(), result.getAppId());
Assert.assertTrue(result.getId() > 0);
Boolean falseUnique =
restTemplate.getForObject(getBaseAppUrl() + dto.getAppId() + "/unique", Boolean.class);
Assert.assertFalse(falseUnique);
Boolean trueUnique = restTemplate
.getForObject(getBaseAppUrl() + dto.getAppId() + "true" + "/unique", Boolean.class);
Assert.assertTrue(trueUnique);
}
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCreate() {
AppDTO dto = generateSampleDTOData();
......
......@@ -6,4 +6,6 @@ import com.ctrip.apollo.biz.entity.AppNamespace;
public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNamespace, Long>{
AppNamespace findByAppIdAndName(String appId, String namespaceName);
}
......@@ -5,85 +5,36 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.AppNamespace;
import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.AppNamespaceRepository;
import com.ctrip.apollo.biz.repository.AppRepository;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.core.ConfigConsts;
@Service
public class AdminService {
@Autowired
private AppRepository appRepository;
private AppService appService;
@Autowired
private AppNamespaceRepository appNamespaceRepository;
private AppNamespaceService appNamespaceService;
@Autowired
private NamespaceRepository namespaceRepository;
private ClusterService clusterService;
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private AuditService auditService;
private NamespaceService namespaceService;
@Transactional
public App createNewApp(App app) {
String createBy = app.getDataChangeCreatedBy();
App createdApp = appRepository.save(app);
auditService.audit(App.class.getSimpleName(), createdApp.getId(), Audit.OP.INSERT, createBy);
App createdApp = appService.save(app);
String appId = createdApp.getAppId();
createDefaultAppNamespace(appId, createBy);
appNamespaceService.createDefaultAppNamespace(appId, createBy);
createDefaultCluster(appId, createBy);
clusterService.createDefaultCluster(appId, createBy);
createDefaultNamespace(appId, createBy);
namespaceService.createDefaultNamespace(appId, createBy);
return app;
}
private void createDefaultAppNamespace(String appId, String createBy) {
AppNamespace appNs = new AppNamespace();
appNs.setAppId(appId);
appNs.setName(appId);
appNs.setComment("default app namespace");
appNs.setDataChangeCreatedBy(createBy);
appNs.setDataChangeLastModifiedBy(createBy);
appNamespaceRepository.save(appNs);
auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,
createBy);
}
private void createDefaultCluster(String appId, String createBy) {
Cluster cluster = new Cluster();
cluster.setName(ConfigConsts.CLUSTER_NAME_DEFAULT);
cluster.setAppId(appId);
cluster.setDataChangeCreatedBy(createBy);
cluster.setDataChangeLastModifiedBy(createBy);
clusterRepository.save(cluster);
auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, createBy);
}
private void createDefaultNamespace(String appId, String createBy) {
Namespace ns = new Namespace();
ns.setAppId(appId);
ns.setClusterName(ConfigConsts.CLUSTER_NAME_DEFAULT);
ns.setNamespaceName(appId);
ns.setDataChangeCreatedBy(createBy);
ns.setDataChangeLastModifiedBy(createBy);
namespaceRepository.save(ns);
auditService.audit(Namespace.class.getSimpleName(), ns.getId(), Audit.OP.INSERT, createBy);
}
}
package com.ctrip.apollo.biz.service;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.AppNamespace;
import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.repository.AppNamespaceRepository;
import com.ctrip.apollo.core.exception.ServiceException;
@Service
public class AppNamespaceService {
@Autowired
private AppNamespaceRepository appNamespaceRepository;
@Autowired
private AuditService auditService;
public boolean isAppNamespaceNameUnique(String appId, String namespaceName) {
Objects.requireNonNull(appId, "AppId must not be null");
Objects.requireNonNull(namespaceName, "Namespace must not be null");
return Objects.isNull(appNamespaceRepository.findByAppIdAndName(appId, namespaceName));
}
@Transactional
public void createDefaultAppNamespace(String appId, String createBy) {
if (!isAppNamespaceNameUnique(appId, appId)) {
throw new ServiceException("appnamespace not unique");
}
AppNamespace appNs = new AppNamespace();
appNs.setAppId(appId);
appNs.setName(appId);
appNs.setComment("default app namespace");
appNs.setDataChangeCreatedBy(createBy);
appNs.setDataChangeLastModifiedBy(createBy);
appNamespaceRepository.save(appNs);
auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,
createBy);
}
}
package com.ctrip.apollo.biz.service;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
......@@ -12,6 +13,7 @@ import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.repository.AppRepository;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.exception.ServiceException;
@Service
public class AppService {
......@@ -22,6 +24,11 @@ public class AppService {
@Autowired
private AuditService auditService;
public boolean isAppIdUnique(String appId) {
Objects.requireNonNull(appId, "AppId must not be null");
return Objects.isNull(appRepository.findByAppId(appId));
}
@Transactional
public void delete(long id, String owner) {
appRepository.delete(id);
......@@ -44,6 +51,9 @@ public class AppService {
@Transactional
public App save(App entity) {
if (!isAppIdUnique(entity.getAppId())) {
throw new ServiceException("appId not unique");
}
App app = appRepository.save(entity);
auditService.audit(App.class.getSimpleName(), app.getId(), Audit.OP.INSERT,
......
package com.ctrip.apollo.biz.service;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -8,6 +12,9 @@ import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.exception.ServiceException;
import com.google.common.base.Strings;
@Service
public class ClusterService {
......@@ -18,12 +25,33 @@ public class ClusterService {
@Autowired
private AuditService auditService;
public boolean isClusterNameUnique(String appId, String clusterName) {
Objects.requireNonNull(appId, "AppId must not be null");
Objects.requireNonNull(clusterName, "ClusterName must not be null");
return Objects.isNull(clusterRepository.findByAppIdAndName(appId, clusterName));
}
public Cluster findOne(String appId, String name) {
return clusterRepository.findByAppIdAndName(appId, name);
}
public List<Cluster> findClusters(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.emptyList();
}
List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null) {
return Collections.emptyList();
}
return clusters;
}
@Transactional
public Cluster save(Cluster entity) {
if (!isClusterNameUnique(entity.getAppId(), entity.getName())) {
throw new ServiceException("cluster not unique");
}
Cluster cluster = clusterRepository.save(entity);
auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT,
......@@ -51,4 +79,19 @@ public class ClusterService {
return managedCluster;
}
@Transactional
public void createDefaultCluster(String appId, String createBy) {
if (!isClusterNameUnique(appId, ConfigConsts.CLUSTER_NAME_DEFAULT)) {
throw new ServiceException("cluster not unique");
}
Cluster cluster = new Cluster();
cluster.setName(ConfigConsts.CLUSTER_NAME_DEFAULT);
cluster.setAppId(appId);
cluster.setDataChangeCreatedBy(createBy);
cluster.setDataChangeLastModifiedBy(createBy);
clusterRepository.save(cluster);
auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, createBy);
}
}
package com.ctrip.apollo.biz.service;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -47,6 +50,24 @@ public class ItemService {
return item;
}
public List<Item> findItems(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
if (items == null) {
return Collections.emptyList();
}
return items;
}
public List<Item> findItems(String appId, String clusterName, String namespaceName) {
Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
namespaceName);
if (group != null) {
return findItems(group.getId());
} else {
return Collections.emptyList();
}
}
@Transactional
public Item save(Item entity) {
Item item = itemRepository.save(entity);
......
package com.ctrip.apollo.biz.service;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -8,6 +12,8 @@ import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.exception.ServiceException;
@Service
public class NamespaceService {
......@@ -18,6 +24,14 @@ public class NamespaceService {
@Autowired
private AuditService auditService;
public boolean isNamespaceUnique(String appId, String cluster, String namespace) {
Objects.requireNonNull(appId, "AppId must not be null");
Objects.requireNonNull(cluster, "Cluster must not be null");
Objects.requireNonNull(namespace, "Namespace must not be null");
return Objects.isNull(
namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, cluster, namespace));
}
@Transactional
public void delete(long id, String owner) {
namespaceRepository.delete(id);
......@@ -34,8 +48,19 @@ public class NamespaceService {
namespaceName);
}
public List<Namespace> findNamespaces(String appId, String clusterName) {
List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) {
return Collections.emptyList();
}
return groups;
}
@Transactional
public Namespace save(Namespace entity) {
if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
throw new ServiceException("namespace not unique");
}
Namespace namespace = namespaceRepository.save(entity);
auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT,
......@@ -53,7 +78,24 @@ public class NamespaceService {
auditService.audit(Namespace.class.getSimpleName(), managedNamespace.getId(), Audit.OP.UPDATE,
managedNamespace.getDataChangeLastModifiedBy());
return managedNamespace;
}
@Transactional
public void createDefaultNamespace(String appId, String createBy) {
if (!isNamespaceUnique(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, appId)) {
throw new ServiceException("namespace not unique");
}
Namespace ns = new Namespace();
ns.setAppId(appId);
ns.setClusterName(ConfigConsts.CLUSTER_NAME_DEFAULT);
ns.setNamespaceName(appId);
ns.setDataChangeCreatedBy(createBy);
ns.setDataChangeLastModifiedBy(createBy);
namespaceRepository.save(ns);
auditService.audit(Namespace.class.getSimpleName(), ns.getId(), Audit.OP.INSERT, createBy);
}
}
package com.ctrip.apollo.biz.service;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
......@@ -40,6 +41,15 @@ public class ReleaseService {
return release;
}
public List<Release> findReleases(String appId, String clusterName, String namespaceName) {
List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (releases == null) {
return Collections.emptyList();
}
return releases;
}
@Transactional
public Release buildRelease(String name, String comment, Namespace namespace, String owner) {
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
......
package com.ctrip.apollo.biz.service;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.google.common.base.Strings;
/**
* config service for admin
*/
@Service
public class ViewService {
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private NamespaceRepository namespaceRepository;
@Autowired
private ItemRepository itemRepository;
@Autowired
private ReleaseRepository releaseRepository;
public List<Cluster> findClusters(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.emptyList();
}
List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null) {
return Collections.emptyList();
}
return clusters;
}
public List<Namespace> findNamespaces(String appId, String clusterName) {
List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) {
return Collections.emptyList();
}
return groups;
}
public List<Item> findItems(String appId, String clusterName, String namespaceName) {
Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
namespaceName);
if (group != null) {
return findItems(group.getId());
} else {
return Collections.emptyList();
}
}
public List<Item> findItems(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
if (items == null) {
return Collections.emptyList();
}
return items;
}
public List<Release> findReleases(String appId, String clusterName, String namespaceName) {
List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (releases == null) {
return Collections.emptyList();
}
return releases;
}
}
......@@ -17,7 +17,9 @@ import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Audit;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.AppRepository;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.exception.ServiceException;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class)
......@@ -29,10 +31,16 @@ public class AdminServiceTest {
private AdminService adminService;
@Autowired
private ViewService viewService;
private AuditService auditService;
@Autowired
private AuditService auditService;
private AppRepository appRepository;
@Autowired
private ClusterService clsuterService;
@Autowired
private NamespaceService namespaceService;
@Test
public void testCreateNewApp() {
......@@ -50,11 +58,11 @@ public class AdminServiceTest {
app = adminService.createNewApp(app);
Assert.assertEquals(appId, app.getAppId());
List<Cluster> clusters = viewService.findClusters(app.getAppId());
List<Cluster> clusters = clsuterService.findClusters(app.getAppId());
Assert.assertEquals(1, clusters.size());
Assert.assertEquals(ConfigConsts.CLUSTER_NAME_DEFAULT, clusters.get(0).getName());
List<Namespace> namespaces = viewService.findNamespaces(appId, clusters.get(0).getName());
List<Namespace> namespaces = namespaceService.findNamespaces(appId, clusters.get(0).getName());
Assert.assertEquals(1, namespaces.size());
Assert.assertEquals(appId, namespaces.get(0).getNamespaceName());
......@@ -62,4 +70,22 @@ public class AdminServiceTest {
Assert.assertEquals(4, audits.size());
}
@Test(expected = ServiceException.class)
public void testCreateDuplicateApp() {
String appId = "someAppId";
App app = new App();
app.setAppId(appId);
app.setName("someAppName");
String owner = "someOwnerName";
app.setOwnerName(owner);
app.setOwnerEmail("someOwnerName@ctrip.com");
app.setDataChangeCreatedBy(owner);
app.setDataChangeLastModifiedBy(owner);
app.setDataChangeCreatedTime(new Date());
appRepository.save(app);
adminService.createNewApp(app);
}
}
package com.ctrip.apollo.biz.service;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.BizTestConfiguration;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.core.exception.ServiceException;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class)
@Transactional
@Rollback
public class ClusterServiceTest {
@Autowired
private AdminService adminService;
@Autowired
private ClusterService clusterService;
@Test(expected = ServiceException.class)
public void testCreateDuplicateCluster() {
String appId = "someAppId";
App app = new App();
app.setAppId(appId);
app.setName("someAppName");
String owner = "someOwnerName";
app.setOwnerName(owner);
app.setOwnerEmail("someOwnerName@ctrip.com");
app.setDataChangeCreatedBy(owner);
app.setDataChangeLastModifiedBy(owner);
app.setDataChangeCreatedTime(new Date());
adminService.createNewApp(app);
clusterService.createDefaultCluster(appId, owner);
}
}
......@@ -28,9 +28,12 @@ public class PrivilegeServiceTest {
private AdminService adminService;
@Autowired
private ViewService viewService;
private ClusterService clusterService;
@Autowired
private NamespaceService namespaceService;
@Autowired
private PrivilegeService privilService;
@Test
......@@ -46,9 +49,9 @@ public class PrivilegeServiceTest {
app.setDataChangeCreatedTime(new Date());
App newApp = adminService.createNewApp(app);
List<Cluster> clusters = viewService.findClusters(newApp.getAppId());
List<Cluster> clusters = clusterService.findClusters(newApp.getAppId());
List<Namespace> namespaces =
viewService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
namespaceService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
Namespace namespace = namespaces.get(0);
privilService.addPrivilege(namespace.getId(), newApp.getOwnerName(),
......@@ -76,9 +79,9 @@ public class PrivilegeServiceTest {
app.setDataChangeLastModifiedBy(owner);
app.setDataChangeCreatedTime(new Date());
App newApp = adminService.createNewApp(app);
List<Cluster> clusters = viewService.findClusters(newApp.getAppId());
List<Cluster> clusters = clusterService.findClusters(newApp.getAppId());
List<Namespace> namespaces =
viewService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
namespaceService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
Namespace namespace = namespaces.get(0);
privilService.addPrivilege(namespace.getId(), newApp.getOwnerName(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment