use of org.opensearch.common.io.stream.BytesStreamOutput in project anomaly-detection by opensearch-project.
the class ADStatsTests method testADStatsNodesResponse.
@Test
public void testADStatsNodesResponse() throws IOException, JsonPathNotFoundException {
Map<String, Object> nodeStats = new HashMap<String, Object>() {
{
put("testNodeKey", "testNodeValue");
}
};
ADStatsNodeResponse adStatsNodeResponse = new ADStatsNodeResponse(discoveryNode1, nodeStats);
List<ADStatsNodeResponse> adStatsNodeResponses = Collections.singletonList(adStatsNodeResponse);
List<FailedNodeException> failures = Collections.emptyList();
ADStatsNodesResponse adStatsNodesResponse = new ADStatsNodesResponse(new ClusterName(clusterName), adStatsNodeResponses, failures);
// Test toXContent
XContentBuilder builder = jsonBuilder();
adStatsNodesResponse.toXContent(builder.startObject(), ToXContent.EMPTY_PARAMS).endObject();
String json = Strings.toString(builder);
logger.info("JSON: " + json);
// nodeStats
String nodesJson = JsonDeserializer.getChildNode(json, "nodes").toString();
String node1Json = JsonDeserializer.getChildNode(nodesJson, node1).toString();
for (Map.Entry<String, Object> stat : nodeStats.entrySet()) {
assertEquals("toXContent does not work for node stats", JsonDeserializer.getTextValue(node1Json, stat.getKey()), stat.getValue());
}
// Test Serialization
BytesStreamOutput output = new BytesStreamOutput();
adStatsNodesResponse.writeTo(output);
StreamInput streamInput = output.bytes().streamInput();
ADStatsNodesResponse readRequest = new ADStatsNodesResponse(streamInput);
builder = jsonBuilder();
String readJson = Strings.toString(readRequest.toXContent(builder.startObject(), ToXContent.EMPTY_PARAMS).endObject());
assertEquals("Serialization fails", readJson, json);
}
use of org.opensearch.common.io.stream.BytesStreamOutput in project anomaly-detection by opensearch-project.
the class ADTaskProfileResponseTests method testSerializeResponse.
public void testSerializeResponse() throws IOException {
String taskId = randomAlphaOfLength(5);
ADTaskProfile adTaskProfile = new ADTaskProfile();
adTaskProfile.setTaskId(taskId);
Version remoteAdVersion = Version.CURRENT;
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(randomDiscoveryNode(), adTaskProfile, remoteAdVersion);
List<ADTaskProfileNodeResponse> nodeResponses = ImmutableList.of(nodeResponse);
ADTaskProfileResponse response = new ADTaskProfileResponse(new ClusterName("test"), nodeResponses, ImmutableList.of());
BytesStreamOutput output = new BytesStreamOutput();
response.writeNodesTo(output, nodeResponses);
StreamInput input = output.bytes().streamInput();
List<ADTaskProfileNodeResponse> adTaskProfileNodeResponses = response.readNodesFrom(input);
assertEquals(1, adTaskProfileNodeResponses.size());
assertEquals(taskId, adTaskProfileNodeResponses.get(0).getAdTaskProfile().getTaskId());
BytesStreamOutput output2 = new BytesStreamOutput();
response.writeTo(output2);
StreamInput input2 = output2.bytes().streamInput();
ADTaskProfileResponse response2 = new ADTaskProfileResponse(input2);
assertEquals(1, response2.getNodes().size());
assertEquals(taskId, response2.getNodes().get(0).getAdTaskProfile().getTaskId());
}
use of org.opensearch.common.io.stream.BytesStreamOutput in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testADTaskProfileRequest.
public void testADTaskProfileRequest() throws IOException {
ADTaskProfileRequest request = new ADTaskProfileRequest(randomAlphaOfLength(5), randomDiscoveryNode());
BytesStreamOutput output = new BytesStreamOutput();
request.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ADTaskProfileRequest parsedRequest = new ADTaskProfileRequest(input);
assertEquals(request.getDetectorId(), parsedRequest.getDetectorId());
}
use of org.opensearch.common.io.stream.BytesStreamOutput in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testSerializeResponse.
@Ignore
public void testSerializeResponse() throws IOException {
DiscoveryNode node = randomDiscoveryNode();
ADTaskProfile profile = new ADTaskProfile(TestHelpers.randomAdTask(), randomInt(), randomLong(), randomBoolean(), randomInt(), randomLong(), randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5), randomInt(), randomBoolean(), randomInt(), randomInt(), randomInt(), ImmutableList.of(randomAlphaOfLength(5)), Instant.now().toEpochMilli());
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(node, profile, Version.CURRENT);
ImmutableList<ADTaskProfileNodeResponse> nodes = ImmutableList.of(nodeResponse);
ADTaskProfileResponse response = new ADTaskProfileResponse(new ClusterName("test"), nodes, ImmutableList.of());
BytesStreamOutput output = new BytesStreamOutput();
response.writeNodesTo(output, nodes);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
List<ADTaskProfileNodeResponse> adTaskProfileNodeResponses = response.readNodesFrom(input);
assertEquals(1, adTaskProfileNodeResponses.size());
ADTaskProfileNodeResponse parsedProfile = adTaskProfileNodeResponses.get(0);
if (Version.CURRENT.onOrBefore(Version.V_1_0_0)) {
assertEquals(profile.getNodeId(), parsedProfile.getAdTaskProfile().getNodeId());
assertNull(parsedProfile.getAdTaskProfile().getTaskId());
} else {
assertEquals(profile.getTaskId(), parsedProfile.getAdTaskProfile().getTaskId());
}
// assertEquals(profile, adTaskProfileNodeResponses.get(0).getAdTaskProfile());
// assertEquals(profile, response2.getNodes().get(0).getAdTaskProfile());
}
use of org.opensearch.common.io.stream.BytesStreamOutput in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testADTaskProfileResponse.
private void testADTaskProfileResponse(ADTaskProfileNodeResponse response) throws IOException {
BytesStreamOutput output = new BytesStreamOutput();
response.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ADTaskProfileNodeResponse parsedResponse = ADTaskProfileNodeResponse.readNodeResponse(input);
if (response.getAdTaskProfile() != null) {
assertTrue(response.getAdTaskProfile().equals(parsedResponse.getAdTaskProfile()));
} else {
assertNull(parsedResponse.getAdTaskProfile());
}
}
Aggregations