Search in sources :

Example 1 with CountResponse

use of org.teiid.odata.api.CountResponse in project teiid by teiid.

the class TestODataSQLBuilder method setup.

private BaseState setup(String ddl, String url, String method, ServletInputStream stream, BaseState state) throws Exception {
    Client client = Mockito.mock(Client.class);
    DDLHolder model = new DDLHolder("PM1", ddl);
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("vdb", model);
    MetadataStore store = metadata.getMetadataStore();
    // TranslationUtility utility = new TranslationUtility(metadata);
    OData odata = OData4Impl.newInstance();
    org.teiid.metadata.Schema teiidSchema = store.getSchema("PM1");
    CsdlSchema schema = ODataSchemaBuilder.buildMetadata("vdb", teiidSchema);
    TeiidEdmProvider edmProvider = new TeiidEdmProvider("baseuri", schema, "x");
    ServiceMetadata serviceMetadata = odata.createServiceMetadata(edmProvider, Collections.<EdmxReference>emptyList());
    ODataHttpHandler handler = odata.createHandler(serviceMetadata);
    Hashtable<String, String> headers = new Hashtable<String, String>();
    headers.put("Content-Type", "application/json");
    Mockito.stub(client.getMetadataStore()).toReturn(store);
    Mockito.stub(client.executeCount(Mockito.any(Query.class), Mockito.anyListOf(SQLParameter.class))).toReturn(new CountResponse() {

        @Override
        public int getCount() {
            return 10;
        }
    });
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.stub(request.getHeaderNames()).toReturn(headers.keys());
    Mockito.stub(request.getHeaders("Content-Type")).toReturn(headers.elements());
    Mockito.stub(request.getMethod()).toReturn(method);
    String requestURL = url;
    String queryString = "";
    int idx = url.indexOf("?");
    if (idx != -1) {
        requestURL = url.substring(0, idx);
        queryString = url.substring(idx + 1);
    }
    Mockito.stub(request.getRequestURL()).toReturn(new StringBuffer(requestURL));
    Mockito.stub(request.getQueryString()).toReturn(queryString);
    Mockito.stub(request.getServletPath()).toReturn("");
    Mockito.stub(request.getContextPath()).toReturn("/odata4/vdb/PM1");
    Mockito.stub(request.getInputStream()).toReturn(stream);
    final StringBuffer sb = new StringBuffer();
    ServletOutputStream out = new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            sb.append((char) b);
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setWriteListener(WriteListener writeListener) {
        }
    };
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.stub(response.getOutputStream()).toReturn(out);
    try {
        TeiidServiceHandler tsh = new TeiidServiceHandler("PM1");
        tsh.setPrepared(false);
        TeiidServiceHandler.setClient(client);
        handler.register(tsh);
        handler.process(request, response);
    } finally {
        TeiidServiceHandler.setClient(null);
    }
    ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCapture.capture());
    state.client = client;
    state.response = sb.toString();
    state.status = statusCapture.getValue();
    return state;
}
Also used : Query(org.teiid.query.sql.lang.Query) ServletOutputStream(javax.servlet.ServletOutputStream) CsdlSchema(org.apache.olingo.commons.api.edm.provider.CsdlSchema) HttpServletRequest(javax.servlet.http.HttpServletRequest) TeiidEdmProvider(org.teiid.olingo.service.TeiidEdmProvider) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) Client(org.teiid.odata.api.Client) WriteListener(javax.servlet.WriteListener) OData(org.apache.olingo.server.api.OData) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Hashtable(java.util.Hashtable) SQLParameter(org.teiid.odata.api.SQLParameter) CountResponse(org.teiid.odata.api.CountResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ODataHttpHandler(org.apache.olingo.server.api.ODataHttpHandler) MetadataStore(org.teiid.metadata.MetadataStore) TeiidServiceHandler(org.teiid.olingo.service.TeiidServiceHandler) ServiceMetadata(org.apache.olingo.server.api.ServiceMetadata)

Example 2 with CountResponse

use of org.teiid.odata.api.CountResponse in project teiid by teiid.

the class LocalClient method executeCount.

@Override
public CountResponse executeCount(Query query, List<SQLParameter> parameters) throws SQLException {
    String sql = query.toString();
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql);
    final PreparedStatement stmt = getConnection().prepareStatement(sql);
    if (!parameters.isEmpty()) {
        for (int i = 0; i < parameters.size(); i++) {
            stmt.setObject(i + 1, parameters.get(i).getValue(), parameters.get(i).getSqlType());
        }
    }
    ResultSet rs = stmt.executeQuery();
    rs.next();
    final int count = rs.getInt(1);
    rs.close();
    stmt.close();
    return new CountResponse() {

        @Override
        public int getCount() {
            return count;
        }
    };
}
Also used : ResultSet(java.sql.ResultSet) CountResponse(org.teiid.odata.api.CountResponse) PreparedStatement(java.sql.PreparedStatement) CacheHint(org.teiid.query.sql.lang.CacheHint)

Aggregations

CountResponse (org.teiid.odata.api.CountResponse)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Hashtable (java.util.Hashtable)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 WriteListener (javax.servlet.WriteListener)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CsdlSchema (org.apache.olingo.commons.api.edm.provider.CsdlSchema)1 OData (org.apache.olingo.server.api.OData)1 ODataHttpHandler (org.apache.olingo.server.api.ODataHttpHandler)1 ServiceMetadata (org.apache.olingo.server.api.ServiceMetadata)1 MetadataStore (org.teiid.metadata.MetadataStore)1 Client (org.teiid.odata.api.Client)1 SQLParameter (org.teiid.odata.api.SQLParameter)1 TeiidEdmProvider (org.teiid.olingo.service.TeiidEdmProvider)1 TeiidServiceHandler (org.teiid.olingo.service.TeiidServiceHandler)1 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1 Query (org.teiid.query.sql.lang.Query)1