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
5f192dd6
Unverified
Commit
5f192dd6
authored
Aug 29, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix NPE when instance has no status page url.
fixes gh-1285
parent
6819922d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
2 deletions
+130
-2
EurekaController.java
...amework/cloud/netflix/eureka/server/EurekaController.java
+4
-2
EurekaControllerTest.java
...ork/cloud/netflix/eureka/server/EurekaControllerTest.java
+126
-0
No files found.
spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaController.java
View file @
5f192dd6
...
...
@@ -233,8 +233,10 @@ public class EurekaController {
LinkedHashMap
<
String
,
Object
>
instance
=
new
LinkedHashMap
<>();
instances
.
add
(
instance
);
instance
.
put
(
"id"
,
p
.
first
());
instance
.
put
(
"url"
,
p
.
second
());
instance
.
put
(
"isHref"
,
p
.
second
().
startsWith
(
"http"
));
String
url
=
p
.
second
();
instance
.
put
(
"url"
,
url
);
boolean
isHref
=
url
!=
null
&&
url
.
startsWith
(
"http"
);
instance
.
put
(
"isHref"
,
isHref
);
/*
* String id = p.first(); String url = p.second(); if(url != null &&
* url.startsWith("http")){
...
...
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaControllerTest.java
0 → 100644
View file @
5f192dd6
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org
.
springframework
.
cloud
.
netflix
.
eureka
.
server
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.util.ReflectionUtils
;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.DataCenterInfo
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.appinfo.MyDataCenterInfo
;
import
com.netflix.discovery.shared.Application
;
import
com.netflix.eureka.EurekaServerContext
;
import
com.netflix.eureka.EurekaServerContextHolder
;
import
com.netflix.eureka.cluster.PeerEurekaNode
;
import
com.netflix.eureka.cluster.PeerEurekaNodes
;
import
com.netflix.eureka.registry.PeerAwareInstanceRegistry
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
nullValue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
EurekaControllerTest
{
private
ApplicationInfoManager
infoManager
;
@Before
public
void
setup
()
throws
Exception
{
PeerEurekaNodes
peerEurekaNodes
=
mock
(
PeerEurekaNodes
.
class
);
when
(
peerEurekaNodes
.
getPeerNodesView
()).
thenReturn
(
Collections
.<
PeerEurekaNode
>
emptyList
());
InstanceInfo
instanceInfo
=
InstanceInfo
.
Builder
.
newBuilder
()
.
setAppName
(
"test"
)
.
setDataCenterInfo
(
new
MyDataCenterInfo
(
DataCenterInfo
.
Name
.
MyOwn
))
.
build
();
this
.
infoManager
=
mock
(
ApplicationInfoManager
.
class
);
setInstance
(
this
.
infoManager
);
when
(
this
.
infoManager
.
getInfo
()).
thenReturn
(
instanceInfo
);
Application
myapp
=
new
Application
(
"myapp"
);
myapp
.
addInstance
(
InstanceInfo
.
Builder
.
newBuilder
()
.
setAppName
(
"myapp"
)
.
setDataCenterInfo
(
new
MyDataCenterInfo
(
DataCenterInfo
.
Name
.
MyOwn
))
.
setInstanceId
(
"myapp:1"
)
.
build
());
ArrayList
<
Application
>
applications
=
new
ArrayList
<>();
applications
.
add
(
myapp
);
PeerAwareInstanceRegistry
registry
=
mock
(
PeerAwareInstanceRegistry
.
class
);
when
(
registry
.
getSortedApplications
()).
thenReturn
(
applications
);
EurekaServerContext
serverContext
=
mock
(
EurekaServerContext
.
class
);
EurekaServerContextHolder
.
initialize
(
serverContext
);
when
(
serverContext
.
getRegistry
()).
thenReturn
(
registry
);
when
(
serverContext
.
getPeerEurekaNodes
()).
thenReturn
(
peerEurekaNodes
);
when
(
serverContext
.
getApplicationInfoManager
()).
thenReturn
(
this
.
infoManager
);
}
@After
public
void
teardown
()
throws
Exception
{
setInstance
(
null
);
}
void
setInstance
(
ApplicationInfoManager
infoManager
)
throws
IllegalAccessException
{
Field
instance
=
ReflectionUtils
.
findField
(
ApplicationInfoManager
.
class
,
"instance"
);
ReflectionUtils
.
makeAccessible
(
instance
);
instance
.
set
(
null
,
infoManager
);
}
@Test
public
void
testStatus
()
throws
Exception
{
Map
<
String
,
Object
>
model
=
new
HashMap
<>();
EurekaController
controller
=
new
EurekaController
(
infoManager
);
controller
.
status
(
new
MockHttpServletRequest
(
"GET"
,
"/"
),
model
);
Map
<
String
,
Object
>
app
=
getFirst
(
model
,
"apps"
);
Map
<
String
,
Object
>
instanceInfo
=
getFirst
(
app
,
"instanceInfos"
);
Map
<
String
,
Object
>
instance
=
getFirst
(
instanceInfo
,
"instances"
);
assertThat
(
"id was wrong"
,
(
String
)
instance
.
get
(
"id"
),
is
(
equalTo
(
"myapp:1"
)));
assertThat
(
"url was not null"
,
instance
.
get
(
"url"
),
is
(
nullValue
()));
assertThat
(
"isHref was wrong"
,
(
Boolean
)
instance
.
get
(
"isHref"
),
is
(
false
));
}
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
getFirst
(
Map
<
String
,
Object
>
model
,
String
key
)
{
List
<
Map
<
String
,
Object
>>
apps
=
(
List
<
Map
<
String
,
Object
>>)
model
.
get
(
key
);
assertThat
(
key
+
" was wrong size"
,
apps
,
is
(
hasSize
(
1
)));
return
apps
.
get
(
0
);
}
}
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