Как я могу вставить документ в MongoDB через сэмплер JSR223, используя groovy? - PullRequest
0 голосов
/ 02 ноября 2019

Я успешно построил соединение от Jmeter к MongoDB через сэмплер JSR223 с использованием скриптов Groovy. Проблема возникает, когда я пытаюсь вставить документ. Вот мой скрипт

  import com.mongodb.MongoClientSettings;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.ServerAddress;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.client.MongoDatabase;
  import org.bson.Document;
  import java.util.Arrays;
  try {
  MongoClientSettings settings = MongoClientSettings.builder()
  .applyToClusterSettings {builder -> 
  builder.hosts(Arrays.asList(new
  ServerAddress(vars.get("mongoHost"),vars.get("mongoPort").
  toInteger())))}
  .build();
  MongoClient mongoClient = MongoClients.create(settings);
  MongoDatabase database = 
  mongoClient.getDatabase(vars.get("databaseName"));
  MongoCollection<Document> collection = 
  database.getCollection(vars.get("collectionName"));
  vars.putObject("collection", collection);
  return "Connected to " + vars.get("collectionName");



 db.getCollection(vars.get("collectionName")).insert(
 {"EmployeeOID":12345,"EmployeeName":"Test Automation through 
 Jmeter","Employee_Type_OID":4,"Rank":0,"Rating":0,
 "Score":0,"Supervisor_OID":56789,"CompanyID":"012345T"})
 }
 catch (Exception e) {
 SampleResult.setSuccessful(false);
 SampleResult.setResponseCode("500");
 SampleResult.setResponseMessage("Exception: " + e);
}

Я получаю следующую ошибку:

  Response code: 500
  Response message: javax.script.ScriptException: 
  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
  failed:
  Script62.groovy: 21: expecting '}', found ':' @ line 21, column 67.
  nName")).insert({"EmployeeOID":12345,"Em
                             ^
...