Search in sources :

Example 71 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class BallerinaDocGenForCurrentPackageTest method testStruct.

@Test(description = "Test a bal file with a function in the current package")
public void testStruct() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balFileInCurrentPackage.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BLangPackage balPackage = docsMap.get(".");
        List<BLangFunction> functions = balPackage.getFunctions();
        Assert.assertEquals(functions.size(), 1);
        BLangFunction function = functions.iterator().next();
        Assert.assertEquals(function.getParameters().size(), 1);
        Assert.assertEquals(function.getAnnotationAttachments().size(), 2);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 72 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class BallerinaFunctionDocGenTest method testABalWithMultipleFunctions.

@Test(description = "Test a Bal file with multiple Functions")
public void testABalWithMultipleFunctions() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balWith2Functions.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BallerinaDocGenTestUtils.printDocMap(docsMap);
        BLangPackage doc = docsMap.get(".");
        Collection<BLangFunction> functions = doc.getFunctions();
        Assert.assertEquals(functions.size(), 2);
        Iterator<BLangFunction> iterator = functions.iterator();
        BLangFunction function = iterator.next();
        Assert.assertEquals(function.getParameters().size(), 1);
        Assert.assertEquals(function.getReturnParameters().size(), 1);
        BLangFunction function1 = iterator.next();
        Assert.assertEquals(function1.getParameters().size(), 2);
        Assert.assertEquals(function1.getReturnParameters().size(), 0);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 73 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class CreateSessionIfAbsent method execute.

@Override
public void execute(Context context) {
    try {
        BStruct requestStruct = ((BStruct) context.getRefArgument(0));
        // TODO check below line
        HTTPCarbonMessage httpCarbonMessage = HttpUtil.getCarbonMsg(requestStruct, HttpUtil.createHttpCarbonMessage(true));
        String cookieHeader = httpCarbonMessage.getHeader(HttpConstants.COOKIE_HEADER);
        String path = (String) httpCarbonMessage.getProperty(HttpConstants.BASE_PATH);
        Session session = (Session) httpCarbonMessage.getProperty(HttpConstants.HTTP_SESSION);
        if (cookieHeader != null) {
            try {
                String sessionId = HttpUtil.getSessionID(cookieHeader);
                // return value from cached session
                if (session != null && sessionId.equals(session.getId())) {
                    session = session.setAccessed();
                    context.setReturnValues(HttpUtil.createSessionStruct(context, session));
                    return;
                }
                session = SessionManager.getInstance().getHTTPSession(sessionId);
            } catch (NoSuchElementException e) {
                // ignore throwable
                logger.info("Failed to get session: Incorrect Session cookie");
            }
            if (session == null) {
                session = SessionManager.getInstance().createHTTPSession(path);
            } else if (session != null && session.getPath().equals(path)) {
                // path validity check
                session.setNew(false);
                session.setAccessed();
            } else {
                throw new BallerinaException("Failed to get session: " + path + " is not an allowed path");
            }
        } else {
            // cached session will return of this function is called twice.
            if (session != null) {
                session = session.setAccessed();
                context.setReturnValues(HttpUtil.createSessionStruct(context, session));
                return;
            }
            // create session since request doesn't have a cookie
            session = SessionManager.getInstance().createHTTPSession(path);
        }
        httpCarbonMessage.setProperty(HttpConstants.HTTP_SESSION, session);
        httpCarbonMessage.removeHeader(HttpConstants.COOKIE_HEADER);
        context.setReturnValues(HttpUtil.createSessionStruct(context, session));
        return;
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) NoSuchElementException(java.util.NoSuchElementException) Session(org.ballerinalang.net.http.session.Session)

Example 74 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class GlobalVarServicePkgTest method testAssigningFuncInvFromDiffPkg.

@Test(description = "Test assigning function invocation from different package", enabled = false)
public void testAssigningFuncInvFromDiffPkg() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/globalvar-pkg/func-inv-from-diff-pkg", "GET");
    HTTPCarbonMessage response = Services.invokeNew(result, cMsg);
    Assert.assertNotNull(response);
    // Expected Json message : {"glbVarPkgFunc":8876}
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("glbVarPkgFunc").asText(), "8876");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 75 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class GlobalVarServicePkgTest method testAssigningFuncInvFromSamePkg.

@Test(description = "Test assigning function invocation from same package", enabled = false)
public void testAssigningFuncInvFromSamePkg() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/globalvar-pkg/func-inv-from-same-pkg", "GET");
    HTTPCarbonMessage response = Services.invokeNew(result, cMsg);
    Assert.assertNotNull(response);
    // Expected Json message : {"glbVarFunc":423277.72343}
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("glbVarFunc").asText(), "423277.72343");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)94 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)49 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)44 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)39 ArrayList (java.util.ArrayList)37 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)35 BString (org.ballerinalang.model.values.BString)30 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)29 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)20 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)20 BJSON (org.ballerinalang.model.values.BJSON)19 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)19 List (java.util.List)18 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)18 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)16 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)15 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)14 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14