Search in sources :

Example 21 with Annotation

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
        }
    }
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.ballerinalang.testerina.core.entity.Test) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 22 with 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");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 23 with Annotation

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");
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Test(org.testng.annotations.Test)

Example 24 with Annotation

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 25 with Annotation

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);
    }
}
Also used : Function(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Function) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Connector(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Connector) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) AnnotationDef(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationDef)

Aggregations

Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)14 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)12 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)11 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)11 HashMap (java.util.HashMap)9 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)9 List (java.util.List)8 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)8 Annotation (org.wso2.siddhi.query.api.annotation.Annotation)8 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)7 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)7 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)7 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)7 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)7 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)7