[Mongo] Added default values for query for Find, Count and Distinct commands (#6960)
* Added smart defaults for query in Count, Distinct & Find commands * Added test cases for smart inputs for query and limit for find, and query for count and distinct
This commit is contained in:
parent
8dc0c81488
commit
23ce13ce98
|
|
@ -28,24 +28,16 @@ public class Count extends MongoCommand {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isValid() {
|
||||
if (super.isValid()) {
|
||||
if (!StringUtils.isNullOrEmpty(query)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
fieldNamesWithNoConfiguration.add("Query");
|
||||
}
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document parseCommand() {
|
||||
Document document = new Document();
|
||||
|
||||
document.put("count", this.collection);
|
||||
|
||||
if (StringUtils.isNullOrEmpty(this.query)) {
|
||||
this.query = "{}";
|
||||
}
|
||||
|
||||
document.put("query", parseSafely("Query", this.query));
|
||||
|
||||
return document;
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ public class Delete extends MongoCommand {
|
|||
if (!StringUtils.isNullOrEmpty(query)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
// Not adding smart defaults for query due to data impact
|
||||
fieldNamesWithNoConfiguration.add("Query");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,15 +37,10 @@ public class Distinct extends MongoCommand {
|
|||
@Override
|
||||
public Boolean isValid() {
|
||||
if (super.isValid()) {
|
||||
if (!StringUtils.isNullOrEmpty(query) && !StringUtils.isNullOrEmpty(key)) {
|
||||
if (!StringUtils.isNullOrEmpty(key)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
if (StringUtils.isNullOrEmpty(query)) {
|
||||
fieldNamesWithNoConfiguration.add("Query");
|
||||
}
|
||||
if (StringUtils.isNullOrEmpty(key)) {
|
||||
} else if (StringUtils.isNullOrEmpty(key)) {
|
||||
fieldNamesWithNoConfiguration.add("Key/Field");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,6 +53,10 @@ public class Distinct extends MongoCommand {
|
|||
|
||||
document.put("distinct", this.collection);
|
||||
|
||||
if (StringUtils.isNullOrEmpty(this.query)) {
|
||||
this.query = "{}";
|
||||
}
|
||||
|
||||
document.put("query", parseSafely("Query", this.query));
|
||||
|
||||
document.put("key", this.key);
|
||||
|
|
|
|||
|
|
@ -63,23 +63,14 @@ public class Find extends MongoCommand {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isValid() {
|
||||
if (super.isValid()) {
|
||||
if (!StringUtils.isNullOrEmpty(query)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
fieldNamesWithNoConfiguration.add("Query");
|
||||
}
|
||||
}
|
||||
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document parseCommand() {
|
||||
Document document = new Document();
|
||||
|
||||
if (StringUtils.isNullOrEmpty(this.query)) {
|
||||
this.query = "{}";
|
||||
}
|
||||
|
||||
document.put("find", this.collection);
|
||||
|
||||
document.put("filter", parseSafely("Query", this.query));
|
||||
|
|
|
|||
|
|
@ -62,9 +62,11 @@ public class UpdateMany extends MongoCommand {
|
|||
if (!StringUtils.isNullOrEmpty(query) && !StringUtils.isNullOrEmpty(update)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
// Not adding smart defaults for query due to data impact
|
||||
if (StringUtils.isNullOrEmpty(query)) {
|
||||
fieldNamesWithNoConfiguration.add("Query");
|
||||
}
|
||||
// Not adding smart defaults for query due to data impact
|
||||
if (StringUtils.isNullOrEmpty(update)) {
|
||||
fieldNamesWithNoConfiguration.add("Update");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1524,4 +1524,115 @@ public class MongoPluginTest {
|
|||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormSmartInputFind() {
|
||||
DatasourceConfiguration datasourceConfiguration = createDatasourceConfiguration();
|
||||
|
||||
ActionConfiguration actionConfiguration = new ActionConfiguration();
|
||||
|
||||
Map<Integer, Object> configMap = new HashMap<>();
|
||||
configMap.put(SMART_BSON_SUBSTITUTION, Boolean.TRUE);
|
||||
configMap.put(COMMAND, "FIND");
|
||||
// Skip adding the query
|
||||
configMap.put(FIND_SORT, "{ id: {{Input2.text}} }");
|
||||
// Skip adding limit
|
||||
configMap.put(COLLECTION, "{{Input4.text}}");
|
||||
|
||||
actionConfiguration.setPluginSpecifiedTemplates(generateMongoFormConfigTemplates(configMap));
|
||||
|
||||
ExecuteActionDTO executeActionDTO = new ExecuteActionDTO();
|
||||
List<Param> params = new ArrayList<>();
|
||||
Param param1 = new Param();
|
||||
param1.setKey("Input2.text");
|
||||
param1.setValue("1");
|
||||
params.add(param1);
|
||||
Param param2 = new Param();
|
||||
param2.setKey("Input4.text");
|
||||
param2.setValue("users");
|
||||
params.add(param2);
|
||||
executeActionDTO.setParams(params);
|
||||
|
||||
Mono<MongoClient> dsConnectionMono = pluginExecutor.datasourceCreate(datasourceConfiguration);
|
||||
Mono<ActionExecutionResult> executeMono = dsConnectionMono.flatMap(conn -> pluginExecutor.executeParameterized(conn,
|
||||
executeActionDTO,
|
||||
datasourceConfiguration,
|
||||
actionConfiguration));
|
||||
|
||||
StepVerifier.create(executeMono)
|
||||
.assertNext(obj -> {
|
||||
ActionExecutionResult result = obj;
|
||||
assertNotNull(result);
|
||||
assertTrue(result.getIsExecutionSuccess());
|
||||
assertNotNull(result.getBody());
|
||||
assertEquals(3, ((ArrayNode) result.getBody()).size());
|
||||
|
||||
assertEquals(
|
||||
List.of(new ParsedDataType(JSON), new ParsedDataType(RAW)).toString(),
|
||||
result.getDataTypes().toString()
|
||||
);
|
||||
|
||||
String expectedQuery = "{\"find\": \"users\", \"filter\": {}, \"sort\": {\"id\": 1}, \"limit\": 10, \"batchSize\": 10}";
|
||||
assertEquals(expectedQuery,
|
||||
((RequestParamDTO)(((List)result.getRequest().getRequestParams())).get(0)).getValue());
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormSmartInputCount() {
|
||||
DatasourceConfiguration dsConfig = createDatasourceConfiguration();
|
||||
Mono<MongoClient> dsConnectionMono = pluginExecutor.datasourceCreate(dsConfig);
|
||||
|
||||
ActionConfiguration actionConfiguration = new ActionConfiguration();
|
||||
|
||||
Map<Integer, Object> configMap = new HashMap<>();
|
||||
configMap.put(SMART_BSON_SUBSTITUTION, Boolean.TRUE);
|
||||
configMap.put(COMMAND, "COUNT");
|
||||
configMap.put(COLLECTION, "users");
|
||||
// Skip adding the query
|
||||
|
||||
actionConfiguration.setPluginSpecifiedTemplates(generateMongoFormConfigTemplates(configMap));
|
||||
|
||||
Mono<Object> executeMono = dsConnectionMono.flatMap(conn -> pluginExecutor.executeParameterized(conn, new ExecuteActionDTO(), dsConfig, actionConfiguration));
|
||||
StepVerifier.create(executeMono)
|
||||
.assertNext(obj -> {
|
||||
ActionExecutionResult result = (ActionExecutionResult) obj;
|
||||
assertNotNull(result);
|
||||
assertTrue(result.getIsExecutionSuccess());
|
||||
assertNotNull(result.getBody());
|
||||
JsonNode value = ((ObjectNode) result.getBody()).get("n");
|
||||
assertEquals(value.asInt(), 3);
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormSmartInputDistinct() {
|
||||
DatasourceConfiguration dsConfig = createDatasourceConfiguration();
|
||||
Mono<MongoClient> dsConnectionMono = pluginExecutor.datasourceCreate(dsConfig);
|
||||
|
||||
ActionConfiguration actionConfiguration = new ActionConfiguration();
|
||||
|
||||
Map<Integer, Object> configMap = new HashMap<>();
|
||||
configMap.put(SMART_BSON_SUBSTITUTION, Boolean.TRUE);
|
||||
configMap.put(COMMAND, "DISTINCT");
|
||||
configMap.put(COLLECTION, "users");
|
||||
// Skip adding the query
|
||||
configMap.put(DISTINCT_KEY, "name");
|
||||
|
||||
actionConfiguration.setPluginSpecifiedTemplates(generateMongoFormConfigTemplates(configMap));
|
||||
|
||||
Mono<Object> executeMono = dsConnectionMono.flatMap(conn -> pluginExecutor.executeParameterized(conn, new ExecuteActionDTO(), dsConfig, actionConfiguration));
|
||||
StepVerifier.create(executeMono)
|
||||
.assertNext(obj -> {
|
||||
ActionExecutionResult result = (ActionExecutionResult) obj;
|
||||
assertNotNull(result);
|
||||
assertTrue(result.getIsExecutionSuccess());
|
||||
assertNotNull(result.getBody());
|
||||
int valuesSize = ((ArrayNode) result.getBody()).size();
|
||||
assertEquals(valuesSize, 3);
|
||||
})
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
1
app/util/plugin-generation/.gitignore
vendored
Normal file
1
app/util/plugin-generation/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
node_modules/
|
||||
Loading…
Reference in New Issue
Block a user