Search in sources :

Example 56 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class PatternQueryTestCase method testPatternQuery9.

// from every (e1=Stream1[price >= 30]) -> e2=Stream1[ prev.price >= 20]<5:> within 3 min -> e3=Stream2[ price >=
// e1.price] -> e4=Stream3[price>74]
// select e1.symbol, avg(e2.price ) as avgPrice
// insert into OutputStream
@Test
public void testPatternQuery9() {
    Query query = Query.query();
    query.from(InputStream.patternStream(State.next(State.every(State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(30))))), State.next(State.countMoreThanEqual(State.stream(InputStream.stream("e2", "Stream1").filter(Expression.compare(Expression.variable("price").ofStream("e2", Variable.LAST), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(20)))), 5, Expression.Time.minute(3)), State.next(State.stream(InputStream.stream("e3", "Stream2").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("price").ofStream("e1")))), State.stream(InputStream.stream("e4", "Stream3").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.value(74)))))))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol").ofStream("e1")).select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2"))));
    query.insertInto("OutputStream");
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 57 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method generateOAS3Definitions.

/**
 * This method will generate open API 3.X specification for given ballerina service. Since we will need to
 * support both swagger 2.0 and OAS 3.0 it was implemented to convert to swagger by default and convert it
 * to OAS on demand.
 *
 * @param ballerinaSource ballerina source to be converted to swagger/OAS definition
 * @param serviceName specific service name within ballerina source that need to map OAS
 * @return Generated OAS3 string output.
 * @throws IOException When error occurs while converting, parsing input source.
 */
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    // Create empty swagger object.
    Swagger swaggerDefinition = new Swagger();
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                break;
            }
        }
    }
    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
    SwaggerConverter converter = new SwaggerConverter();
    return Yaml.pretty(converter.readContents(swaggerSource, null, null).getOpenAPI());
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) Swagger(io.swagger.models.Swagger) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 58 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project ballerina by ballerina-lang.

the class LocksInServicesTest method testServiceLvlVarLockComplex.

@Test(description = "Test locking service level variable complex", enabled = false)
public void testServiceLvlVarLockComplex() {
    Semaphore semaphore = new Semaphore(-11);
    ExecutorService executor = TestThreadPool.getInstance().getExecutor();
    for (int i = 0; i < 4; i++) {
        executor.submit(new TestRequestSender(compileResult, semaphore, "/sample1/echo"));
        executor.submit(new TestRequestSender(compileResult, semaphore, "/sample1/echo1"));
        executor.submit(new TestRequestSender(compileResult, semaphore, "/sample1/echo2"));
    }
    try {
        if (!semaphore.tryAcquire(10, TimeUnit.MINUTES)) {
            Assert.fail("request execution not finished within 2s");
        }
        String path = "/sample1/getResult";
        HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
        HTTPCarbonMessage response = Services.invokeNew(compileResult, cMsg);
        Assert.assertNotNull(response, "Response message not found");
        String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
        Assert.assertEquals(responseMsgPayload, "3333333333331224.01455555555555513.026.0777777777777", "incorrect request count");
    } catch (InterruptedException e) {
        Assert.fail("thread interrupted before request execution finished - " + e.getMessage(), e);
    }
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ExecutorService(java.util.concurrent.ExecutorService) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Semaphore(java.util.concurrent.Semaphore) Test(org.testng.annotations.Test)

Example 59 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project ballerina by ballerina-lang.

the class LocksInServicesTest method testServiceLvlVarLockBasic.

@Test(description = "Test locking service level variable basic", enabled = false)
public void testServiceLvlVarLockBasic() {
    Semaphore semaphore = new Semaphore(-9999);
    ExecutorService executor = TestThreadPool.getInstance().getExecutor();
    for (int i = 0; i < 10000; i++) {
        executor.submit(new TestRequestSender(compileResult, semaphore, "/sample/echo"));
    }
    try {
        if (!semaphore.tryAcquire(20, TimeUnit.MINUTES)) {
            Assert.fail("request execution not finished within 2s");
        }
        String path = "/sample/getCount";
        HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
        HTTPCarbonMessage response = Services.invokeNew(compileResult, cMsg);
        Assert.assertNotNull(response, "Response message not found");
        String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
        Assert.assertEquals(responseMsgPayload, "count - 10000", "incorrect request count");
    } catch (InterruptedException e) {
        Assert.fail("thread interrupted before request execution finished - " + e.getMessage(), e);
    }
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ExecutorService(java.util.concurrent.ExecutorService) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Semaphore(java.util.concurrent.Semaphore) Test(org.testng.annotations.Test)

Example 60 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project ballerina by ballerina-lang.

the class ServiceEndpointTest method testGetProtocolConnectionStruct.

@Test(description = "Test the protocol value of ServiceEndpoint struct within a service")
public void testGetProtocolConnectionStruct() {
    String protocolValue = "http";
    String path = "/hello/protocol";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_GET);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(response.getProperty(HttpConstants.HTTP_STATUS_CODE), 200);
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("protocol").asText(), protocolValue);
}
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)135 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)101 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)100 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)90 TestUtil (org.wso2.siddhi.core.TestUtil)60 Event (org.wso2.siddhi.core.event.Event)37 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)28 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)27 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)23 BString (org.ballerinalang.model.values.BString)22 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)19 BJSON (org.ballerinalang.model.values.BJSON)18 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)6 Expression (org.wso2.siddhi.query.api.expression.Expression)6 Map (java.util.Map)4 Semaphore (java.util.concurrent.Semaphore)4 Header (org.wso2.carbon.messaging.Header)4 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)4