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
80bd483b
Commit
80bd483b
authored
Mar 13, 2016
by
Yiming Liu
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8 from ctripcorp/portal_update
Update portal sample test case
parents
f03318c7
a70b7d2b
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
216 additions
and
38 deletions
+216
-38
AppController.java
...ava/com/ctrip/apollo/portal/controller/AppController.java
+20
-8
GlobalDefaultExceptionHandler.java
...ollo/portal/controller/GlobalDefaultExceptionHandler.java
+66
-0
AppRepository.java
...in/java/com/ctrip/apollo/portal/domain/AppRepository.java
+0
-10
App.java
...tal/src/main/java/com/ctrip/apollo/portal/entity/App.java
+1
-1
NotFoundException.java
.../com/ctrip/apollo/portal/exception/NotFoundException.java
+8
-0
AppRepository.java
...ava/com/ctrip/apollo/portal/repository/AppRepository.java
+12
-0
AppService.java
...main/java/com/ctrip/apollo/portal/service/AppService.java
+6
-2
application.properties
apollo-portal/src/main/resources/application.properties
+1
-1
AbstractPortalTest.java
...test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
+11
-0
AppControllerTest.java
...com/ctrip/apollo/portal/controller/AppControllerTest.java
+76
-0
AppRepositoryTest.java
...com/ctrip/apollo/portal/repository/AppRepositoryTest.java
+4
-12
application.properties
apollo-portal/src/test/resources/application.properties
+4
-2
pom.xml
pom.xml
+7
-2
No files found.
apollo-portal/src/main/java/com/ctrip/apollo/portal/
web
/AppController.java
→
apollo-portal/src/main/java/com/ctrip/apollo/portal/
controller
/AppController.java
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
web
;
package
com
.
ctrip
.
apollo
.
portal
.
controller
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.web.PageableDefault
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.ctrip.apollo.portal.domain.App
;
import
com.ctrip.apollo.portal.entity.App
;
import
com.ctrip.apollo.portal.exception.NotFoundException
;
import
com.ctrip.apollo.portal.service.AppService
;
@RestController
...
...
@@ -19,18 +22,27 @@ public class AppController {
@Autowired
private
AppService
appService
;
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
POST
)
public
App
create
(
App
app
)
{
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
POST
,
consumes
=
{
"application/json"
}
)
public
App
create
(
@RequestBody
App
app
)
{
return
appService
.
save
(
app
);
}
@RequestMapping
(
"/{appid}"
)
public
App
detail
(
@PathVariable
String
appId
)
{
return
appService
.
detail
(
appId
);
App
app
=
appService
.
detail
(
appId
);
if
(
app
==
null
)
{
throw
new
NotFoundException
();
}
return
app
;
}
@RequestMapping
(
""
)
public
Page
<
App
>
list
(
@PageableDefault
(
size
=
50
)
Pageable
pageable
)
{
return
appService
.
list
(
pageable
);
public
List
<
App
>
list
(
Pageable
pageable
)
{
Page
<
App
>
page
=
appService
.
list
(
pageable
);
if
(
pageable
.
getPageNumber
()
>
page
.
getTotalPages
())
{
throw
new
NotFoundException
();
}
return
page
.
getContent
();
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/GlobalDefaultExceptionHandler.java
0 → 100644
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
controller
;
import
static
org
.
springframework
.
http
.
HttpStatus
.
BAD_REQUEST
;
import
static
org
.
springframework
.
http
.
HttpStatus
.
INTERNAL_SERVER_ERROR
;
import
static
org
.
springframework
.
http
.
HttpStatus
.
NOT_FOUND
;
import
static
org
.
springframework
.
http
.
MediaType
.
APPLICATION_JSON
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.HttpMediaTypeException
;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
com.ctrip.apollo.portal.exception.NotFoundException
;
@ControllerAdvice
public
class
GlobalDefaultExceptionHandler
{
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Exception
ex
)
{
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
HttpStatus
status
,
Throwable
ex
)
{
return
handleError
(
request
,
status
,
ex
,
ex
.
getMessage
());
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
HttpStatus
status
,
Throwable
ex
,
String
message
)
{
ex
=
resolveError
(
ex
);
Map
<
String
,
Object
>
errorAttributes
=
new
LinkedHashMap
<>();
errorAttributes
.
put
(
"status"
,
status
.
value
());
errorAttributes
.
put
(
"message"
,
message
);
errorAttributes
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
errorAttributes
.
put
(
"exception"
,
resolveError
(
ex
).
getClass
().
getName
());
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
APPLICATION_JSON
);
return
new
ResponseEntity
<>(
errorAttributes
,
headers
,
status
);
}
@ExceptionHandler
({
HttpRequestMethodNotSupportedException
.
class
,
HttpMediaTypeException
.
class
})
public
ResponseEntity
<
Map
<
String
,
Object
>>
methodNotSupportedException
(
HttpServletRequest
request
,
ServletException
ex
)
{
return
handleError
(
request
,
BAD_REQUEST
,
ex
);
}
@ExceptionHandler
(
NotFoundException
.
class
)
@ResponseStatus
(
value
=
NOT_FOUND
)
public
void
notFound
(
HttpServletRequest
req
,
NotFoundException
ex
)
{}
private
Throwable
resolveError
(
Throwable
ex
)
{
while
(
ex
instanceof
ServletException
&&
ex
.
getCause
()
!=
null
)
{
ex
=
((
ServletException
)
ex
).
getCause
();
}
return
ex
;
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/domain/AppRepository.java
deleted
100644 → 0
View file @
f03318c7
package
com
.
ctrip
.
apollo
.
portal
.
domain
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.repository.CrudRepository
;
public
interface
AppRepository
extends
CrudRepository
<
App
,
String
>
{
Page
<
App
>
findAll
(
Pageable
pageable
);
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/
domain
/App.java
→
apollo-portal/src/main/java/com/ctrip/apollo/portal/
entity
/App.java
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
domain
;
package
com
.
ctrip
.
apollo
.
portal
.
entity
;
import
java.io.Serializable
;
import
java.util.Date
;
...
...
apollo-portal/src/main/java/com/ctrip/apollo/portal/exception/NotFoundException.java
0 → 100644
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
exception
;
public
class
NotFoundException
extends
RuntimeException
{
/**
*
*/
private
static
final
long
serialVersionUID
=
7611357629749481796L
;
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/AppRepository.java
0 → 100644
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
repository
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.repository.PagingAndSortingRepository
;
import
com.ctrip.apollo.portal.entity.App
;
public
interface
AppRepository
extends
PagingAndSortingRepository
<
App
,
String
>
{
Page
<
App
>
findAll
(
Pageable
pageable
);
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/AppService.java
View file @
80bd483b
...
...
@@ -7,8 +7,8 @@ import org.springframework.data.domain.Page;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
com.ctrip.apollo.portal.
domain
.App
;
import
com.ctrip.apollo.portal.
domain
.AppRepository
;
import
com.ctrip.apollo.portal.
entity
.App
;
import
com.ctrip.apollo.portal.
repository
.AppRepository
;
@Service
public
class
AppService
{
...
...
@@ -24,6 +24,10 @@ public class AppService {
return
appRepository
.
findAll
(
pageable
);
}
public
Iterable
<
App
>
list
()
{
return
appRepository
.
findAll
();
}
public
App
save
(
App
app
)
{
app
.
setCreateTimestamp
(
new
Date
());
return
appRepository
.
save
(
app
);
...
...
apollo-portal/src/main/resources/application.properties
View file @
80bd483b
spring.datasource.url
=
jdbc:h2:file:~/fxapolloportaldb
;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url
=
jdbc:h2:file:~/fxapolloportaldb
spring.datasource.username
=
sa
spring.datasource.password
=
sa
apollo-portal/src/test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
0 → 100644
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
PortalApplicationTestConfiguration
.
class
)
public
abstract
class
AbstractPortalTest
{
}
apollo-portal/src/test/java/com/ctrip/apollo/portal/controller/AppControllerTest.java
0 → 100644
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
controller
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.boot.test.WebIntegrationTest
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.client.RestTemplate
;
import
com.ctrip.apollo.portal.AbstractPortalTest
;
import
com.ctrip.apollo.portal.entity.App
;
import
com.ctrip.apollo.portal.repository.AppRepository
;
@WebIntegrationTest
public
class
AppControllerTest
extends
AbstractPortalTest
{
RestTemplate
restTemplate
=
new
TestRestTemplate
();
@Autowired
AppRepository
appRepository
;
@Test
public
void
testCreate
()
throws
URISyntaxException
{
App
newApp
=
new
App
();
newApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
newApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
newApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
URI
uri
=
new
URI
(
"http://localhost:8080/apps"
);
App
createdApp
=
restTemplate
.
postForObject
(
uri
,
newApp
,
App
.
class
);
Assert
.
assertEquals
(
newApp
.
getId
(),
createdApp
.
getId
());
Assert
.
assertNull
(
newApp
.
getCreateTimestamp
());
Assert
.
assertNotNull
(
createdApp
.
getCreateTimestamp
());
App
foundApp
=
appRepository
.
findOne
(
newApp
.
getId
());
Assert
.
assertEquals
(
newApp
.
getId
(),
foundApp
.
getId
());
}
@Test
public
void
testList
()
throws
URISyntaxException
{
App
newApp
=
new
App
();
newApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
newApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
newApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
appRepository
.
save
(
newApp
);
URI
uri
=
new
URI
(
"http://localhost:8080/apps"
);
App
[]
apps
=
restTemplate
.
getForObject
(
uri
,
App
[].
class
);
Assert
.
assertEquals
(
1
,
apps
.
length
);
Assert
.
assertEquals
(
newApp
.
getId
(),
apps
[
0
].
getId
());
}
@Test
public
void
testListOutOfRange
()
throws
URISyntaxException
{
App
newApp
=
new
App
();
newApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
newApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
newApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
appRepository
.
save
(
newApp
);
URI
uri
=
new
URI
(
"http://localhost:8080/apps?page=2"
);
ResponseEntity
<
App
[]>
entity
=
restTemplate
.
getForEntity
(
uri
,
App
[].
class
);
Assert
.
assertEquals
(
HttpStatus
.
NOT_FOUND
,
entity
.
getStatusCode
());
Assert
.
assertNull
(
entity
.
getBody
());
}
}
apollo-portal/src/test/java/com/ctrip/apollo/portal/repository/AppRepositoryTest.java
View file @
80bd483b
package
com
.
ctrip
.
apollo
.
portal
.
repository
;
import
java.util.Date
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.ctrip.apollo.portal.
PortalApplicationTestConfiguration
;
import
com.ctrip.apollo.portal.
domain
.App
;
import
com.ctrip.apollo.portal.domain.AppRepository
;
import
com.ctrip.apollo.portal.
AbstractPortalTest
;
import
com.ctrip.apollo.portal.
entity
.App
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
PortalApplicationTestConfiguration
.
class
)
public
class
AppRepositoryTest
{
public
class
AppRepositoryTest
extends
AbstractPortalTest
{
@Autowired
AppRepository
repository
;
...
...
@@ -28,7 +21,6 @@ public class AppRepositoryTest {
ramdomApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
ramdomApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
ramdomApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
ramdomApp
.
setCreateTimestamp
(
new
Date
());
repository
.
save
(
ramdomApp
);
Assert
.
assertEquals
(
1
,
repository
.
count
());
...
...
apollo-portal/src/test/resources/application.properties
View file @
80bd483b
spring.datasource.url
=
jdbc:h2:
file:~/fxapolloportaldb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url
=
jdbc:h2:
mem:fxapolloportaldb
spring.datasource.username
=
sa
spring.datasource.password
=
sa
spring.datasource.password
=
spring.jpa.show-sql: true
\ No newline at end of file
pom.xml
View file @
80bd483b
...
...
@@ -121,8 +121,8 @@
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.5.1
</version>
<configuration>
<source>
${java.
source
}
</source>
<target>
${java.
target
}
</target>
<source>
${java.
version
}
</source>
<target>
${java.
version
}
</target>
</configuration>
</plugin>
<plugin>
...
...
@@ -151,6 +151,11 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>
maven-source-plugin
</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
...
...
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