需要帮助找到导致从 alm 中提取的错误

问题描述 投票:0回答:3

这是为使用 api 密钥从 alm 中提取测试用例而创建的 java 程序 但不确定如何在 HP ALM 中配置 API 密钥 有人可以解释一下如何设置 api 密钥吗 是为使用 api 密钥从 alm 中提取测试用例而创建的 java 程序 但不确定如何在 HP ALM 中配置 API 密钥 有人可以向我解释一下如何设置 api keyis java 程序,该程序是为使用 api 密钥从 alm 中提取测试用例而创建的 但不确定如何在 HP ALM 中配置 API 密钥 有人可以向我解释一下如何设置 api keyis java 程序,该程序是为使用 api 密钥从 alm 中提取测试用例而创建的 但不确定如何在 HP ALM 中配置 API 密钥 有人可以向我解释一下如何设置 api keyis java 程序,该程序是为使用 api 密钥从 alm 中提取测试用例而创建的 但不确定如何在 HP ALM 中配置 API 密钥 有人可以解释一下如何设置 api 密钥

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ALMTestCaseExtractor {

    public static void main(String[] args) {
        String almURL = "http://your_alm_server:port/qcbin";
        String almDomain = "your_domain";
        String almProject = "your_project";
        String almAPIKey = "your_api_key";
        String almFolderPath = "your_folder_path"; // Example: "Subject\Folder1\Folder2"
        String excelFilePath = "path/to/excel/file.xlsx";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // Set ALM API key
            String authorizationHeader = "Bearer " + almAPIKey;

            // Retrieve test cases from ALM
            String testCasesUrl = almURL + "/rest/domains/" + almDomain + "/projects/" + almProject
                    + "/test-instances?query=" + almFolderPath;
            HttpGet httpGet = new HttpGet(testCasesUrl);
            httpGet.setHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);
            httpGet.setHeader(HttpHeaders.ACCEPT, "application/json");

            String responseString = EntityUtils.toString(httpClient.execute(httpGet).getEntity());
            List<Map<String, Object>> testCases = extractTestCasesFromResponse(responseString);

            // Create Excel workbook
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Test Cases");

            // Create headers
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("Test Case Name");
            headerRow.createCell(1).setCellValue("Description");
            headerRow.createCell(2).setCellValue("Steps");
            headerRow.createCell(3).setCellValue("Expected Results");

            // Populate test case data
            int rowIndex = 1;
            for (Map<String, Object> testCase : testCases) {
                Row row = sheet.createRow(rowIndex++);
                row.createCell(0).setCellValue((String) testCase.get("name"));
                row.createCell(1).setCellValue((String) testCase.get("description"));
                row.createCell(2).setCellValue((String) testCase.get("steps"));
                row.createCell(3).setCellValue((String) testCase.get("expected-results"));
            }

            // Save Excel file
            try (OutputStream outputStream = new FileOutputStream(excelFilePath)) {
                workbook.write(outputStream);
                System.out.println("Test cases exported to Excel successfully.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<Map<String, Object>> extractTestCasesFromResponse(String responseString) {
        // Parse the JSON response and extract the relevant test case information
        // You can use your preferred JSON parsing library for this task
        // Here, we're assuming the response is in a specific JSON structure
        // Modify the code accordingly if your ALM API response structure is different
        // This is just a sample implementation
        // You may need to adjust it to fit your requirements
        // ...
        // Your JSON parsing logic goes here
        // ...
        return null; // Modify the return statement to return the actual test case data
    }
}


this is java program created to extract testcases from alm using api key
but not sure how to configure API key in HP ALM
can someone explain me setting up api key
is java program created to extract testcases from alm using api key
but not sure how to configure API key in HP ALM
can someone explain me setting up api keyis java program created to extract testcases from alm using api key
but not sure how to configure API key in HP ALM
can someone explain me setting up api keyis java program created to extract testcases from alm using api key
but not sure how to configure API key in HP ALM
can someone explain me setting up api keyis java program created to extract testcases from alm using api key
but not sure how to configure API key in HP ALM
can someone explain me setting up api key
alm
3个回答
0
投票
latest
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class ReadTestCasesFromALM {

    private static final String ALM_URL = "http://your_alm_server:port/qcbin";
    private static final String ALM_DOMAIN = "your_domain";
    private static final String ALM_PROJECT = "your_project";
    private static final String ALM_USERNAME = "your_username";
    private static final String ALM_PASSWORD = "your_password";

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Please provide the folder path as an argument.");
            return;
        }

        String folderPath = args[0];
        try (CloseableHttpClient httpClient = createHttpClientWithCredentials()) {

            // Encode the folder path
            String encodedFolderPath = URLEncoder.encode(folderPath, StandardCharsets.UTF_8.toString());

            // Set up the request
            String url = ALM_URL + "/rest/domains/" + ALM_DOMAIN + "/projects/" + ALM_PROJECT + "/test-instances";
            String query = "{test-folder.path[" + encodedFolderPath + "];recursive}";
            url += "?query=" + query;

            HttpGet request = new HttpGet(url);
            request.setHeader(HttpHeaders.ACCEPT, "application/json");

            // Send the request and process the response
            HttpResponse response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == 200) {
                String responseBody = EntityUtils.toString(response.getEntity());
                processTestCases(responseBody);
            } else {
                System.out.println("Failed to retrieve test cases. Status code: " + statusCode);
            }
        } catch (IOException e) {
            System.out.println("Error executing the request: " + e.getMessage());
        }
    }

    private static CloseableHttpClient createHttpClientWithCredentials() {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(ALM_USERNAME, ALM_PASSWORD));

        return HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();
    }

    private static void processTestCases(String responseBody) {
        JSONArray entities = new JSONObject(responseBody).getJSONArray("entities");

        for (int i = 0; i < entities.length(); i++) {
            JSONObject entity = entities.getJSONObject(i);
            JSONObject fields = entity.getJSONObject("Fields");

            String testCaseId = fields.getJSONObject("id").getString("values");
            String testCaseName = fields.getJSONObject("name").getString("values");

            // Process the test case
            System.out.println("Test Case ID: " + testCaseId);
            System.out.println("Test Case Name: " + testCaseName);
            System.out.println("-------------------------------------------");
        }
    }
}

