MongodbBuilder extends AbstractBuilder { @Override public MongodbQueryResult build(String query) throws IOException, ParserNotFoundException { DBObject result = (DBObject) super.parse(query); return new MongodbQueryResult(query, result); } MongodbBuilder(IGroupParser groupParser, List<IRuleParser> ruleParsers, List<IRuleFilter> ruleFilters); @Override MongodbQueryResult build(String query);  }
@Test public void testOperatorEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-equal.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":\"Mistic\"}]}" ); }
@Test public void testOperatorNotEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-equal.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$ne\":\"Mistic\"}}]}" ); }
@Test public void testOperatorIn() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-in.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$in\":[1,5,10]}}]}" ); }
@Test public void testOperatorNotIn() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-in.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$nin\":[1,5,10]}}]}" ); }
@Test public void testOperatorLess() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-less.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$lt\":50}}]}" ); }
@Test public void testOperatorLessOrEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-less-or-equal.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$lte\":50}}]}" ); }
@Test public void testOperatorGreater() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-greater.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$gt\":50}}]}" ); }
@Test public void testOperatorGreaterOrEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-greater-or-equal.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$gte\":50}}]}" ); }
@Test public void testOperatorBetween() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-between.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$gte\":5,\"$lte\":10}}]}" ); }
@Test public void testOperatorNotBetween() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-between.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"age\":{\"$lt\":5,\"$gt\":10}}]}" ); }
@Test public void testOperatorBeginsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-begins-with.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"^Mistic\"}}]}" ); }
@Test public void testOperatorNotBeginsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-begins-with.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"^(?!Mistic)\"}}]}" ); }
@Test public void testOperatorContains() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-contains.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"Mistic\"}}]}" ); }
@Test public void testOperatorNotContains() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-contains.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"^((?!Mistic).)*$\",\"$options\":\"s\"}}]}" ); }
@Test public void testOperatorEndsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-ends-with.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"Mistic$\"}}]}" ); }
@Test public void testOperatorNotEndsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-ends-with.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$regex\":\"(?<!Mistic)$\"}}]}" ); }
@Test public void testOperatorIsEmpty() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-empty.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":\"\"}]}" ); }
@Test public void testOperatorIsNotEmpty() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-not-empty.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$ne\":\"\"}}]}" ); }
@Test public void testOperatorIsNull() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-null.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":null}]}" ); }
@Test public void testOperatorIsNotNull() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-not-null.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":{\"$ne\":null}}]}" ); }
@Test public void testConditionAnd() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-and-1.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$and\":[{\"username\":\"Mistic\"}]}" ); json = FileHelper.getStringFrom("tasks/condition-and-more.json"); result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$and\":[{\"username\":\"Mistic\"},{\"age\":10}]}" ); }
@Test public void testConditionOr() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-or-1.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":\"Mistic\"}]}" ); json = FileHelper.getStringFrom("tasks/condition-or-more.json"); result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$or\":[{\"username\":\"Mistic\"},{\"age\":10}]}" ); }
@Test public void testConditionNot() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-not-and-1.json"); MongodbQueryResult result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$nor\":[{\"$and\":[{\"username\":\"Mistic\"}]}]}" ); json = FileHelper.getStringFrom("tasks/condition-not-and-more.json"); result = builder.build(json); Assert.assertEquals( StringUtils.trimAllWhitespace(result.toString()), "{\"$nor\":[{\"$and\":[{\"username\":\"Mistic\"},{\"age\":10}]}]}" ); }