use of org.opensearch.ad.transport.CronNodeResponse in project anomaly-detection by opensearch-project.
the class HourlyCronTests method templateHourlyCron.
@SuppressWarnings("unchecked")
public void templateHourlyCron(HourlyCronTestExecutionMode mode) {
super.setUpLog4jForJUnit(HourlyCron.class);
ClusterService clusterService = mock(ClusterService.class);
ClusterState state = ClusterCreation.state(1);
when(clusterService.state()).thenReturn(state);
HashMap<String, String> ignoredAttributes = new HashMap<String, String>();
ignoredAttributes.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
DiscoveryNodeFilterer nodeFilter = new DiscoveryNodeFilterer(clusterService);
Client client = mock(Client.class);
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 == 3);
assertTrue(args[2] instanceof ActionListener);
ActionListener<CronResponse> listener = (ActionListener<CronResponse>) args[2];
if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
listener.onResponse(new CronResponse(new ClusterName("test"), Collections.singletonList(new CronNodeResponse(state.nodes().getLocalNode())), Collections.singletonList(new FailedNodeException("foo0", "blah", new OpenSearchException("bar")))));
} else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
listener.onFailure(new OpenSearchException("bar"));
} else {
CronNodeResponse nodeResponse = new CronNodeResponse(state.nodes().getLocalNode());
BytesStreamOutput nodeResponseOut = new BytesStreamOutput();
nodeResponseOut.setVersion(Version.CURRENT);
nodeResponse.writeTo(nodeResponseOut);
StreamInput siNode = nodeResponseOut.bytes().streamInput();
CronNodeResponse nodeResponseRead = new CronNodeResponse(siNode);
CronResponse response = new CronResponse(new ClusterName("test"), Collections.singletonList(nodeResponseRead), Collections.EMPTY_LIST);
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(Version.CURRENT);
response.writeTo(out);
StreamInput si = out.bytes().streamInput();
CronResponse responseRead = new CronResponse(si);
listener.onResponse(responseRead);
}
return null;
}).when(client).execute(eq(CronAction.INSTANCE), any(), any());
HourlyCron cron = new HourlyCron(client, nodeFilter);
cron.run();
Logger LOG = LogManager.getLogger(HourlyCron.class);
LOG.info(testAppender.messages);
if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
assertTrue(testAppender.containsMessage(HourlyCron.NODE_EXCEPTION_LOG_MSG));
} else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
assertTrue(testAppender.containsMessage(HourlyCron.EXCEPTION_LOG_MSG));
} else {
assertTrue(testAppender.containsMessage(HourlyCron.SUCCEEDS_LOG_MSG));
}
super.tearDownLog4jForJUnit();
}
Aggregations