Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.stdlib.http.compiler;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.io.IOException;
Expand All @@ -37,7 +38,17 @@ public class OpenAPISpecGenerationTest {
.toAbsolutePath();
private static final Path DISTRIBUTION_PATH = Paths.get("../", "target", "ballerina-runtime")
.toAbsolutePath();

private static final String[] OPENAPI_SAMPLE_PACKAGES = {
"sample_package_20", "sample_package_42", "sample_package_43", "sample_package_44",
"sample_package_45", "sample_package_46", "sample_package_47", "sample_package_48", "sample_package_49"
};

@AfterMethod(alwaysRun = true)
public void cleanOpenApiSampleTargets() throws IOException {
for (String pkg : OPENAPI_SAMPLE_PACKAGES) {
deleteDirectories(RESOURCE_DIRECTORY.resolve(pkg));
}
}
Comment on lines +46 to +51

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup is only done in @AfterMethod. If a developer runs a single test method (or the suite is re-run in a workspace that already has stale target/openapi from a previous run), tests that assert the spec is not generated (e.g., testSpecGenerationWithoutFlag, testSpecGenerationWithCompilationErrors) can still fail because the old OpenAPI artifacts exist before the test executes. Consider also cleaning the relevant package(s) in a @BeforeMethod(alwaysRun = true) (or cleaning at the start of each test) so each test is hermetic regardless of prior filesystem state.

Copilot uses AI. Check for mistakes.

@Test
public void testSpecGenerationWithSimpleService() throws Exception {
Expand All @@ -46,7 +57,6 @@ public void testSpecGenerationWithSimpleService() throws Exception {
Path actualFile = projectDirPath.resolve("target/openapi").resolve("service_openapi.yaml");
Path expectedFile = RESOURCE_DIRECTORY.resolve("../yaml_files").resolve("service_openapi_3.yaml");
verifySpecContent(actualFile, expectedFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -55,7 +65,6 @@ public void testSpecGenerationWithInvalidServicePath() throws Exception {
executeBallerinaCommand(projectDirPath, true);
Path openApiDir = projectDirPath.resolve("target/openapi");
Assert.assertTrue(Files.notExists(openApiDir));
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -67,7 +76,6 @@ public void testSpecGenerationWithEmptyServicePath() throws Exception {
Path actualFile = projectDirPath.resolve("target/openapi").resolve("service_352312370_openapi.yaml");
Path expectedFile = RESOURCE_DIRECTORY.resolve("../yaml_files").resolve("service_openapi_2.yaml");
verifySpecContent(actualFile, expectedFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -77,16 +85,14 @@ public void testConstructFileNameWithRegularServicePath() throws Exception {
Path actualFile = projectDirPath.resolve("target/openapi").resolve("userservice_openapi.yaml");
Path expectedFile = RESOURCE_DIRECTORY.resolve("../yaml_files").resolve("service_openapi_4.yaml");
verifySpecContent(actualFile, expectedFile);
deleteDirectories(projectDirPath);
}

@Test
public void testSpecGenerationWithInvalidService() throws Exception {
Path projectDirPath = RESOURCE_DIRECTORY.resolve("sample_package_49");
executeBallerinaCommand(projectDirPath, true);
Path openApiDir = projectDirPath.resolve("target/openapi");
Assert.assertTrue(Files.notExists(openApiDir), "OpenAPI directory should exist");
deleteDirectories(projectDirPath);
Assert.assertTrue(Files.notExists(openApiDir), "OpenAPI directory should not exist");
}

@Test
Expand All @@ -96,7 +102,6 @@ public void testSpecGeneration() throws Exception {
Path actualFile = projectDirPath.resolve("target/openapi").resolve("service_openapi.yaml");
Path expectedFile = RESOURCE_DIRECTORY.resolve("../yaml_files").resolve("service_openapi_1.yaml");
verifySpecContent(actualFile, expectedFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -107,7 +112,6 @@ public void testSpecGenerationInComplexServices() throws Exception {
.resolve("api_v1_openapi.yaml");
Path expectedFile = RESOURCE_DIRECTORY.resolve("../yaml_files").resolve("complex_openapi.yaml");
verifySpecContent(actualFile, expectedFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -116,7 +120,6 @@ public void testSpecGenerationWithoutFlag() throws Exception {
executeBallerinaCommand(projectDirPath, false);
Path yamlFile = projectDirPath.resolve("target/openapi").resolve("service_openapi.yaml");
Assert.assertTrue(Files.notExists(yamlFile), "OpenAPI spec file should not be generated: " + yamlFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -125,7 +128,6 @@ public void testSpecGenerationWithCompilationErrors() throws Exception {
executeBallerinaCommand(projectDirPath, false);
Path yamlFile = projectDirPath.resolve("target/openapi").resolve("service_openapi.yaml");
Assert.assertTrue(Files.notExists(yamlFile), "OpenAPI spec file should not be generated: " + yamlFile);
deleteDirectories(projectDirPath);
}

@Test
Expand All @@ -141,7 +143,6 @@ public void testSpecGenerationForMultipleServices() throws Exception {
.collect(Collectors.toList());
}
Assert.assertEquals(yamlFiles.size(), 5);
deleteDirectories(projectDirPath);
}

public void addJavaAgents(Map<String, String> envProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ type SuccessResponse record {|
json data?;
|};

type Caller record {|
int id;
|};

map<User> userStore = {};

service /api/v1 on new http:Listener(9090) {
Expand Down Expand Up @@ -313,19 +309,7 @@ service /api/v1 on new http:Listener(9090) {
return "done";
}

resource function get callerInf(@http:CallerInfo Caller abc) returns string {
return "Caller ID: " + abc.id.toString();
}

resource function get callerErr1(@http:CallerInfo string abc) returns string {
return "Invalid caller info type";
}

resource function post callerErr2(@http:CallerInfo @http:Payload Caller abc) returns string {
return "Invalid annotation combination";
}

resource function get callerErr3(@http:CallerInfo Caller abc) returns string {
return "Caller test: " + abc.id.toString();
resource function get callerInf(@http:CallerInfo http:Caller abc) returns error? {
return;
}
}
Loading
Loading