@Test public void updateTest() throws Exception { MongoConnectionHandler connectionHandler = Mockito.mock(MongoConnectionHandler.class); Connection<MongoClient> connection = Mockito.mock(Connection.class); MongoClient client = Mockito.mock(MongoClient.class); DB database = Mockito.mock(DB.class); DBCollection collection = Mockito.mock(DBCollection.class); MongoStorageEngine mongoStorageEngine; when(connectionHandler.getConnection(CLUSTER_NAME)).thenReturn(connection); when(connection.getNativeConnection()).thenReturn(client); mongoStorageEngine = MongoStorageEngine.getInstance((MongoConnectionHandler) connectionHandler); when(client.getDB(DB_NAME)).thenReturn(database); TableName tableName = new TableName(DB_NAME, COLLECTION_NAME); when(client.getDB(DB_NAME)).thenReturn(database); when(database.getCollection(COLLECTION_NAME)).thenReturn(collection); Relation rel1 = getBasicRelation(COLUMN_NAME, Operator.ASSIGN, CELL_VALUE); Relation rel2 = getBasicRelation(OTHER_COLUMN_NAME, Operator.ASSIGN, OTHER_CELL_VALUE); Collection<Relation> assignments = Arrays.asList(rel1, rel2); Set<Operations> operations = new HashSet<>(); operations.add(Operations.FILTER_INDEXED_GT); Collection<Filter> whereClauses = Arrays.asList(new Filter(operations, getBasicRelation( COLUMN_NAME, Operator.GET, 20))); UpdateDBObjectBuilder updateDBObjectBuilder = mock(UpdateDBObjectBuilder.class); PowerMockito.whenNew(UpdateDBObjectBuilder.class).withNoArguments().thenReturn(updateDBObjectBuilder); mongoStorageEngine.update(tableName, assignments, whereClauses, connection); verify(updateDBObjectBuilder, times(2)).addUpdateRelation(Matchers.any(Selector.class), Matchers.any(Operator.class), Matchers.any(Selector.class)); verify(updateDBObjectBuilder, times(1)).addUpdateRelation(rel1.getLeftTerm(), rel1.getOperator(), rel1.getRightTerm()); verify(updateDBObjectBuilder, times(1)).addUpdateRelation(rel2.getLeftTerm(), rel2.getOperator(), rel2.getRightTerm()); verify(collection, times(1)).update(Matchers.any(BasicDBObject.class), Matchers.any(BasicDBObject.class), Matchers.eq(false), Matchers.eq(true)); PowerMockito.verifyPrivate(mongoStorageEngine, times(1)).invoke("buildFilter", whereClauses); }
@Override protected void update(TableName tableName, Collection<Relation> assignments, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection coll = db.getCollection(tableName.getName()); UpdateDBObjectBuilder updateBuilder = new UpdateDBObjectBuilder(); for (Relation rel : assignments) { updateBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); } try { coll.update(buildFilter(whereClauses), updateBuilder.build(), false, true); } catch (MongoException e) { logger.error("Error updating the data: " + e.getMessage()); throw new ExecutionException(e.getMessage(), e); } }
MongoStorageEngine extends CommonsStorageEngine<MongoClient> { @Override protected void update(TableName tableName, Collection<Relation> assignments, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection coll = db.getCollection(tableName.getName()); UpdateDBObjectBuilder updateBuilder = new UpdateDBObjectBuilder(); for (Relation rel : assignments) { updateBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); } try { coll.update(buildFilter(whereClauses), updateBuilder.build(), false, true); } catch (MongoException e) { logger.error("Error updating the data: " + e.getMessage()); throw new ExecutionException(e.getMessage(), e); } } }
MongoStorageEngine extends CommonsStorageEngine<MongoClient> { @Override protected void update(TableName tableName, Collection<Relation> assignments, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection coll = db.getCollection(tableName.getName()); UpdateDBObjectBuilder updateBuilder = new UpdateDBObjectBuilder(); for (Relation rel : assignments) { updateBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); } try { coll.update(buildFilter(whereClauses), updateBuilder.build(), false, true); } catch (MongoException e) { logger.error("Error updating the data: " + e.getMessage()); throw new ExecutionException(e.getMessage(), e); } } private  MongoStorageEngine(MongoConnectionHandler connectionHandler); }
MongoStorageEngine extends CommonsStorageEngine<MongoClient> { @Override protected void update(TableName tableName, Collection<Relation> assignments, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection coll = db.getCollection(tableName.getName()); UpdateDBObjectBuilder updateBuilder = new UpdateDBObjectBuilder(); for (Relation rel : assignments) { updateBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); } try { coll.update(buildFilter(whereClauses), updateBuilder.build(), false, true); } catch (MongoException e) { logger.error("Error updating the data: " + e.getMessage()); throw new ExecutionException(e.getMessage(), e); } } private  MongoStorageEngine(MongoConnectionHandler connectionHandler); static MongoStorageEngine getInstance(MongoConnectionHandler connectionHandler); }
MongoStorageEngine extends CommonsStorageEngine<MongoClient> { @Override protected void update(TableName tableName, Collection<Relation> assignments, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); DBCollection coll = db.getCollection(tableName.getName()); UpdateDBObjectBuilder updateBuilder = new UpdateDBObjectBuilder(); for (Relation rel : assignments) { updateBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); } try { coll.update(buildFilter(whereClauses), updateBuilder.build(), false, true); } catch (MongoException e) { logger.error("Error updating the data: " + e.getMessage()); throw new ExecutionException(e.getMessage(), e); } } private  MongoStorageEngine(MongoConnectionHandler connectionHandler); static MongoStorageEngine getInstance(MongoConnectionHandler connectionHandler);  }