Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
8d641a9a
Commit
8d641a9a
authored
Apr 20, 2016
by
Yiming Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhance exception process in portal
parent
d237cec2
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
205 additions
and
108 deletions
+205
-108
AllTests.java
...src/test/java/com/ctrip/apollo/adminservice/AllTests.java
+4
-1
AbstractControllerTest.java
...pollo/adminservice/controller/AbstractControllerTest.java
+1
-1
AppControllerTest.java
...rip/apollo/adminservice/controller/AppControllerTest.java
+0
-2
ControllerIntegrationExceptionTest.java
...ervice/controller/ControllerIntegrationExceptionTest.java
+0
-1
GlobalDefaultExceptionHandler.java
...ollo/common/controller/GlobalDefaultExceptionHandler.java
+19
-0
ExceptionUtils.java
...in/java/com/ctrip/apollo/common/utils/ExceptionUtils.java
+28
-0
AbstractBaseException.java
...om/ctrip/apollo/core/exception/AbstractBaseException.java
+4
-0
ServiceException.java
...ava/com/ctrip/apollo/core/exception/ServiceException.java
+3
-0
API.java
...portal/src/main/java/com/ctrip/apollo/portal/api/API.java
+0
-8
AdminServiceAPI.java
...ain/java/com/ctrip/apollo/portal/api/AdminServiceAPI.java
+36
-88
AppService.java
...main/java/com/ctrip/apollo/portal/service/AppService.java
+6
-5
AbstractPortalTest.java
...test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
+17
-0
AppServiceTest.java
...src/test/java/com/ctrip/apollo/portal/AppServiceTest.java
+1
-1
ConfigServiceTest.java
.../test/java/com/ctrip/apollo/portal/ConfigServiceTest.java
+1
-1
ServiceExceptionTest.java
...st/java/com/ctrip/apollo/portal/ServiceExceptionTest.java
+85
-0
No files found.
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/AllTests.java
View file @
8d641a9a
...
@@ -5,11 +5,14 @@ import org.junit.runners.Suite;
...
@@ -5,11 +5,14 @@ import org.junit.runners.Suite;
import
org.junit.runners.Suite.SuiteClasses
;
import
org.junit.runners.Suite.SuiteClasses
;
import
com.ctrip.apollo.adminservice.controller.AppControllerTest
;
import
com.ctrip.apollo.adminservice.controller.AppControllerTest
;
import
com.ctrip.apollo.adminservice.controller.ControllerExceptionTest
;
import
com.ctrip.apollo.adminservice.controller.ControllerIntegrationExceptionTest
;
import
com.ctrip.apollo.adminservice.controller.ItemSetControllerTest
;
import
com.ctrip.apollo.adminservice.controller.ItemSetControllerTest
;
import
com.ctrip.apollo.adminservice.controller.ReleaseControllerTest
;
import
com.ctrip.apollo.adminservice.controller.ReleaseControllerTest
;
@RunWith
(
Suite
.
class
)
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
AppControllerTest
.
class
,
ReleaseControllerTest
.
class
,
ItemSetControllerTest
.
class
})
@SuiteClasses
({
AppControllerTest
.
class
,
ReleaseControllerTest
.
class
,
ItemSetControllerTest
.
class
,
ControllerExceptionTest
.
class
,
ControllerIntegrationExceptionTest
.
class
})
public
class
AllTests
{
public
class
AllTests
{
}
}
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AbstractControllerTest.java
View file @
8d641a9a
...
@@ -18,7 +18,7 @@ import com.ctrip.apollo.AdminServiceTestConfiguration;
...
@@ -18,7 +18,7 @@ import com.ctrip.apollo.AdminServiceTestConfiguration;
@WebIntegrationTest
(
randomPort
=
true
)
@WebIntegrationTest
(
randomPort
=
true
)
public
abstract
class
AbstractControllerTest
{
public
abstract
class
AbstractControllerTest
{
RestTemplate
restTemplate
=
new
TestRestTemplate
(
"
user
"
,
""
);
RestTemplate
restTemplate
=
new
TestRestTemplate
(
"
apollo
"
,
""
);
@PostConstruct
@PostConstruct
private
void
postConstruct
()
{
private
void
postConstruct
()
{
...
...
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
View file @
8d641a9a
...
@@ -53,7 +53,6 @@ public class AppControllerTest extends AbstractControllerTest {
...
@@ -53,7 +53,6 @@ public class AppControllerTest extends AbstractControllerTest {
App
savedApp
=
appRepository
.
findOne
(
first
.
getId
());
App
savedApp
=
appRepository
.
findOne
(
first
.
getId
());
Assert
.
assertEquals
(
dto
.
getAppId
(),
savedApp
.
getAppId
());
Assert
.
assertEquals
(
dto
.
getAppId
(),
savedApp
.
getAppId
());
Assert
.
assertNotNull
(
savedApp
.
getDataChangeCreatedTime
());
Assert
.
assertNotNull
(
savedApp
.
getDataChangeCreatedTime
());
Assert
.
assertNull
(
savedApp
.
getDataChangeLastModifiedTime
());
response
=
restTemplate
.
postForEntity
(
getBaseAppUrl
(),
dto
,
AppDTO
.
class
);
response
=
restTemplate
.
postForEntity
(
getBaseAppUrl
(),
dto
,
AppDTO
.
class
);
AppDTO
second
=
response
.
getBody
();
AppDTO
second
=
response
.
getBody
();
...
@@ -82,7 +81,6 @@ public class AppControllerTest extends AbstractControllerTest {
...
@@ -82,7 +81,6 @@ public class AppControllerTest extends AbstractControllerTest {
@Test
(
expected
=
HttpClientErrorException
.
class
)
@Test
(
expected
=
HttpClientErrorException
.
class
)
@Sql
(
scripts
=
"/controller/cleanup.sql"
,
executionPhase
=
ExecutionPhase
.
AFTER_TEST_METHOD
)
@Sql
(
scripts
=
"/controller/cleanup.sql"
,
executionPhase
=
ExecutionPhase
.
AFTER_TEST_METHOD
)
public
void
testFindNotExist
()
{
public
void
testFindNotExist
()
{
ResponseEntity
<
AppDTO
>
result
=
restTemplate
.
getForEntity
(
getBaseAppUrl
()
+
"notExists"
,
AppDTO
.
class
);
restTemplate
.
getForEntity
(
getBaseAppUrl
()
+
"notExists"
,
AppDTO
.
class
);
}
}
...
...
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerIntegrationExceptionTest.java
View file @
8d641a9a
...
@@ -11,7 +11,6 @@ import org.junit.Test;
...
@@ -11,7 +11,6 @@ import org.junit.Test;
import
org.mockito.Mock
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.mockito.MockitoAnnotations
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.jdbc.Sql
;
import
org.springframework.test.context.jdbc.Sql
;
import
org.springframework.test.context.jdbc.Sql.ExecutionPhase
;
import
org.springframework.test.context.jdbc.Sql.ExecutionPhase
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.test.util.ReflectionTestUtils
;
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
View file @
8d641a9a
...
@@ -7,10 +7,12 @@ import org.springframework.web.HttpMediaTypeException;
...
@@ -7,10 +7,12 @@ import org.springframework.web.HttpMediaTypeException;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.ctrip.apollo.core.exception.AbstractBaseException
;
import
com.ctrip.apollo.core.exception.AbstractBaseException
;
import
com.ctrip.apollo.core.exception.BadRequestException
;
import
com.ctrip.apollo.core.exception.BadRequestException
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
import
com.google.gson.Gson
;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeFormatter
;
...
@@ -27,6 +29,9 @@ import static org.springframework.http.MediaType.APPLICATION_JSON;
...
@@ -27,6 +29,9 @@ import static org.springframework.http.MediaType.APPLICATION_JSON;
@ControllerAdvice
@ControllerAdvice
public
class
GlobalDefaultExceptionHandler
{
public
class
GlobalDefaultExceptionHandler
{
private
Gson
gson
=
new
Gson
();
@ExceptionHandler
(
Exception
.
class
)
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Exception
ex
)
{
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Exception
ex
)
{
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
...
@@ -40,6 +45,10 @@ public class GlobalDefaultExceptionHandler {
...
@@ -40,6 +45,10 @@ public class GlobalDefaultExceptionHandler {
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
HttpStatus
status
,
Throwable
ex
,
String
message
)
{
HttpStatus
status
,
Throwable
ex
,
String
message
)
{
ex
=
resolveError
(
ex
);
ex
=
resolveError
(
ex
);
if
(
ex
.
getCause
()
instanceof
HttpStatusCodeException
)
{
return
restTemplateException
(
request
,
(
HttpStatusCodeException
)
ex
.
getCause
());
}
Map
<
String
,
Object
>
errorAttributes
=
new
LinkedHashMap
<>();
Map
<
String
,
Object
>
errorAttributes
=
new
LinkedHashMap
<>();
errorAttributes
.
put
(
"status"
,
status
.
value
());
errorAttributes
.
put
(
"status"
,
status
.
value
());
errorAttributes
.
put
(
"message"
,
message
);
errorAttributes
.
put
(
"message"
,
message
);
...
@@ -73,6 +82,16 @@ public class GlobalDefaultExceptionHandler {
...
@@ -73,6 +82,16 @@ public class GlobalDefaultExceptionHandler {
return
handleError
(
request
,
BAD_REQUEST
,
ex
);
return
handleError
(
request
,
BAD_REQUEST
,
ex
);
}
}
@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
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
APPLICATION_JSON
);
return
new
ResponseEntity
<>(
errorAttributes
,
headers
,
ex
.
getStatusCode
());
}
private
Throwable
resolveError
(
Throwable
ex
)
{
private
Throwable
resolveError
(
Throwable
ex
)
{
while
(
ex
instanceof
ServletException
&&
ex
.
getCause
()
!=
null
)
{
while
(
ex
instanceof
ServletException
&&
ex
.
getCause
()
!=
null
)
{
ex
=
((
ServletException
)
ex
).
getCause
();
ex
=
((
ServletException
)
ex
).
getCause
();
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/utils/ExceptionUtils.java
0 → 100644
View file @
8d641a9a
package
com
.
ctrip
.
apollo
.
common
.
utils
;
import
java.util.Map
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.google.common.base.MoreObjects
;
import
com.google.gson.Gson
;
public
final
class
ExceptionUtils
{
private
static
Gson
gson
=
new
Gson
();
public
static
String
toString
(
HttpStatusCodeException
e
)
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
errorAttributes
=
gson
.
fromJson
(
e
.
getResponseBodyAsString
(),
Map
.
class
);
if
(
errorAttributes
!=
null
)
{
return
MoreObjects
.
toStringHelper
(
HttpStatusCodeException
.
class
)
.
add
(
"status"
,
errorAttributes
.
get
(
"status"
))
.
add
(
"message"
,
errorAttributes
.
get
(
"message"
))
.
add
(
"timestamp"
,
errorAttributes
.
get
(
"timestamp"
))
.
add
(
"exception"
,
errorAttributes
.
get
(
"exception"
))
.
add
(
"errorCode"
,
errorAttributes
.
get
(
"errorCode"
))
.
add
(
"stackTrace"
,
errorAttributes
.
get
(
"stackTrace"
)).
toString
();
}
return
""
;
}
}
apollo-core/src/main/java/com/ctrip/apollo/core/exception/AbstractBaseException.java
View file @
8d641a9a
...
@@ -17,6 +17,10 @@ public abstract class AbstractBaseException extends RuntimeException{
...
@@ -17,6 +17,10 @@ public abstract class AbstractBaseException extends RuntimeException{
super
(
str
);
super
(
str
);
}
}
public
AbstractBaseException
(
String
str
,
Exception
e
){
super
(
str
,
e
);
}
public
AbstractBaseException
(
String
str
,
String
errorCode
){
public
AbstractBaseException
(
String
str
,
String
errorCode
){
super
(
str
);
super
(
str
);
this
.
setErrorCode
(
errorCode
);
this
.
setErrorCode
(
errorCode
);
...
...
apollo-core/src/main/java/com/ctrip/apollo/core/exception/ServiceException.java
View file @
8d641a9a
...
@@ -11,4 +11,7 @@ public class ServiceException extends AbstractBaseException {
...
@@ -11,4 +11,7 @@ public class ServiceException extends AbstractBaseException {
super
(
str
);
super
(
str
);
}
}
public
ServiceException
(
String
str
,
Exception
e
)
{
super
(
str
,
e
);
}
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/api/API.java
View file @
8d641a9a
...
@@ -7,7 +7,6 @@ import org.springframework.web.client.RestTemplate;
...
@@ -7,7 +7,6 @@ import org.springframework.web.client.RestTemplate;
import
com.ctrip.apollo.common.auth.RestTemplateFactory
;
import
com.ctrip.apollo.common.auth.RestTemplateFactory
;
import
com.ctrip.apollo.core.enums.Env
;
import
com.ctrip.apollo.core.enums.Env
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.ctrip.apollo.portal.service.ServiceLocator
;
import
com.ctrip.apollo.portal.service.ServiceLocator
;
public
class
API
{
public
class
API
{
...
@@ -26,14 +25,7 @@ public class API {
...
@@ -26,14 +25,7 @@ public class API {
}
}
public
String
getAdminServiceHost
(
Env
env
)
{
public
String
getAdminServiceHost
(
Env
env
)
{
// 本地测试用
// return "http://localhost:8090";
try
{
return
serviceLocator
.
getAdminService
(
env
).
getHomepageUrl
();
return
serviceLocator
.
getAdminService
(
env
).
getHomepageUrl
();
}
catch
(
ServiceException
e
)
{
e
.
printStackTrace
();
}
return
""
;
}
}
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/api/AdminServiceAPI.java
View file @
8d641a9a
...
@@ -8,40 +8,34 @@ import com.ctrip.apollo.core.dto.ItemChangeSets;
...
@@ -8,40 +8,34 @@ import com.ctrip.apollo.core.dto.ItemChangeSets;
import
com.ctrip.apollo.core.dto.ItemDTO
;
import
com.ctrip.apollo.core.dto.ItemDTO
;
import
com.ctrip.apollo.core.dto.NamespaceDTO
;
import
com.ctrip.apollo.core.dto.NamespaceDTO
;
import
com.ctrip.apollo.core.dto.ReleaseDTO
;
import
com.ctrip.apollo.core.dto.ReleaseDTO
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.ctrip.apollo.core.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.HttpClientErrorException
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
@Service
@Service
public
class
AdminServiceAPI
{
public
class
AdminServiceAPI
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AdminServiceAPI
.
class
);
@Service
@Service
public
static
class
AppAPI
extends
API
{
public
static
class
AppAPI
extends
API
{
public
static
String
APP_API
=
"/apps"
;
public
static
String
APP_API
=
"/apps"
;
public
List
<
AppDTO
>
getApps
(
Env
env
)
{
public
List
<
AppDTO
>
getApps
(
Env
env
)
{
return
Arrays
.
asList
(
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
APP_API
,
AppDTO
[].
class
));
AppDTO
[]
appDTOs
=
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
APP_API
,
AppDTO
[].
class
);
return
Arrays
.
asList
(
appDTOs
);
}
}
public
AppDTO
save
(
Env
env
,
AppDTO
app
)
{
public
AppDTO
save
(
Env
env
,
AppDTO
app
)
{
return
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
APP_API
,
app
,
AppDTO
.
class
).
getBody
();
return
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
APP_API
,
app
,
AppDTO
.
class
)
.
getBody
();
}
}
}
}
...
@@ -49,31 +43,19 @@ public class AdminServiceAPI {
...
@@ -49,31 +43,19 @@ public class AdminServiceAPI {
@Service
@Service
public
static
class
NamespaceAPI
extends
API
{
public
static
class
NamespaceAPI
extends
API
{
public
List
<
NamespaceDTO
>
findNamespaceByCluster
(
String
appId
,
Env
env
,
public
List
<
NamespaceDTO
>
findNamespaceByCluster
(
String
appId
,
Env
env
,
String
clusterName
)
{
String
clusterName
)
{
if
(
StringUtils
.
isContainEmpty
(
appId
,
clusterName
))
{
return
null
;
}
NamespaceDTO
[]
namespaceDTOs
=
restTemplate
.
getForObject
(
NamespaceDTO
[]
namespaceDTOs
=
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters/%s/namespaces"
,
appId
,
clusterName
),
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters/%s/namespaces"
,
appId
,
clusterName
),
NamespaceDTO
[].
class
);
NamespaceDTO
[].
class
);
if
(
namespaceDTOs
==
null
){
return
Collections
.
emptyList
();
}
else
{
return
Arrays
.
asList
(
namespaceDTOs
);
return
Arrays
.
asList
(
namespaceDTOs
);
}
}
}
public
NamespaceDTO
loadNamespace
(
String
appId
,
Env
env
,
public
NamespaceDTO
loadNamespace
(
String
appId
,
Env
env
,
String
clusterName
,
String
clusterName
,
String
namespaceName
)
{
String
namespaceName
)
{
if
(
StringUtils
.
isContainEmpty
(
appId
,
clusterName
,
namespaceName
))
{
return
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
return
null
;
+
String
.
format
(
"apps/%s/clusters/%s/namespaces/%s"
,
appId
,
clusterName
,
namespaceName
),
}
NamespaceDTO
.
class
);
return
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters/%s/namespaces/%s"
,
appId
,
clusterName
,
namespaceName
),
NamespaceDTO
.
class
);
}
}
}
}
...
@@ -81,93 +63,59 @@ public class AdminServiceAPI {
...
@@ -81,93 +63,59 @@ public class AdminServiceAPI {
public
static
class
ItemAPI
extends
API
{
public
static
class
ItemAPI
extends
API
{
public
List
<
ItemDTO
>
findItems
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
)
{
public
List
<
ItemDTO
>
findItems
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
)
{
if
(
StringUtils
.
isContainEmpty
(
appId
,
clusterName
,
namespace
))
{
ItemDTO
[]
itemDTOs
=
return
Collections
.
emptyList
();
restTemplate
}
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
.
format
(
ItemDTO
[]
itemDTOs
=
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
"apps/%s/clusters/%s/namespaces/%s/items"
,
appId
,
clusterName
,
namespace
),
.
format
(
"apps/%s/clusters/%s/namespaces/%s/items"
,
appId
,
clusterName
,
namespace
),
ItemDTO
[].
class
);
ItemDTO
[].
class
);
if
(
itemDTOs
==
null
)
{
return
Collections
.
emptyList
();
}
else
{
return
Arrays
.
asList
(
itemDTOs
);
return
Arrays
.
asList
(
itemDTOs
);
}
}
}
public
void
updateItems
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
,
public
void
updateItems
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
,
ItemChangeSets
changeSets
)
{
ItemChangeSets
changeSets
)
{
if
(
StringUtils
.
isContainEmpty
(
appId
,
clusterName
,
namespace
))
{
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
String
return
;
.
format
(
"apps/%s/clusters/%s/namespaces/%s/itemset"
,
appId
,
clusterName
,
namespace
),
}
changeSets
,
Void
.
class
);
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters/%s/namespaces/%s/itemset"
,
appId
,
clusterName
,
namespace
),
changeSets
,
Void
.
class
);
}
}
}
}
@Service
@Service
public
static
class
ClusterAPI
extends
API
{
public
static
class
ClusterAPI
extends
API
{
public
List
<
ClusterDTO
>
findClustersByApp
(
String
appId
,
Env
env
)
{
public
List
<
ClusterDTO
>
findClustersByApp
(
String
appId
,
Env
env
)
{
if
(
StringUtils
.
isContainEmpty
(
appId
))
{
ClusterDTO
[]
clusterDTOs
=
restTemplate
.
getForObject
(
return
null
;
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters"
,
appId
),
ClusterDTO
[].
class
);
}
ClusterDTO
[]
clusterDTOs
=
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters"
,
appId
),
ClusterDTO
[].
class
);
if
(
clusterDTOs
==
null
){
return
Collections
.
emptyList
();
}
else
{
return
Arrays
.
asList
(
clusterDTOs
);
return
Arrays
.
asList
(
clusterDTOs
);
}
}
}
}
}
@Service
@Service
public
static
class
ReleaseAPI
extends
API
{
public
static
class
ReleaseAPI
extends
API
{
public
ReleaseDTO
loadLatestRelease
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
)
{
public
ReleaseDTO
loadLatestRelease
(
String
appId
,
Env
env
,
String
clusterName
,
if
(
StringUtils
.
isContainEmpty
(
appId
,
clusterName
,
namespace
))
{
String
namespace
)
{
return
null
;
ReleaseDTO
releaseDTO
=
restTemplate
.
getForObject
(
}
getAdminServiceHost
(
env
)
+
String
.
format
(
try
{
"apps/%s/clusters/%s/namespaces/%s/releases/latest"
,
appId
,
clusterName
,
namespace
),
ReleaseDTO
releaseDTO
=
restTemplate
.
getForObject
(
getAdminServiceHost
(
env
)
+
String
ReleaseDTO
.
class
);
.
format
(
"apps/%s/clusters/%s/namespaces/%s/releases/latest"
,
appId
,
clusterName
,
namespace
),
ReleaseDTO
.
class
);
return
releaseDTO
;
return
releaseDTO
;
}
catch
(
HttpClientErrorException
e
)
{
logger
.
warn
(
" call [ReleaseAPI.loadLatestRelease] and return not fount exception.app id:{}, env:{}, clusterName:{}, namespace:{}"
,
appId
,
env
,
clusterName
,
namespace
);
return
null
;
}
}
}
public
ReleaseDTO
release
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
,
String
releaseBy
,
public
ReleaseDTO
release
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespace
,
String
comment
)
{
String
releaseBy
,
String
comment
)
{
MultiValueMap
<
String
,
String
>
parameters
=
new
LinkedMultiValueMap
<
String
,
String
>();
MultiValueMap
<
String
,
String
>
parameters
=
new
LinkedMultiValueMap
<
String
,
String
>();
parameters
.
add
(
"name"
,
releaseBy
);
parameters
.
add
(
"name"
,
releaseBy
);
parameters
.
add
(
"comment"
,
comment
);
parameters
.
add
(
"comment"
,
comment
);
HttpEntity
<
MultiValueMap
<
String
,
String
>>
entity
=
HttpEntity
<
MultiValueMap
<
String
,
String
>>
entity
=
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
parameters
,
null
);
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
parameters
,
null
);
ResponseEntity
<
ReleaseDTO
>
response
=
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
String
.
ResponseEntity
<
ReleaseDTO
>
response
=
format
(
"apps/%s/clusters/%s/namespaces/%s/releases"
,
appId
,
clusterName
,
namespace
),
restTemplate
.
postForEntity
(
getAdminServiceHost
(
env
)
+
String
.
format
(
"apps/%s/clusters/%s/namespaces/%s/releases"
,
appId
,
clusterName
,
namespace
),
entity
,
ReleaseDTO
.
class
);
entity
,
ReleaseDTO
.
class
);
if
(
response
!=
null
&&
response
.
getStatusCode
()
==
HttpStatus
.
OK
){
return
response
.
getBody
();
return
response
.
getBody
();
}
else
{
logger
.
error
(
"createRelease fail.id:{}, env:{}, clusterName:{}, namespace:{},releaseBy{}"
,
appId
,
env
,
clusterName
,
namespace
,
releaseBy
);
throw
new
ServiceException
(
" call create createRelease api error."
);
}
}
}
}
}
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/AppService.java
View file @
8d641a9a
...
@@ -6,10 +6,11 @@ import org.slf4j.Logger;
...
@@ -6,10 +6,11 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
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.web.client.HttpStatusCodeException
;
import
com.ctrip.apollo.co
re.enums.Env
;
import
com.ctrip.apollo.co
mmon.utils.ExceptionUtils
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.ctrip.apollo.core.e
xception.ServiceException
;
import
com.ctrip.apollo.core.e
nums.Env
;
import
com.ctrip.apollo.portal.PortalSettings
;
import
com.ctrip.apollo.portal.PortalSettings
;
import
com.ctrip.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.apollo.portal.entity.ClusterNavTree
;
import
com.ctrip.apollo.portal.entity.ClusterNavTree
;
...
@@ -49,9 +50,9 @@ public class AppService {
...
@@ -49,9 +50,9 @@ public class AppService {
for
(
Env
env
:
envs
)
{
for
(
Env
env
:
envs
)
{
try
{
try
{
appAPI
.
save
(
env
,
app
);
appAPI
.
save
(
env
,
app
);
}
catch
(
Exception
e
)
{
}
catch
(
HttpStatusCode
Exception
e
)
{
logger
.
error
(
"oops! save app error. app id:{}"
,
app
.
getAppId
(),
e
);
logger
.
error
(
ExceptionUtils
.
toString
(
e
)
);
throw
new
ServiceException
(
"call service error."
)
;
throw
e
;
}
}
}
}
}
}
...
...
apollo-portal/src/test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
View file @
8d641a9a
package
com
.
ctrip
.
apollo
.
portal
;
package
com
.
ctrip
.
apollo
.
portal
;
import
javax.annotation.PostConstruct
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.boot.test.WebIntegrationTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.web.client.DefaultResponseErrorHandler
;
import
org.springframework.web.client.RestTemplate
;
import
com.ctrip.apollo.PortalApplication
;
import
com.ctrip.apollo.PortalApplication
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
PortalApplication
.
class
)
@SpringApplicationConfiguration
(
classes
=
PortalApplication
.
class
)
@WebIntegrationTest
(
randomPort
=
true
)
public
abstract
class
AbstractPortalTest
{
public
abstract
class
AbstractPortalTest
{
RestTemplate
restTemplate
=
new
TestRestTemplate
(
"apollo"
,
""
);
@PostConstruct
private
void
postConstruct
()
{
restTemplate
.
setErrorHandler
(
new
DefaultResponseErrorHandler
());
}
@Value
(
"${local.server.port}"
)
int
port
;
}
}
apollo-portal/src/test/java/com/ctrip/apollo/portal/AppServiceTest.java
View file @
8d641a9a
...
@@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals;
...
@@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
AppServiceTest
extends
AbstractPortalTest
{
public
class
AppServiceTest
{
@Mock
@Mock
private
PortalSettings
settings
;
private
PortalSettings
settings
;
...
...
apollo-portal/src/test/java/com/ctrip/apollo/portal/ConfigServiceTest.java
View file @
8d641a9a
...
@@ -26,7 +26,7 @@ import static org.junit.Assert.assertEquals;
...
@@ -26,7 +26,7 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ConfigServiceTest
extends
AbstractPortalTest
{
public
class
ConfigServiceTest
{
@Mock
@Mock
private
AdminServiceAPI
.
NamespaceAPI
namespaceAPI
;
private
AdminServiceAPI
.
NamespaceAPI
namespaceAPI
;
...
...
apollo-portal/src/test/java/com/ctrip/apollo/portal/ServiceExceptionTest.java
0 → 100644
View file @
8d641a9a
package
com
.
ctrip
.
apollo
.
portal
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
java.nio.charset.Charset
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.web.client.HttpServerErrorException
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.ctrip.apollo.common.utils.ExceptionUtils
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.ctrip.apollo.core.enums.Env
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.ctrip.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.apollo.portal.service.AppService
;
import
com.google.gson.Gson
;
public
class
ServiceExceptionTest
extends
AbstractPortalTest
{
@Autowired
private
AppService
appService
;
@Mock
private
AdminServiceAPI
.
AppAPI
appAPI
;
@Before
public
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
ReflectionTestUtils
.
setField
(
appService
,
"appAPI"
,
appAPI
);
}
private
String
getBaseAppUrl
()
{
return
"http://localhost:"
+
port
+
"/apps/"
;
}
@Test
public
void
testAdminServiceException
()
{
Map
<
String
,
Object
>
errorAttributes
=
new
LinkedHashMap
<>();
errorAttributes
.
put
(
"status"
,
500
);
errorAttributes
.
put
(
"message"
,
"admin server error"
);
errorAttributes
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
errorAttributes
.
put
(
"exception"
,
ServiceException
.
class
.
getName
());
errorAttributes
.
put
(
"errorCode"
,
"8848"
);
HttpStatusCodeException
adminException
=
new
HttpServerErrorException
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
"admin server error"
,
new
Gson
().
toJson
(
errorAttributes
).
getBytes
(),
Charset
.
defaultCharset
());
when
(
appAPI
.
save
(
any
(
Env
.
class
),
any
(
AppDTO
.
class
))).
thenThrow
(
adminException
);
AppDTO
dto
=
generateSampleDTOData
();
try
{
restTemplate
.
postForEntity
(
getBaseAppUrl
(),
dto
,
AppDTO
.
class
);
}
catch
(
HttpStatusCodeException
e
)
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
String
>
attr
=
new
Gson
().
fromJson
(
e
.
getResponseBodyAsString
(),
Map
.
class
);
System
.
out
.
println
(
ExceptionUtils
.
toString
(
e
));
Assert
.
assertEquals
(
"admin server error"
,
attr
.
get
(
"message"
));
Assert
.
assertEquals
(
"8848"
,
attr
.
get
(
"errorCode"
));
}
}
private
AppDTO
generateSampleDTOData
()
{
AppDTO
dto
=
new
AppDTO
();
dto
.
setAppId
(
"someAppId"
);
dto
.
setName
(
"someName"
);
dto
.
setOwnerName
(
"someOwner"
);
dto
.
setOwnerEmail
(
"someOwner@ctrip.com"
);
return
dto
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment