-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NP] Graph: get rid of saved objects class wrapper (#59917)
* Implement find saved workspace * Implement get saved workspace * Create helper function as applyESResp * Fix eslint * Implement savedWorkspaceLoader.get() * Implement deleteWS * Implement saveWS * Remove applyESRespUtil * Refactoring * Refactoring * Remove savedWorkspaceLoader * Update unit test * Fix merge conflicts * Add unit tests for saveWithConfirmation * Fix TS * Move saveWithConfirmation to a separate file Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
3cb7053
commit 78e0bdf
Showing
20 changed files
with
478 additions
and
180 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
93 changes: 93 additions & 0 deletions
93
src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.test.ts
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,93 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License 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 { SavedObjectAttributes, SavedObjectsCreateOptions, OverlayStart } from 'kibana/public'; | ||
import { SavedObjectsClientContract } from '../../../../../core/public'; | ||
import { saveWithConfirmation } from './save_with_confirmation'; | ||
import * as deps from './confirm_modal_promise'; | ||
import { OVERWRITE_REJECTED } from '../../constants'; | ||
|
||
describe('saveWithConfirmation', () => { | ||
const savedObjectsClient: SavedObjectsClientContract = {} as SavedObjectsClientContract; | ||
const overlays: OverlayStart = {} as OverlayStart; | ||
const source: SavedObjectAttributes = {} as SavedObjectAttributes; | ||
const options: SavedObjectsCreateOptions = {} as SavedObjectsCreateOptions; | ||
const savedObject = { | ||
getEsType: () => 'test type', | ||
title: 'test title', | ||
displayName: 'test display name', | ||
}; | ||
|
||
beforeEach(() => { | ||
savedObjectsClient.create = jest.fn(); | ||
jest.spyOn(deps, 'confirmModalPromise').mockReturnValue(Promise.resolve({} as any)); | ||
}); | ||
|
||
test('should call create of savedObjectsClient', async () => { | ||
await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); | ||
expect(savedObjectsClient.create).toHaveBeenCalledWith( | ||
savedObject.getEsType(), | ||
source, | ||
options | ||
); | ||
}); | ||
|
||
test('should call confirmModalPromise when such record exists', async () => { | ||
savedObjectsClient.create = jest | ||
.fn() | ||
.mockImplementation((type, src, opt) => | ||
opt && opt.overwrite ? Promise.resolve({} as any) : Promise.reject({ res: { status: 409 } }) | ||
); | ||
|
||
await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); | ||
expect(deps.confirmModalPromise).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.any(String), | ||
expect.any(String), | ||
overlays | ||
); | ||
}); | ||
|
||
test('should call create of savedObjectsClient when overwriting confirmed', async () => { | ||
savedObjectsClient.create = jest | ||
.fn() | ||
.mockImplementation((type, src, opt) => | ||
opt && opt.overwrite ? Promise.resolve({} as any) : Promise.reject({ res: { status: 409 } }) | ||
); | ||
|
||
await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); | ||
expect(savedObjectsClient.create).toHaveBeenLastCalledWith(savedObject.getEsType(), source, { | ||
overwrite: true, | ||
...options, | ||
}); | ||
}); | ||
|
||
test('should reject when overwriting denied', async () => { | ||
savedObjectsClient.create = jest.fn().mockReturnValue(Promise.reject({ res: { status: 409 } })); | ||
jest.spyOn(deps, 'confirmModalPromise').mockReturnValue(Promise.reject()); | ||
|
||
expect.assertions(1); | ||
await expect( | ||
saveWithConfirmation(source, savedObject, options, { | ||
savedObjectsClient, | ||
overlays, | ||
}) | ||
).rejects.toThrow(OVERWRITE_REJECTED); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.ts
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,87 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License 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 { get } from 'lodash'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { | ||
SavedObjectAttributes, | ||
SavedObjectsCreateOptions, | ||
OverlayStart, | ||
SavedObjectsClientContract, | ||
} from 'kibana/public'; | ||
import { OVERWRITE_REJECTED } from '../../constants'; | ||
import { confirmModalPromise } from './confirm_modal_promise'; | ||
|
||
/** | ||
* Attempts to create the current object using the serialized source. If an object already | ||
* exists, a warning message requests an overwrite confirmation. | ||
* @param source - serialized version of this object what will be indexed into elasticsearch. | ||
* @param savedObject - a simple object that contains properties title and displayName, and getEsType method | ||
* @param options - options to pass to the saved object create method | ||
* @param services - provides Kibana services savedObjectsClient and overlays | ||
* @returns {Promise} - A promise that is resolved with the objects id if the object is | ||
* successfully indexed. If the overwrite confirmation was rejected, an error is thrown with | ||
* a confirmRejected = true parameter so that case can be handled differently than | ||
* a create or index error. | ||
* @resolved {SavedObject} | ||
*/ | ||
export async function saveWithConfirmation( | ||
source: SavedObjectAttributes, | ||
savedObject: { | ||
getEsType(): string; | ||
title: string; | ||
displayName: string; | ||
}, | ||
options: SavedObjectsCreateOptions, | ||
services: { savedObjectsClient: SavedObjectsClientContract; overlays: OverlayStart } | ||
) { | ||
const { savedObjectsClient, overlays } = services; | ||
try { | ||
return await savedObjectsClient.create(savedObject.getEsType(), source, options); | ||
} catch (err) { | ||
// record exists, confirm overwriting | ||
if (get(err, 'res.status') === 409) { | ||
const confirmMessage = i18n.translate( | ||
'savedObjects.confirmModal.overwriteConfirmationMessage', | ||
{ | ||
defaultMessage: 'Are you sure you want to overwrite {title}?', | ||
values: { title: savedObject.title }, | ||
} | ||
); | ||
|
||
const title = i18n.translate('savedObjects.confirmModal.overwriteTitle', { | ||
defaultMessage: 'Overwrite {name}?', | ||
values: { name: savedObject.displayName }, | ||
}); | ||
const confirmButtonText = i18n.translate('savedObjects.confirmModal.overwriteButtonLabel', { | ||
defaultMessage: 'Overwrite', | ||
}); | ||
|
||
return confirmModalPromise(confirmMessage, title, confirmButtonText, overlays) | ||
.then(() => | ||
savedObjectsClient.create(savedObject.getEsType(), source, { | ||
overwrite: true, | ||
...options, | ||
}) | ||
) | ||
.catch(() => Promise.reject(new Error(OVERWRITE_REJECTED))); | ||
} | ||
return await Promise.reject(err); | ||
} | ||
} |
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
Oops, something went wrong.