Skip to content

Commit

Permalink
changed extended nodeClient to use doAnswer and timezone to timeunit
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
  • Loading branch information
amitgalitz committed Dec 10, 2021
1 parent c9971a4 commit 0f2c7a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static String getTooManyCategoricalFieldErr(int limit) {
public static String HISTORICAL_ANALYSIS_CANCELLED = "Historical analysis cancelled by user";
public static String HC_DETECTOR_TASK_IS_UPDATING = "HC detector task is updating";
public static String NEGATIVE_TIME_CONFIGURATION = "should be non-negative";
public static String INVALID_TIME_CONFIGURATION_UNITS = "Timezone %s is not supported";
public static String INVALID_TIME_CONFIGURATION_UNITS = "Time unit %s is not supported";
public static String INVALID_DETECTOR_NAME =
"Valid characters for detector name are a-z, A-Z, 0-9, -(hyphen), _(underscore) and .(period)";
public static String DUPLICATE_FEATURE_AGGREGATION_NAMES = "Detector has duplicate feature aggregation query names: ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,30 +714,21 @@ public void testCreateAnomalyDetectorWithDuplicateFeatureAggregationNames() thro
Feature featureOne = TestHelpers.randomFeature("featureName", "test-1");
Feature featureTwo = TestHelpers.randomFeature("featureNameTwo", "test-1");
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(ImmutableList.of(featureOne, featureTwo));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 9;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));

// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
ActionType<Response> action,
Request request,
ActionListener<Response> listener
) {
try {
listener.onResponse((Response) detectorResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
};
SearchResponse mockResponse = mock(SearchResponse.class);
when(mockResponse.getHits()).thenReturn(TestHelpers.createSearchHits(9));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assertTrue(String.format("The size of args is %d. Its content is %s", args.length, Arrays.toString(args)), args.length == 2);
assertTrue(args[0] instanceof SearchRequest);
assertTrue(args[1] instanceof ActionListener);
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(mockResponse);
return null;
}).when(clientMock).search(any(SearchRequest.class), any());

handler = new IndexAnomalyDetectorActionHandler(
clusterService,
client,
clientMock,
transportService,
channel,
anomalyDetectionIndices,
Expand Down

0 comments on commit 0f2c7a8

Please sign in to comment.