-1
投票
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExtractTestCasesFromALM {

    private static final String ALM_URL = "http://your_alm_server:port/qcbin";
    private static final String ALM_DOMAIN = "your_domain";
    private static final String ALM_PROJECT = "your_project";
    private static final String ALM_FOLDER_PATH = "your_folder_path"; // Example: "Subject\Folder1\Folder2"
    private static final String API_KEY = "your_api_key";
    private static final String API_SECRET = "your_api_secret";
    private static final String EXCEL_FILE_PATH = "path/to/excel/file.xlsx";

    public static void main(String[] args) throws IOException {
        // Create an HTTP client
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // Set up the request
        String url = ALM_URL + "/rest/domains/" + ALM_DOMAIN + "/projects/" + ALM_PROJECT + "/test-instances";
        String query = "{test-set.folder-path[" + ALM_FOLDER_PATH + "]}";
        url += "?query=" + query;

        HttpGet request = new HttpGet(url);
        request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodeCredentials(API_KEY, API_SECRET));

        // Send the request and process the response
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == 200) {
            String responseBody = EntityUtils.toString(response.getEntity());
            List<TestInstance> testInstances = extractTestInstances(responseBody);

            // Export the test instances to Excel
            exportToExcel(testInstances);
        } else {
            System.out.println("Failed to retrieve test instances. Status code: " + statusCode);
        }

        // Close the HTTP client
        httpClient.close();
    }

    private static String encodeCredentials(String apiKey, String apiSecret) {
        String credentials = apiKey + ":" + apiSecret;
        return java.util.Base64.getEncoder().encodeToString(credentials.getBytes());
    }

    private static List<TestInstance> extractTestInstances(String responseBody) {
        List<TestInstance> testInstances = new ArrayList<>();

        JSONObject responseJson = new JSONObject(responseBody);
        JSONArray entities = responseJson.getJSONArray("entities");

        for (int i = 0; i < entities.length(); i++) {
            JSONObject entity = entities.getJSONObject(i);
            JSONObject fields = entity.getJSONObject("Fields");

            String name = fields.getJSONObject("name").getString("values");
            String description = fields.getJSONObject("description").getString("values");
            String steps = fields.getJSONObject("steps").getString("values");
            String expectedResults = fields.getJSONObject("expected-results").getString("values");

            TestInstance testInstance = new TestInstance(name, description, steps, expectedResults);
            testInstances.add(testInstance);
        }

        return testInstances;
    }

    private static void exportToExcel(List<TestInstance> testInstances) {
        // Code to export the test instances to Excel
        // You can use a library like Apache PO

-1
投票
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpHeaders;
        import org.apache.http.HttpResponse;
        import org.apache.http.auth.AuthScope;
        import org.apache.http.auth.UsernamePasswordCredentials;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.impl.client.CloseableHttpClient;
        import org.apache.http.impl.client.HttpClients;
        import org.apache.http.util.EntityUtils;
        import org.json.JSONArray;
        import org.json.JSONObject;
        
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.nio.charset.StandardCharsets;
        import java.util.ArrayList;
        import java.util.Base64;
        import java.util.List;
        
        public class ExtractTestCasesFromALM {
        
            private static final String ALM_URL = "http://your_alm_server:port/qcbin";
            private static final String ALM_DOMAIN = "your_domain";
            private static final String ALM_PROJECT = "your_project";
            private static final String ALM_FOLDER_PATH = "your_folder_path"; // Example: "Subject\Folder1\Folder2"
            private static final String ALM_USERNAME = "your_username";
            private static final String ALM_PASSWORD = "your_password";
            private static final String EXCEL_FILE_PATH = "path/to/excel/file.xlsx";
        
            public static void main(String[] args) throws IOException {
                // Create an HTTP client with basic authentication
                CloseableHttpClient httpClient = HttpClients.custom()
                        .setDefaultCredentialsProvider(createCredentialsProvider())
                        .build();
        
                // Set up the request
                String url = ALM_URL + "/rest/domains/" + ALM_DOMAIN + "/projects/" + ALM_PROJECT + "/test-instances";
                String query = "{test-set.folder-path[" + ALM_FOLDER_PATH + "]}";
                url += "?query=" + query;
        
                HttpGet request = new HttpGet(url);
                request.setHeader(HttpHeaders.ACCEPT, "application/json");
        
                // Send the request and process the response
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
        
                if (statusCode == 200) {
                    String responseBody = EntityUtils.toString(response.getEntity());
                    List<TestInstance> testInstances = extractTestInstances(responseBody);
        
                    // Export the test instances to Excel
                    exportToExcel(testInstances);
                } else {
                    System.out.println("Failed to retrieve test instances. Status code: " + statusCode);
                }
        
                // Close the HTTP client
                httpClient.close();
            }
        
            private static CredentialsProvider createCredentialsProvider() {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(ALM_USERNAME, ALM_PASSWORD));
                return credentialsProvider;
            }
        
            private static List<TestInstance> extractTestInstances(String responseBody) {
                List<TestInstance> testInstances = new ArrayList<>();
        
                JSONObject responseJson = new JSONObject(responseBody);
                JSONArray entities = responseJson.getJSONArray("entities");
        
                for (int i = 0; i < entities.length(); i++) {
                    JSONObject entity = entities.getJSONObject(i);
                    JSONObject fields = entity.getJSONObject("Fields");
        
                    String name = fields.getJSONObject("name").getString("values");
                    String description = fields.getJSONObject("description").getString("values");
                    String steps = fields.getJSONObject("steps").getString("values");
                    String expectedResults = fields.getJSONObject("expected-results").getString("values");
        
                    TestInstance testInstance = new TestInstance(name, description, steps, expectedResults);
                    testInstances.add(testInstance);
                }
        
                return testInstances;
            }
        
            private static void exportToExcel(List<TestInstance> testInstances, String filePath) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Test Cases");
    
            // Create headers
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("Name");
            headerRow.createCell(1).setCellValue("Description");
            headerRow.createCell(2).setCellValue("Steps");
            headerRow.createCell(3).setCellValue("Expected Results");
    
            // Populate data
            int rowNum = 1;
            for (TestInstance testInstance : testInstances) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(testInstance.getName());
                row.createCell(1).setCellValue(testInstance.getDescription());
                row.createCell(2).setCellValue(testInstance.getSteps());
                row.createCell(3).setCellValue(testInstance.getExpectedResults());
            }
    
            // Auto-size columns
            for (int i = 0; i < 4; i++) {
                sheet.autoSizeColumn(i);
            }
    
            // Write the workbook to the Excel file
            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
                workbook.write(outputStream);
            }
    
            System.out.println("Test cases exported to Excel successfully!");
        } catch (IOException e) {
            System.out.println("Error exporting test cases to Excel: " + e.getMessage());
        }
    }
    In this implementation, we create a new XSSFWorkbook instance to represent the Excel workbook. Then, we create a sheet named "Test Cases" and add headers for the test case fields. We iterate over the list of TestIns


public class TestInstance {
    private String name;
    private String description;
    private String steps;
    private String expectedResults;

    public TestInstance(String name, String description, String steps, String expectedResults) {
        this.name = name;
        this.description = description;
        this.steps = steps;
        this.expectedResults = expectedResults;
    }

    // Getters and setters

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSteps() {
        return steps;
    }

    public void setSteps(String steps) {
        this.steps = steps;
    }

    public String getExpectedResults() {
        return expectedResults;
    }

    public void setExpectedResults(String expectedResults) {
        this.expectedResults = expectedResults;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.