Skip to content

Commit

Permalink
fix: error toast message while configuring trigger while creating a m…
Browse files Browse the repository at this point in the history
…onitor
  • Loading branch information
vikhy-aws committed Dec 18, 2024
1 parent aec20bd commit 2efda3d
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 3 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.22.9",
"@elastic/elastic-eslint-config-kibana": "link:../../packages/opensearch-eslint-config-opensearch-dashboards",
"@elastic/eslint-import-resolver-kibana": "link:../../packages/osd-eslint-import-resolver-opensearch-dashboards",
"cypress": "9.5.4",
"cypress": "12.17.4",
"husky": "^8.0.0",
"lint-staged": "^10.2.0",
"@types/react": "^16.14.23"
Expand Down Expand Up @@ -67,5 +67,6 @@
},
"engines": {
"yarn": "^1.21.1"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
6 changes: 5 additions & 1 deletion server/services/DestinationsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ export default class DestinationsService extends MDSEnabledClientService {
},
});
} catch (err) {
// Indices will be created when the monitor is created.
if (isIndexNotFoundError(err)) {
return res.ok({
body: { ok: false, resp: {} },
body: {
ok: true,
resp: "Indices will be configured when the monitor is created: [.opendistro-alerting-config]"
},
});
}
return res.ok({
Expand Down
186 changes: 186 additions & 0 deletions server/services/DestinationsService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import DestinationsService from "./DestinationsService";

describe("Test DestinationsService -- getDestinations", () => {
let destinationsService;
let mockContext;
let mockReq;
let mockRes;
let mockClient;

beforeEach(() => {
mockClient = jest.fn();

mockContext = {};

mockRes = {
ok: jest.fn().mockReturnValue({ body: {} }),
};

destinationsService = new DestinationsService();
destinationsService.getClientBasedOnDataSource = jest.fn().mockReturnValue(mockClient);

});

describe("Test getDestinations", () => {

test("should successfully get destinations list -- name as sort string", async () => {
const mockReq = {
query: {
from: 0,
size: 20,
search: "",
sortDirection: "desc",
sortField: "name",
type: "ALL",
},
};

const mockResponse = {
destinations: [{
id: "1",
name: "Sample Destination",
schema_version: 1,
seq_no: 1,
primary_term: 1,
}],
totalDestinations: 1,
};
mockClient.mockResolvedValueOnce(mockResponse);

await destinationsService.getDestinations(mockContext, mockReq, mockRes);

expect(mockClient).toHaveBeenCalledWith("alerting.searchDestinations", {
sortString: "destination.name.keyword",
sortOrder: "desc",
startIndex: 0,
size: 20,
searchString: "",
destinationType: "ALL",
});
expect(mockRes.ok).toHaveBeenCalledWith({
body: {
ok: true,
destinations: [{
id: "1",
name: "Sample Destination",
schema_version: 1,
seq_no: 1,
primary_term: 1,
version: 1,
ifSeqNo: 1,
ifPrimaryTerm: 1,
}],
totalDestinations: 1,
},
});
});

test("should successfully get destinations list -- type as sort string", async () => {
const mockReq = {
query: {
from: 0,
size: 20,
search: "",
sortDirection: "desc",
sortField: "type",
type: "ALL",
},
};

const mockResponse = {
destinations: [{
id: "1",
name: "Sample Destination",
schema_version: 1,
seq_no: 1,
primary_term: 1,
}],
totalDestinations: 1,
};
mockClient.mockResolvedValueOnce(mockResponse);

await destinationsService.getDestinations(mockContext, mockReq, mockRes);

expect(mockClient).toHaveBeenCalledWith("alerting.searchDestinations", {
sortString: "destination.type",
sortOrder: "desc",
startIndex: 0,
size: 20,
searchString: "",
destinationType: "ALL",
});
expect(mockRes.ok).toHaveBeenCalledWith({
body: {
ok: true,
destinations: [{
id: "1",
name: "Sample Destination",
schema_version: 1,
seq_no: 1,
primary_term: 1,
version: 1,
ifSeqNo: 1,
ifPrimaryTerm: 1,
}],
totalDestinations: 1,
},
});
});

test("should handle index not found error", async () => {
const mockReq = {
query: {
from: 0,
size: 20,
search: "",
sortDirection: "desc",
sortField: "name",
type: "ALL",
},
};
const error = new Error();
error.statusCode = 404;
error.body = {
error: {
reason: 'Configured indices are not found: [.opendistro-alerting-config]'
}
};
mockClient.mockRejectedValueOnce(error);

await destinationsService.getDestinations(mockContext, mockReq, mockRes);

expect(mockRes.ok).toHaveBeenCalledWith({
body: {
ok: true,
resp: "Indices will be configured when the monitor is created: [.opendistro-alerting-config]"
},
});
});

test("should handle other errors", async () => {
const mockReq = {
query: {
from: 0,
size: 20,
search: "",
sortDirection: "desc",
sortField: "name",
type: "ALL",
},
};

const error = new Error("Some error");
mockClient.mockRejectedValueOnce(error);

await destinationsService.getDestinations(mockContext, mockReq, mockRes);

expect(mockRes.ok).toHaveBeenCalledWith({
body: {
ok: false,
err: "Some error"
},
});
});

});
});

0 comments on commit 2efda3d

Please sign in to comment.