UpdateDBObjectBuilder { public void addUpdateRelation(Selector left, Operator operator, Selector right) throws ExecutionException { BasicDBObject basicDBObject; switch (operator) { case EQ: case ASSIGN: if (containsAnInnerRelation(left, right)) { Relation innerRelation = ((RelationSelector) right).getRelation(); addUpdateRelation(innerRelation.getLeftTerm(), innerRelation.getOperator(), innerRelation.getRightTerm()); } else { if (relations.containsField(SET_COMMAND)) { basicDBObject = (BasicDBObject) relations.get(SET_COMMAND); basicDBObject.putAll(getBasicRelation(left, right)); } else { basicDBObject = new BasicDBObject(); basicDBObject.putAll(getBasicRelation(left, right)); relations.put(SET_COMMAND, basicDBObject); } } break; case ADD: if (relations.containsField(INCREMENT_COMMAND)) { basicDBObject = (BasicDBObject) relations.get(INCREMENT_COMMAND); basicDBObject.putAll(getIncrementalRelation(left, right, false)); } else { basicDBObject = new BasicDBObject(); basicDBObject.putAll(getIncrementalRelation(left, right, false)); relations.put(INCREMENT_COMMAND, basicDBObject); } break; case SUBTRACT: if (relations.containsField(INCREMENT_COMMAND)) { basicDBObject = (BasicDBObject) relations.get(INCREMENT_COMMAND); basicDBObject.putAll(getIncrementalRelation(left, right, true)); } else { basicDBObject = new BasicDBObject(); basicDBObject.putAll(getIncrementalRelation(left, right, true)); relations.put(INCREMENT_COMMAND, basicDBObject); } break; case MULTIPLICATION: if (relations.containsField(MUTLTIPLICATION_COMMAND)) { basicDBObject = (BasicDBObject) relations.get(MUTLTIPLICATION_COMMAND); basicDBObject.putAll(getNumberRelation(left, right)); } else { basicDBObject = new BasicDBObject(); basicDBObject.putAll(getNumberRelation(left, right)); relations.put(MUTLTIPLICATION_COMMAND, basicDBObject); } break; default: String msg = "Operator: " + operator + " is not supported in update queries"; LOGGER.error(msg); throw new MongoValidationException(msg); } } UpdateDBObjectBuilder(); void addUpdateRelation(Selector left, Operator operator, Selector right); DBObject build();  }
@Test public void addUpdateRelationtest() throws UnsupportedException, ExecutionException { UpdateDBObjectBuilder updateDBObjectBuilder = new UpdateDBObjectBuilder(); Assert.assertEquals("The update query should be empty", new BasicDBObject(), updateDBObjectBuilder.build()); Relation rel = getBasicRelation(COLUMN_NAME, Operator.ASSIGN, STRING_VALUE); updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); BasicDBObject updatedExpected = new BasicDBObject(); updatedExpected.append("$set", new BasicDBObject(COLUMN_NAME, STRING_VALUE)); Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), new StringSelector( STRING_VALUE_OTHER)); updatedExpected.append("$set", new BasicDBObject(COLUMN_NAME, STRING_VALUE_OTHER)); Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); Relation rel2 = getBasicRelation(OTHER_COLUMN_NAME, Operator.SUBTRACT, 20l); updateDBObjectBuilder.addUpdateRelation(rel2.getLeftTerm(), rel2.getOperator(), rel2.getRightTerm()); updatedExpected.append("$inc", new BasicDBObject(OTHER_COLUMN_NAME, -20l)); Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); }