Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/io/nats/client/support/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,21 @@ public static void readNanos(String json, Pattern pattern, Consumer<Duration> c)
}
}

public static <T> boolean listEquals(List<T> l1, List<T> l2)
{
if (l1 == null)
{
return l2 == null;
}

if (l2 == null)
{
return false;
}

return l1.equals(l2);
}

public static boolean mapEquals(Map<String, String> map1, Map<String, String> map2) {
if (map1 == null) {
return map2 == null;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/nats/service/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ public Endpoint(String name) {
this(name, null, null, true);
}

public Endpoint(String name, Map<String, String> metadata) {
this(name, null, metadata, true);
}

public Endpoint(String name, String subject) {
this(name, subject, null, true);
}

public Endpoint(String name, String subject, Map<String, String> metadata) {
this(name, subject, metadata, true);
}

// internal use constructors
Endpoint(String name, String subject, Map<String, String> metadata, boolean validate) {
if (validate) {
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/io/nats/service/InfoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.util.Objects;

import static io.nats.client.support.ApiConstants.DESCRIPTION;
import static io.nats.client.support.ApiConstants.SUBJECTS;
import static io.nats.client.support.JsonValueUtils.readString;
import static io.nats.client.support.JsonValueUtils.readStringList;
import static io.nats.client.support.ApiConstants.ENDPOINTS;
import static io.nats.client.support.JsonUtils.listEquals;
import static io.nats.client.support.JsonValueUtils.*;

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

private final String description;
private final List<String> subjects;
private final List<Endpoint> endpoints;

public InfoResponse(String id, String name, String version, Map<String, String> metadata, String description, List<String> subjects) {
public InfoResponse(String id, String name, String version, Map<String, String> metadata, String description, List<Endpoint> endpoints) {
super(TYPE, id, name, version, metadata);
this.description = description;
this.subjects = subjects;
this.endpoints = endpoints;
}

public InfoResponse(byte[] jsonBytes) {
Expand All @@ -47,13 +47,14 @@ public InfoResponse(byte[] jsonBytes) {
private InfoResponse(JsonValue jv) {
super(TYPE, jv);
description = readString(jv, DESCRIPTION);
subjects = readStringList(jv, SUBJECTS);
endpoints = read(jv, ENDPOINTS, v -> listOf(v, Endpoint::new));

}

@Override
protected void subToJson(StringBuilder sb) {
JsonUtils.addField(sb, DESCRIPTION, description);
JsonUtils.addStrings(sb, SUBJECTS, subjects);
JsonUtils.addJsons(sb, ENDPOINTS, endpoints);
}

/**
Expand All @@ -65,11 +66,11 @@ public String getDescription() {
}

/**
* Subjects that can be invoked
* @return the subjects
* List of endpoints
* @return the endpoints
*/
public List<String> getSubjects() {
return subjects;
public List<Endpoint> getEndpoints() {
return endpoints;
}

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

if (!Objects.equals(description, that.description)) return false;
return Objects.equals(subjects, that.subjects);
return listEquals(endpoints, that.endpoints);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (subjects != null ? subjects.hashCode() : 0);
result = 31 * result + (endpoints != null ? endpoints.hashCode() : 0);
return result;
}
}
4 changes: 3 additions & 1 deletion src/main/java/io/nats/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class Service {
// ! also while we are here, we need to collect the endpoints for the SchemaResponse
Dispatcher dTemp = null;
List<String> infoSubjects = new ArrayList<>();
List<Endpoint> infoEndpoints = new ArrayList<>();
serviceContexts = new HashMap<>();
for (ServiceEndpoint se : b.serviceEndpoints.values()) {
if (se.getDispatcher() == null) {
Expand All @@ -76,14 +77,15 @@ public class Service {
serviceContexts.put(se.getName(), new EndpointContext(conn, null, true, se));
}
infoSubjects.add(se.getSubject());
infoEndpoints.add(se.getEndpoint());
}
if (dTemp != null) {
dInternals.add(dTemp);
}

// build static responses
pingResponse = new PingResponse(id, b.name, b.version, b.metadata);
infoResponse = new InfoResponse(id, b.name, b.version, b.metadata, b.description, infoSubjects);
infoResponse = new InfoResponse(id, b.name, b.version, b.metadata, b.description, infoEndpoints);

if (b.pingDispatcher == null || b.infoDispatcher == null || b.schemaDispatcher == null || b.statsDispatcher == null) {
dTemp = conn.createDispatcher();
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/io/nats/service/ServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void testServiceWorkflow() throws Exception {
InfoResponse exp = (InfoResponse) expected;
InfoResponse r = (InfoResponse) response;
assertEquals(exp.getDescription(), r.getDescription());
assertEquals(exp.getSubjects(), r.getSubjects());
assertEquals(exp.getEndpoints(), r.getEndpoints());
};
verifyDiscovery(discovery.info(), infoVerifier, infoResponse1, infoResponse2);
verifyDiscovery(discovery.info(SERVICE_NAME_1), infoVerifier, infoResponse1);
Expand Down Expand Up @@ -992,15 +992,15 @@ public void testServiceResponsesConstruction() {
iae = assertThrows(IllegalArgumentException.class, () -> new TestServiceResponses(json4.getBytes()));
assertTrue(iae.getMessage().contains("Version cannot be null"));

InfoResponse ir1 = new InfoResponse("id", "name", "0.0.0", metadata, "desc", Arrays.asList("subject1", "subject2"));
Map<String, String> meta = new HashMap<>();
meta.put("foo", "bar");
Endpoint end1 = new Endpoint("endfoo", meta);
InfoResponse ir1 = new InfoResponse("id", "name", "0.0.0", metadata, "desc", Collections.singletonList(end1));
InfoResponse ir2 = new InfoResponse(ir1.toJson().getBytes());
System.out.println(ir1.toJson());
validateApiInOutInfoResponse(ir1);
validateApiInOutInfoResponse(ir2);

List<EndpointResponse> endpoints = new ArrayList<>();
endpoints.add(new EndpointResponse("endName0", "endSubject0"));
endpoints.add(new EndpointResponse("endName1", "endSubject1"));

ZonedDateTime serviceStarted = DateTimeUtils.gmtNow();
ZonedDateTime[] endStarteds = new ZonedDateTime[2];
sleep(100);
Expand Down Expand Up @@ -1049,9 +1049,10 @@ private static void validateApiInOutStatsResponse(StatsResponse stat, ZonedDateT
private static void validateApiInOutInfoResponse(InfoResponse r) {
validateApiInOutServiceResponse(r, InfoResponse.TYPE);
assertEquals("desc", r.getDescription());
assertEquals(2, r.getSubjects().size());
assertTrue(r.getSubjects().contains("subject1"));
assertTrue(r.getSubjects().contains("subject2"));
assertEquals(1, r.getEndpoints().size());
Endpoint endpoint = r.getEndpoints().get(0);
assertEquals("endfoo", endpoint.getName());
assertEquals("bar", endpoint.getMetadata().get("foo"));
}

private static void validateApiInOutPingResponse(PingResponse r) {
Expand Down