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]);
}
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());
}
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);
}
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"));
}
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"));
}
Aggregations