Search in sources :

Example 1 with Converter

use of org.wso2.ballerinalang.compiler.packaging.converters.Converter in project siddhi by wso2.

the class EventTestCase method testSimpleStreamEventConverter.

@Test
public void testSimpleStreamEventConverter() {
    Attribute price = new Attribute("price", Attribute.Type.DOUBLE);
    Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addOutputData(symbol);
    metaStreamEvent.addOutputData(price);
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.INT);
    Event event = new Event(System.currentTimeMillis(), new Object[] { "WSO2", 200, 50 });
    metaStreamEvent.addInputDefinition(streamDefinition);
    StreamEventConverter converter = StreamEventConverterFactory.constructEventConverter(metaStreamEvent);
    StreamEventPool eventPool = new StreamEventPool(metaStreamEvent, 5);
    StreamEvent borrowedEvent = eventPool.borrowEvent();
    converter.convertEvent(event, borrowedEvent);
    AssertJUnit.assertTrue(converter instanceof SimpleStreamEventConverter);
    AssertJUnit.assertNull(borrowedEvent.getBeforeWindowData());
    AssertJUnit.assertNull(borrowedEvent.getOnAfterWindowData());
    AssertJUnit.assertEquals(2, borrowedEvent.getOutputData().length);
    AssertJUnit.assertEquals(200, borrowedEvent.getOutputData()[1]);
    AssertJUnit.assertEquals("WSO2", borrowedEvent.getOutputData()[0]);
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Attribute(org.wso2.siddhi.query.api.definition.Attribute) SelectiveStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.SelectiveStreamEventConverter) StreamEventConverter(org.wso2.siddhi.core.event.stream.converter.StreamEventConverter) SimpleStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.SimpleStreamEventConverter) ZeroStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.ZeroStreamEventConverter) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) Event(org.wso2.siddhi.core.event.Event) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) SimpleStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.SimpleStreamEventConverter) Test(org.testng.annotations.Test)

Example 2 with Converter

use of org.wso2.ballerinalang.compiler.packaging.converters.Converter 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 3 with Converter

use of org.wso2.ballerinalang.compiler.packaging.converters.Converter in project ballerina by ballerina-lang.

the class JarIntegrationTest method balInsideJar.

@Test
public void balInsideJar() throws IOException {
    Patten balPatten = new Patten(path("very"), Patten.WILDCARD_SOURCE);
    JarRepo repo = new JarRepo(tempJar.toUri());
    Converter<Path> subject = repo.getConverterInstance();
    List<PackageSourceEntry> sources = balPatten.convertToSources(subject, null).collect(Collectors.toList());
    Assert.assertEquals(sources.size(), 1);
    Assert.assertEquals(sources.get(0).getCode(), BAL_CONTENT);
}
Also used : Path(java.nio.file.Path) PackageSourceEntry(org.ballerinalang.repository.PackageSourceEntry) Patten(org.wso2.ballerinalang.compiler.packaging.Patten) JarRepo(org.wso2.ballerinalang.compiler.packaging.repo.JarRepo) Test(org.testng.annotations.Test)

Example 4 with Converter

use of org.wso2.ballerinalang.compiler.packaging.converters.Converter in project ballerina by ballerina-lang.

the class PattenTest method testReductionAndExpansion.

@Test
public void testReductionAndExpansion() {
    Converter<String> mock = mockResolver("my-dir", (a, b) -> a + " > " + b, s -> Stream.of(s + " > cache1", s + " > cache2", s + " > cache3"), q -> Stream.of(q + " > dir1 > x.bal", q + " > y.bal", q + " > dir2 > dir3 > f.bal"));
    Patten subject = new Patten(path("hello"), Patten.WILDCARD_DIR, path("world"), Patten.WILDCARD_SOURCE);
    List<String> strings = subject.convert(mock).collect(Collectors.toList());
    Assert.assertEquals(strings, Arrays.asList("my-dir > hello > cache1 > world > dir1 > x.bal", "my-dir > hello > cache1 > world > y.bal", "my-dir > hello > cache1 > world > dir2 > dir3 > f.bal", "my-dir > hello > cache2 > world > dir1 > x.bal", "my-dir > hello > cache2 > world > y.bal", "my-dir > hello > cache2 > world > dir2 > dir3 > f.bal", "my-dir > hello > cache3 > world > dir1 > x.bal", "my-dir > hello > cache3 > world > y.bal", "my-dir > hello > cache3 > world > dir2 > dir3 > f.bal"));
}
Also used : Patten(org.wso2.ballerinalang.compiler.packaging.Patten) Test(org.testng.annotations.Test)

Example 5 with Converter

use of org.wso2.ballerinalang.compiler.packaging.converters.Converter in project ballerina by ballerina-lang.

the class PattenTest method testDirExpansion.

@Test
public void testDirExpansion() {
    Converter<String> mock = mockResolver("root-dir", null, s -> Stream.of(s + " > cache1", s + " > cache2", s + " > cache3"), null);
    Patten subject = new Patten(Patten.WILDCARD_DIR);
    List<String> strings = subject.convert(mock).collect(Collectors.toList());
    Assert.assertEquals(strings, Arrays.asList("root-dir > cache1", "root-dir > cache2", "root-dir > cache3"));
}
Also used : Patten(org.wso2.ballerinalang.compiler.packaging.Patten) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)9 Patten (org.wso2.ballerinalang.compiler.packaging.Patten)8 RemoteRepo (org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo)3 Repo (org.wso2.ballerinalang.compiler.packaging.repo.Repo)3 Event (org.wso2.siddhi.core.event.Event)3 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)3 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)3 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)3 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)3 SelectiveStreamEventConverter (org.wso2.siddhi.core.event.stream.converter.SelectiveStreamEventConverter)3 SimpleStreamEventConverter (org.wso2.siddhi.core.event.stream.converter.SimpleStreamEventConverter)3 StreamEventConverter (org.wso2.siddhi.core.event.stream.converter.StreamEventConverter)3 ZeroStreamEventConverter (org.wso2.siddhi.core.event.stream.converter.ZeroStreamEventConverter)3 Attribute (org.wso2.siddhi.query.api.definition.Attribute)3 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)3 URI (java.net.URI)2 Path (java.nio.file.Path)2 BLangCompilerException (org.ballerinalang.compiler.BLangCompilerException)2 PackageSourceEntry (org.ballerinalang.repository.PackageSourceEntry)2 Converter (org.wso2.ballerinalang.compiler.packaging.converters.Converter)2