Package org.hibernate.ogm.datastore.mongodb.query.impl

Examples of org.hibernate.ogm.datastore.mongodb.query.impl.MongoDBQueryDescriptor


*/
public class MongoDBQueryDescriptorSerializationTest {

  @Test
  public void canSerializeAndDeserialize() throws Exception {
    MongoDBQueryDescriptor descriptor = new MongoDBQueryDescriptor(
        "test",
        Operation.FIND,
        new BasicDBObject( "foo", "bar" ),
        new BasicDBObject( "foo", 1 ),
        new BasicDBObject( "bar", 1 )
    );

    byte[] bytes = serialize( descriptor );
    MongoDBQueryDescriptor deserializedDescriptor = deserialize( bytes );

    assertThat( deserializedDescriptor.getCollectionName() ).isEqualTo( descriptor.getCollectionName() );
    assertThat( deserializedDescriptor.getOperation() ).isEqualTo( descriptor.getOperation() );
    assertThat( deserializedDescriptor.getCriteria() ).isEqualTo( descriptor.getCriteria() );
    assertThat( deserializedDescriptor.getProjection() ).isEqualTo( descriptor.getProjection() );
    assertThat( deserializedDescriptor.getOrderBy() ).isEqualTo( descriptor.getOrderBy() );
  }
View Full Code Here


    return outputStream.toByteArray();
  }

  private MongoDBQueryDescriptor deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ObjectInputStream inputStream = new ObjectInputStream( new ByteArrayInputStream( bytes ) );
    MongoDBQueryDescriptor deserializedDescriptor = (MongoDBQueryDescriptor) inputStream.readObject();
    return deserializedDescriptor;
  }
View Full Code Here

    return orderBy;
  }

  @Override
  public Object getQueryObject() {
    return new MongoDBQueryDescriptor(
      collectionName,
      Operation.FIND, //so far only SELECT is supported
      query,
      projection,
      orderBy
View Full Code Here

  public void shouldParseSimplifiedFindQuery() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "{ \"foo\" : true }");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();

    assertThat( queryDescriptor.getCollectionName() ).isNull();
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"foo\" : true }" ) );
    assertThat( queryDescriptor.getProjection() ).isNull();
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

  public void shouldParseSimpleQuery() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "db.Order.find({\"foo\":true})");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();

    assertThat( queryDescriptor.getCollectionName() ).isEqualTo( "Order" );
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"foo\" : true }" ) );
    assertThat( queryDescriptor.getProjection() ).isNull();
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

  public void shouldParseSimpleQueryUsingSingleQuotes() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "db.Order.find( { 'foo' : true } )");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();

    assertThat( queryDescriptor.getCollectionName() ).isEqualTo( "Order" );
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"foo\" : true }" ) );
    assertThat( queryDescriptor.getProjection() ).isNull();
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

  public void shouldParseQueryWithEmptyFind() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "db.Order.find({})");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( new BasicDBObject() );
  }
View Full Code Here

  public void shouldParseQueryWithProjection() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "db.Order.find( { \"foo\" : true }, { \"foo\" : 1 } )");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();

    assertThat( queryDescriptor.getCollectionName() ).isEqualTo( "Order" );
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"foo\" : true }" ) );
    assertThat( queryDescriptor.getProjection() ).isEqualTo( JSON.parse( "{ \"foo\" : 1 }" ) );
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

  public void shouldParseQueryWithWhitespace() {
    NativeQueryParser parser = Parboiled.createParser( NativeQueryParser.class );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() )
        .run( "  db  .  Order  .  find  (  {  \"  foo  \"  :  true  }  ,  {  \"foo\"  :  1  }  )  ");

    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();
    assertThat( run.hasErrors() ).isFalse();
    assertThat( queryDescriptor.getCollectionName() ).isEqualTo( "Order" );
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"  foo  \" : true }" ) );
    assertThat( queryDescriptor.getProjection() ).isEqualTo( JSON.parse( "{ \"foo\" : 1 }" ) );
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

    ReportingParseRunner<MongoDBQueryDescriptorBuilder> runner = new ReportingParseRunner<MongoDBQueryDescriptorBuilder>( parser.Query() );
    ParsingResult<MongoDBQueryDescriptorBuilder> run =  runner
        .run( "db.Order.find( { \"foo\" : true, \"bar\" : 42, \"baz\" : \"qux\" } )");

    assertThat( run.hasErrors() ).isFalse();
    MongoDBQueryDescriptor queryDescriptor = run.resultValue.build();

    assertThat( queryDescriptor.getCollectionName() ).isEqualTo( "Order" );
    assertThat( queryDescriptor.getOperation() ).isEqualTo( Operation.FIND );
    assertThat( queryDescriptor.getCriteria() ).isEqualTo( JSON.parse( "{ \"foo\" : true, \"bar\" : 42, \"baz\" : \"qux\" }" ) );
    assertThat( queryDescriptor.getProjection() ).isNull();
    assertThat( queryDescriptor.getOrderBy() ).isNull();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.ogm.datastore.mongodb.query.impl.MongoDBQueryDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.