Skip to content

Commit

Permalink
Merge pull request #591 from LakshanWeerasinghe/fix-trigger-issues
Browse files Browse the repository at this point in the history
Fix several issues related to creating triggers and loading triggers
  • Loading branch information
LakshanWeerasinghe authored Feb 11, 2025
2 parents 0b33cfe + 3e72373 commit 4e53e97
Show file tree
Hide file tree
Showing 16 changed files with 1,577 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ task copyStdlibs() {
}
}

def pullBallerinaModule(String packageName) {
tasks.register("pullBallerinaModule_${packageName.replace('/', '_')}") {
doLast {
def errOutput = new ByteArrayOutputStream()
def result = exec {
ignoreExitValue = true
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
commandLine 'cmd', '/c', 'bal', 'pull', packageName
} else {
commandLine 'bal', 'pull', packageName
}
errorOutput = errOutput
}
if (result.exitValue != 0 && !errOutput.toString().contains("package already exists in the home repository")) {
println errOutput
throw new GradleException("Failed to pull Ballerina module: ${packageName}")
}
}
}
}

test.dependsOn pullBallerinaModule('ballerinax/kafka')
test.dependsOn pullBallerinaModule('ballerinax/rabbitmq')

test {
dependsOn {
copyStdlibs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public CompletableFuture<ServiceFromSourceResponse> getServiceFromSource(CommonM
}
Set<String> listeners = ListenerUtil.getCompatibleListeners(serviceName.get(), semanticModel, project);
List<String> allValues = serviceModel.getListener().getValues();
if (allValues.isEmpty()) {
if (Objects.isNull(allValues) || allValues.isEmpty()) {
listeners.add(serviceModel.getListener().getValue());
} else {
listeners.addAll(allValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static Set<String> getCompatibleListeners(String moduleName, SemanticMode
continue;
}
String listenerName = variableSymbol.getName().get();
if (isHttp && listenerName.startsWith(ServiceModelGeneratorConstants.HTTP_DEFAULT_LISTENER_VAR_NAME)) {
if (isHttp) {
if (variableSymbol.getLocation().isPresent()) {
Location location = variableSymbol.getLocation().get();
Path path = project.sourceRoot().resolve(location.lineRange().fileName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,15 @@ private static void populateListenerInfo(Service serviceModel, ServiceDeclaratio
}
NodeList<Node> paths = serviceNode.absoluteResourcePath();
if (!paths.isEmpty()) {
serviceModel.getBasePath().setValue(getPath(paths));
String path = getPath(paths);
if (serviceModel.getPackageName().equals("rabbitmq")) {
Value queueName = serviceModel.getProperty("queueName");
if (Objects.nonNull(queueName)) {
queueName.setValue(path);
}
} else {
serviceModel.getBasePath().setValue(path);
}
}
}

Expand Down Expand Up @@ -1020,7 +1028,15 @@ public static String getServiceDeclarationNode(Service service) {
} else if (Objects.nonNull(service.getBasePath()) && service.getBasePath().isEnabledWithValue()) {
builder.append(getValueString(service.getBasePath()));
builder.append(ServiceModelGeneratorConstants.SPACE);
} else if (service.getModuleName().equals("rabbitmq")) {
Value queueName = service.getProperty("queueName");
if (Objects.nonNull(queueName) && queueName.isEnabledWithValue()) {
builder.append(queueName.getValue());
builder.append(ServiceModelGeneratorConstants.SPACE);
}
}


builder.append(ServiceModelGeneratorConstants.ON).append(ServiceModelGeneratorConstants.SPACE);
if (Objects.nonNull(service.getListener()) && service.getListener().isEnabledWithValue()) {
builder.append(service.getListener().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"enabled": true,
"editable": false,
"value": "",
"valueType": "EXPRESSION",
"valueType": "SINGLE_SELECT",
"valueTypeConstraint": "kafka:Listener",
"isType": false,
"placeholder": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.servicemodelgenerator.extension;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.ballerina.servicemodelgenerator.extension.model.Service;
import io.ballerina.servicemodelgenerator.extension.request.ServiceSourceRequest;
import org.eclipse.lsp4j.TextEdit;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Tests for the service model source generator addService service.
*
* @since 2.0.0
*/
public class AddServiceTest extends AbstractLSTest {

private static final Type TEXT_EDIT_LIST_TYPE = new TypeToken<Map<String, List<TextEdit>>>() {
}.getType();

@Override
@Test(dataProvider = "data-provider")
public void test(Path config) throws IOException {
Path configJsonPath = configDir.resolve(config);
TestConfig testConfig = gson.fromJson(Files.newBufferedReader(configJsonPath), TestConfig.class);

ServiceSourceRequest request = new ServiceSourceRequest(
sourceDir.resolve(testConfig.filePath()).toAbsolutePath().toString(), testConfig.service());
JsonObject jsonMap = getResponse(request).getAsJsonObject("textEdits");

Map<String, List<TextEdit>> actualTextEdits = gson.fromJson(jsonMap, TEXT_EDIT_LIST_TYPE);

boolean assertFailure = false;

if (actualTextEdits.size() != testConfig.output().size()) {
log.info("The number of text edits does not match the expected output.");
assertFailure = true;
}

Map<String, List<TextEdit>> newMap = new HashMap<>();
for (Map.Entry<String, List<TextEdit>> entry : actualTextEdits.entrySet()) {
Path fullPath = Paths.get(entry.getKey());
String relativePath = sourceDir.relativize(fullPath).toString();

List<TextEdit> textEdits = testConfig.output().get(relativePath.replace("\\", "/"));
if (textEdits == null) {
log.info("No text edits found for the file: " + relativePath);
assertFailure = true;
} else if (!assertArray("text edits", entry.getValue(), textEdits)) {
assertFailure = true;
}

newMap.put(relativePath, entry.getValue());
}

if (assertFailure) {
TestConfig updatedConfig =
new TestConfig(testConfig.filePath(), testConfig.description(), testConfig.service(), newMap);
// updateConfig(configJsonPath, updatedConfig);
Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath));
}
}

@Override
protected String getResourceDir() {
return "add_service";
}

@Override
protected Class<? extends AbstractLSTest> clazz() {
return AddServiceTest.class;
}

@Override
protected String getApiName() {
return "addService";
}

/**
* Represents the test configuration for the source generator test.
*
* @param filePath The path to the source file.
* @param description The description of the test.
* @param service The service to be added.
* @param output The expected output.
*/
private record TestConfig(String filePath, String description, Service service,
Map<String, List<TextEdit>> output) {
public String description() {
return description == null ? "" : description;
}
}
}
Loading

0 comments on commit 4e53e97

Please sign in to comment.