@Test public void testStreaming() throws Exception { WSExecutionFactory ef = new WSExecutionFactory(); MetadataFactory mf = new MetadataFactory( "vdb", 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null); ef.getMetadata(mf, null); Procedure p = mf.getSchema().getProcedure(WSExecutionFactory.INVOKE_HTTP); assertEquals(7, p.getParameters().size()); TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb"); RuntimeMetadataImpl rm = new RuntimeMetadataImpl(tm); WSConnection mockConnection = Mockito.mock(WSConnection.class); Dispatch<Object> mockDispatch = mockDispatch(); DataSource mock = Mockito.mock(DataSource.class); ByteArrayInputStream baos = new ByteArrayInputStream(new byte[100]); Mockito.stub(mock.getInputStream()).toReturn(baos); Mockito.stub(mockDispatch.invoke(Mockito.any(DataSource.class))).toReturn(mock); Mockito.stub( mockConnection.createDispatch( Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Class.class), Mockito.any(Service.Mode.class))) .toReturn(mockDispatch); CommandBuilder cb = new CommandBuilder(tm); Call call = (Call) cb.getCommand("call invokeHttp('GET', null, null, true)"); BinaryWSProcedureExecution pe = new BinaryWSProcedureExecution( call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection); pe.execute(); List<?> result = pe.getOutputParameterValues(); Blob b = (Blob) result.get(0); assertEquals(100, ObjectConverterUtil.convertToByteArray(b.getBinaryStream()).length); try { ObjectConverterUtil.convertToByteArray(b.getBinaryStream()); fail(); } catch (SQLException e) { // should only be able to read once } }
@Test public void testTypeConversion() throws Exception { BufferManager bm = BufferManagerFactory.getStandaloneBufferManager(); String str = "hello world"; Object source = new StreamSource(new StringReader(str)); XMLType xml = (XMLType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.XML, null); assertEquals(str, xml.getString()); source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a/>"))); xml = (XMLType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.XML, null); XMLInputFactory in = XMLType.getXmlInputFactory(); XMLStreamReader reader = in.createXMLStreamReader(new StringReader(xml.getString())); assertEquals(XMLEvent.START_DOCUMENT, reader.getEventType()); assertEquals(XMLEvent.START_ELEMENT, reader.next()); assertEquals("a", reader.getLocalName()); assertEquals(XMLEvent.END_ELEMENT, reader.next()); byte[] bytes = str.getBytes(Streamable.ENCODING); source = new InputStreamFactory.BlobInputStreamFactory(BlobType.createBlob(bytes)); BlobType blob = (BlobType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.BLOB, null); assertArrayEquals(bytes, ObjectConverterUtil.convertToByteArray(blob.getBinaryStream())); }
@Test public void testOdata() throws Exception { String vdb = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<vdb name=\"Loopy\" version=\"1\">\n" + " <model name=\"MarketData\">\n" + " <source name=\"text-connector2\" translator-name=\"loopback\" />\n" + " <metadata type=\"DDL\"><![CDATA[\n" + " CREATE FOREIGN TABLE G1 (e1 string, e2 integer PRIMARY KEY);\n" + " CREATE FOREIGN TABLE G2 (e1 string, e2 integer PRIMARY KEY) OPTIONS (UPDATABLE 'true');\n" + " ]]> </metadata>\n" + " </model>\n" + "</vdb>"; admin.deploy( "loopy-vdb.xml", new ReaderInputStream(new StringReader(vdb), Charset.forName("UTF-8"))); assertTrue(AdminUtil.waitForVDBLoad(admin, "Loopy", 1, 3)); WebClient client = WebClient.create("http://*****:*****@mm://localhost:31000;user=user;password=user", null); PreparedStatement ps = conn.prepareCall( "select t.* from xmltable('/*:Edmx/*:DataServices/*:Schema[@Alias=\"MarketData\"]' passing xmlparse(document cast(? as clob))) as t"); ps.setAsciiStream(1, (InputStream) response.getEntity()); ResultSet rs = ps.executeQuery(); rs.next(); assertEquals( ObjectConverterUtil.convertFileToString( UnitTestUtil.getTestDataFile("loopy-metadata4-results.txt")), rs.getString(1)); conn.close(); // try an invalid url client = WebClient.create("http://localhost:8080/odata/x/y$metadata"); client.header( "Authorization", "Basic " + Base64.encodeBytes(("user:user").getBytes())); // $NON-NLS-1$ //$NON-NLS-2$ response = client.invoke("GET", null); assertEquals(500, response.getStatus()); admin.undeploy("loopy-vdb.xml"); }
@Test public void testProjectedColumns() throws Exception { TransformationMetadata metadata = RealMetadataFactory.fromDDL( ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("northwind.ddl")), "northwind", "nw"); EdmDataServices eds = ODataEntitySchemaBuilder.buildMetadata(metadata.getMetadataStore()); Client client = mock(Client.class); stub(client.getMetadataStore()).toReturn(metadata.getMetadataStore()); stub(client.getMetadata()).toReturn(eds); MockProvider.CLIENT = client; ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class); ArgumentCaptor<EdmEntitySet> entitySet = ArgumentCaptor.forClass(EdmEntitySet.class); OEntity entity = createCustomersEntity(eds); List<OEntity> result = new ArrayList<OEntity>(); result.add(entity); when(client.sqlExecute( anyString(), anyListOf(SQLParam.class), any(EdmEntitySet.class), anyMapOf(String.class, Boolean.class))) .thenReturn(result); ClientRequest request = new ClientRequest( TestPortProvider.generateURL( "/odata/northwind/Customers?$select=CustomerID,CompanyName,Address")); ClientResponse<String> response = request.get(String.class); verify(client) .sqlExecute( sql.capture(), anyListOf(SQLParam.class), entitySet.capture(), anyMapOf(String.class, Boolean.class)); Assert.assertEquals( "SELECT g0.Address, g0.CustomerID, g0.CompanyName FROM Customers AS g0", sql.getValue()); Assert.assertEquals(200, response.getStatus()); // Assert.assertEquals("", response.getEntity()); }
@Test public void testMetadata() throws Exception { TransformationMetadata metadata = RealMetadataFactory.fromDDL( ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("northwind.ddl")), "northwind", "nw"); EdmDataServices eds = ODataEntitySchemaBuilder.buildMetadata(metadata.getMetadataStore()); Client client = mock(Client.class); stub(client.getMetadataStore()).toReturn(metadata.getMetadataStore()); stub(client.getMetadata()).toReturn(eds); MockProvider.CLIENT = client; StringWriter sw = new StringWriter(); EdmxFormatWriter.write(eds, sw); ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/$metadata")); ClientResponse<String> response = request.get(String.class); Assert.assertEquals(200, response.getStatus()); Assert.assertEquals(sw.toString(), response.getEntity()); }
@Test public void testLobPeristence() throws Exception { BufferManager buffMgr = BufferManagerFactory.getStandaloneBufferManager(); FileStore fs = buffMgr.createFileStore("temp"); ClobType clob = new ClobType( new ClobImpl( new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ReaderInputStream( new StringReader("Clob contents One"), Charset.forName(Streamable.ENCODING)); } }, -1)); BlobType blob = new BlobType( new BlobImpl( new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ReaderInputStream( new StringReader("Blob contents Two"), Charset.forName(Streamable.ENCODING)); } })); BlobType blobEmpty = new BlobType( new BlobImpl( new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(new byte[0]); } })); FileStore fs1 = buffMgr.createFileStore("blob"); FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs1, Streamable.ENCODING); FileStoreOutputStream fsos = fsisf.getOuputStream(); byte[] b = new byte[DataTypeManager.MAX_LOB_MEMORY_BYTES + 1]; fsos.write(b); fsos.close(); BlobType blob1 = new BlobType(new BlobImpl(fsisf)); assertNotNull(blob1.getReferenceStreamId()); LobManager lobManager = new LobManager(new int[] {0, 1, 2, 3}, fs); lobManager.setMaxMemoryBytes(4); List<?> tuple = Arrays.asList(clob, blob, blob1, blobEmpty); lobManager.updateReferences(tuple, ReferenceMode.CREATE); assertNotNull(blob1.getReferenceStreamId()); lobManager.persist(); Streamable<?> lob = lobManager.getLobReference(clob.getReferenceStreamId()); assertTrue(lob.getClass().isAssignableFrom(ClobType.class)); ClobType clobRead = (ClobType) lob; assertEquals(ClobType.getString(clob), ClobType.getString(clobRead)); assertTrue(clobRead.length() != -1); lob = lobManager.getLobReference(blob.getReferenceStreamId()); assertTrue(lob.getClass().isAssignableFrom(BlobType.class)); BlobType blobRead = (BlobType) lob; assertTrue( Arrays.equals( ObjectConverterUtil.convertToByteArray(blob.getBinaryStream()), ObjectConverterUtil.convertToByteArray(blobRead.getBinaryStream()))); lobManager.updateReferences(tuple, ReferenceMode.REMOVE); assertEquals(0, lobManager.getLobCount()); }