Search in sources :

Example 11 with BytesStreamOutput

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);
}
Also used : HashMap(java.util.HashMap) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) TreeMap(java.util.TreeMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Example 12 with BytesStreamOutput

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());
}
Also used : ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) Version(org.opensearch.Version) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 13 with BytesStreamOutput

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());
}
Also used : NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 14 with BytesStreamOutput

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());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TestHelpers.randomDiscoveryNode(org.opensearch.ad.TestHelpers.randomDiscoveryNode) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ClusterName(org.opensearch.cluster.ClusterName) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Ignore(org.junit.Ignore)

Example 15 with BytesStreamOutput

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());
    }
}
Also used : NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)385 StreamInput (org.opensearch.common.io.stream.StreamInput)192 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)68 Test (org.junit.Test)64 IOException (java.io.IOException)44 BytesReference (org.opensearch.common.bytes.BytesReference)39 Version (org.opensearch.Version)31 ArrayList (java.util.ArrayList)27 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)26 HashMap (java.util.HashMap)22 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)21 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)19 OpenSearchException (org.opensearch.OpenSearchException)17 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)15 BytesArray (org.opensearch.common.bytes.BytesArray)14 Map (java.util.Map)13 List (java.util.List)10 ClusterName (org.opensearch.cluster.ClusterName)10 TimeValue (org.opensearch.common.unit.TimeValue)10 Collections (java.util.Collections)9