@Test
  public void testSchema() throws Exception {
    translator = new ODataExecutionFactory();
    translator.start();

    MetadataFactory mf = tripPinMetadata();
    TransformationMetadata metadata = getTransformationMetadata(mf, this.translator);

    String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    // System.out.println(ddl);

    MetadataFactory mf2 =
        new MetadataFactory(
            null,
            1,
            "northwind",
            SystemMetadata.getInstance().getRuntimeTypeMap(),
            new Properties(),
            null);
    QueryParser.getQueryParser().parseDDL(mf2, ddl);

    Procedure p = mf.getSchema().getProcedure("ResetDataSource");
    assertNotNull(p);
    assertEquals(0, p.getParameters().size());
  }
Esempio n. 2
0
 public static String getDDLString(
     Schema schema, EnumSet<SchemaObjectType> types, String regexPattern) {
   DDLStringVisitor visitor = new DDLStringVisitor(types, regexPattern);
   visitor.visit(schema);
   return visitor.toString();
 }
  @Test
  public void testMetadata() throws TranslatorException {
    MongoDBMetadataProcessor mp = new MongoDBMetadataProcessor();

    MetadataFactory mf =
        new MetadataFactory(
            "vdb",
            1,
            "mongodb",
            SystemMetadata.getInstance().getRuntimeTypeMap(),
            new Properties(),
            null);
    MongoDBConnection conn = Mockito.mock(MongoDBConnection.class);
    DBCollection tableDBCollection = Mockito.mock(DBCollection.class);
    DBCollection embeddedDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyFirstDBCollection = Mockito.mock(DBCollection.class);
    LinkedHashSet<String> tables = new LinkedHashSet<String>();
    tables.add("table");
    tables.add("embedded");
    tables.add("empty");
    tables.add("emptyFirst");

    DB db = Mockito.mock(DB.class);

    BasicDBList array = new BasicDBList();
    array.add("one");
    array.add("two");

    BasicDBObject row = new BasicDBObject();
    row.append("_id", new Integer(1));
    row.append("col2", new Double(2.0));
    row.append("col3", new Long(3L));
    row.append("col5", Boolean.TRUE);
    row.append("col6", new Date(0L));
    row.append("col6", new DBRef(db, "ns", "one"));
    row.append("col7", array);
    row.append("col8", new Binary("binary".getBytes()));

    BasicDBObject child = new BasicDBObject();
    child.append("col1", "one");
    child.append("col2", "two");
    row.append("child", child);

    BasicDBObject emptyFirstRow = new BasicDBObject();
    emptyFirstRow.append("_id", new Integer(1));
    emptyFirstRow.append("col2", new Double(2.0));
    emptyFirstRow.append("col3", new Long(3L));

    BasicDBObject embedded = new BasicDBObject();
    embedded.append("col1", "one");
    embedded.append("col2", "two");
    row.append("embedded", embedded);

    Mockito.stub(db.getCollectionNames()).toReturn(tables);
    Mockito.stub(db.getCollection(Mockito.eq("table"))).toReturn(tableDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("embedded"))).toReturn(embeddedDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("empty"))).toReturn(emptyDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("emptyFirst"))).toReturn(emptyFirstDBCollection);

    DBCursor tableCursor = Mockito.mock(DBCursor.class);
    Mockito.when(tableCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(tableCursor.next()).thenReturn(row);
    Mockito.when(tableDBCollection.find()).thenReturn(tableCursor);

    DBCursor embeddedCursor = Mockito.mock(DBCursor.class);
    Mockito.when(embeddedCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(embeddedCursor.next()).thenReturn(child);
    Mockito.when(embeddedDBCollection.find()).thenReturn(embeddedCursor);

    DBCursor emptyFirstCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyFirstCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(emptyFirstCursor.next()).thenReturn(null).thenReturn(emptyFirstRow);
    Mockito.when(emptyFirstDBCollection.find()).thenReturn(emptyFirstCursor);

    DBCursor emptyCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(emptyCursor.next()).thenReturn(null);
    Mockito.when(emptyDBCollection.find()).thenReturn(emptyCursor);

    Mockito.stub(conn.getDatabase()).toReturn(db);

    mp.process(mf, conn);

    String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    String expected =
        "SET NAMESPACE 'http://www.teiid.org/translator/mongodb/2013' AS teiid_mongo;\n\n"
            + "CREATE FOREIGN TABLE child (\n"
            + "\tcol1 string,\n"
            + "\tcol2 string,\n"
            + "\t\"_id\" integer OPTIONS (UPDATABLE FALSE),\n"
            + "\tCONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n"
            + "\tFOREIGN KEY(\"_id\") REFERENCES \"table\" \n"
            + ") OPTIONS (UPDATABLE TRUE, \"teiid_mongo:MERGE\" 'table');\n"
            + "\n"
            + "CREATE FOREIGN TABLE embedded (\n"
            + "\tcol1 string,\n"
            + "\tcol2 string,\n"
            + "\t\"_id\" integer OPTIONS (UPDATABLE FALSE),\n"
            + "\tCONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n"
            + "\tFOREIGN KEY(\"_id\") REFERENCES \"table\" \n"
            + ") OPTIONS (UPDATABLE TRUE, \"teiid_mongo:EMBEDDABLE\" 'true');\n"
            + "\n"
            + "CREATE FOREIGN TABLE emptyFirst (\n"
            + "\t\"_id\" integer,\n"
            + "\tcol2 double,\n"
            + "\tcol3 long,\n"
            + "\tCONSTRAINT PK0 PRIMARY KEY(\"_id\")\n"
            + ") OPTIONS (UPDATABLE TRUE);\n"
            + "\n"
            + "CREATE FOREIGN TABLE \"table\" (\n"
            + "\t\"_id\" integer,\n"
            + "\tcol2 double,\n"
            + "\tcol3 long,\n"
            + "\tcol5 boolean,\n"
            + "\tcol6 string,\n"
            + "\tcol7 object[] OPTIONS (SEARCHABLE 'Unsearchable'),\n"
            + "\tcol8 varbinary,\n"
            + "\tCONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n"
            + "\tCONSTRAINT FK_col6 FOREIGN KEY(col6) REFERENCES ns \n"
            + ") OPTIONS (UPDATABLE TRUE);";
    assertEquals(expected, metadataDDL);
  }
  @Test
  public void testMetadata() throws TranslatorException {
    MongoDBMetadataProcessor mp = new MongoDBMetadataProcessor();

    MetadataFactory mf =
        new MetadataFactory(
            "vdb",
            1,
            "mongodb",
            SystemMetadata.getInstance().getRuntimeTypeMap(),
            new Properties(),
            null);
    MongoDBConnection conn = Mockito.mock(MongoDBConnection.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    HashSet<String> tables = new HashSet<String>();
    tables.add("table");

    DB db = Mockito.mock(DB.class);

    BasicDBList array = new BasicDBList();
    array.add("one");
    array.add("two");

    BasicDBObject row = new BasicDBObject();
    row.append("_id", new Integer(1));
    row.append("col2", new Double(2.0));
    row.append("col3", new Long(3L));
    row.append("col5", Boolean.TRUE);
    row.append("col6", new Date(0L));
    row.append("col6", new DBRef(db, "ns", "one"));
    row.append("col7", array);

    BasicDBObject child = new BasicDBObject();
    child.append("col1", "one");
    child.append("col2", "two");

    row.append("child", child);

    Mockito.stub(db.getCollectionNames()).toReturn(tables);
    Mockito.stub(db.getCollection(Mockito.anyString())).toReturn(dbCollection);
    Mockito.stub(dbCollection.findOne()).toReturn(row);
    Mockito.stub(conn.getDatabase()).toReturn(db);

    mp.process(mf, conn);

    String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    String expected =
        "CREATE FOREIGN TABLE child (\n"
            + "\tcol1 string,\n"
            + "\tcol2 string\n"
            + ") OPTIONS (UPDATABLE TRUE, \"teiid_mongo:MERGE\" 'table');\n"
            + "\n"
            + "CREATE FOREIGN TABLE \"table\" (\n"
            + "\t\"_id\" integer,\n"
            + "\tcol2 double,\n"
            + "\tcol3 long,\n"
            + "\tcol5 boolean,\n"
            + "\tcol6 string,\n"
            + "\tcol7 string[] OPTIONS (SEARCHABLE 'Unsearchable'),\n"
            + "\tCONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n"
            + "\tCONSTRAINT FK_col6 FOREIGN KEY(col6) REFERENCES ns \n"
            + ") OPTIONS (UPDATABLE TRUE);";
    assertEquals(expected, metadataDDL);
  }