當前位置: 首頁>>代碼示例>>Java>>正文


Java ObjectId.toString方法代碼示例

本文整理匯總了Java中org.bson.types.ObjectId.toString方法的典型用法代碼示例。如果您正苦於以下問題:Java ObjectId.toString方法的具體用法?Java ObjectId.toString怎麽用?Java ObjectId.toString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.bson.types.ObjectId的用法示例。


在下文中一共展示了ObjectId.toString方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testUpdateGroup

import org.bson.types.ObjectId; //導入方法依賴的package包/類
/**
 * Add a new group object to the database. Call PUT using the id of the new mongo object to update
 * the group information (name, members list, occasions list). Verify that the group information
 * has been updated
 *
 * @throws GeneralSecurityException
 */
@Test
public void testUpdateGroup() throws IOException, GeneralSecurityException {
  System.out.println("\nStarting testUpdateGroup");

  // Create group in database
  Group group = new Group(null, "testGroup", new String[] {"12345", "23456"});
  BasicDBObject dbGroup = group.getDBObject(false);
  db.getCollection(Group.DB_COLLECTION_NAME).insert(dbGroup);
  group.setId(dbGroup.getObjectId(Group.DB_ID).toString());

  // Create updated group
  ObjectId groupId = dbGroup.getObjectId(Group.DB_ID);
  Group newGroup = new Group(groupId.toString(), "newTestGroup", new String[] {"12345"});
  String url = groupServiceURL + "/" + groupId;
  makeConnection("PUT", url, newGroup.getJson(), 200);

  // Verify that the new group information is in mongo
  BasicDBObject newDBGroup = (BasicDBObject) db.getCollection("groups").findOne(groupId);
  assertNotNull("Group testGroup was not found in the database.", newDBGroup);
  assertTrue(
      "Group in database does not contain the expected data", newGroup.isEqual(newDBGroup));
}
 
開發者ID:OpenLiberty,項目名稱:sample-acmegifts,代碼行數:30,代碼來源:GroupResourceTest.java

示例2: insertCityItem

import org.bson.types.ObjectId; //導入方法依賴的package包/類
/**
 *
 * @param cityItem Wrapper object for JSON values
 * @return id of inserted document
 */
public static String insertCityItem(CityItem cityItem)
{
    ObjectId id = new ObjectId();
    Document post = new Document();
    post.put("_id",id);
    post.put("type", Integer.toString(cityItem.getType()));
    post.put("location",new Document("x",String.valueOf(cityItem.getLongitude()))
            .append("y",String.valueOf(cityItem.getLatitude())));
    post.put("description", cityItem.getDescription());
    MongoCollection<Document> collection = Configurator.INSTANCE.getDatabase().getCollection(COLLECTION);
    try {
        collection.insertOne(post);
        return id.toString();
    } catch (DuplicateKeyException e) {
        e.printStackTrace();
        return "-1";
    }
}
 
開發者ID:Labas-Vakaras,項目名稱:Smart_City,代碼行數:24,代碼來源:CityItemDAO.java

示例3: testDuplicateKeyFailureOrderedFalse

import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void testDuplicateKeyFailureOrderedFalse() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());
    addMongoService(runner);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "insert_test");
    runner.setProperty(MongoProps.BATCH_SIZE, "10");
    runner.setProperty(MongoProps.ORDERED, "false");

    ObjectId objectId = ObjectId.get();

    String success = FileUtils.readFileToString(Paths.get("src/test/resources/payload.json").toFile());
    String successTwo = "{\"a\":\"a\"}";
    String payloadOne = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"first value\"}";
    String payloadTwo = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"second value\"}";

    runner.enqueue(payloadOne.getBytes());
    runner.enqueue(success.getBytes());
    runner.enqueue(payloadTwo.getBytes());
    runner.enqueue(successTwo.getBytes()); // add another successful message

    runner.run();

    runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 1);
    runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 3);

    FlowFile failure = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_FAILURE).get(0);
    String errorCode = failure.getAttribute("mongo.errorcode");
    assertEquals("11000", errorCode); // duplicate key error code
    String errorMessage = failure.getAttribute("mongo.errormessage");
    assertTrue(StringUtils.isNotBlank(errorMessage));

    // Test contents of mongo
    MongoCollection<Document> collection = mongo.getMongoClient().getDatabase(MONGO_DATABASE_NAME).getCollection("insert_test");
    assertEquals(3L, collection.count());
}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:37,代碼來源:StoreInMongoIT.java

