Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Adding unit tests for Transport Actions (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
saratvemulapalli authored Dec 8, 2020
1 parent c25e630 commit f442efd
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 26 deletions.
6 changes: 0 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ List<String> jacocoExclusions = [
'com.amazon.opendistroforelasticsearch.ad.common.exception.AnomalyDetectionException',
'com.amazon.opendistroforelasticsearch.ad.util.ClientUtil',

'com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorAction',
'com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorRequest',
'com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorResponse',
'com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorTransportAction',
Expand All @@ -291,13 +290,8 @@ List<String> jacocoExclusions = [
'com.amazon.opendistroforelasticsearch.ad.transport.DeleteAnomalyDetectorTransportAction*',
'com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorTransportAction*',
'com.amazon.opendistroforelasticsearch.ad.transport.GetAnomalyDetectorTransportAction*',
'com.amazon.opendistroforelasticsearch.ad.transport.GetAnomalyDetectorResponse',
'com.amazon.opendistroforelasticsearch.ad.transport.IndexAnomalyDetectorRequest',
'com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyResultTransportAction*',
'com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorInfoTransportAction*',
'com.amazon.opendistroforelasticsearch.ad.transport.GetAnomalyDetectorRequest',
'com.amazon.opendistroforelasticsearch.ad.transport.IndexAnomalyDetectorResponse',
'com.amazon.opendistroforelasticsearch.ad.transport.IndexAnomalyDetectorTransportAction',

// TODO: hc caused coverage to drop
'com.amazon.opendistroforelasticsearch.ad.NodeStateManager',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.get.GetResult;
Expand Down Expand Up @@ -192,6 +193,10 @@ public static XContentParser parser(String xc) throws IOException {
return parser;
}

public static Map<String, Object> XContentBuilderToMap(XContentBuilder builder) {
return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
}

public static NamedXContentRegistry xContentRegistry() {
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
return new NamedXContentRegistry(searchModule.getNamedXContents());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@
package com.amazon.opendistroforelasticsearch.ad.transport;

import java.io.IOException;
import java.time.Instant;
import java.util.Map;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.transport.TransportService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetectorJob;
import com.amazon.opendistroforelasticsearch.ad.model.EntityProfile;
import com.amazon.opendistroforelasticsearch.ad.util.DiscoveryNodeFilterer;
import com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils;
import com.google.common.collect.ImmutableMap;

public class GetAnomalyDetectorTransportActionTests extends ESIntegTestCase {
public class GetAnomalyDetectorTransportActionTests extends ESSingleNodeTestCase {
private GetAnomalyDetectorTransportAction action;
private Task task;
private ActionListener<GetAnomalyDetectorResponse> response;
Expand Down Expand Up @@ -58,6 +73,11 @@ public void onFailure(Exception e) {}
};
}

@Override
protected NamedWriteableRegistry writableRegistry() {
return getInstanceFromNode(NamedWriteableRegistry.class);
}

@Test
public void testGetTransportAction() throws IOException {
GetAnomalyDetectorRequest getAnomalyDetectorRequest = new GetAnomalyDetectorRequest(
Expand All @@ -83,4 +103,87 @@ public void testGetAction() {
Assert.assertNotNull(GetAnomalyDetectorAction.INSTANCE.name());
Assert.assertEquals(GetAnomalyDetectorAction.INSTANCE.name(), GetAnomalyDetectorAction.NAME);
}

@Test
public void testGetAnomalyDetectorRequest() throws IOException {
GetAnomalyDetectorRequest request = new GetAnomalyDetectorRequest("1234", 4321, true, "", "abcd", false, "value");
BytesStreamOutput out = new BytesStreamOutput();
request.writeTo(out);
StreamInput input = out.bytes().streamInput();
GetAnomalyDetectorRequest newRequest = new GetAnomalyDetectorRequest(input);
Assert.assertEquals(request.getDetectorID(), newRequest.getDetectorID());
Assert.assertEquals(request.getRawPath(), newRequest.getRawPath());
Assert.assertNull(newRequest.validate());
}

@Test
public void testGetAnomalyDetectorRequestNoEntityValue() throws IOException {
GetAnomalyDetectorRequest request = new GetAnomalyDetectorRequest("1234", 4321, true, "", "abcd", false, null);
BytesStreamOutput out = new BytesStreamOutput();
request.writeTo(out);
StreamInput input = out.bytes().streamInput();
GetAnomalyDetectorRequest newRequest = new GetAnomalyDetectorRequest(input);
Assert.assertNull(newRequest.getEntityValue());
}

@SuppressWarnings("unchecked")
@Test
public void testGetAnomalyDetectorResponse() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
AnomalyDetectorJob adJob = TestHelpers.randomAnomalyDetectorJob();
GetAnomalyDetectorResponse response = new GetAnomalyDetectorResponse(
4321,
"1234",
5678,
9867,
detector,
adJob,
false,
RestStatus.OK,
null,
null,
false
);
response.writeTo(out);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry());
GetAnomalyDetectorResponse newResponse = new GetAnomalyDetectorResponse(input);
XContentBuilder builder = TestHelpers.builder();
Assert.assertNotNull(newResponse.toXContent(builder, ToXContent.EMPTY_PARAMS));

Map<String, Object> map = TestHelpers.XContentBuilderToMap(builder);
Assert.assertTrue(map.get(RestHandlerUtils.ANOMALY_DETECTOR) instanceof Map);
Map<String, Object> map1 = (Map<String, Object>) map.get(RestHandlerUtils.ANOMALY_DETECTOR);
Assert.assertEquals(map1.get("name"), detector.getName());
}

@Test
public void testGetAnomalyDetectorProfileResponse() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
AnomalyDetectorJob adJob = TestHelpers.randomAnomalyDetectorJob();
EntityProfile entityProfile = new EntityProfile.Builder("catField", "app-0").build();
GetAnomalyDetectorResponse response = new GetAnomalyDetectorResponse(
4321,
"1234",
5678,
9867,
detector,
adJob,
false,
RestStatus.OK,
null,
entityProfile,
true
);
response.writeTo(out);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry());
GetAnomalyDetectorResponse newResponse = new GetAnomalyDetectorResponse(input);
XContentBuilder builder = TestHelpers.builder();
Assert.assertNotNull(newResponse.toXContent(builder, ToXContent.EMPTY_PARAMS));

Map<String, Object> map = TestHelpers.XContentBuilderToMap(builder);
Assert.assertEquals(map.get(EntityProfile.CATEGORY_FIELD), "catField");
Assert.assertEquals(map.get(EntityProfile.ENTITY_VALUE), "app-0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,43 @@

package com.amazon.opendistroforelasticsearch.ad.transport;

import java.time.Instant;
import java.util.Map;

import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils;
import com.google.common.collect.ImmutableMap;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ IndexAnomalyDetectorRequest.class, IndexAnomalyDetectorResponse.class })
public class IndexAnomalyDetectorActionTests {
public class IndexAnomalyDetectorActionTests extends ESSingleNodeTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
}

@Override
protected NamedWriteableRegistry writableRegistry() {
return getInstanceFromNode(NamedWriteableRegistry.class);
}

@Test
public void testIndexRequest() throws Exception {
BytesStreamOutput out = new BytesStreamOutput();
AnomalyDetector detector = Mockito.mock(AnomalyDetector.class);
Mockito.doNothing().when(detector).writeTo(out);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
IndexAnomalyDetectorRequest request = new IndexAnomalyDetectorRequest(
"1234",
4321,
Expand All @@ -58,23 +65,25 @@ public void testIndexRequest() throws Exception {
5
);
request.writeTo(out);
StreamInput input = out.bytes().streamInput();
PowerMockito.whenNew(AnomalyDetector.class).withAnyArguments().thenReturn(detector);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry());
IndexAnomalyDetectorRequest newRequest = new IndexAnomalyDetectorRequest(input);
Assert.assertEquals(request.getDetectorID(), newRequest.getDetectorID());

Assert.assertNull(newRequest.validate());
}

@Test
public void testIndexResponse() throws Exception {
BytesStreamOutput out = new BytesStreamOutput();
AnomalyDetector detector = Mockito.mock(AnomalyDetector.class);
Mockito.doNothing().when(detector).writeTo(out);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
IndexAnomalyDetectorResponse response = new IndexAnomalyDetectorResponse("1234", 56, 78, 90, detector, RestStatus.OK);
response.writeTo(out);
StreamInput input = out.bytes().streamInput();
PowerMockito.whenNew(AnomalyDetector.class).withAnyArguments().thenReturn(detector);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry());
IndexAnomalyDetectorResponse newResponse = new IndexAnomalyDetectorResponse(input);
Assert.assertEquals(response.getId(), newResponse.getId());
XContentBuilder builder = TestHelpers.builder();
Assert.assertNotNull(newResponse.toXContent(builder, ToXContent.EMPTY_PARAMS));

Map<String, Object> map = TestHelpers.XContentBuilderToMap(builder);
Assert.assertEquals(map.get(RestHandlerUtils._ID), "1234");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.ad.transport;

import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class StopDetectorActionTests extends ESIntegTestCase {

@Override
@Before
public void setUp() throws Exception {
super.setUp();
}

@Test
public void testStopDetectorAction() {
Assert.assertNotNull(StopDetectorAction.INSTANCE.name());
Assert.assertEquals(StopDetectorAction.INSTANCE.name(), StopDetectorAction.NAME);
}
}

0 comments on commit f442efd

Please sign in to comment.