use of org.wso2.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method process.
@Override
public void process(FunctionNode functionNode, List<AnnotationAttachmentNode> annotations) {
// to avoid processing those, we have to have below check.
if (!suite.getSuiteName().equals(functionNode.getPosition().getSource().getPackageName())) {
return;
}
// traverse through the annotations of this function
for (AnnotationAttachmentNode attachmentNode : annotations) {
String annotationName = attachmentNode.getAnnotationName().getValue();
String functionName = functionNode.getName().getValue();
if (BEFORE_SUITE_ANNOTATION_NAME.equals(annotationName)) {
suite.addBeforeSuiteFunction(functionName);
} else if (AFTER_SUITE_ANNOTATION_NAME.equals(annotationName)) {
suite.addAfterSuiteFunction(functionName);
} else if (BEFORE_EACH_ANNOTATION_NAME.equals(annotationName)) {
suite.addBeforeEachFunction(functionName);
} else if (AFTER_EACH_ANNOTATION_NAME.equals(annotationName)) {
suite.addAfterEachFunction(functionName);
} else if (MOCK_ANNOTATION_NAME.equals(annotationName)) {
String[] vals = new String[2];
// If package property not present the package is .
// TODO: when default values are supported in annotation struct we can remove this
vals[0] = ".";
if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
attributes.forEach(attributeNode -> {
String name = attributeNode.getKey().toString();
String value = attributeNode.getValue().toString();
if (PACKAGE.equals(name)) {
vals[0] = value;
} else if (FUNCTION.equals(name)) {
vals[1] = value;
}
});
suite.addMockFunction(vals[0] + MOCK_ANNOTATION_DELIMITER + vals[1], functionName);
}
} else if (TEST_ANNOTATION_NAME.equals(annotationName)) {
Test test = new Test();
test.setTestName(functionName);
AtomicBoolean shouldSkip = new AtomicBoolean();
AtomicBoolean groupsFound = new AtomicBoolean();
List<String> groups = registry.getGroups();
boolean shouldIncludeGroups = registry.shouldIncludeGroups();
if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
attributes.forEach(attributeNode -> {
String name = attributeNode.getKey().toString();
// Check if enable property is present in the annotation
if (TEST_ENABLE_ANNOTATION_NAME.equals(name) && "false".equals(attributeNode.getValue().toString())) {
// If enable is false, disable the test, no further processing is needed
shouldSkip.set(true);
return;
}
// Check whether user has provided a group list
if (groups != null && !groups.isEmpty()) {
// check if groups attribute is present in the annotation
if (GROUP_ANNOTATION_NAME.equals(name)) {
if (attributeNode.getValue() instanceof BLangArrayLiteral) {
BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
boolean isGroupPresent = isGroupAvailable(groups, values.exprs.stream().map(node -> node.toString()).collect(Collectors.toList()));
if (shouldIncludeGroups) {
// include only if the test belong to one of these groups
if (!isGroupPresent) {
// skip the test if this group is not defined in this test
shouldSkip.set(true);
return;
}
} else {
// exclude only if the test belong to one of these groups
if (isGroupPresent) {
// skip if this test belongs to one of the excluded groups
shouldSkip.set(true);
return;
}
}
groupsFound.set(true);
}
}
}
if (VALUE_SET_ANNOTATION_NAME.equals(name)) {
test.setDataProvider(attributeNode.getValue().toString());
}
if (BEFORE_FUNCTION.equals(name)) {
test.setBeforeTestFunction(attributeNode.getValue().toString());
}
if (AFTER_FUNCTION.equals(name)) {
test.setAfterTestFunction(attributeNode.getValue().toString());
}
if (DEPENDS_ON_FUNCTIONS.equals(name)) {
if (attributeNode.getValue() instanceof BLangArrayLiteral) {
BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
values.exprs.stream().map(node -> node.toString()).forEach(test::addDependsOnTestFunction);
}
}
});
}
if (groups != null && !groups.isEmpty() && !groupsFound.get() && shouldIncludeGroups) {
// if the user has asked to run only a specific list of groups and this test doesn't have
// that group, we should skip the test
shouldSkip.set(true);
}
if (!shouldSkip.get()) {
suite.addTests(test);
}
} else {
// disregard this annotation
}
}
}
use of org.wso2.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.
the class HtmlDocTest method testGlobalVariables.
@Test(description = "Annotation in a package should be shown in the constructs")
public void testGlobalVariables() throws Exception {
BLangPackage bLangPackage = createPackage("package x.y; " + "public int total = 98;" + "public string content = \"Name\";");
Page page = generatePage(bLangPackage);
Assert.assertEquals(page.constructs.size(), 2);
Assert.assertEquals(page.constructs.get(0).name, "total");
Assert.assertEquals(page.constructs.get(1).name, "content");
}
use of org.wso2.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.
the class HtmlDocTest method testAnnotationPropertiesExtracted.
@Test(description = "Annotation properties should be available via construct", enabled = false)
public void testAnnotationPropertiesExtracted() throws Exception {
BLangPackage bLangPackage = createPackage("package x.y; " + "@Description {value: \"AnnotationDoc to upgrade connection from HTTP to WS in the " + "same base path\"}\n" + "@Field {value:\"upgradePath:Upgrade path for the WebSocket service from " + "HTTP to WS\"}\n" + "@Field {value:\"serviceName:Name of the WebSocket service where the HTTP service should " + " upgrade to\"}\n" + "public annotation webSocket attach service<> {\n" + " string upgradePath;\n" + " string serviceName;\n" + "}");
AnnotationDoc annotationDoc = Generator.createDocForNode(bLangPackage.getAnnotations().get(0));
Assert.assertEquals(annotationDoc.name, "webSocket", "Annotation name should be extracted");
Assert.assertEquals(annotationDoc.description, "AnnotationDoc to upgrade connection from HTTP to WS " + "in the same base path", "Description of the annotation should be extracted");
// Annotation Fields
Assert.assertEquals(annotationDoc.attributes.get(0).name, "upgradePath", "Annotation attribute name " + "should be extracted");
Assert.assertEquals(annotationDoc.attributes.get(0).dataType, "string", "Annotation attribute type " + "should be extracted");
Assert.assertEquals(annotationDoc.attributes.get(0).description, "Upgrade path for the WebSocket service " + "from HTTP to WS", "Description of the annotation attribute should be extracted");
}
use of org.wso2.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.
the class CommandUtil method getServiceDocumentationByPosition.
/**
* Get the Documentation attachment for the service.
* @param bLangPackage BLangPackage built
* @param line Start line of the service in the source
* @return {@link String} Documentation attachment for the service
*/
static DocAttachmentInfo getServiceDocumentationByPosition(BLangPackage bLangPackage, int line) {
// TODO: Currently resource position is invalid and we use the annotation attachment positions.
for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
if (topLevelNode instanceof BLangService) {
BLangService serviceNode = (BLangService) topLevelNode;
DiagnosticPos servicePos = CommonUtil.toZeroBasedPosition(serviceNode.getPosition());
List<BLangAnnotationAttachment> annotationAttachments = serviceNode.getAnnotationAttachments();
if (!annotationAttachments.isEmpty()) {
DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
if (lastAttachmentPos.getEndLine() < line && line < servicePos.getEndLine()) {
return getServiceNodeDocumentation(serviceNode, lastAttachmentPos.getEndLine() + 1);
}
} else if (servicePos.getStartLine() == line) {
return getServiceNodeDocumentation(serviceNode, line);
}
}
}
return null;
}
use of org.wso2.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.
the class ParserUtils method removeConstructsOfFile.
/**
* Remove constructs from given file in given package from given package map.
*
* @param pkgName Name of the package
* @param fileName Name of the File
* @param packageMap Package constructs map
*/
public static void removeConstructsOfFile(String pkgName, String fileName, Map<String, ModelPackage> packageMap) {
ModelPackage newPkg = new ModelPackage();
newPkg.setName(pkgName);
if (packageMap.containsKey(pkgName)) {
ModelPackage currentPkg = packageMap.get(pkgName);
currentPkg.getFunctions().forEach((Function func) -> {
if (!func.getFileName().equals(fileName)) {
newPkg.addFunctionsItem(func);
}
});
currentPkg.getStructs().forEach((Struct struct) -> {
if (!struct.getFileName().equals(fileName)) {
newPkg.addStructsItem(struct);
}
});
currentPkg.getAnnotations().forEach((AnnotationDef annotation) -> {
if (!annotation.getFileName().equals(fileName)) {
newPkg.addAnnotationsItem(annotation);
}
});
currentPkg.getConnectors().forEach((Connector connector) -> {
if (!connector.getFileName().equals(fileName)) {
newPkg.addConnectorsItem(connector);
}
});
packageMap.remove(pkgName);
packageMap.put(pkgName, newPkg);
}
}
Aggregations