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
aa44e0a0
Commit
aa44e0a0
authored
Apr 20, 2016
by
Yiming Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more testcase for exception
parent
93d1ce67
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
189 additions
and
4 deletions
+189
-4
AbstractControllerTest.java
...pollo/adminservice/controller/AbstractControllerTest.java
+8
-0
AppControllerTest.java
...rip/apollo/adminservice/controller/AppControllerTest.java
+2
-2
ControllerExceptionTest.java
...ollo/adminservice/controller/ControllerExceptionTest.java
+100
-0
ControllerIntegrationExceptionTest.java
...ervice/controller/ControllerIntegrationExceptionTest.java
+75
-0
application.yml
apollo-adminservice/src/test/resources/application.yml
+1
-1
bootstrap.yml
apollo-adminservice/src/test/resources/bootstrap.yml
+2
-1
GlobalDefaultExceptionHandler.java
...ollo/common/controller/GlobalDefaultExceptionHandler.java
+1
-0
No files found.
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AbstractControllerTest.java
View file @
aa44e0a0
package
com
.
ctrip
.
apollo
.
adminservice
.
controller
;
package
com
.
ctrip
.
apollo
.
adminservice
.
controller
;
import
javax.annotation.PostConstruct
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Value
;
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.TestRestTemplate
;
import
org.springframework.boot.test.WebIntegrationTest
;
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
org.springframework.web.client.RestTemplate
;
import
com.ctrip.apollo.AdminServiceTestConfiguration
;
import
com.ctrip.apollo.AdminServiceTestConfiguration
;
...
@@ -17,6 +20,11 @@ public abstract class AbstractControllerTest {
...
@@ -17,6 +20,11 @@ public abstract class AbstractControllerTest {
RestTemplate
restTemplate
=
new
TestRestTemplate
(
"user"
,
""
);
RestTemplate
restTemplate
=
new
TestRestTemplate
(
"user"
,
""
);
@PostConstruct
private
void
postConstruct
()
{
restTemplate
.
setErrorHandler
(
new
DefaultResponseErrorHandler
());
}
@Value
(
"${local.server.port}"
)
@Value
(
"${local.server.port}"
)
int
port
;
int
port
;
}
}
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
View file @
aa44e0a0
...
@@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus;
...
@@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus;
import
org.springframework.http.ResponseEntity
;
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.web.client.HttpClientErrorException
;
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
;
...
@@ -78,12 +79,11 @@ public class AppControllerTest extends AbstractControllerTest {
...
@@ -78,12 +79,11 @@ public class AppControllerTest extends AbstractControllerTest {
Assert
.
assertEquals
(
dto
.
getName
(),
result
.
getName
());
Assert
.
assertEquals
(
dto
.
getName
(),
result
.
getName
());
}
}
@Test
@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
=
ResponseEntity
<
AppDTO
>
result
=
restTemplate
.
getForEntity
(
getBaseAppUrl
()
+
"notExists"
,
AppDTO
.
class
);
restTemplate
.
getForEntity
(
getBaseAppUrl
()
+
"notExists"
,
AppDTO
.
class
);
Assert
.
assertEquals
(
HttpStatus
.
NOT_FOUND
,
result
.
getStatusCode
());
}
}
@Test
@Test
...
...
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerExceptionTest.java
0 → 100644
View file @
aa44e0a0
package
com
.
ctrip
.
apollo
.
adminservice
.
controller
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.userdetails.User
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
com.ctrip.apollo.biz.entity.App
;
import
com.ctrip.apollo.biz.service.AdminService
;
import
com.ctrip.apollo.biz.service.AppService
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ControllerExceptionTest
{
private
AppController
appController
;
@Mock
private
AppService
appService
;
@Mock
private
AdminService
adminService
;
@Before
public
void
setUp
()
{
appController
=
new
AppController
();
ReflectionTestUtils
.
setField
(
appController
,
"appService"
,
appService
);
ReflectionTestUtils
.
setField
(
appController
,
"adminService"
,
adminService
);
}
@Test
(
expected
=
NotFoundException
.
class
)
public
void
testFindNotExists
()
{
when
(
appService
.
findOne
(
any
(
String
.
class
))).
thenReturn
(
null
);
appController
.
get
(
"unexist"
);
}
@Test
(
expected
=
NotFoundException
.
class
)
public
void
testDeleteNotExists
()
{
when
(
appService
.
findOne
(
any
(
String
.
class
))).
thenReturn
(
null
);
appController
.
delete
(
"unexist"
,
null
);
}
@Test
public
void
testFindEmpty
()
{
when
(
appService
.
findAll
(
any
(
Pageable
.
class
))).
thenReturn
(
new
ArrayList
<
App
>());
Pageable
pageable
=
new
PageRequest
(
0
,
10
);
List
<
AppDTO
>
appDTOs
=
appController
.
find
(
null
,
pageable
);
Assert
.
assertNotNull
(
appDTOs
);
Assert
.
assertEquals
(
0
,
appDTOs
.
size
());
appDTOs
=
appController
.
find
(
""
,
pageable
);
Assert
.
assertNotNull
(
appDTOs
);
Assert
.
assertEquals
(
0
,
appDTOs
.
size
());
}
@Test
public
void
testFindByName
()
{
Pageable
pageable
=
new
PageRequest
(
0
,
10
);
List
<
AppDTO
>
appDTOs
=
appController
.
find
(
"unexist"
,
pageable
);
Assert
.
assertNotNull
(
appDTOs
);
Assert
.
assertEquals
(
0
,
appDTOs
.
size
());
}
@Test
(
expected
=
ServiceException
.
class
)
public
void
createFailed
()
{
AppDTO
dto
=
generateSampleDTOData
();
when
(
appService
.
findOne
(
any
(
String
.
class
))).
thenReturn
(
null
);
when
(
adminService
.
createNewApp
(
any
(
App
.
class
)))
.
thenThrow
(
new
ServiceException
(
"create app failed"
));
UserDetails
user
=
new
User
(
"user"
,
""
,
new
ArrayList
<
GrantedAuthority
>());
appController
.
createOrUpdate
(
dto
,
user
);
}
private
AppDTO
generateSampleDTOData
()
{
AppDTO
dto
=
new
AppDTO
();
dto
.
setAppId
(
"someAppId"
);
dto
.
setName
(
"someName"
);
dto
.
setOwnerName
(
"someOwner"
);
dto
.
setOwnerEmail
(
"someOwner@ctrip.com"
);
return
dto
;
}
}
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerIntegrationExceptionTest.java
0 → 100644
View file @
aa44e0a0
package
com
.
ctrip
.
apollo
.
adminservice
.
controller
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
when
;
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.ResponseEntity
;
import
org.springframework.test.context.jdbc.Sql
;
import
org.springframework.test.context.jdbc.Sql.ExecutionPhase
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.ctrip.apollo.biz.entity.App
;
import
com.ctrip.apollo.biz.service.AdminService
;
import
com.ctrip.apollo.biz.service.AppService
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.google.gson.Gson
;
public
class
ControllerIntegrationExceptionTest
extends
AbstractControllerTest
{
@Autowired
AppController
appController
;
@Mock
AdminService
adminService
;
@Autowired
AppService
appService
;
Gson
gson
=
new
Gson
();
@Before
public
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
ReflectionTestUtils
.
setField
(
appController
,
"adminService"
,
adminService
);
}
private
String
getBaseAppUrl
()
{
return
"http://localhost:"
+
port
+
"/apps/"
;
}
@Test
@Sql
(
scripts
=
"/controller/cleanup.sql"
,
executionPhase
=
ExecutionPhase
.
AFTER_TEST_METHOD
)
public
void
testCreateFailed
()
{
AppDTO
dto
=
generateSampleDTOData
();
when
(
adminService
.
createNewApp
(
any
(
App
.
class
))).
thenThrow
(
new
RuntimeException
(
"save failed"
));
try
{
restTemplate
.
postForEntity
(
getBaseAppUrl
(),
dto
,
AppDTO
.
class
);
}
catch
(
HttpStatusCodeException
e
)
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
String
>
attr
=
gson
.
fromJson
(
e
.
getResponseBodyAsString
(),
Map
.
class
);
Assert
.
assertEquals
(
"save failed"
,
attr
.
get
(
"message"
));
}
App
savedApp
=
appService
.
findOne
(
dto
.
getAppId
());
Assert
.
assertNull
(
savedApp
);
}
private
AppDTO
generateSampleDTOData
()
{
AppDTO
dto
=
new
AppDTO
();
dto
.
setAppId
(
"someAppId"
);
dto
.
setName
(
"someName"
);
dto
.
setOwnerName
(
"someOwner"
);
dto
.
setOwnerEmail
(
"someOwner@ctrip.com"
);
return
dto
;
}
}
apollo-adminservice/src/test/resources/application.yml
View file @
aa44e0a0
...
@@ -3,7 +3,7 @@ spring:
...
@@ -3,7 +3,7 @@ spring:
name
:
apollo-adminservice
name
:
apollo-adminservice
server
:
server
:
port
:
${port:80
8
0}
port
:
${port:80
9
0}
logging
:
logging
:
level
:
level
:
...
...
apollo-adminservice/src/test/resources/bootstrap.yml
View file @
aa44e0a0
...
@@ -3,6 +3,6 @@ eureka:
...
@@ -3,6 +3,6 @@ eureka:
hostname
:
${hostname:localhost}
hostname
:
${hostname:localhost}
client
:
client
:
serviceUrl
:
serviceUrl
:
defaultZone
:
http://${eureka.instance.hostname}:80
8
0/eureka/
defaultZone
:
http://${eureka.instance.hostname}:80
9
0/eureka/
healthcheck
:
healthcheck
:
enabled
:
true
enabled
:
true
\ No newline at end of file
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
View file @
aa44e0a0
...
@@ -46,6 +46,7 @@ public class GlobalDefaultExceptionHandler {
...
@@ -46,6 +46,7 @@ public class GlobalDefaultExceptionHandler {
errorAttributes
.
put
(
"timestamp"
,
errorAttributes
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
errorAttributes
.
put
(
"exception"
,
resolveError
(
ex
).
getClass
().
getName
());
errorAttributes
.
put
(
"exception"
,
resolveError
(
ex
).
getClass
().
getName
());
errorAttributes
.
put
(
"stackTrace"
,
ex
.
getStackTrace
());
if
(
ex
instanceof
AbstractBaseException
)
{
if
(
ex
instanceof
AbstractBaseException
)
{
errorAttributes
.
put
(
"errorCode"
,
((
AbstractBaseException
)
ex
).
getErrorCode
());
errorAttributes
.
put
(
"errorCode"
,
((
AbstractBaseException
)
ex
).
getErrorCode
());
}
}
...
...
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