Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
80bd483b
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,发现更多精彩内容 >>
提交
80bd483b
编写于
3月 13, 2016
作者:
Y
Yiming Liu
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8 from ctripcorp/portal_update
Update portal sample test case
上级
f03318c7
a70b7d2b
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
215 addition
and
28 deletion
+215
-28
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/AppController.java
...ava/com/ctrip/apollo/portal/controller/AppController.java
+20
-8
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/GlobalDefaultExceptionHandler.java
...ollo/portal/controller/GlobalDefaultExceptionHandler.java
+66
-0
apollo-portal/src/main/java/com/ctrip/apollo/portal/entity/App.java
...tal/src/main/java/com/ctrip/apollo/portal/entity/App.java
+1
-1
apollo-portal/src/main/java/com/ctrip/apollo/portal/exception/NotFoundException.java
.../com/ctrip/apollo/portal/exception/NotFoundException.java
+8
-0
apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/AppRepository.java
...ava/com/ctrip/apollo/portal/repository/AppRepository.java
+12
-0
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/AppService.java
...main/java/com/ctrip/apollo/portal/service/AppService.java
+6
-2
apollo-portal/src/main/resources/application.properties
apollo-portal/src/main/resources/application.properties
+1
-1
apollo-portal/src/test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
...test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
+11
-0
apollo-portal/src/test/java/com/ctrip/apollo/portal/controller/AppControllerTest.java
...com/ctrip/apollo/portal/controller/AppControllerTest.java
+76
-0
apollo-portal/src/test/java/com/ctrip/apollo/portal/repository/AppRepositoryTest.java
...com/ctrip/apollo/portal/repository/AppRepositoryTest.java
+4
-12
apollo-portal/src/test/resources/application.properties
apollo-portal/src/test/resources/application.properties
+3
-2
pom.xml
pom.xml
+7
-2
未找到文件。
apollo-portal/src/main/java/com/ctrip/apollo/portal/
web
/AppController.java
→
apollo-portal/src/main/java/com/ctrip/apollo/portal/
controller
/AppController.java
浏览文件 @
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.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
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.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
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
;
import
com.ctrip.apollo.portal.service.AppService
;
@RestController
@RestController
...
@@ -19,18 +22,27 @@ public class AppController {
...
@@ -19,18 +22,27 @@ public class AppController {
@Autowired
@Autowired
private
AppService
appService
;
private
AppService
appService
;
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
POST
,
consumes
=
{
"application/json"
}
)
public
App
create
(
App
app
)
{
public
App
create
(
@RequestBody
App
app
)
{
return
appService
.
save
(
app
);
return
appService
.
save
(
app
);
}
}
@RequestMapping
(
"/{appid}"
)
@RequestMapping
(
"/{appid}"
)
public
App
detail
(
@PathVariable
String
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
(
""
)
@RequestMapping
(
""
)
public
Page
<
App
>
list
(
@PageableDefault
(
size
=
50
)
Pageable
pageable
)
{
public
List
<
App
>
list
(
Pageable
pageable
)
{
return
appService
.
list
(
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
浏览文件 @
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
/App.java
→
apollo-portal/src/main/java/com/ctrip/apollo/portal/
entity
/App.java
浏览文件 @
80bd483b
package
com.ctrip.apollo.portal.
domain
;
package
com.ctrip.apollo.portal.
entity
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.util.Date
;
import
java.util.Date
;
...
...
apollo-portal/src/main/java/com/ctrip/apollo/portal/exception/NotFoundException.java
0 → 100644
浏览文件 @
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/
domain
/AppRepository.java
→
apollo-portal/src/main/java/com/ctrip/apollo/portal/
repository
/AppRepository.java
浏览文件 @
80bd483b
package
com.ctrip.apollo.portal.
domain
;
package
com.ctrip.apollo.portal.
repository
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.repository.
Crud
Repository
;
import
org.springframework.data.repository.
PagingAndSorting
Repository
;
public
interface
AppRepository
extends
CrudRepository
<
App
,
String
>
{
import
com.ctrip.apollo.portal.entity.App
;
public
interface
AppRepository
extends
PagingAndSortingRepository
<
App
,
String
>
{
Page
<
App
>
findAll
(
Pageable
pageable
);
Page
<
App
>
findAll
(
Pageable
pageable
);
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/AppService.java
浏览文件 @
80bd483b
...
@@ -7,8 +7,8 @@ import org.springframework.data.domain.Page;
...
@@ -7,8 +7,8 @@ import org.springframework.data.domain.Page;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
com.ctrip.apollo.portal.
domain
.App
;
import
com.ctrip.apollo.portal.
entity
.App
;
import
com.ctrip.apollo.portal.
domain
.AppRepository
;
import
com.ctrip.apollo.portal.
repository
.AppRepository
;
@Service
@Service
public
class
AppService
{
public
class
AppService
{
...
@@ -24,6 +24,10 @@ public class AppService {
...
@@ -24,6 +24,10 @@ public class AppService {
return
appRepository
.
findAll
(
pageable
);
return
appRepository
.
findAll
(
pageable
);
}
}
public
Iterable
<
App
>
list
()
{
return
appRepository
.
findAll
();
}
public
App
save
(
App
app
)
{
public
App
save
(
App
app
)
{
app
.
setCreateTimestamp
(
new
Date
());
app
.
setCreateTimestamp
(
new
Date
());
return
appRepository
.
save
(
app
);
return
appRepository
.
save
(
app
);
...
...
apollo-portal/src/main/resources/application.properties
浏览文件 @
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.username
=
sa
spring.datasource.password
=
sa
spring.datasource.password
=
sa
apollo-portal/src/test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
0 → 100644
浏览文件 @
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
浏览文件 @
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
浏览文件 @
80bd483b
package
com.ctrip.apollo.portal.repository
;
package
com.ctrip.apollo.portal.repository
;
import
java.util.Date
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.
AbstractPortalTest
;
import
com.ctrip.apollo.portal.
domain
.App
;
import
com.ctrip.apollo.portal.
entity
.App
;
import
com.ctrip.apollo.portal.domain.AppRepository
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
public
class
AppRepositoryTest
extends
AbstractPortalTest
{
@SpringApplicationConfiguration
(
classes
=
PortalApplicationTestConfiguration
.
class
)
public
class
AppRepositoryTest
{
@Autowired
@Autowired
AppRepository
repository
;
AppRepository
repository
;
...
@@ -28,7 +21,6 @@ public class AppRepositoryTest {
...
@@ -28,7 +21,6 @@ public class AppRepositoryTest {
ramdomApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
ramdomApp
.
setId
(
String
.
valueOf
(
System
.
currentTimeMillis
()));
ramdomApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
ramdomApp
.
setName
(
"new app "
+
System
.
currentTimeMillis
());
ramdomApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
ramdomApp
.
setOwner
(
"owner "
+
System
.
currentTimeMillis
());
ramdomApp
.
setCreateTimestamp
(
new
Date
());
repository
.
save
(
ramdomApp
);
repository
.
save
(
ramdomApp
);
Assert
.
assertEquals
(
1
,
repository
.
count
());
Assert
.
assertEquals
(
1
,
repository
.
count
());
...
...
apollo-portal/src/test/resources/application.properties
浏览文件 @
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.username
=
sa
spring.datasource.password
=
sa
spring.datasource.password
=
spring.jpa.show-sql: true
\ No newline at end of file
pom.xml
浏览文件 @
80bd483b
...
@@ -121,8 +121,8 @@
...
@@ -121,8 +121,8 @@
<artifactId>
maven-compiler-plugin
</artifactId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.5.1
</version>
<version>
3.5.1
</version>
<configuration>
<configuration>
<source>
${java.
source
}
</source>
<source>
${java.
version
}
</source>
<target>
${java.
target
}
</target>
<target>
${java.
version
}
</target>
</configuration>
</configuration>
</plugin>
</plugin>
<plugin>
<plugin>
...
@@ -151,6 +151,11 @@
...
@@ -151,6 +151,11 @@
</plugin>
</plugin>
</plugins>
</plugins>
</pluginManagement>
</pluginManagement>
<plugins>
<plugin>
<artifactId>
maven-source-plugin
</artifactId>
</plugin>
</plugins>
</build>
</build>
<profiles>
<profiles>
<profile>
<profile>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录