示例4: testDuplicateKeyFailureWithoutBatching

import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void testDuplicateKeyFailureWithoutBatching() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());
    addMongoService(runner);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "insert_test");
    runner.setProperty(MongoProps.BATCH_SIZE, "1");
    runner.setProperty(MongoProps.ORDERED, "true");

    ObjectId objectId = ObjectId.get();

    String success = FileUtils.readFileToString(Paths.get("src/test/resources/payload.json").toFile());
    String successTwo = "{\"a\":\"a\"}";
    String payloadOne = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"first value\"}";
    String payloadTwo = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"second value\"}";

    runner.enqueue(payloadOne.getBytes());
    runner.enqueue(success.getBytes());
    runner.enqueue(payloadTwo.getBytes());
    runner.enqueue(successTwo.getBytes()); // add another successful message

    runner.run(runner.getQueueSize().getObjectCount()); // run through the entire queue

    runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 1);
    runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 3);

    FlowFile failure = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_FAILURE).get(0);
    String errorCode = failure.getAttribute("mongo.errorcode");
    assertEquals("11000", errorCode); // duplicate key error code
    String errorMessage = failure.getAttribute("mongo.errormessage");
    assertTrue(StringUtils.isNotBlank(errorMessage));

}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:34,代碼來源:StoreInMongoIT.java

示例5: testDuplicateKeyFailureOrderedTrue

import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void testDuplicateKeyFailureOrderedTrue() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());
    addMongoService(runner);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "insert_test");
    runner.setProperty(MongoProps.BATCH_SIZE, "10");
    runner.setProperty(MongoProps.ORDERED, "true");

    ObjectId objectId = ObjectId.get();

    String success = FileUtils.readFileToString(Paths.get("src/test/resources/payload.json").toFile());
    String successTwo = "{\"a\":\"a\"}";
    String payloadOne = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"first value\"}";
    String payloadTwo = "{\"_id\":\"" + objectId.toString() + "\", \"text\": \"second value\"}";

    runner.enqueue(payloadOne.getBytes());
    runner.enqueue(success.getBytes());
    runner.enqueue(payloadTwo.getBytes());
    runner.enqueue(successTwo.getBytes()); // add another successful message

    runner.run();

    runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 2);
    runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 2);

    List<MockFlowFile> failures = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_FAILURE);
    FlowFile failure1 = failures.get(0);
    String errorCode1 = failure1.getAttribute("mongo.errorcode");
    assertEquals("11000", errorCode1); // duplicate key error code
    String errorMessage1 = failure1.getAttribute("mongo.errormessage");
    assertTrue(StringUtils.isNotBlank(errorMessage1));

    FlowFile failure2 = failures.get(1);
    String errorMessage2 = failure2.getAttribute("storeinmongo.error");
    assertTrue(StringUtils.isNotBlank(errorMessage2));
    String mongoErrorCode2 = failure2.getAttribute("mongo.errorcode");
    assertTrue(StringUtils.isBlank(mongoErrorCode2));

    // Test contents of mongo
    MongoCollection<Document> collection = mongo.getMongoClient().getDatabase(MONGO_DATABASE_NAME).getCollection("insert_test");
    assertEquals(2L, collection.count());
}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:44,代碼來源:StoreInMongoIT.java


注:本文中的org.bson.types.ObjectId.toString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。