Search in sources :

Example 56 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetDocumentationContentFileWithNullStream.

@Test(description = "Getting Documentation content when source type is FILE and the input stream is null", expectedExceptions = APIManagementException.class)
public void testGetDocumentationContentFileWithNullStream() throws APIManagementException {
    ApiDAO apiDAO = mock(ApiDAO.class);
    AbstractAPIManager apiPublisher = getApiPublisherImpl(apiDAO);
    DocumentInfo.Builder builder = new DocumentInfo.Builder();
    builder.name("CalculatorDoc");
    builder.sourceType(DocumentInfo.SourceType.FILE);
    DocumentInfo documentInfo = builder.build();
    when(apiDAO.getDocumentInfo(DOC_ID)).thenReturn(documentInfo);
    when(apiDAO.getDocumentFileContent(DOC_ID)).thenReturn(null);
    apiPublisher.getDocumentationContent(DOC_ID);
    verify(apiDAO, times(0)).getDocumentFileContent(DOC_ID);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 57 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentGet.

/**
 * Retrieves the content of the document
 *
 * @param apiId           API ID
 * @param documentId      Document ID
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return content of the document
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdContentGet(String apiId, String documentId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        DocumentContent documentationContent = apiStore.getDocumentationContent(documentId);
        DocumentInfo documentInfo = documentationContent.getDocumentInfo();
        if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
            String filename = documentInfo.getFileName();
            return Response.ok(documentationContent.getFileContent()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"").header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        } else if (DocumentInfo.SourceType.INLINE.equals(documentInfo.getSourceType())) {
            String content = documentationContent.getInlineContent();
            return Response.ok(content).header(RestApiConstants.HEADER_CONTENT_TYPE, MediaType.TEXT_PLAIN).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        } else if (DocumentInfo.SourceType.URL.equals(documentInfo.getSourceType())) {
            String sourceUrl = documentInfo.getSourceURL();
            return Response.seeOther(new URI(sourceUrl)).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving document " + documentId + " of the API " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving source URI location of " + documentId;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 58 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryWithFailingSource.

@Test
public void inMemoryWithFailingSource() throws InterruptedException {
    log.info("Test failing inMemorySource");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testFailingInMemory', topic='WSO2', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                wso2Count.incrementAndGet();
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    TestFailingInMemorySource.fail = true;
    TestFailingInMemorySource.connectionCallback.onError(new ConnectionUnavailableException("Connection Lost"));
    Thread.sleep(500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    TestFailingInMemorySource.fail = false;
    Thread.sleep(5500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of errors", 1, TestFailingInMemorySource.numberOfErrorOccurred);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ConnectionUnavailableException(org.wso2.siddhi.core.exception.ConnectionUnavailableException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 59 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemorySourceAndEventMappingWithSiddhiQL.

@Test
public void inMemorySourceAndEventMappingWithSiddhiQL() throws InterruptedException {
    log.info("Test inMemorySource And EventMapping With SiddhiQL");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='WSO2', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                switch(wso2Count.incrementAndGet()) {
                    case 1:
                        org.testng.AssertJUnit.assertEquals(55.6f, event.getData(1));
                        break;
                    case 2:
                        org.testng.AssertJUnit.assertEquals(57.6f, event.getData(1));
                        break;
                    default:
                        org.testng.AssertJUnit.fail();
                }
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of IBM events", 0, ibmCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 60 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryTestCase8.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void inMemoryTestCase8() throws InterruptedException {
    log.info("Test inMemory 8");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testEventInMemory', topic='Foo', prop1='hi', prop2='test', fail='true', " + "   @map(type='testString', @attributes(symbol='trp:symbol'," + "        volume='volume',price='trp:price'))) " + "define stream FooStream (symbol string, price string, volume long); " + "define stream BarStream (symbol string, price string, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6