Verify that the API correctly handles database backups and restores and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.response.Response;3import static io.restassured.RestAssured.*;4import static org.hamcrest.Matchers.*;56public class DatabaseBackupRestoreCheck {78 //Assuming the API endpoint and authentication details9 String baseURL = "https://example.com/api/backup_restore";10 String username = "testuser";11 String password = "testpassword";1213 @Test14 public void testDatabaseBackupRestore() {15 //Assuming the backup file location16 String backupFilePath = "/opt/database/backup.sql";1718 //Request for backup19 given()20 .auth().preemptive().basic(username, password)21 .contentType("application/json")22 .body("{\"action\": \"backup\", \"file\": \""+backupFilePath+"\"}")23 .when()24 .post(baseURL)25 .then()26 .statusCode(200)27 .body("message", equalTo("Database backup successful"));2829 //Assuming the restore file location30 String restoreFilePath = "/opt/database/restore.sql";3132 //Request for restore33 given()34 .auth().preemptive().basic(username, password)35 .contentType("application/json")36 .body("{\"action\": \"restore\", \"file\": \""+restoreFilePath+"\"}")37 .when()38 .post(baseURL)39 .then()40 .statusCode(200)41 .body("message", equalTo("Database restore successful"));42 }43 44 //Commented code to connect to remote client with desired capabilities45 /*46 public void testDatabaseBackupRestoreOnRemoteClient() {47 DesiredCapabilities capabilities = new DesiredCapabilities();48 capabilities.setCapability("browserName", "Chrome");49 capabilities.setCapability("version", "91.0");50 capabilities.setCapability("platform", "Windows 10"); //Assuming the remote client is on Windows 1051 capabilities.setCapability("name", "Database Backup Restore Check");52 RemoteWebDriver driver = null;53 try {54 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities); //Assuming remote client URL55 //Add the test steps here56 } catch (MalformedURLException e) {57 e.printStackTrace();58 }59 driver.quit();60 }61 */62}
Language: Javascript
1// Mocha and Chai.23// Assumptions: 4// - The database to test backup and restore exists on the backend server.5// - The API endpoint for backup and restore functionality exists and is accessible.6// - The API returns the correct HTTP status code for successful and unsuccessful backup and restore operations.7// - The API returns the correct error message for unsuccessful backup and restore operations.89describe('API Backup and Restore Testing', function() {10 let backupFilePath = '/path/to/backup/file';11 12 it('should backup database successfully', function(done) {13 // Add code here to backup database using API endpoint and store backup file in backupFilePath variable.14 // Connect to remote client with desired capabilities if running remotely. 15 // Assume backup has been successfully created and is stored at backupFilePath.16 17 chai.request('http://localhost:5000')18 .get('/backup')19 .end(function(err, res) {20 expect(res).to.have.status(200);21 expect(res.body.message).to.equal('Database backup successful');22 done();23 });24 });25 26 it('should restore database successfully', function(done) {27 // Add code here to restore database using API endpoint and backup file from backupFilePath variable.28 // Assume database has been successfully restored.29 // Connect to remote client with desired capabilities if running remotely. 3031 chai.request('http://localhost:5000')32 .post('/restore')33 .attach('backupFile', backupFilePath)34 .end(function(err, res) {35 expect(res).to.have.status(200);36 expect(res.body.message).to.equal('Database restore successful');37 done();38 });39 });4041 it('should return error message for invalid backup/restore file', function(done) {42 // Add code here to test invalid backup/restore file.43 // Assume API correctly handles invalid backup/restore files.44 // Connect to remote client with desired capabilities if running remotely. 4546 chai.request('http://localhost:5000')47 .post('/restore')48 .attach('backupFile', '/path/to/invalid/backup/file')49 .end(function(err, res) {50 expect(res).to.have.status(400);51 expect(res.body.message).to.equal('Invalid backup/restore file');52 done();53 });54 });5556 it('should return error message for failed restore operation', function(done) {57 // Add code here to test failed restore operation.58 // Assume API correctly handles failed restore operations.59 // Connect to remote client with desired capabilities if running remotely. 6061 chai.request('http://localhost:5000')62 .post('/restore')63 .attach('backupFile', '/path/to/fake/backup/file')64 .end(function(err, res) {65 expect(res).to.have.status(500);66 expect(res.body.message).to.equal('Failed to restore database');67 done();68 });69 });70});
Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing