-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error toast message while configuring trigger while creating a m…
…onitor (#1178) * fix: error toast message while configuring trigger while creating a monitor Signed-off-by: vikhy-aws <[email protected]> * fix: gracefully handle exceptions related to config index not created Signed-off-by: vikhy-aws <[email protected]> * fix: gracefully handle exceptions related to config index not created Signed-off-by: vikhy-aws <[email protected]> * fix: fix unit test to conform to code changes Signed-off-by: vikhy-aws <[email protected]> --------- Signed-off-by: vikhy-aws <[email protected]>
- Loading branch information
Showing
4 changed files
with
245 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
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: false, | ||
totalMonitors: 0, | ||
monitors: [], | ||
message: "Config index will be created automatically when the monitor is created" | ||
}, | ||
}); | ||
}); | ||
|
||
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" | ||
}, | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters