I was not able to fetch the corresponding dependency for azure devops so with the help of chatgpt I pretty much tried with below code.
import java.io.IOException;import java.util.*;import org.apache.poi.xssf.usermodel.*;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import okhttp3.MediaType;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;public class bulkupload { public static void main(String[] args) throws IOException, InterruptedException { List<TestCases> testcases=readTestCasesFromExcel("src/test/resources/testcase.xlsx"); String pat="personal access token"; uploadTestCases("project", "planID", testcases, pat); System.out.println("Test Case Uploaded successfully"); } public static void uploadTestCases(String projectName, String testPlanId, List<TestCases> testCases, String accessToken) throws IOException, InterruptedException { final String baseUrl = "https://dev.azure.com/organization/"; // Replace with your organization URL final String apiVersion = "api-version=1.0"; //"api-version=6.0-preview.1"; OkHttpClient client = new OkHttpClient(); for (TestCases testCase : testCases) { String jsonBody = convertObjectToJson(testCase); String url = String.format("%s%s/_apis/test/plans/%s/suites/%s/testcases?%s", baseUrl, projectName, testPlanId, "suiteID", apiVersion); Request request = new Request.Builder() .url(url) .addHeader("Authorization", "Bearer " + accessToken) .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Failed to upload test case: " + response); Thread.sleep(1000); // Add a delay to avoid overwhelming API rate limits } } public static List<TestCases> readTestCasesFromExcel(String filePath) throws IOException { ArrayList<TestCases> testCases = new ArrayList<TestCases>(); XSSFWorkbook workbook = new XSSFWorkbook(filePath); XSSFSheet sheet = workbook.getSheetAt(0); // Assuming test cases in first sheet for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { XSSFRow row = sheet.getRow(rowNum); if (row == null) continue; String Title = row.getCell(1).getStringCellValue(); String StepAction = row.getCell(2).getStringCellValue(); String StepExpectedResult = row.getCell(3).getStringCellValue(); String AssignedTo = row.getCell(4).getStringCellValue(); String State = row.getCell(5).getStringCellValue(); // Add other relevant fields based on your Excel structure TestCases testCase = new TestCases(Title, StepAction, StepExpectedResult, AssignedTo, State); testCases.add(testCase); } workbook.close(); return testCases; } public static String convertObjectToJson(Object object) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(object); }}
Another java class for getting the data in json format
public class TestCases { private String title; private String stepaction; private String stepexpectedresult; private String assignedto; private String state; // Add other fields like steps, expected results etc. public TestCases(String title, String stepaction, String stepexpectedresult, String assignedto, String state) { this.title = title; this.stepaction = stepaction; this.stepexpectedresult = stepexpectedresult; this.assignedto = assignedto; this.state = state; } // Getters and Setters // Getters public String getTitle() { return title; } public String getStepaction() { return stepaction; } public String getStepexpectedresult() { return stepexpectedresult; } public String getAssignedto() { return assignedto; } public String getState() { return state; } // Setters public void setTitle(String title) { this.title = title; } public void setStepaction(String stepaction) { this.stepaction = stepaction; } public void setStepexpectedresult(String stepexpectedresult) { this.stepexpectedresult = stepexpectedresult; } public void setAssignedto(String assignedto) { this.assignedto = assignedto; } public void setState(String state) { this.state = state; }}
Tried running the above code but got the below error
Exception in thread "main" java.io.IOException: Failed to upload test case: Response{protocol=http/1.1, code=405, message=Method Not Allowed, url=https://dev.azure.com/organization/project/_apis/test/plans/planID/suites/suiteID/testcases?api-version=1.0} at bulkupload.uploadTestCases(bulkupload.java:43) at bulkupload.main(bulkupload.java:20)
I understood that I'm getting error code=405 that this url is not for post type of http request.So please provide the correct url to (POST) upload test case by creating a test suite by passing user story ID as argument and by running query to fin the user story and attaching the test suite to it.
If there is an alternate solution is also appreciated. Also need the maven dependency for azure devops.