Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
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
spring-cloud-netflix
Commits
d6a60405
Commit
d6a60405
authored
Jun 21, 2017
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split tests up into individual methods
parent
51a03b30
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
43 deletions
+122
-43
AbstractDocumentationTests.java
...netflix/eureka/server/doc/AbstractDocumentationTests.java
+37
-10
AppRegistrationTests.java
...cloud/netflix/eureka/server/doc/AppRegistrationTests.java
+63
-31
RequestVerifierFilter.java
...loud/netflix/eureka/server/doc/RequestVerifierFilter.java
+22
-2
No files found.
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AbstractDocumentationTests.java
View file @
d6a60405
...
...
@@ -20,6 +20,8 @@ import com.jayway.restassured.RestAssured;
import
com.jayway.restassured.builder.RequestSpecBuilder
;
import
com.jayway.restassured.filter.Filter
;
import
com.jayway.restassured.specification.RequestSpecification
;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl
;
import
org.junit.After
;
...
...
@@ -33,6 +35,7 @@ import org.springframework.boot.context.embedded.LocalServerPort;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.cloud.contract.wiremock.restdocs.WireMockSnippet
;
import
org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean
;
import
org.springframework.cloud.netflix.eureka.server.EnableEurekaServer
;
import
org.springframework.cloud.netflix.eureka.server.doc.AbstractDocumentationTests.Application
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -61,6 +64,12 @@ public abstract class AbstractDocumentationTests {
@Autowired
private
PeerAwareInstanceRegistryImpl
registry
;
@Autowired
private
EurekaInstanceConfigBean
instanceConfig
;
@Autowired
private
ApplicationInfoManager
applicationInfoManager
;
@Rule
public
JUnitRestDocumentation
restDocumentation
=
new
JUnitRestDocumentation
(
"target/generated-snippets"
);
...
...
@@ -72,6 +81,18 @@ public abstract class AbstractDocumentationTests {
registry
.
initializedResponseCache
();
}
protected
void
register
(
String
name
,
String
id
)
{
registry
.
register
(
getInstance
(
name
,
id
),
false
);
}
protected
InstanceInfo
getInstance
(
String
name
,
String
id
)
{
instanceConfig
.
setAppname
(
name
);
instanceConfig
.
setInstanceId
(
id
);
instanceConfig
.
setHostname
(
"foo.example.com"
);
applicationInfoManager
.
initComponent
(
instanceConfig
);
return
applicationInfoManager
.
getInfo
();
}
private
RestDocumentationFilter
filter
(
String
name
)
{
return
RestAssuredRestDocumentation
.
document
(
name
,
preprocessRequest
(
modifyUris
().
host
(
"eureka.example.com"
).
removePort
(),
...
...
@@ -79,11 +100,11 @@ public abstract class AbstractDocumentationTests {
preprocessResponse
(
prettyPrint
()));
}
private
RequestSpecification
document
(
Filter
...
filters
)
{
return
document
(
null
,
filters
);
private
RequestSpecification
spec
(
Filter
...
filters
)
{
return
spec
(
null
,
filters
);
}
private
RequestSpecification
document
(
Object
body
,
Filter
...
filters
)
{
private
RequestSpecification
spec
(
Object
body
,
Filter
...
filters
)
{
RequestSpecBuilder
builder
=
new
RequestSpecBuilder
()
.
addFilter
(
documentationConfiguration
(
this
.
restDocumentation
).
snippets
()
.
withAdditionalDefaults
(
new
WireMockSnippet
()));
...
...
@@ -97,19 +118,25 @@ public abstract class AbstractDocumentationTests {
return
spec
;
}
protected
RequestSpecification
assure
(
String
name
,
Object
body
)
{
RestDocumentationFilter
filter
=
filter
(
name
);
RequestSpecification
assured
=
RestAssured
.
given
(
document
(
body
,
filter
));
protected
RequestSpecification
document
()
{
return
document
(
"{method-name}"
);
}
protected
RequestSpecification
document
(
Object
body
)
{
RestDocumentationFilter
filter
=
filter
(
"{method-name}"
);
RequestSpecification
assured
=
RestAssured
.
given
(
spec
(
body
,
filter
));
return
assured
.
filter
(
filter
);
}
protected
RequestSpecification
assure
(
String
name
)
{
protected
RequestSpecification
document
(
String
name
,
Object
body
)
{
RestDocumentationFilter
filter
=
filter
(
name
);
return
RestAssured
.
given
(
document
(
filter
)).
filter
(
filter
);
RequestSpecification
assured
=
RestAssured
.
given
(
spec
(
body
,
filter
));
return
assured
.
filter
(
filter
);
}
protected
RequestSpecification
assure
()
{
return
assure
(
"{method-name}"
);
protected
RequestSpecification
document
(
String
name
)
{
RestDocumentationFilter
filter
=
filter
(
name
);
return
RestAssured
.
given
(
spec
(
filter
)).
filter
(
filter
);
}
@Configuration
...
...
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AppRegistrationTests.java
View file @
d6a60405
...
...
@@ -19,13 +19,10 @@ package org.springframework.cloud.netflix.eureka.server.doc;
import
java.util.UUID
;
import
com.github.tomakehurst.wiremock.client.WireMock
;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
...
...
@@ -37,50 +34,85 @@ import static org.springframework.cloud.netflix.eureka.server.doc.RequestVerifie
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
public
class
AppRegistrationTests
extends
AbstractDocumentationTests
{
@Autowired
private
EurekaInstanceConfigBean
instanceConfig
;
@Autowired
private
ApplicationInfoManager
applicationInfoManager
;
@Test
public
void
startingApp
()
throws
Exception
{
register
(
"foo"
,
UUID
.
randomUUID
().
toString
());
document
().
accept
(
"application/json"
).
when
().
get
(
"/eureka/apps"
).
then
()
.
assertThat
()
.
body
(
"applications.application"
,
hasSize
(
1
),
"applications.application[0].instance[0].status"
,
equalTo
(
"STARTING"
))
.
statusCode
(
is
(
200
));
}
@Test
public
void
addApp
()
throws
Exception
{
instanceConfig
.
setAppname
(
"foo"
);
instanceConfig
.
setInstanceId
(
UUID
.
randomUUID
().
toString
());
instanceConfig
.
setHostname
(
"foo.example.com"
);
applicationInfoManager
.
initComponent
(
instanceConfig
);
assure
(
"add-app"
,
applicationInfoManager
.
getInfo
())
public
void
addInstance
()
throws
Exception
{
document
(
getInstance
(
"foo"
,
UUID
.
randomUUID
().
toString
()))
.
filter
(
verify
(
"$.instance.app"
).
json
(
"$.instance.hostName"
)
.
json
(
"$.instance[?(@.status=='STARTING')]"
)
.
json
(
"$.instance.instanceId"
)
.
json
(
"$.instance.dataCenterInfo.name"
))
.
when
().
post
(
"/eureka/apps/FOO"
).
then
().
assertThat
().
statusCode
(
is
(
204
));
assure
(
"starting-app"
).
accept
(
"application/json"
).
when
().
get
(
"/eureka/apps"
)
.
then
().
assertThat
()
.
body
(
"applications.application"
,
hasSize
(
1
),
"applications.application[0].instance[0].status"
,
equalTo
(
"STARTING"
))
.
statusCode
(
is
(
200
)
);
assure
(
"up-app"
)
}
@Test
public
void
setStatus
()
throws
Exception
{
String
id
=
UUID
.
randomUUID
().
toString
();
register
(
"foo"
,
id
);
document
(
)
.
filter
(
verify
(
WireMock
.
put
(
WireMock
.
urlPathMatching
(
"/eureka/apps/FOO/.*"
))
.
withQueryParam
(
"value"
,
WireMock
.
matching
(
"UP"
))))
.
when
()
.
put
(
"/eureka/apps/FOO/{id}/status?value={value}"
,
applicationInfoManager
.
getInfo
().
getInstanceId
(),
"UP"
)
.
then
().
assertThat
().
statusCode
(
is
(
200
));
assure
(
"one-app"
).
accept
(
"application/json"
).
when
().
get
(
"/eureka/apps"
).
then
()
.
when
().
put
(
"/eureka/apps/FOO/{id}/status?value={value}"
,
id
,
"UP"
).
then
()
.
assertThat
().
statusCode
(
is
(
200
));
}
@Test
public
void
allApps
()
throws
Exception
{
register
(
"foo"
,
UUID
.
randomUUID
().
toString
());
document
().
accept
(
"application/json"
).
when
().
get
(
"/eureka/apps"
).
then
()
.
assertThat
().
body
(
"applications.application"
,
hasSize
(
1
))
.
statusCode
(
is
(
200
));
assure
(
"delete-app"
).
when
()
.
delete
(
"/eureka/apps/FOO/{id}"
,
applicationInfoManager
.
getInfo
().
getInstanceId
())
.
then
().
assertThat
().
statusCode
(
is
(
200
));
}
@Test
public
void
oneInstance
()
throws
Exception
{
String
id
=
UUID
.
randomUUID
().
toString
();
register
(
"foo"
,
id
);
document
()
.
filter
(
verify
(
WireMock
.
get
(
WireMock
.
urlPathMatching
(
"/eureka/apps/FOO/.*"
))))
.
accept
(
"application/json"
).
when
().
get
(
"/eureka/apps/FOO/{id}"
,
id
).
then
()
.
assertThat
().
body
(
"instance.app"
,
equalTo
(
"FOO"
)).
statusCode
(
is
(
200
));
}
@Test
public
void
renew
()
throws
Exception
{
String
id
=
UUID
.
randomUUID
().
toString
();
register
(
"foo"
,
id
);
document
()
.
filter
(
verify
(
WireMock
.
put
(
WireMock
.
urlPathMatching
(
"/eureka/apps/FOO/.*"
))))
.
accept
(
"application/json"
).
when
().
put
(
"/eureka/apps/FOO/{id}"
,
id
).
then
()
.
assertThat
().
statusCode
(
is
(
200
));
}
@Test
public
void
deleteInstance
()
throws
Exception
{
String
id
=
UUID
.
randomUUID
().
toString
();
register
(
"foo"
,
id
);
document
()
.
filter
(
verify
(
WireMock
.
delete
(
WireMock
.
urlPathMatching
(
"/eureka/apps/FOO/.*"
))))
.
when
().
delete
(
"/eureka/apps/FOO/{id}"
,
id
).
then
().
assertThat
()
.
statusCode
(
is
(
200
));
}
@Test
public
void
emptyApps
()
{
assure
().
when
().
accept
(
"application/json"
).
get
(
"/eureka/apps"
).
then
().
assertThat
()
.
body
(
"applications.application"
,
emptyIterable
()).
statusCode
(
is
(
200
));
document
().
when
().
accept
(
"application/json"
).
get
(
"/eureka/apps"
).
then
()
.
assertThat
().
body
(
"applications.application"
,
emptyIterable
())
.
statusCode
(
is
(
200
));
}
}
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/RequestVerifierFilter.java
View file @
d6a60405
...
...
@@ -223,12 +223,20 @@ class WireMockRestAssuredRequestAdapter implements Request {
@Override
public
String
getHeader
(
String
key
)
{
return
request
.
getHeaders
().
getValue
(
key
);
String
value
=
request
.
getHeaders
().
getValue
(
key
);
if
(
"accept"
.
equals
(
key
.
toLowerCase
())
&&
"*/*"
.
equals
(
value
))
{
return
null
;
}
return
value
;
}
@Override
public
HttpHeader
header
(
String
key
)
{
return
new
HttpHeader
(
key
,
request
.
getHeaders
().
getValue
(
key
));
String
value
=
request
.
getHeaders
().
getValue
(
key
);
if
(
"accept"
.
equals
(
key
.
toLowerCase
())
&&
"*/*"
.
equals
(
value
))
{
return
null
;
}
return
new
HttpHeader
(
key
,
value
);
}
@Override
...
...
@@ -240,6 +248,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
public
HttpHeaders
getHeaders
()
{
List
<
HttpHeader
>
headers
=
new
ArrayList
<>();
for
(
Header
header
:
request
.
getHeaders
())
{
String
value
=
header
.
getValue
();
if
(
"accept"
.
equals
(
header
.
getName
().
toLowerCase
())
&&
"*/*"
.
equals
(
value
))
{
continue
;
}
headers
.
add
(
new
HttpHeader
(
header
.
getName
(),
header
.
getValue
()));
}
return
new
HttpHeaders
(
headers
);
...
...
@@ -247,6 +259,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
@Override
public
boolean
containsHeader
(
String
key
)
{
String
value
=
request
.
getHeaders
().
getValue
(
key
);
if
(
"accept"
.
equals
(
key
.
toLowerCase
())
&&
"*/*"
.
equals
(
value
))
{
return
false
;
}
return
request
.
getHeaders
().
hasHeaderWithName
(
key
);
}
...
...
@@ -254,6 +270,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
public
Set
<
String
>
getAllHeaderKeys
()
{
Set
<
String
>
headers
=
new
LinkedHashSet
<>();
for
(
Header
header
:
request
.
getHeaders
())
{
String
value
=
header
.
getValue
();
if
(
"accept"
.
equals
(
header
.
getName
().
toLowerCase
())
&&
"*/*"
.
equals
(
value
))
{
continue
;
}
headers
.
add
(
header
.
getName
());
}
return
headers
;
...
...
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