Commit 1def2409 authored by Yiming Liu's avatar Yiming Liu

Merge pull request #99 from yiming187/update

Transactional Support
parents bd502e16 1d123a22
package com.ctrip.apollo; package com.ctrip.apollo;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
import org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication @SpringBootApplication
@EnableEurekaClient @EnableEurekaClient
public class AdminServiceApplication { public class AdminServiceApplication {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder(AdminServiceApplication.class).run(args); ConfigurableApplicationContext context = new SpringApplicationBuilder(AdminServiceApplication.class).run(args);
context.addApplicationListener(new ApplicationPidFileWriter());
context.addApplicationListener(new EmbeddedServerPortFileWriter());
} }
} }
...@@ -16,10 +16,6 @@ ...@@ -16,10 +16,6 @@
<groupId>com.ctrip.apollo</groupId> <groupId>com.ctrip.apollo</groupId>
<artifactId>apollo-common</artifactId> <artifactId>apollo-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
......
...@@ -2,25 +2,27 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,25 +2,27 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
@Entity @Entity
@Table(name = "App")
@SQLDelete(sql = "Update App set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update App set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class App extends BaseEntity { public class App extends BaseEntity {
@Column(nullable = false) @Column(name = "Name", nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(name = "AppId", nullable = false)
private String appId; private String appId;
@Column(nullable = false) @Column(name = "OwnerName", nullable = false)
private String ownerName; private String ownerName;
@Column(nullable = false) @Column(name = "OwnerEmail", nullable = false)
private String ownerEmail; private String ownerEmail;
public String getAppId() { public String getAppId() {
......
...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
@Entity @Entity
@Table(name = "AppNamespace")
@SQLDelete(sql = "Update AppNamespace set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update AppNamespace set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class AppNamespace extends BaseEntity{ public class AppNamespace extends BaseEntity {
@Column(nullable = false) @Column(name = "Name", nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(name = "AppId", nullable = false)
private String appId; private String appId;
@Column @Column(name = "Comment")
private String comment; private String comment;
public String getAppId() { public String getAppId() {
......
...@@ -18,9 +18,10 @@ public abstract class BaseEntity { ...@@ -18,9 +18,10 @@ public abstract class BaseEntity {
@Id @Id
@GeneratedValue @GeneratedValue
@Column(name = "Id")
private long id; private long id;
@Column(name = "IsDeleted", columnDefinition="Bit default '0'") @Column(name = "IsDeleted", columnDefinition = "Bit default '0'")
protected boolean isDeleted = false; protected boolean isDeleted = false;
@Column(name = "DataChange_CreatedBy") @Column(name = "DataChange_CreatedBy")
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
...@@ -10,14 +11,15 @@ import org.hibernate.annotations.Where; ...@@ -10,14 +11,15 @@ import org.hibernate.annotations.Where;
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@Table(name = "Cluster")
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class Cluster extends BaseEntity { public class Cluster extends BaseEntity {
@Column(nullable = false) @Column(name = "Name", nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(name = "AppId", nullable = false)
private String appId; private String appId;
public String getAppId() { public String getAppId() {
......
...@@ -2,28 +2,30 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,28 +2,30 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
@Entity @Entity
@Table(name = "Item")
@SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class Item extends BaseEntity { public class Item extends BaseEntity {
@Column(nullable = false) @Column(name = "NamespaceId", nullable = false)
private long namespaceId; private long namespaceId;
@Column(nullable = false) @Column(name = "key", nullable = false)
private String key; private String key;
@Column @Column(name = "value")
private String value; private String value;
@Column @Column(name = "comment")
private String comment; private String comment;
@Column @Column(name = "LineNum")
private Integer lineNum; private Integer lineNum;
public String getComment() { public String getComment() {
......
...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
@Entity @Entity
@Table(name = "Namespace")
@SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class Namespace extends BaseEntity { public class Namespace extends BaseEntity {
@Column(nullable = false) @Column(name = "appId", nullable = false)
private String appId; private String appId;
@Column(nullable = false) @Column(name = "ClusterName", nullable = false)
private String clusterName; private String clusterName;
@Column(nullable = false) @Column(name = "NamespaceName", nullable = false)
private String namespaceName; private String namespaceName;
public String getAppId() { public String getAppId() {
......
...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,22 +2,24 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
@Entity @Entity
@Table(name = "Privilege")
@SQLDelete(sql = "Update Privilege set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Privilege set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class Privilege extends BaseEntity { public class Privilege extends BaseEntity {
@Column @Column(name = "Name", nullable = false)
private String name; private String name;
@Column @Column(name = "PrivilType", nullable = false)
private String privilType; private String privilType;
@Column @Column(name = "NamespaceId")
private long namespaceId; private long namespaceId;
public String getName() { public String getName() {
......
...@@ -6,32 +6,34 @@ import org.hibernate.annotations.Where; ...@@ -6,32 +6,34 @@ import org.hibernate.annotations.Where;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Lob; import javax.persistence.Lob;
import javax.persistence.Table;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@Table(name = "Release")
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
public class Release extends BaseEntity { public class Release extends BaseEntity {
@Column(nullable = false) @Column(name = "Name", nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(name = "AppId", nullable = false)
private String appId; private String appId;
@Column(nullable = false) @Column(name = "ClusterName", nullable = false)
private String clusterName; private String clusterName;
@Column @Column(name = "NamespaceName", nullable = false)
private String namespaceName; private String namespaceName;
@Column(nullable = false) @Column(name = "Configurations", nullable = false)
@Lob @Lob
private String configurations; private String configurations;
@Column(nullable = false) @Column(name = "Comment", nullable = false)
private String comment; private String comment;
public String getAppId() { public String getAppId() {
......
...@@ -2,8 +2,8 @@ package com.ctrip.apollo.biz.service; ...@@ -2,8 +2,8 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.boot.actuate.metrics.CounterService;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.AppNamespace; import com.ctrip.apollo.biz.entity.AppNamespace;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
...@@ -16,6 +16,7 @@ import com.ctrip.apollo.core.ConfigConsts; ...@@ -16,6 +16,7 @@ import com.ctrip.apollo.core.ConfigConsts;
import java.util.Date; import java.util.Date;
@Service @Service
public class AdminService { public class AdminService {
...@@ -30,12 +31,9 @@ public class AdminService { ...@@ -30,12 +31,9 @@ public class AdminService {
@Autowired @Autowired
private ClusterRepository clusterRepository; private ClusterRepository clusterRepository;
@Autowired
private CounterService counter;
@Transactional
public App createNewApp(App app) { public App createNewApp(App app) {
counter.increment("admin.createNewApp.start");
String createBy = app.getDataChangeCreatedBy(); String createBy = app.getDataChangeCreatedBy();
App createdApp = appRepository.save(app); App createdApp = appRepository.save(app);
String appId = createdApp.getAppId(); String appId = createdApp.getAppId();
...@@ -46,7 +44,6 @@ public class AdminService { ...@@ -46,7 +44,6 @@ public class AdminService {
createDefaultNamespace(appId, createBy); createDefaultNamespace(appId, createBy);
counter.increment("admin.createNewApp.success");
return app; return app;
} }
......
...@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.repository.AppRepository; import com.ctrip.apollo.biz.repository.AppRepository;
...@@ -17,6 +18,7 @@ public class AppService { ...@@ -17,6 +18,7 @@ public class AppService {
@Autowired @Autowired
private AppRepository appRepository; private AppRepository appRepository;
@Transactional
public void delete(long id) { public void delete(long id) {
appRepository.delete(id); appRepository.delete(id);
} }
...@@ -34,10 +36,12 @@ public class AppService { ...@@ -34,10 +36,12 @@ public class AppService {
return appRepository.findByAppId(appId); return appRepository.findByAppId(appId);
} }
@Transactional
public App save(App entity) { public App save(App entity) {
return appRepository.save(entity); return appRepository.save(entity);
} }
@Transactional
public App update(App app) { public App update(App app) {
App managedApp = appRepository.findByAppId(app.getAppId()); App managedApp = appRepository.findByAppId(app.getAppId());
BeanUtils.copyEntityProperties(app, managedApp); BeanUtils.copyEntityProperties(app, managedApp);
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.repository.ClusterRepository; import com.ctrip.apollo.biz.repository.ClusterRepository;
...@@ -17,14 +18,17 @@ public class ClusterService { ...@@ -17,14 +18,17 @@ public class ClusterService {
return clusterRepository.findByAppIdAndName(appId, name); return clusterRepository.findByAppIdAndName(appId, name);
} }
@Transactional
public Cluster save(Cluster entity) { public Cluster save(Cluster entity) {
return clusterRepository.save(entity); return clusterRepository.save(entity);
} }
@Transactional
public void delete(long id) { public void delete(long id) {
clusterRepository.delete(id); clusterRepository.delete(id);
} }
@Transactional
public Cluster update(Cluster cluster) { public Cluster update(Cluster cluster) {
Cluster managedCluster = Cluster managedCluster =
clusterRepository.findByAppIdAndName(cluster.getAppId(), cluster.getName()); clusterRepository.findByAppIdAndName(cluster.getAppId(), cluster.getName());
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository; import com.ctrip.apollo.biz.repository.ItemRepository;
...@@ -13,6 +14,7 @@ public class ItemService { ...@@ -13,6 +14,7 @@ public class ItemService {
@Autowired @Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
@Transactional
public void delete(long id) { public void delete(long id) {
itemRepository.delete(id); itemRepository.delete(id);
} }
...@@ -22,10 +24,12 @@ public class ItemService { ...@@ -22,10 +24,12 @@ public class ItemService {
return item; return item;
} }
@Transactional
public Item save(Item item) { public Item save(Item item) {
return itemRepository.save(item); return itemRepository.save(item);
} }
@Transactional
public Item update(Item item) { public Item update(Item item) {
Item managedItem = itemRepository.findOne(item.getId()); Item managedItem = itemRepository.findOne(item.getId());
BeanUtils.copyEntityProperties(item, managedItem); BeanUtils.copyEntityProperties(item, managedItem);
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository; import com.ctrip.apollo.biz.repository.ItemRepository;
...@@ -15,6 +16,7 @@ public class ItemSetService { ...@@ -15,6 +16,7 @@ public class ItemSetService {
@Autowired @Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
@Transactional
public void updateSet(ItemChangeSets changeSet) { public void updateSet(ItemChangeSets changeSet) {
if (changeSet.getCreateItems() != null) { if (changeSet.getCreateItems() != null) {
for (ItemDTO item : changeSet.getCreateItems()) { for (ItemDTO item : changeSet.getCreateItems()) {
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.NamespaceRepository; import com.ctrip.apollo.biz.repository.NamespaceRepository;
...@@ -13,6 +14,7 @@ public class NamespaceService { ...@@ -13,6 +14,7 @@ public class NamespaceService {
@Autowired @Autowired
private NamespaceRepository namespaceRepository; private NamespaceRepository namespaceRepository;
@Transactional
public void delete(long id) { public void delete(long id) {
namespaceRepository.delete(id); namespaceRepository.delete(id);
} }
...@@ -26,10 +28,12 @@ public class NamespaceService { ...@@ -26,10 +28,12 @@ public class NamespaceService {
namespaceName); namespaceName);
} }
@Transactional
public Namespace save(Namespace entity) { public Namespace save(Namespace entity) {
return namespaceRepository.save(entity); return namespaceRepository.save(entity);
} }
@Transactional
public Namespace update(Namespace namespace) { public Namespace update(Namespace namespace) {
Namespace managedNamespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName( Namespace managedNamespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(
namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName()); namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
......
...@@ -5,6 +5,7 @@ import com.ctrip.apollo.biz.repository.PrivilegeRepository; ...@@ -5,6 +5,7 @@ import com.ctrip.apollo.biz.repository.PrivilegeRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
...@@ -18,6 +19,7 @@ public class PrivilegeService { ...@@ -18,6 +19,7 @@ public class PrivilegeService {
@Autowired @Autowired
private PrivilegeRepository privilRepo; private PrivilegeRepository privilRepo;
@Transactional
public Privilege addPrivilege(long namespaceId, String name, PrivilType privilType) { public Privilege addPrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil = Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name()); privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
...@@ -41,6 +43,7 @@ public class PrivilegeService { ...@@ -41,6 +43,7 @@ public class PrivilegeService {
return privilRepo.findByNamespaceId(namespaceId); return privilRepo.findByNamespaceId(namespaceId);
} }
@Transactional
public void removePrivilege(long namespaceId, String name, PrivilType privilType) { public void removePrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil = Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name()); privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
......
...@@ -7,6 +7,7 @@ import java.util.Map; ...@@ -7,6 +7,7 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Namespace;
...@@ -40,6 +41,7 @@ public class ReleaseService { ...@@ -40,6 +41,7 @@ public class ReleaseService {
return release; return release;
} }
@Transactional
public Release buildRelease(String name, String comment, String appId, String clusterName, public Release buildRelease(String name, String comment, String appId, String clusterName,
String namespaceName) { String namespaceName) {
Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
......
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.biz;
import com.ctrip.apollo.biz.repository.AppRepositoryTest; import com.ctrip.apollo.biz.repository.AppRepositoryTest;
import com.ctrip.apollo.biz.service.AdminServiceTest; import com.ctrip.apollo.biz.service.AdminServiceTest;
import com.ctrip.apollo.biz.service.AdminServiceTransactionTest;
import com.ctrip.apollo.biz.service.ConfigServiceTest; import com.ctrip.apollo.biz.service.ConfigServiceTest;
import com.ctrip.apollo.biz.service.PrivilegeServiceTest; import com.ctrip.apollo.biz.service.PrivilegeServiceTest;
...@@ -14,7 +15,8 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -14,7 +15,8 @@ import org.junit.runners.Suite.SuiteClasses;
AppRepositoryTest.class, AppRepositoryTest.class,
AdminServiceTest.class, AdminServiceTest.class,
ConfigServiceTest.class, ConfigServiceTest.class,
PrivilegeServiceTest.class}) PrivilegeServiceTest.class,
AdminServiceTransactionTest.class})
public class AllTests { public class AllTests {
} }
package com.ctrip.apollo.biz.repository; package com.ctrip.apollo.biz.repository;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.BizTestConfiguration; import com.ctrip.apollo.biz.BizTestConfiguration;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class) @SpringApplicationConfiguration(classes = BizTestConfiguration.class)
@Transactional
@Rollback
public class AppRepositoryTest { public class AppRepositoryTest {
@Autowired @Autowired
private AppRepository appRepository; private AppRepository appRepository;
@Before
public void before() {
appRepository.deleteAll();
}
@Test @Test
public void testCreate() { public void testCreate() {
String appId = "someAppId"; String appId = "someAppId";
...@@ -66,4 +64,5 @@ public class AppRepositoryTest { ...@@ -66,4 +64,5 @@ public class AppRepositoryTest {
Assert.assertEquals(0, appRepository.count()); Assert.assertEquals(0, appRepository.count());
} }
} }
...@@ -8,7 +8,9 @@ import org.junit.Test; ...@@ -8,7 +8,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.BizTestConfiguration; import com.ctrip.apollo.biz.BizTestConfiguration;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
...@@ -17,6 +19,8 @@ import com.ctrip.apollo.biz.entity.Namespace; ...@@ -17,6 +19,8 @@ import com.ctrip.apollo.biz.entity.Namespace;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class) @SpringApplicationConfiguration(classes = BizTestConfiguration.class)
@Transactional
@Rollback
public class AdminServiceTest { public class AdminServiceTest {
@Autowired @Autowired
...@@ -49,4 +53,5 @@ public class AdminServiceTest { ...@@ -49,4 +53,5 @@ public class AdminServiceTest {
Assert.assertEquals(1, namespaces.size()); Assert.assertEquals(1, namespaces.size());
Assert.assertEquals("application", namespaces.get(0).getNamespaceName()); Assert.assertEquals("application", namespaces.get(0).getNamespaceName());
} }
} }
package com.ctrip.apollo.biz.service;
import java.util.Date;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
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.Commit;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.BizTestConfiguration;
import com.ctrip.apollo.biz.entity.App;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class)
@Transactional
@Commit
public class AdminServiceTransactionTest {
@Autowired
AdminService adminService;
@Autowired
private AppRepository appRepository;
@Autowired
private AppNamespaceRepository appNamespaceRepository;
@Autowired
private NamespaceRepository namespaceRepository;
@Autowired
private ClusterRepository clusterRepository;
@BeforeTransaction
public void verifyInitialDatabaseState() {
for (App app : appRepository.findAll()) {
System.out.println(app.getAppId());
}
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}
@Before
public void setUpTestDataWithinTransaction() {
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}
@Test
@Rollback
public void modifyDatabaseWithinTransaction() {
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);
}
@After
public void tearDownWithinTransaction() {
Assert.assertEquals(1, appRepository.count());
Assert.assertEquals(1, appNamespaceRepository.count());
Assert.assertEquals(1, namespaceRepository.count());
Assert.assertEquals(1, clusterRepository.count());
}
@AfterTransaction
public void verifyFinalDatabaseState() {
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}
}
...@@ -8,7 +8,9 @@ import org.junit.Test; ...@@ -8,7 +8,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.ctrip.apollo.biz.BizTestConfiguration; import com.ctrip.apollo.biz.BizTestConfiguration;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
...@@ -18,6 +20,8 @@ import com.ctrip.apollo.biz.entity.Privilege; ...@@ -18,6 +20,8 @@ import com.ctrip.apollo.biz.entity.Privilege;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BizTestConfiguration.class) @SpringApplicationConfiguration(classes = BizTestConfiguration.class)
@Transactional
@Rollback
public class PrivilegeServiceTest { public class PrivilegeServiceTest {
@Autowired @Autowired
...@@ -96,4 +100,5 @@ public class PrivilegeServiceTest { ...@@ -96,4 +100,5 @@ public class PrivilegeServiceTest {
Assert.assertTrue(privilService.hasPrivilege(namespace.getId(), "nobody", Assert.assertTrue(privilService.hasPrivilege(namespace.getId(), "nobody",
PrivilegeService.PrivilType.RELEASE)); PrivilegeService.PrivilType.RELEASE));
} }
} }
...@@ -19,9 +19,21 @@ ...@@ -19,9 +19,21 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-spectator</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId> <artifactId>spring-data-commons</artifactId>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
package com.ctrip.apollo; package com.ctrip.apollo;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
import org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* Spring boot application entry point * Spring boot application entry point
...@@ -14,7 +17,10 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; ...@@ -14,7 +17,10 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
public class ConfigServiceApplication { public class ConfigServiceApplication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(ConfigServiceApplication.class).run(args); new SpringApplicationBuilder(ConfigServiceApplication.class).run(args);
context.addApplicationListener(new ApplicationPidFileWriter());
context.addApplicationListener(new EmbeddedServerPortFileWriter());
} }
} }
package com.ctrip.apollo.portal; package com.ctrip.apollo.portal;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
import org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication @SpringBootApplication
public class PortalApplication { public class PortalApplication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
SpringApplication.run(PortalApplication.class, args); ConfigurableApplicationContext context = SpringApplication.run(PortalApplication.class, args);
context.addApplicationListener(new ApplicationPidFileWriter());
context.addApplicationListener(new EmbeddedServerPortFileWriter());
} }
} }
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