本文整理匯總了Java中org.bson.types.ObjectId.get方法的典型用法代碼示例。如果您正苦於以下問題:Java ObjectId.get方法的具體用法?Java ObjectId.get怎麽用?Java ObjectId.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.bson.types.ObjectId
的用法示例。
在下文中一共展示了ObjectId.get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: shouldReturnOneCustomerById
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldReturnOneCustomerById() throws Exception {
final LocalDate birthDate = LocalDate.of(1990, Month.JULY, 31);
final Customer mockCustomer = Customer.ofType(PERSON).withBirthDate(birthDate).build();
final ObjectId id = ObjectId.get();
given(repo.findById(any(ObjectId.class))).willReturn(Mono.just(mockCustomer));
webClient.get().uri(String.format("/customers/%s", id)).accept(APPLICATION_JSON_UTF8).exchange()
.expectStatus().isOk() // HTTP 200
.expectBody(Customer.class)
.consumeWith(customer -> {
assertThat(customer.getResponseBody().getCustomerType()).isEqualTo(PERSON);
assertThat(customer.getResponseBody().getBirthDate()).isEqualTo(LocalDate.of(1990, 07, 31));
});
}
示例2: shouldAddANewCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldAddANewCustomer() throws Exception {
final Customer newCustomer = Customer.ofType(PERSON).build();
final ObjectId id = ObjectId.get();
ReflectionTestUtils.setField(newCustomer, "id", id);
given(repo.existsById(any(ObjectId.class))).willReturn(Mono.just(false));
given(repo.save(any(Customer.class))).willReturn(Mono.just(newCustomer));
webClient.post().uri("/customers")
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject("{\"customer_type\":\"PERSON\"}"))
.exchange()
.expectStatus().isCreated() // HTTP 201
.expectHeader().valueEquals("Location", String.format("/customers/%s", id));
}
示例3: shouldAddANewCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldAddANewCustomer() {
// Given
final Customer newCustomer = Customer.ofType(PERSON).build();
final ObjectId id = ObjectId.get();
ReflectionTestUtils.setField(newCustomer, "id", id);
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(false));
when(repo.save(any(Customer.class))).thenReturn(Mono.just(newCustomer));
// When
final ResponseEntity<?> response = controller.addCustomer(newCustomer).block();
// Then
assertThat(response.getStatusCode()).isEqualTo(CREATED);
assertThat(response.getHeaders().getLocation().toString()).isEqualTo(format("/customers/%s", id));
}
示例4: shouldUpdateAnExistingCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldUpdateAnExistingCustomer() {
// Given
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(true));
when(repo.save(any(Customer.class))).thenReturn(Mono.just(Customer.ofType(PERSON).build()));
final ObjectId id = ObjectId.get();
final Customer existingCustomer = Customer.ofType(CustomerType.PERSON).build();
ReflectionTestUtils.setField(existingCustomer, "id", id);
// When
final ResponseEntity<?> response = controller.updateCustomer(id, existingCustomer).block();
// Then
assertThat(response.getStatusCode()).isEqualTo(NO_CONTENT);
}
示例5: shouldDeleteExistingCustomerAndIgnoreSubsequentCalls
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldDeleteExistingCustomerAndIgnoreSubsequentCalls() throws Exception {
// Given
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(true)).thenReturn(Mono.just(false));
when(repo.deleteById(any(ObjectId.class))).thenReturn(Mono.empty());
final ObjectId id = ObjectId.get();
// When
final ResponseEntity<?> response1 = controller.deleteCustomer(id).block();
final ResponseEntity<?> response2 = controller.deleteCustomer(id).block();
final ResponseEntity<?> response3 = controller.deleteCustomer(id).block();
// Then
verify(repo).deleteById(any(ObjectId.class)); // Must be called only once
assertThat(response1.getStatusCode()).isEqualTo(NO_CONTENT);
assertThat(response2.getStatusCode()).isEqualTo(NO_CONTENT);
assertThat(response3.getStatusCode()).isEqualTo(NO_CONTENT);
}
示例6: shouldNotAddCustomerIfCustomerAlreadyExists
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldNotAddCustomerIfCustomerAlreadyExists() throws Exception {
given(repo.existsById(any(ObjectId.class))).willReturn(Mono.just(true));
final ObjectId id = ObjectId.get();
final String EXISTING_CUSTOMER = String.format("{\"id\":\"%s\",\"customer_type\":\"COMPANY\"}", id);
webClient.post().uri("/customers")
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject(EXISTING_CUSTOMER))
.exchange()
.expectStatus().is5xxServerError(); // HTTP 500 because the exception handler is not working yet.
}
示例7: shouldUpdateAnExistingCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldUpdateAnExistingCustomer() throws Exception {
given(repo.existsById(any(ObjectId.class))).willReturn(Mono.just(true));
given(repo.save(any(Customer.class))).willReturn(Mono.just(Customer.ofType(PERSON).build()));
final ObjectId id = ObjectId.get();
final String UPDATE = String.format(
"{\"id\":\"%s\",\"first_name\":\"John\",\"last_name\":\"Doe\",\"customer_type\":\"COMPANY\"}", id);
webClient.put().uri(String.format("/customers/%s", id))
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject(UPDATE)).exchange()
.expectStatus().isNoContent(); // HTTP 204
}
示例8: shouldFailUpdatingNonExistingCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldFailUpdatingNonExistingCustomer() throws Exception {
given(repo.existsById(any(ObjectId.class))).willReturn(Mono.just(false));
final ObjectId id = ObjectId.get();
final String UPDATE = String.format(
"{\"id\":\"%s\",\"first_name\":\"John\",\"last_name\":\"Doe\",\"customer_type\":\"COMPANY\"}", id);
webClient.put().uri(String.format("/customers/%s", id))
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject(UPDATE)).exchange()
.expectStatus().is5xxServerError(); // HTTP 500 because the exception handler is not working yet.
}
示例9: shouldNotAddACustomerIfCustomerAlreadyExists
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldNotAddACustomerIfCustomerAlreadyExists() throws Exception {
// Given
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(true));
final ObjectId id = ObjectId.get();
final Customer customer = Customer.ofType(PERSON).build();
ReflectionTestUtils.setField(customer, "id", id);
// When
// Then
assertThatThrownBy(() -> controller.addCustomer(customer).block())
.isInstanceOf(CustomerServiceException.class)
.hasMessageContaining("Customer already exists");
}
示例10: shouldFailUpdatingNonExistingCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldFailUpdatingNonExistingCustomer() {
// Given
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(false));
final ObjectId id = ObjectId.get();
final Customer newCustomer = Customer.ofType(CustomerType.PERSON).build();
ReflectionTestUtils.setField(newCustomer, "id", id);
// When
// Then
assertThatThrownBy(() -> controller.updateCustomer(newCustomer.getId(), newCustomer).block())
.isInstanceOf(CustomerServiceException.class)
.hasMessageContaining("Customer does not exist");
}
示例11: shouldDeleteAnExistingCustomer
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void shouldDeleteAnExistingCustomer() {
// Given
when(repo.existsById(any(ObjectId.class))).thenReturn(Mono.just(true));
when(repo.deleteById(any(ObjectId.class))).thenReturn(Mono.empty());
final ObjectId id = ObjectId.get();
// When
final ResponseEntity<?> response = controller.deleteCustomer(id).block();
// Then
assertThat(response.getStatusCode()).isEqualTo(NO_CONTENT);
}
示例12: 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());
}
示例13: 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));
}
示例14: 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());
}
示例15: testObjectId
import org.bson.types.ObjectId; //導入方法依賴的package包/類
@Test
public void testObjectId() throws Exception {
ObjectId objectId = ObjectId.get();
System.out.println(objectId);
System.out.println(objectId.toHexString());
}