Commit 98b4c848 by Yiming Liu

Remove Version Entity

parent a68834f5
......@@ -15,21 +15,22 @@ import com.ctrip.apollo.core.dto.GroupDTO;
@RestController
public class GroupController {
@Autowired
private ViewService viewService;
@Autowired
private GroupService groupService;
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups")
public List<GroupDTO> findGroups(@PathVariable("clusterId") Long clusterId) {
List<Group> groups = viewService.findGroups(clusterId);
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups")
public List<GroupDTO> findGroups(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
List<Group> groups = viewService.findGroups(appId, clusterName);
return BeanUtils.batchTransform(GroupDTO.class, groups);
}
@RequestMapping("/groups/{groupId}")
public GroupDTO findOne(@PathVariable("groupId") Long groupId){
public GroupDTO findOne(@PathVariable("groupId") Long groupId) {
Group group = groupService.findOne(groupId);
return BeanUtils.transfrom(GroupDTO.class, group);
}
......
......@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
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.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
......@@ -17,11 +18,21 @@ public class ItemController {
@Autowired
private ViewService viewService;
@Autowired
private ItemService itemService;
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/items")
public List<ItemDTO> findItems(
@PathVariable("groupId") Long groupId) {
List<Item> items = viewService.findItems(groupId);
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups/{groupName}/items")
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("groupName") String groupName) {
List<Item> items = viewService.findItems(appId, clusterName,groupName);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
@RequestMapping("/items/{itemId}")
public ItemDTO findOne(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
return BeanUtils.transfrom(ItemDTO.class, item);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
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.entity.Version;
import com.ctrip.apollo.biz.service.VersionService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.VersionDTO;
@RestController
public class VersionController {
@Autowired
private ViewService viewService;
@Autowired
private VersionService versionService;
@RequestMapping("/app/{appId}/clusters/{clusterId}/versions")
public List<VersionDTO> findVersions(@PathVariable("appId") String appId, @PathVariable("clusterId") Long clusterId) {
List<Version> versions = viewService.findVersions(clusterId);
return BeanUtils.batchTransform(VersionDTO.class, versions);
}
@RequestMapping("/versions/{versionId}")
public VersionDTO findOne(@PathVariable("versionId") long versionId) {
Version version = versionService.findOne(versionId);
return BeanUtils.transfrom(VersionDTO.class, version);
}
}
......@@ -10,18 +10,32 @@ import org.hibernate.annotations.SQLDelete;
public class Group extends BaseEntity {
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String appId;
@Column(nullable = false)
private long clusterId;
@Column(nullable = false)
private long namespaceId;
private String clusterName;
@Column(nullable = false)
private String name;
private long namespaceId;
public String getAppId() {
return appId;
}
public long getClusterId() {
return clusterId;
}
public String getClusterName() {
return clusterName;
}
public String getName() {
return name;
}
......@@ -30,10 +44,18 @@ public class Group extends BaseEntity {
return namespaceId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public void setName(String name) {
this.name = name;
}
......
package com.ctrip.apollo.biz.entity;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
@Entity
@SQLDelete(sql = "Update Mapping set isDeleted = 1 where id = ?")
public class Mapping extends BaseEntity {
private long versionId;
private long releaseId;
public long getReleaseId() {
return releaseId;
}
public long getVersionId() {
return versionId;
}
public void setReleaseId(long releaseId) {
this.releaseId = releaseId;
}
public void setVersionId(long versionId) {
this.versionId = versionId;
}
}
package com.ctrip.apollo.biz.entity;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.SQLDelete;
......@@ -14,33 +11,27 @@ import org.hibernate.annotations.SQLDelete;
*/
@Entity
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
public class Release extends BaseEntity{
public class Release extends BaseEntity {
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String appId;
@Column(nullable = false)
private long groupId;
private String clusterName;
@Column
private String groupName;
@Column(nullable = false)
@Lob
private String configurations;
@Column(nullable = false)
private String comment;
@ManyToMany
private List<Version> versions;
@Column(nullable=false)
private String appId;
@Column(nullable=false)
private String clusterName;
public String getAppId() {
return appId;
}
......@@ -57,10 +48,6 @@ public class Release extends BaseEntity{
return configurations;
}
public long getGroupId() {
return groupId;
}
public String getGroupName() {
return groupName;
}
......@@ -69,10 +56,6 @@ public class Release extends BaseEntity{
return name;
}
public List<Version> getVersions() {
return versions;
}
public void setAppId(String appId) {
this.appId = appId;
}
......@@ -89,10 +72,6 @@ public class Release extends BaseEntity{
this.configurations = configurations;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
......@@ -101,7 +80,4 @@ public class Release extends BaseEntity{
this.name = name;
}
public void setVersions(List<Version> versions) {
this.versions = versions;
}
}
package com.ctrip.apollo.biz.entity;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.SQLDelete;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
@SQLDelete(sql = "Update Version set isDeleted = 1 where id = ?")
public class Version extends BaseEntity {
@Column(nullable = false)
private String name;
@Column(nullable = false)
private long clusterId;
// parent version could be null
@Column
private Long parentVersionId;
@ManyToMany
private List<Release> releases;
public long getClusterId() {
return clusterId;
}
public String getName() {
return name;
}
public Long getParentVersionId() {
return parentVersionId;
}
public List<Release> getReleases() {
return releases;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setName(String name) {
this.name = name;
}
public void setParentVersionId(Long parentVersionId) {
this.parentVersionId = parentVersionId;
}
public void setReleases(List<Release> releases) {
this.releases = releases;
}
}
......@@ -8,6 +8,7 @@ import com.ctrip.apollo.biz.entity.Group;
public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {
List<Group> findByClusterId(Long clusterId);
List<Group> findByAppIdAndClusterName(String appId, String clusterName);
Group findByAppIdAndClusterNameAndGroupName(String appId, String clusterName, String groupName);
}
......@@ -13,8 +13,9 @@ import com.ctrip.apollo.biz.entity.Release;
*/
public interface ReleaseRepository extends PagingAndSortingRepository<Release, Long> {
@Query("SELECT r FROM VersionReleaseMapping m INNER JOIN Release r on m.releaseId = r.Id INNER JOIN Version v on m.versionId = v.id WHERE r.id = :versionId AND r.groupName = :groupName")
Release findByGroupNameAndVersionId(@Param("groupName") String groupName, @Param("versionId") long versionId);
@Query("SELECT r FROM Release r WHERE r.appId = :appId AND r.clusterName = :clusterName AND r.groupName = :groupName order by id desc litmit 1")
Release findLatest(@Param("appId") String appId, @Param("clusterName") String clusterName,
@Param("groupName") String groupName);
List<Release> findByGroupId(Long groupId);
}
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Version;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface VersionRepository extends PagingAndSortingRepository<Version, Long> {
Version findByClusterIdAndName(Long id, String name);
List<Version> findByClusterId(Long clusterId);
}
......@@ -6,12 +6,8 @@ import java.util.Map;
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.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -26,12 +22,6 @@ import com.google.common.collect.Maps;
public class ConfigService {
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private VersionRepository versionRepository;
@Autowired
private ReleaseRepository releaseRepository;
@Autowired
......@@ -40,16 +30,8 @@ public class ConfigService {
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {};
public Release findRelease(String appId, String clusterName, String groupName, String versionName) {
Cluster cluster = clusterRepository.findByAppIdAndName(appId, clusterName);
if (cluster == null) {
return null;
}
Version version = versionRepository.findByClusterIdAndName(cluster.getId(), versionName);
if (version == null) {
return null;
}
Release release = releaseRepository.findByGroupNameAndVersionId(groupName, version.getId());
public Release findRelease(String appId, String clusterName, String groupName) {
Release release = releaseRepository.findLatest(appId, clusterName, groupName);
return release;
}
......@@ -57,11 +39,11 @@ public class ConfigService {
* Load configuration from database
*/
public ApolloConfig loadConfig(Release release, String groupName, String versionName) {
if(release==null){
if (release == null) {
return null;
}
ApolloConfig config =
new ApolloConfig(release.getAppId(), release.getClusterName(), groupName, versionName, release.getId());
ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(), groupName,
versionName, release.getId());
config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
return config;
}
......
......@@ -3,16 +3,18 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
@Service
public class VersionService {
public class ItemService {
@Autowired
private VersionRepository versionRepository;
public Version findOne(Long versionId){
return versionRepository.findOne(versionId);
private ItemRepository itemRepository;
public Item findOne(long itemId) {
Item item = itemRepository.findOne(itemId);
return item;
}
}
......@@ -10,12 +10,10 @@ import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.GroupRepository;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.google.common.base.Strings;
/**
......@@ -31,11 +29,8 @@ public class ViewService {
private GroupRepository groupRepository;
@Autowired
private VersionRepository versionRepository;
@Autowired
private ItemRepository itemRepository;
@Autowired
private ReleaseRepository releaseRepository;
......@@ -51,22 +46,24 @@ public class ViewService {
return clusters;
}
public List<Group> findGroups(Long clusterId) {
List<Group> groups = groupRepository.findByClusterId(clusterId);
public List<Group> findGroups(String appId, String clusterName) {
List<Group> groups = groupRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) {
return Collections.EMPTY_LIST;
}
return groups;
}
public List<Version> findVersions(Long clusterId) {
List<Version> versions = versionRepository.findByClusterId(clusterId);
if (versions == null) {
public List<Item> findItems(String appId, String clusterName, String groupName) {
Group group =
groupRepository.findByAppIdAndClusterNameAndGroupName(appId, clusterName, groupName);
if (group != null) {
return findItems(group.getId());
} else {
return Collections.EMPTY_LIST;
}
return versions;
}
public List<Item> findItems(Long groupId) {
List<Item> items = itemRepository.findByGroupId(groupId);
if (items == null) {
......@@ -74,12 +71,13 @@ public class ViewService {
}
return items;
}
public List<Release> findReleases(Long groupId){
public List<Release> findReleases(Long groupId) {
List<Release> releases = releaseRepository.findByGroupId(groupId);
if(releases==null){
if (releases == null) {
return Collections.EMPTY_LIST;
}
return releases;
}
}
......@@ -2,11 +2,8 @@ package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -21,7 +18,6 @@ import java.io.IOException;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.eq;
......@@ -35,8 +31,6 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ConfigServiceTest {
@Mock
private VersionRepository versionRepository;
@Mock
private ReleaseRepository releaseRepository;
@Mock
private ObjectMapper objectMapper;
......@@ -45,7 +39,6 @@ public class ConfigServiceTest {
@Before
public void setUp() throws Exception {
configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository);
ReflectionTestUtils
.setField(configService, "releaseRepository", releaseRepository);
ReflectionTestUtils.setField(configService, "objectMapper", objectMapper);
......
......@@ -29,7 +29,7 @@ public class ConfigController {
@PathVariable String groupName, @PathVariable String versionName,
@RequestParam(value = "releaseId", defaultValue = "-1") long clientSideReleaseId,
HttpServletResponse response) throws IOException {
Release release = configService.findRelease(appId, clusterName, groupName, versionName);
Release release = configService.findRelease(appId, clusterName, groupName);
if (release == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND,
String.format(
......
......@@ -39,95 +39,94 @@ public class ConfigControllerTest {
ReflectionTestUtils.setField(configController, "configService", configService);
}
@Test
public void testQueryConfig() throws Exception {
ApolloConfig someApolloConfig = mock(ApolloConfig.class);
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideNewReleaseId = 2;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Release someRelease = mock(Release.class);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfig(someRelease, someGroupName, someVersionName))
.thenReturn(someApolloConfig);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertEquals(someApolloConfig, result);
verify(configService, times(1)).findRelease(someAppId, someClusterName, someGroupName,
someVersionName);
verify(configService, times(1)).loadConfig(someRelease, someGroupName, someVersionName);
}
@Test
public void testQueryConfigWithVersionNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(null);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}
@Test
public void testQueryConfigWithApolloConfigNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideNewReleaseId = 2;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Release someRelease = mock(Release.class);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfig(someRelease, someGroupName, someVersionName)).thenReturn(null);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}
@Test
public void testQueryConfigWithApolloConfigNotModified() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideReleaseId = someClientSideReleaseId;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Release someRelease = mock(Release.class);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideReleaseId);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
verify(configService, never()).loadConfig(any(Release.class), anyString(), anyString());
}
// @Test
// public void testQueryConfig() throws Exception {
// ApolloConfig someApolloConfig = mock(ApolloConfig.class);
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// long someClientSideReleaseId = 1;
// long someServerSideNewReleaseId = 2;
// HttpServletResponse someResponse = mock(HttpServletResponse.class);
// Release someRelease = mock(Release.class);
//
// when(configService.findRelease(someAppId, someClusterName, someGroupName))
// .thenReturn(someRelease);
// when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
// when(configService.loadConfig(someRelease, someGroupName))
// .thenReturn(someApolloConfig);
//
// ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
// someClientSideReleaseId, someResponse);
//
// assertEquals(someApolloConfig, result);
// verify(configService, times(1)).findRelease(someAppId, someClusterName, someGroupName
// someVersinName);
// verify(configService, times(1)).loadConfig(someRelease, someGroupName,);
// }
//
// @Test
// public void testQueryConfigWithVersionNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// String someVersionName = "someVersion";
// long someClientSideReleaseId = 1;
// HttpServletResponse someResponse = mock(HttpServletResponse.class);
//
// when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
// .thenReturn(null);
//
// ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
// someVersionName, someClientSideReleaseId, someResponse);
//
// assertNull(result);
// verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
// }
//
// @Test
// public void testQueryConfigWithApolloConfigNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// String someVersionName = "someVersion";
// long someClientSideReleaseId = 1;
// long someServerSideNewReleaseId = 2;
// HttpServletResponse someResponse = mock(HttpServletResponse.class);
// Release someRelease = mock(Release.class);
//
// when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
// .thenReturn(someRelease);
// when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
// when(configService.loadConfig(someRelease, someGroupName, someVersionName)).thenReturn(null);
//
// ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
// someVersionName, someClientSideReleaseId, someResponse);
//
// assertNull(result);
// verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
// }
//
// @Test
// public void testQueryConfigWithApolloConfigNotModified() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// String someVersionName = "someVersion";
// long someClientSideReleaseId = 1;
// long someServerSideReleaseId = someClientSideReleaseId;
// HttpServletResponse someResponse = mock(HttpServletResponse.class);
// Release someRelease = mock(Release.class);
//
// when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
// .thenReturn(someRelease);
// when(someRelease.getId()).thenReturn(someServerSideReleaseId);
//
// ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
// someVersionName, someClientSideReleaseId, someResponse);
//
// assertNull(result);
// verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
// verify(configService, never()).loadConfig(any(Release.class), anyString(), anyString());
// }
}
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