Skip to content

Commit

Permalink
Make suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hasathcharu committed Jun 27, 2024
1 parent ae722b4 commit 1ace1b1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,21 @@ private void verifyValidInputsForHttp() {
private void ballerinaToAsyncApiWs(String fileName) {
List<AsyncApiConverterDiagnostic> errors = new ArrayList<>();
final File balFile = new File(fileName);
Path balFilePath = null;
try {
balFilePath = Paths.get(balFile.getCanonicalPath());
Path balFilePath = Paths.get(balFile.getCanonicalPath());
setOutputPathWs();
// Check service name it is mandatory
List<AsyncApiConverterDiagnostic> generationErrors = BallerinaToAsyncApiGenerator
.generateAsyncAPIDefinitionsAllService(balFilePath, targetOutputPath, service, generatedFileType,
outStream);
errors.addAll(generationErrors);
} catch (IOException e) {
DiagnosticMessages message = DiagnosticMessages.AAS_CONVERTOR_102;
ExceptionDiagnostic error = new ExceptionDiagnostic(message.getCode(),
message.getDescription(), null, e.getLocalizedMessage());
ExceptionDiagnostic error = new ExceptionDiagnostic(message.getCode(), message.getDescription(), null,
e.getLocalizedMessage());
errors.add(error);
}
getTargetOutputPathWs();
// Check service name it is mandatory
List<AsyncApiConverterDiagnostic> generationErrors = BallerinaToAsyncApiGenerator
.generateAsyncAPIDefinitionsAllService(balFilePath, targetOutputPath, service, generatedFileType,
outStream);

errors.addAll(generationErrors);
if (!errors.isEmpty()) {
for (AsyncApiConverterDiagnostic error : errors) {
if (error instanceof ExceptionDiagnostic exceptionDiagnostic) {
Expand All @@ -296,21 +295,20 @@ private void ballerinaToAsyncApiWs(String fileName) {
}

private void asyncApiToBallerinaWs(String fileName) throws IOException {
AsyncApiToBallerinaGenerator generator = new AsyncApiToBallerinaGenerator();
generator.setLicenseHeader(this.setLicenseHeaderWs());
generator.setIncludeTestFiles(this.includeTestFiles);
AsyncApiToBallerinaGenerator generator = new AsyncApiToBallerinaGenerator(this.extractLicenseHeaderWs(),
this.includeTestFiles);
final File asyncApiFile = new File(fileName);
getTargetOutputPathWs();
setOutputPathWs();
Path resourcePath = Paths.get(asyncApiFile.getCanonicalPath());
generatesClientFileWs(generator, resourcePath);
}

/**
* This util is to set the license header content which is to be added at the beginning of the ballerina files.
*/
private String setLicenseHeaderWs() {
String licenseHeader = "";
private String extractLicenseHeaderWs() {
try {
String licenseHeader;
if (this.licenseFilePath != null && !this.licenseFilePath.isBlank()) {
Path filePath = Paths.get((new File(this.licenseFilePath).getCanonicalPath()));
licenseHeader = Files.readString(Paths.get(filePath.toString()));
Expand All @@ -319,18 +317,19 @@ private String setLicenseHeaderWs() {
} else if (!licenseHeader.endsWith(LINE_SEPARATOR + LINE_SEPARATOR)) {
licenseHeader = licenseHeader + LINE_SEPARATOR;
}
return licenseHeader;
}
} catch (IOException e) {
outStream.println(String.format(MESSAGE_INVALID_LICENSE_STREAM, this.licenseFilePath, e.getMessage()));
exitError(this.exitWhenFinish);
}
return licenseHeader;
return "";
}

/**
* This util is to get the output Path.
*/
private void getTargetOutputPathWs() {
private void setOutputPathWs() {
targetOutputPath = executionPath;
if (this.outputPath != null) {
if (Paths.get(outputPath).isAbsolute()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@
public class AsyncAPIDiagnostic extends Diagnostic {
private final DiagnosticInfo diagnosticInfo;
private final Location location;
private final List<DiagnosticProperty<?>> properties;
private final String message;

public AsyncAPIDiagnostic(DiagnosticInfo diagnosticInfo, Location location, List<DiagnosticProperty<?>> properties,
Object[] args) {
public AsyncAPIDiagnostic(DiagnosticInfo diagnosticInfo, Location location, Object[] args) {
this.diagnosticInfo = diagnosticInfo;
this.location = location;
this.properties = Collections.unmodifiableList(properties);
this.message = MessageFormat.format(diagnosticInfo.messageFormat(), args);
}

Expand All @@ -69,7 +66,7 @@ public String message() {
}

public List<DiagnosticProperty<?>> properties() {
return this.properties;
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@
*/
public class AsyncApiToBallerinaGenerator {
private static final PrintStream outStream = System.err;
private String licenseHeader = "";
private boolean includeTestFiles;
private final String licenseHeader;
private final boolean includeTestFiles;

public AsyncApiToBallerinaGenerator(String licenseHeader, boolean includeTestFiles) {
this.licenseHeader = licenseHeader;
this.includeTestFiles = includeTestFiles;
}

/**
* Generates ballerina websocket client for provided Async API Definition in {@code definitionPath}.
Expand Down Expand Up @@ -213,22 +218,4 @@ private List<GenSrcFile> generateClientFiles(Path asyncApi)
}
return sourceFiles;
}

/**
* Set the content of license header.
*
* @param licenseHeader license header value received from command line.
*/
public void setLicenseHeader(String licenseHeader) {
this.licenseHeader = licenseHeader;
}

/**
* set whether to add test files or not.
*
* @param includeTestFiles value received from command line by "--with tests"
*/
public void setIncludeTestFiles(boolean includeTestFiles) {
this.includeTestFiles = includeTestFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.ballerina.tools.diagnostics.Location;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -44,7 +43,7 @@ public static AsyncAPIDiagnostic constructAsyncAPIDiagnostic(String code, String
if (location == null) {
location = new ConverterCommonUtils.NullLocation();
}
return new AsyncAPIDiagnostic(diagnosticInfo, location, Collections.emptyList(), args);
return new AsyncAPIDiagnostic(diagnosticInfo, location, args);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ExecuteTest {
@Test(description = "Generate Client for testings", enabled = false)
public void generatePathWithPathParameterTests() throws IOException, BallerinaAsyncApiExceptionWs,
FormatterException {
AsyncApiToBallerinaGenerator asyncAPIToBallerinaGenerator = new AsyncApiToBallerinaGenerator();
AsyncApiToBallerinaGenerator asyncAPIToBallerinaGenerator = new AsyncApiToBallerinaGenerator("", false);

asyncAPIToBallerinaGenerator.generateClient(
Paths.get("src/test/resources/websockets/asyncapi-to-ballerina/client/StreamResponse/" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,11 @@ public class PathParameterTests {
@Test(description = "Generate Client for path parameter has parameter name as key word - unit tests for method")
public void generatePathWithPathParameterTests() throws IOException, BallerinaAsyncApiExceptionWs,
FormatterException {
// "/v1/v2"), "/v1/v2"
// "/v1/{version}/v2/{name}", "/v1/${'version}/v2/${name}"
// "/v1/{version}/v2/{limit}", "/v1/${'version}/v2/${'limit}"
// "/v1/{age}/v2/{name}", "/v1/${age}/v2/${name}"

AsyncApiToBallerinaGenerator codeGenerator = new AsyncApiToBallerinaGenerator();
// Path definitionPath = RESDIR.resolve(RESDIR + "/swagger/path_parameter_valid.yaml");
// Path expectedPath = RESDIR.resolve("ballerina/path_parameter_valid.bal");
// AsyncApi25DocumentImpl asyncAPI = GeneratorUtils.normalizeAsyncAPI(definitionPath);
AsyncApiToBallerinaGenerator asyncAPIToBallerinaGenerator = new AsyncApiToBallerinaGenerator();
// asyncAPIToBallerinaGenerator.generateClient("src/test/resources/asyncapi-to-ballerina/client/PathParam" +
// "/path_parameter_valid.yaml", "/Users/thushalya/Documents/out");
AsyncApiToBallerinaGenerator asyncAPIToBallerinaGenerator = new AsyncApiToBallerinaGenerator("", false);
asyncAPIToBallerinaGenerator.generateClient(
Paths.get("src/test/resources/websockets/asyncapi-to-ballerina/client/StreamResponse/" +
"multiple_stream_with_dispatcherStreamId.yaml"),
Paths.get("src/test/resources/websockets/out"));
// AASClientConfig.Builder clientMetaDataBuilder = new AASClientConfig.Builder();
//// AASClientConfig oasClientConfig = clientMetaDataBuilder
////// .withFilters(filter)
//// .withAsyncAPI(asyncAPI).build();
//// .withResourceMode(false).build();
// IntermediateClientGenerator intermediateClientGenerator = new IntermediateClientGenerator(oasClientConfig);
// syntaxTree = intermediateClientGenerator.generateSyntaxTree();
// compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}

//DOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package io.ballerina.asyncapi.wsgenerators.testcases;

import io.apicurio.datamodels.models.asyncapi.v25.AsyncApi25DocumentImpl;
import io.ballerina.asyncapi.cmd.websockets.AsyncApiToBallerinaGenerator;
import io.ballerina.asyncapi.websocketscore.GeneratorUtils;
import io.ballerina.asyncapi.websocketscore.exception.BallerinaAsyncApiExceptionWs;
import io.ballerina.asyncapi.websocketscore.generators.client.IntermediateClientGenerator;
Expand Down Expand Up @@ -66,8 +65,6 @@ public void generateclientWithTestSkel(String yamlFile) throws IOException,
FormatterException, BallerinaAsyncApiExceptionWs, URISyntaxException {
Files.createDirectories(Paths.get(PROJECT_DIR + ASYNCAPI_PATH_SEPARATOR + TEST_DIR));
Path definitionPath = RES_DIR.resolve("sample_yamls/" + yamlFile);
AsyncApiToBallerinaGenerator codeGenerator = new AsyncApiToBallerinaGenerator();
codeGenerator.setIncludeTestFiles(true);
AsyncApi25DocumentImpl asyncAPI = GeneratorUtils.normalizeAsyncAPI(definitionPath);
AasClientConfig.Builder clientMetaDataBuilder = new AasClientConfig.Builder();
AasClientConfig asyncAPIClientConfig = clientMetaDataBuilder
Expand All @@ -88,8 +85,11 @@ public void generateclientWithTestSkel(String yamlFile) throws IOException,
List<Diagnostic> diagnostics = getDiagnostics(syntaxTreeClient, syntaxTreeTest,
syntaxTreeSchema, configFile, utilSyntaxTree);

Assert.assertFalse(diagnostics.stream().anyMatch(diagnostic -> diagnostic.diagnosticInfo().
severity().equals("Error")));
Assert.assertFalse(diagnostics.stream().anyMatch(diagnostic -> {
diagnostic.diagnosticInfo().
severity();
return false;
}));
}

public List<Diagnostic> getDiagnostics(SyntaxTree clientSyntaxTree, SyntaxTree testSyntaxTree,
Expand Down

0 comments on commit 1ace1b1

Please sign in to comment.