diff --git a/cypress/lib/test-fixture-handler.js b/cypress/lib/test-fixture-handler.js new file mode 100644 index 00000000000..e653aa6ad17 --- /dev/null +++ b/cypress/lib/test-fixture-handler.js @@ -0,0 +1,56 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export class TestFixtureHandler { + constructor(inputTestRunner, openSearchUrl = 'localhost:9200') { + this.testRunner = inputTestRunner; + this.openSearchUrl = openSearchUrl; + } + + // TODO: Migrate legacy methods from test library + + importMapping(filename) { + return cy.readFile(filename).then((mappingData) => { + const targetIndex = this._extractIndexNameFromPath(filename); + + return cy.request({ + method: 'PUT', + url: `${this.openSearchUrl}/${targetIndex}`, + headers: { + 'Content-Type': 'application/json', + }, + body: mappingData, + failOnStatusCode: false, + }); + }); + } + + importData(filename) { + return cy.readFile(filename, 'utf8').then((content) => { + return cy + .request({ + method: 'POST', + url: `${this.openSearchUrl}/_bulk`, + headers: { + 'Content-Type': 'application/x-ndjson', + }, + body: content, + failOnStatusCode: false, + }) + .then(() => { + return cy.request({ + method: 'POST', + url: `${this.openSearchUrl}/_all/_refresh`, + }); + }); + }); + } + + _extractIndexNameFromPath(filepath) { + const filename = filepath.split('/').pop(); + const indexName = filename.split('.')[0]; + return indexName; + } +}