Skip to content

Commit 939e31d

Browse files
authored
Change INFO response to contain endpoint details (#918)
1 parent 3eb2924 commit 939e31d

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

src/main/java/io/nats/client/support/JsonUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,21 @@ public static void readNanos(String json, Pattern pattern, Consumer<Duration> c)
950950
}
951951
}
952952

953+
public static <T> boolean listEquals(List<T> l1, List<T> l2)
954+
{
955+
if (l1 == null)
956+
{
957+
return l2 == null;
958+
}
959+
960+
if (l2 == null)
961+
{
962+
return false;
963+
}
964+
965+
return l1.equals(l2);
966+
}
967+
953968
public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
954969
if (map1 == null) {
955970
return map2 == null;

src/main/java/io/nats/service/Endpoint.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ public Endpoint(String name) {
3939
this(name, null, null, true);
4040
}
4141

42+
public Endpoint(String name, Map<String, String> metadata) {
43+
this(name, null, metadata, true);
44+
}
45+
4246
public Endpoint(String name, String subject) {
4347
this(name, subject, null, true);
4448
}
4549

50+
public Endpoint(String name, String subject, Map<String, String> metadata) {
51+
this(name, subject, metadata, true);
52+
}
53+
4654
// internal use constructors
4755
Endpoint(String name, String subject, Map<String, String> metadata, boolean validate) {
4856
if (validate) {

src/main/java/io/nats/service/InfoResponse.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import java.util.Objects;
2222

2323
import static io.nats.client.support.ApiConstants.DESCRIPTION;
24-
import static io.nats.client.support.ApiConstants.SUBJECTS;
25-
import static io.nats.client.support.JsonValueUtils.readString;
26-
import static io.nats.client.support.JsonValueUtils.readStringList;
24+
import static io.nats.client.support.ApiConstants.ENDPOINTS;
25+
import static io.nats.client.support.JsonUtils.listEquals;
26+
import static io.nats.client.support.JsonValueUtils.*;
2727

2828
/**
2929
* SERVICE IS AN EXPERIMENTAL API SUBJECT TO CHANGE
@@ -32,12 +32,12 @@ public class InfoResponse extends ServiceResponse {
3232
public static final String TYPE = "io.nats.micro.v1.info_response";
3333

3434
private final String description;
35-
private final List<String> subjects;
35+
private final List<Endpoint> endpoints;
3636

37-
public InfoResponse(String id, String name, String version, Map<String, String> metadata, String description, List<String> subjects) {
37+
public InfoResponse(String id, String name, String version, Map<String, String> metadata, String description, List<Endpoint> endpoints) {
3838
super(TYPE, id, name, version, metadata);
3939
this.description = description;
40-
this.subjects = subjects;
40+
this.endpoints = endpoints;
4141
}
4242

4343
public InfoResponse(byte[] jsonBytes) {
@@ -47,13 +47,14 @@ public InfoResponse(byte[] jsonBytes) {
4747
private InfoResponse(JsonValue jv) {
4848
super(TYPE, jv);
4949
description = readString(jv, DESCRIPTION);
50-
subjects = readStringList(jv, SUBJECTS);
50+
endpoints = read(jv, ENDPOINTS, v -> listOf(v, Endpoint::new));
51+
5152
}
5253

5354
@Override
5455
protected void subToJson(StringBuilder sb) {
5556
JsonUtils.addField(sb, DESCRIPTION, description);
56-
JsonUtils.addStrings(sb, SUBJECTS, subjects);
57+
JsonUtils.addJsons(sb, ENDPOINTS, endpoints);
5758
}
5859

5960
/**
@@ -65,11 +66,11 @@ public String getDescription() {
6566
}
6667

6768
/**
68-
* Subjects that can be invoked
69-
* @return the subjects
69+
* List of endpoints
70+
* @return the endpoints
7071
*/
71-
public List<String> getSubjects() {
72-
return subjects;
72+
public List<Endpoint> getEndpoints() {
73+
return endpoints;
7374
}
7475

7576
@Override
@@ -81,14 +82,14 @@ public boolean equals(Object o) {
8182
InfoResponse that = (InfoResponse) o;
8283

8384
if (!Objects.equals(description, that.description)) return false;
84-
return Objects.equals(subjects, that.subjects);
85+
return listEquals(endpoints, that.endpoints);
8586
}
8687

8788
@Override
8889
public int hashCode() {
8990
int result = super.hashCode();
9091
result = 31 * result + (description != null ? description.hashCode() : 0);
91-
result = 31 * result + (subjects != null ? subjects.hashCode() : 0);
92+
result = 31 * result + (endpoints != null ? endpoints.hashCode() : 0);
9293
return result;
9394
}
9495
}

src/main/java/io/nats/service/Service.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class Service {
6464
// ! also while we are here, we need to collect the endpoints for the SchemaResponse
6565
Dispatcher dTemp = null;
6666
List<String> infoSubjects = new ArrayList<>();
67+
List<Endpoint> infoEndpoints = new ArrayList<>();
6768
serviceContexts = new HashMap<>();
6869
for (ServiceEndpoint se : b.serviceEndpoints.values()) {
6970
if (se.getDispatcher() == null) {
@@ -76,14 +77,15 @@ public class Service {
7677
serviceContexts.put(se.getName(), new EndpointContext(conn, null, true, se));
7778
}
7879
infoSubjects.add(se.getSubject());
80+
infoEndpoints.add(se.getEndpoint());
7981
}
8082
if (dTemp != null) {
8183
dInternals.add(dTemp);
8284
}
8385

8486
// build static responses
8587
pingResponse = new PingResponse(id, b.name, b.version, b.metadata);
86-
infoResponse = new InfoResponse(id, b.name, b.version, b.metadata, b.description, infoSubjects);
88+
infoResponse = new InfoResponse(id, b.name, b.version, b.metadata, b.description, infoEndpoints);
8789

8890
if (b.pingDispatcher == null || b.infoDispatcher == null || b.schemaDispatcher == null || b.statsDispatcher == null) {
8991
dTemp = conn.createDispatcher();

src/test/java/io/nats/service/ServiceTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void testServiceWorkflow() throws Exception {
208208
InfoResponse exp = (InfoResponse) expected;
209209
InfoResponse r = (InfoResponse) response;
210210
assertEquals(exp.getDescription(), r.getDescription());
211-
assertEquals(exp.getSubjects(), r.getSubjects());
211+
assertEquals(exp.getEndpoints(), r.getEndpoints());
212212
};
213213
verifyDiscovery(discovery.info(), infoVerifier, infoResponse1, infoResponse2);
214214
verifyDiscovery(discovery.info(SERVICE_NAME_1), infoVerifier, infoResponse1);
@@ -992,15 +992,15 @@ public void testServiceResponsesConstruction() {
992992
iae = assertThrows(IllegalArgumentException.class, () -> new TestServiceResponses(json4.getBytes()));
993993
assertTrue(iae.getMessage().contains("Version cannot be null"));
994994

995-
InfoResponse ir1 = new InfoResponse("id", "name", "0.0.0", metadata, "desc", Arrays.asList("subject1", "subject2"));
995+
Map<String, String> meta = new HashMap<>();
996+
meta.put("foo", "bar");
997+
Endpoint end1 = new Endpoint("endfoo", meta);
998+
InfoResponse ir1 = new InfoResponse("id", "name", "0.0.0", metadata, "desc", Collections.singletonList(end1));
996999
InfoResponse ir2 = new InfoResponse(ir1.toJson().getBytes());
1000+
System.out.println(ir1.toJson());
9971001
validateApiInOutInfoResponse(ir1);
9981002
validateApiInOutInfoResponse(ir2);
9991003

1000-
List<EndpointResponse> endpoints = new ArrayList<>();
1001-
endpoints.add(new EndpointResponse("endName0", "endSubject0"));
1002-
endpoints.add(new EndpointResponse("endName1", "endSubject1"));
1003-
10041004
ZonedDateTime serviceStarted = DateTimeUtils.gmtNow();
10051005
ZonedDateTime[] endStarteds = new ZonedDateTime[2];
10061006
sleep(100);
@@ -1049,9 +1049,10 @@ private static void validateApiInOutStatsResponse(StatsResponse stat, ZonedDateT
10491049
private static void validateApiInOutInfoResponse(InfoResponse r) {
10501050
validateApiInOutServiceResponse(r, InfoResponse.TYPE);
10511051
assertEquals("desc", r.getDescription());
1052-
assertEquals(2, r.getSubjects().size());
1053-
assertTrue(r.getSubjects().contains("subject1"));
1054-
assertTrue(r.getSubjects().contains("subject2"));
1052+
assertEquals(1, r.getEndpoints().size());
1053+
Endpoint endpoint = r.getEndpoints().get(0);
1054+
assertEquals("endfoo", endpoint.getName());
1055+
assertEquals("bar", endpoint.getMetadata().get("foo"));
10551056
}
10561057

10571058
private static void validateApiInOutPingResponse(PingResponse r) {

0 commit comments

Comments
 (0)