Skip to content

Commit

Permalink
Adding Unit Tests for throwing errors in trigger results
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Jindal <[email protected]>
  • Loading branch information
Aditya Jindal committed Jul 2, 2021
1 parent 1fd9247 commit 565d365
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ const createActionContext = (context, action) => ({
},
});

export const checkForError = (response, error) => {
for (const trigger_name in response.resp.trigger_results) {
// Check for errors in the trigger response
if (!response.resp.trigger_results[trigger_name].error) {
// Check for errors in the actions configured
for (const action_result in response.resp.trigger_results[trigger_name].action_results) {
error = response.resp.trigger_results[trigger_name].action_results[action_result].error;
}
} else {
error = response.resp.trigger_results[trigger_name].error;
}
}
return error;
};

class ConfigureActions extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -98,21 +113,6 @@ class ConfigureActions extends React.Component {
}
};

checkForError = (response, error) => {
for (const trigger_name in response.resp.trigger_results) {
// Check for errors in the trigger response
if (!response.resp.trigger_results[trigger_name].error) {
// Check for errors in the actions configured
for (const action_result in response.resp.trigger_results[trigger_name].action_results) {
error = response.resp.trigger_results[trigger_name].action_results[action_result].error
}
} else {
error = response.resp.trigger_results[trigger_name].error
}
}
return error
}

sendTestMessage = async (index) => {
const {
context: { monitor, trigger },
Expand All @@ -127,12 +127,12 @@ class ConfigureActions extends React.Component {
query: { dryrun: false },
body: JSON.stringify(testMonitor),
});
let error = null
let error = null;
if (response.ok) {
error = this.checkForError(response, error)
error = checkForError(response, error);
}
if (error || !response.ok) {
const errorMessage = error==null ? response.resp : error
const errorMessage = error == null ? response.resp : error;
console.error('There was an error trying to send test message', errorMessage);
backendErrorNotification(notifications, 'send', 'test message', errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2019 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.
*/

import { checkForError } from './ConfigureActions';

const error =
'java.io.IOException: Failed: HttpResponseProxy{HTTP/1.1 403 Forbidden [Content-Type: application/json, Content-Length: 82, Connection: keep-alive, Date: Fri, 02 Jul 2021 03:54:57 GMT, x-amzn-ErrorType: ForbiddenException';

const noErrorInResponse = {
resp: {
monitor_name: 'TestMonitor',
period_start: 1625198037084,
period_end: 1625198097084,
error: null,
input_results: {
results: [],
error: null,
},
trigger_results: {
PgxaZXoB3e90jg4z8ga8: {
name: '',
triggered: true,
error: null,
action_results: {
PwxaZXoB3e90jg4z8ga8: {
id: 'PwxaZXoB3e90jg4z8ga8',
name: 'sdlkmdslfdsfds',
output: {},
throttled: false,
executionTime: 1625198097035,
error: null,
},
},
},
},
},
};

const errorInActionResponse = {
resp: {
monitor_name: 'TestMonitor',
period_start: 1625198037084,
period_end: 1625198097084,
error: null,
input_results: {
results: [],
error: null,
},
trigger_results: {
PgxaZXoB3e90jg4z8ga8: {
name: '',
triggered: true,
error: null,
action_results: {
PwxaZXoB3e90jg4z8ga8: {
id: 'PwxaZXoB3e90jg4z8ga8',
name: 'sdlkmdslfdsfds',
output: {},
throttled: false,
executionTime: 1625198097035,
error: error,
},
},
},
},
},
};

const errorInTriggerResponse = {
resp: {
monitor_name: 'TestMonitor',
period_start: 1625198037084,
period_end: 1625198097084,
error: null,
input_results: {
results: [],
error: null,
},
trigger_results: {
PgxaZXoB3e90jg4z8ga8: {
name: '',
triggered: true,
error: error,
action_results: {
PwxaZXoB3e90jg4z8ga8: {
id: 'PwxaZXoB3e90jg4z8ga8',
name: 'sdlkmdslfdsfds',
output: {},
throttled: false,
executionTime: 1625198097035,
error: null,
},
},
},
},
},
};

describe('checkForError', () => {
test('return no error in response', () => {
expect(checkForError(noErrorInResponse, null)).toBe(null);
});

test('return error in action response', () => {
expect(checkForError(errorInActionResponse, null)).toBe(error);
});

test('return error in trigger response', () => {
expect(checkForError(errorInTriggerResponse, null)).toBe(error);
});
});

0 comments on commit 565d365

Please sign in to comment.