Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
6141e812
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6141e812
编写于
4月 21, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #130 from yiming187/exception_update
Update exception
上级
cde0de19
166f154a
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
121 addition
and
148 deletion
+121
-148
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java
...rip/apollo/adminservice/controller/ReleaseController.java
+11
-2
apollo-adminservice/src/test/resources/data.sql
apollo-adminservice/src/test/resources/data.sql
+13
-10
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ConfigService.java
...main/java/com/ctrip/apollo/biz/service/ConfigService.java
+6
-11
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemSetService.java
...ain/java/com/ctrip/apollo/biz/service/ItemSetService.java
+0
-2
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java
...ain/java/com/ctrip/apollo/biz/service/ReleaseService.java
+4
-16
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/ConfigServiceTest.java
.../java/com/ctrip/apollo/biz/service/ConfigServiceTest.java
+58
-87
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
...ollo/common/controller/GlobalDefaultExceptionHandler.java
+5
-2
apollo-common/src/main/java/com/ctrip/apollo/common/utils/ExceptionUtils.java
...in/java/com/ctrip/apollo/common/utils/ExceptionUtils.java
+6
-3
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/ConfigController.java
...rip/apollo/configservice/controller/ConfigController.java
+1
-1
apollo-configservice/src/test/java/com/ctrip/apollo/configservice/controller/ConfigControllerTest.java
...apollo/configservice/controller/ConfigControllerTest.java
+4
-4
apollo-configservice/src/test/resources/data.sql
apollo-configservice/src/test/resources/data.sql
+13
-10
未找到文件。
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java
浏览文件 @
6141e812
...
...
@@ -10,8 +10,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.ctrip.apollo.biz.entity.Namespace
;
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
;
...
...
@@ -31,6 +33,9 @@ public class ReleaseController {
@Autowired
private
ConfigService
configService
;
@Autowired
private
NamespaceService
namespaceService
;
@RequestMapping
(
"/release/{releaseId}"
)
public
ReleaseDTO
get
(
@PathVariable
(
"releaseId"
)
long
releaseId
)
{
Release
release
=
releaseService
.
findOne
(
releaseId
);
...
...
@@ -66,8 +71,12 @@ public class ReleaseController {
@PathVariable
(
"namespaceName"
)
String
namespaceName
,
@RequestParam
(
"name"
)
String
name
,
@RequestParam
(
name
=
"comment"
,
required
=
false
)
String
comment
,
@ActiveUser
UserDetails
user
)
{
Release
release
=
releaseService
.
buildRelease
(
name
,
comment
,
appId
,
clusterName
,
namespaceName
,
user
.
getUsername
());
Namespace
namespace
=
namespaceService
.
findOne
(
appId
,
clusterName
,
namespaceName
);
if
(
namespace
==
null
)
{
throw
new
NotFoundException
(
String
.
format
(
"Could not find namespace for %s %s %s"
,
appId
,
clusterName
,
namespaceName
));
}
Release
release
=
releaseService
.
buildRelease
(
name
,
comment
,
namespace
,
user
.
getUsername
());
return
BeanUtils
.
transfrom
(
ReleaseDTO
.
class
,
release
);
}
}
apollo-adminservice/src/test/resources/data.sql
浏览文件 @
6141e812
...
...
@@ -11,21 +11,24 @@ INSERT INTO Cluster (AppId, Name) VALUES ('100003173', 'default');
INSERT
INTO
Cluster
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'cluster3'
);
INSERT
INTO
Cluster
(
AppId
,
Name
)
VALUES
(
'fxhermesproducer'
,
'default'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppID
,
Name
)
VALUES
(
'fxhermesproducer'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'100003171'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'fx.apollo.config'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'100003172'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'fx.apollo.admin'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'100003173'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'fx.apollo.portal'
);
INSERT
INTO
AppNamespace
(
AppID
,
Name
)
VALUES
(
'fxhermesproducer'
,
'fx.hermes.producer'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
1
,
'100003171'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
2
,
'fxhermesproducer'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
3
,
'100003172'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
4
,
'100003173'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
5
,
'100003171'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
1
,
'100003171'
,
'default'
,
'
100003171
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
2
,
'fxhermesproducer'
,
'default'
,
'
fx.hermes.producer
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
3
,
'100003172'
,
'default'
,
'
100003172
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
4
,
'100003173'
,
'default'
,
'
100003173
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
5
,
'100003171'
,
'default'
,
'
100003171
'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
1
,
'k1'
,
'v1'
,
'comment1'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
1
,
'k2'
,
'v2'
,
'comment2'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
2
,
'k3'
,
'v3'
,
'comment3'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
5
,
'k3'
,
'v4'
,
'comment4'
);
INSERT
INTO
RELEASE
(
Name
,
Comment
,
AppId
,
ClusterName
,
NamespaceName
,
Configurations
)
VALUES
(
'REV1'
,
'First Release'
,
'100003171'
,
'default'
,
'
application
'
,
'{"k1":"v1"}'
);
INSERT
INTO
RELEASE
(
Name
,
Comment
,
AppId
,
ClusterName
,
NamespaceName
,
Configurations
)
VALUES
(
'REV1'
,
'First Release'
,
'100003171'
,
'default'
,
'
100003171
'
,
'{"k1":"v1"}'
);
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ConfigService.java
浏览文件 @
6141e812
package
com.ctrip.apollo.biz.service
;
import
com.google.common.collect.Maps
;
import
com.google.gson.Gson
;
import
com.google.gson.reflect.TypeToken
;
...
...
@@ -27,32 +26,28 @@ public class ConfigService {
private
Gson
gson
=
new
Gson
();
private
Type
configurationTypeReference
=
new
TypeToken
<
Map
<
String
,
String
>>(){}.
getType
();
private
Type
configurationTypeReference
=
new
TypeToken
<
Map
<
String
,
String
>>()
{}.
getType
();
public
Release
findRelease
(
String
appId
,
String
clusterName
,
String
namespaceName
)
{
Release
release
=
releaseRepository
.
findFirstByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc
(
appId
,
clusterName
,
namespaceName
);
Release
release
=
releaseRepository
.
findFirstByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc
(
appId
,
clusterName
,
namespaceName
);
return
release
;
}
/**
* Load configuration from database
*/
public
ApolloConfig
loadConfig
(
Release
release
,
String
namespaceName
)
{
public
ApolloConfig
loadConfig
(
Release
release
)
{
if
(
release
==
null
)
{
return
null
;
}
ApolloConfig
config
=
new
ApolloConfig
(
release
.
getAppId
(),
release
.
getClusterName
(),
namespaceName
,
String
.
valueOf
(
release
.
getId
()));
release
.
getNamespaceName
()
,
String
.
valueOf
(
release
.
getId
()));
config
.
setConfigurations
(
transformConfigurationToMap
(
release
.
getConfigurations
()));
return
config
;
}
Map
<
String
,
String
>
transformConfigurationToMap
(
String
configurations
)
{
try
{
return
gson
.
fromJson
(
configurations
,
configurationTypeReference
);
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
return
Maps
.
newHashMap
();
}
return
gson
.
fromJson
(
configurations
,
configurationTypeReference
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemSetService.java
浏览文件 @
6141e812
...
...
@@ -11,8 +11,6 @@ import com.ctrip.apollo.common.utils.BeanUtils;
import
com.ctrip.apollo.core.dto.ItemChangeSets
;
import
com.ctrip.apollo.core.dto.ItemDTO
;
import
java.util.Date
;
@Service
public
class
ItemSetService
{
...
...
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java
浏览文件 @
6141e812
...
...
@@ -14,9 +14,7 @@ import com.ctrip.apollo.biz.entity.Item;
import
com.ctrip.apollo.biz.entity.Namespace
;
import
com.ctrip.apollo.biz.entity.Release
;
import
com.ctrip.apollo.biz.repository.ItemRepository
;
import
com.ctrip.apollo.biz.repository.NamespaceRepository
;
import
com.ctrip.apollo.biz.repository.ReleaseRepository
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
import
com.ctrip.apollo.core.utils.StringUtils
;
import
com.google.gson.Gson
;
...
...
@@ -29,9 +27,6 @@ public class ReleaseService {
@Autowired
private
ReleaseRepository
releaseRepository
;
@Autowired
private
NamespaceRepository
namespaceRepository
;
@Autowired
private
ItemRepository
itemRepository
;
...
...
@@ -46,14 +41,7 @@ public class ReleaseService {
}
@Transactional
public
Release
buildRelease
(
String
name
,
String
comment
,
String
appId
,
String
clusterName
,
String
namespaceName
,
String
owner
)
{
Namespace
namespace
=
namespaceRepository
.
findByAppIdAndClusterNameAndNamespaceName
(
appId
,
clusterName
,
namespaceName
);
if
(
namespace
==
null
)
{
throw
new
NotFoundException
(
String
.
format
(
"Could not find namespace for %s %s %s"
,
appId
,
clusterName
,
namespaceName
));
}
public
Release
buildRelease
(
String
name
,
String
comment
,
Namespace
namespace
,
String
owner
)
{
List
<
Item
>
items
=
itemRepository
.
findByNamespaceIdOrderByLineNumAsc
(
namespace
.
getId
());
Map
<
String
,
String
>
configurations
=
new
HashMap
<
String
,
String
>();
for
(
Item
item
:
items
)
{
...
...
@@ -68,9 +56,9 @@ public class ReleaseService {
release
.
setDataChangeCreatedBy
(
owner
);
release
.
setName
(
name
);
release
.
setComment
(
comment
);
release
.
setAppId
(
appId
);
release
.
setClusterName
(
clusterName
);
release
.
setNamespaceName
(
namespace
Name
);
release
.
setAppId
(
namespace
.
getAppId
()
);
release
.
setClusterName
(
namespace
.
getClusterName
()
);
release
.
setNamespaceName
(
namespace
.
getNamespaceName
()
);
release
.
setConfigurations
(
gson
.
toJson
(
configurations
));
release
=
releaseRepository
.
save
(
release
);
...
...
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/ConfigServiceTest.java
浏览文件 @
6141e812
package
com.ctrip.apollo.biz.service
;
import
com.google.common.collect.Maps
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.apollo.biz.entity.Release
;
import
com.ctrip.apollo.biz.repository.ReleaseRepository
;
import
java.util.Map
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -12,10 +16,11 @@ import org.mockito.Mock;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
java.util.Map
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
com.ctrip.apollo.biz.entity.Release
;
import
com.ctrip.apollo.biz.repository.ReleaseRepository
;
import
com.ctrip.apollo.core.dto.ApolloConfig
;
import
com.google.common.collect.Maps
;
import
com.google.gson.JsonSyntaxException
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
@@ -24,91 +29,58 @@ import static org.junit.Assert.assertTrue;
public
class
ConfigServiceTest
{
@Mock
private
ReleaseRepository
releaseRepository
;
private
ConfigService
configService
;
@Before
public
void
setUp
()
throws
Exception
{
configService
=
new
ConfigService
();
ReflectionTestUtils
.
setField
(
configService
,
"releaseRepository"
,
releaseRepository
);
ReflectionTestUtils
.
setField
(
configService
,
"releaseRepository"
,
releaseRepository
);
}
// @Test
// public void testLoadConfig() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// String someVersionName = "someVersionName";
// long someReleaseId = 1;
// String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
//
// Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
// Release
// someRelease =
// assembleRelease(someReleaseId, someClusterName, someGroupName, someValidConfiguration);
// Map<String, Object> someMap = Maps.newHashMap();
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
// when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
// .thenReturn(someReleaseSnapShot);
// when(objectMapper.readValue(eq(someValidConfiguration), (TypeReference) anyObject()))
// .thenReturn(someMap);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertEquals(someAppId, result.getAppId());
// assertEquals(someClusterName, result.getCluster());
// assertEquals(someVersionName, result.getVersion());
// assertEquals(someReleaseId, result.getReleaseId());
// assertEquals(someMap, result.getConfigurations());
// }
//
// @Test
// public void testLoadConfigWithVersionNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someVersionName = "someVersionName";
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(null);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertNull(result);
// verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
// }
//
// @Test
// public void testLoadConfigWithConfigNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someVersionName = "someVersionName";
// long someReleaseId = 1;
// Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
// when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
// .thenReturn(null);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertNull(result);
// verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
// verify(releaseRepository, times(1))
// .findByReleaseIdAndClusterName(someReleaseId, someClusterName);
// }
//
// private Version assembleVersion(String appId, String versionName, long releaseId) {
// Version version = new Version();
// version.setAppId(appId);
// version.setName(versionName);
// version.setReleaseId(releaseId);
// return version;
// }
private
Release
assembleRelease
(
long
releaseId
,
String
clusterName
,
String
groupName
,
String
configurations
)
{
@Test
public
void
testLoadConfig
()
throws
Exception
{
String
someAppId
=
"1"
;
String
someClusterName
=
"someClusterName"
;
String
someGroupName
=
"someGroupName"
;
String
someReleaseId
=
"1"
;
String
someValidConfiguration
=
"{\"apollo.bar\": \"foo\"}"
;
Release
someRelease
=
assembleRelease
(
someReleaseId
,
someAppId
,
someClusterName
,
someGroupName
,
someValidConfiguration
);
when
(
releaseRepository
.
findFirstByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc
(
someAppId
,
someClusterName
,
someGroupName
)).
thenReturn
(
someRelease
);
ApolloConfig
result
=
configService
.
loadConfig
(
someRelease
);
assertEquals
(
someAppId
,
result
.
getAppId
());
assertEquals
(
someClusterName
,
result
.
getCluster
());
assertEquals
(
someReleaseId
,
result
.
getReleaseId
());
assertEquals
(
"foo"
,
result
.
getConfigurations
().
get
(
"apollo.bar"
));
}
@Test
public
void
testLoadConfigWithConfigNotFound
()
throws
Exception
{
String
someAppId
=
"1"
;
String
someClusterName
=
"someClusterName"
;
String
someNamespaceName
=
"someNamespaceName"
;
when
(
releaseRepository
.
findFirstByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc
(
someAppId
,
someClusterName
,
someNamespaceName
)).
thenReturn
(
null
);
Release
someRelease
=
configService
.
findRelease
(
someAppId
,
someClusterName
,
someNamespaceName
);
ApolloConfig
result
=
configService
.
loadConfig
(
someRelease
);
assertNull
(
result
);
verify
(
releaseRepository
,
times
(
1
)).
findFirstByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc
(
someAppId
,
someClusterName
,
someNamespaceName
);
}
private
Release
assembleRelease
(
String
releaseId
,
String
appId
,
String
clusterName
,
String
groupName
,
String
configurations
)
{
Release
release
=
new
Release
();
release
.
setId
(
releaseId
);
release
.
setId
(
Long
.
valueOf
(
releaseId
));
release
.
setAppId
(
appId
);
release
.
setClusterName
(
clusterName
);
release
.
setNamespaceName
(
groupName
);
release
.
setConfigurations
(
configurations
);
...
...
@@ -127,12 +99,11 @@ public class ConfigServiceTest {
assertEquals
(
someMap
,
result
);
}
@Test
@Test
(
expected
=
JsonSyntaxException
.
class
)
public
void
testTransformConfigurationToMapFailed
()
throws
Exception
{
String
someInvalidConfiguration
=
"xxx"
;
Map
<
String
,
String
>
result
=
Map
<
String
,
String
>
result
=
configService
.
transformConfigurationToMap
(
someInvalidConfiguration
);
assertTrue
(
result
.
isEmpty
());
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
浏览文件 @
6141e812
...
...
@@ -13,7 +13,9 @@ import com.ctrip.apollo.core.exception.AbstractBaseException;
import
com.ctrip.apollo.core.exception.BadRequestException
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
import
com.google.gson.Gson
;
import
com.google.gson.reflect.TypeToken
;
import
java.lang.reflect.Type
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.LinkedHashMap
;
...
...
@@ -32,6 +34,8 @@ public class GlobalDefaultExceptionHandler {
private
Gson
gson
=
new
Gson
();
private
static
Type
mapType
=
new
TypeToken
<
Map
<
String
,
Object
>>()
{}.
getType
();
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Exception
ex
)
{
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
...
...
@@ -85,8 +89,7 @@ public class GlobalDefaultExceptionHandler {
@ExceptionHandler
(
HttpStatusCodeException
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
restTemplateException
(
HttpServletRequest
request
,
HttpStatusCodeException
ex
)
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
errorAttributes
=
gson
.
fromJson
(
ex
.
getResponseBodyAsString
(),
Map
.
class
);
Map
<
String
,
Object
>
errorAttributes
=
gson
.
fromJson
(
ex
.
getResponseBodyAsString
(),
mapType
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
APPLICATION_JSON
);
return
new
ResponseEntity
<>(
errorAttributes
,
headers
,
ex
.
getStatusCode
());
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/utils/ExceptionUtils.java
浏览文件 @
6141e812
package
com.ctrip.apollo.common.utils
;
import
java.lang.reflect.Type
;
import
java.util.Map
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.google.common.base.MoreObjects
;
import
com.google.gson.Gson
;
import
com.google.gson.reflect.TypeToken
;
public
final
class
ExceptionUtils
{
private
static
Gson
gson
=
new
Gson
();
private
static
Type
mapType
=
new
TypeToken
<
Map
<
String
,
Object
>>()
{}.
getType
();
public
static
String
toString
(
HttpStatusCodeException
e
)
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
errorAttributes
=
gson
.
fromJson
(
e
.
getResponseBodyAsString
(),
Map
.
class
);
Map
<
String
,
Object
>
errorAttributes
=
gson
.
fromJson
(
e
.
getResponseBodyAsString
(),
mapType
);
if
(
errorAttributes
!=
null
)
{
return
MoreObjects
.
toStringHelper
(
HttpStatusCodeException
.
class
)
return
MoreObjects
.
toStringHelper
(
HttpStatusCodeException
.
class
)
.
omitNullValues
()
.
add
(
"status"
,
errorAttributes
.
get
(
"status"
))
.
add
(
"message"
,
errorAttributes
.
get
(
"message"
))
.
add
(
"timestamp"
,
errorAttributes
.
get
(
"timestamp"
))
...
...
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/ConfigController.java
浏览文件 @
6141e812
...
...
@@ -55,7 +55,7 @@ public class ConfigController {
return
null
;
}
ApolloConfig
apolloConfig
=
configService
.
loadConfig
(
release
,
namespace
);
ApolloConfig
apolloConfig
=
configService
.
loadConfig
(
release
);
if
(
apolloConfig
==
null
)
{
response
.
sendError
(
HttpServletResponse
.
SC_NOT_FOUND
,
...
...
apollo-configservice/src/test/java/com/ctrip/apollo/configservice/controller/ConfigControllerTest.java
浏览文件 @
6141e812
...
...
@@ -54,14 +54,14 @@ public class ConfigControllerTest {
when
(
configService
.
findRelease
(
someAppId
,
someClusterName
,
someNamespaceName
))
.
thenReturn
(
someRelease
);
when
(
someRelease
.
getId
()).
thenReturn
(
someServerSideNewReleaseId
);
when
(
configService
.
loadConfig
(
someRelease
,
someNamespaceName
)).
thenReturn
(
someApolloConfig
);
when
(
configService
.
loadConfig
(
someRelease
)).
thenReturn
(
someApolloConfig
);
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
someNamespaceName
,
String
.
valueOf
(
someClientSideReleaseId
),
someResponse
);
assertEquals
(
someApolloConfig
,
result
);
verify
(
configService
,
times
(
1
)).
findRelease
(
someAppId
,
someClusterName
,
someNamespaceName
);
verify
(
configService
,
times
(
1
)).
loadConfig
(
someRelease
,
someNamespaceName
);
verify
(
configService
,
times
(
1
)).
loadConfig
(
someRelease
);
}
...
...
@@ -95,7 +95,7 @@ public class ConfigControllerTest {
when
(
configService
.
findRelease
(
someAppId
,
someClusterName
,
someNamespaceName
))
.
thenReturn
(
someRelease
);
when
(
someRelease
.
getId
()).
thenReturn
(
someServerSideNewReleaseId
);
when
(
configService
.
loadConfig
(
someRelease
,
someNamespaceName
)).
thenReturn
(
null
);
when
(
configService
.
loadConfig
(
someRelease
)).
thenReturn
(
null
);
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
someNamespaceName
,
String
.
valueOf
(
someClientSideReleaseId
),
someResponse
);
...
...
@@ -123,6 +123,6 @@ public class ConfigControllerTest {
assertNull
(
result
);
verify
(
someResponse
,
times
(
1
)).
setStatus
(
HttpServletResponse
.
SC_NOT_MODIFIED
);
verify
(
configService
,
never
()).
loadConfig
(
any
(
Release
.
class
)
,
anyString
()
);
verify
(
configService
,
never
()).
loadConfig
(
any
(
Release
.
class
));
}
}
apollo-configservice/src/test/resources/data.sql
浏览文件 @
6141e812
...
...
@@ -11,21 +11,24 @@ INSERT INTO Cluster (AppId, Name) VALUES ('100003173', 'default');
INSERT
INTO
Cluster
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'cluster3'
);
INSERT
INTO
Cluster
(
AppId
,
Name
)
VALUES
(
'fxhermesproducer'
,
'default'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppID
,
Name
)
VALUES
(
'fxhermesproducer'
,
'application'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'100003171'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003171'
,
'fx.apollo.config'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'100003172'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003172'
,
'fx.apollo.admin'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'100003173'
);
INSERT
INTO
AppNamespace
(
AppId
,
Name
)
VALUES
(
'100003173'
,
'fx.apollo.portal'
);
INSERT
INTO
AppNamespace
(
AppID
,
Name
)
VALUES
(
'fxhermesproducer'
,
'fx.hermes.producer'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
1
,
'100003171'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
2
,
'fxhermesproducer'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
3
,
'100003172'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
4
,
'100003173'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
5
,
'100003171'
,
'default'
,
'
application
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
1
,
'100003171'
,
'default'
,
'
100003171
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
2
,
'fxhermesproducer'
,
'default'
,
'
fx.hermes.producer
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
3
,
'100003172'
,
'default'
,
'
100003172
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
4
,
'100003173'
,
'default'
,
'
100003173
'
);
INSERT
INTO
Namespace
(
Id
,
AppId
,
ClusterName
,
NamespaceName
)
VALUES
(
5
,
'100003171'
,
'default'
,
'
100003171
'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
1
,
'k1'
,
'v1'
,
'comment1'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
1
,
'k2'
,
'v2'
,
'comment2'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
2
,
'k3'
,
'v3'
,
'comment3'
);
INSERT
INTO
Item
(
NamespaceId
,
`Key`
,
Value
,
Comment
)
VALUES
(
5
,
'k3'
,
'v4'
,
'comment4'
);
INSERT
INTO
RELEASE
(
Name
,
Comment
,
AppId
,
ClusterName
,
NamespaceName
,
Configurations
)
VALUES
(
'REV1'
,
'First Release'
,
'100003171'
,
'default'
,
'
application
'
,
'{"k1":"v1"}'
);
INSERT
INTO
RELEASE
(
Name
,
Comment
,
AppId
,
ClusterName
,
NamespaceName
,
Configurations
)
VALUES
(
'REV1'
,
'First Release'
,
'100003171'
,
'default'
,
'
100003171
'
,
'{"k1":"v1"}'
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录