Java Code Examples for org.bson.types.ObjectId#get()

The following examples show how to use org.bson.types.ObjectId#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NumbersTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Test
public void advanced() {
  final AdvancedRepository repo = new AdvancedRepository(context.setup());
  ObjectId id = ObjectId.get();

  Advanced doc = ImmutableAdvanced.builder()
          .id(id)
          .bigDecimal(new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(128))) // make number big
          .bigInteger(new BigInteger(String.valueOf(Long.MAX_VALUE)).multiply(new BigInteger("128"))) // make it also big
          .atomicBoolean(new AtomicBoolean())
          .atomicInteger(new AtomicInteger(55))
          .atomicLong(new AtomicLong(77))
          .build();

  repo.insert(doc).getUnchecked();

  final Advanced doc2 = repo.findById(id).fetchFirst().getUnchecked().get();
  check(doc2.id()).is(id);

  check(doc2.bigDecimal()).is(doc.bigDecimal());
  check(doc2.bigInteger()).is(doc.bigInteger());
  check(!doc2.atomicBoolean().get());
  check(doc2.atomicInteger().get()).is(doc.atomicInteger().get());
  check(doc2.atomicLong().get()).is(doc.atomicLong().get());
}
 
Example 2
Source File: RelatedCollectorItemRepositoryTest.java    From hygieia-core with Apache License 2.0 6 votes vote down vote up
@Test
public void saveRelatedItemsDuplicate() {
    relatedCollectorItemRepository.deleteAll();
    ObjectId left = ObjectId.get();
    ObjectId right = ObjectId.get();
    RelatedCollectorItem rc = new RelatedCollectorItem();
    rc.setLeft(left);
    rc.setRight(right);
    rc.setSource("some source");
    rc.setReason("some reason");
    RelatedCollectorItem saved = relatedCollectorItemRepository.save(rc);

    RelatedCollectorItem savedAgain = relatedCollectorItemRepository.saveRelatedItems(left, right, "some source", "some reason");

    List<RelatedCollectorItem> relatedCollectorItemList = Lists.newArrayList(relatedCollectorItemRepository.findAll());
    assertTrue(!CollectionUtils.isEmpty(relatedCollectorItemList));
    assertEquals(1, relatedCollectorItemList.size());
    assertEquals(relatedCollectorItemList.get(0).getLeft(), left);
    assertEquals(relatedCollectorItemList.get(0).getRight(), right);
    assertTrue(relatedCollectorItemList.get(0).getReason().equalsIgnoreCase("some reason"));
    assertTrue(relatedCollectorItemList.get(0).getSource().equalsIgnoreCase("some source"));
    assertTrue(!Objects.equals(saved.getId(), savedAgain.getId()));


}
 
Example 3
Source File: NumbersTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Test
public void boxed() {
  final BoxedRepository repo = new BoxedRepository(context.setup());
  ObjectId id = ObjectId.get();

  Boxed doc = ImmutableBoxed.builder()
          .id(id)
          .booleanValue(Boolean.FALSE)
          .byteValue(Byte.MAX_VALUE)
          .shortValue(Short.MAX_VALUE)
          .intValue(Integer.MAX_VALUE)
          .longValue(Long.MAX_VALUE)
          .floatValue(Float.MAX_VALUE)
          .doubleValue(Double.MAX_VALUE)
          .build();

  repo.insert(doc).getUnchecked();
  check(repo.findById(id).fetchAll().getUnchecked()).hasContentInAnyOrder(doc);
}
 
Example 4
Source File: NumbersTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Test
public void primitives() {
  final PrimitivesRepository repo = new PrimitivesRepository(context.setup());
  ObjectId id = ObjectId.get();
  ImmutablePrimitives doc = ImmutablePrimitives.builder()
          .id(id)
          .booleanValue(true)
          .byteValue((byte) 0)
          .shortValue((short) 12)
          .intValue(42)
          .longValue(123L)
          .floatValue(11.11F)
          .doubleValue(22.22D)
          .build();

  repo.insert(doc).getUnchecked();
  check(repo.findById(id).fetchAll().getUnchecked()).hasContentInAnyOrder(doc);
}
 
Example 5
Source File: JacksonRepoTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Test
public void criteria() {
  final Date date = new Date();

  final ObjectId id = ObjectId.get();
  final UUID uuid = UUID.randomUUID();
  final Jackson expected = ImmutableJackson.builder()
          .id(id)
          .prop1("prop11")
          .prop2("prop22")
          .date(date)
          .uuid(uuid)
          .build();

  repository.insert(expected).getUnchecked();

  check(repository.find(repository.criteria().prop1("prop11")).fetchAll().getUnchecked()).hasContentInAnyOrder(expected);
  check(repository.find(repository.criteria().prop1("missing")).fetchAll().getUnchecked()).isEmpty();
  check(repository.find(repository.criteria().prop2("prop22")).fetchAll().getUnchecked()).hasContentInAnyOrder(expected);
  check(repository.find(repository.criteria().id(id)).fetchAll().getUnchecked()).hasContentInAnyOrder(expected);
  check(repository.find(repository.criteria().date(date)).fetchAll().getUnchecked()).hasContentInAnyOrder(expected);
  check(repository.find(repository.criteria().date(new Date(42))).fetchAll().getUnchecked()).isEmpty();
  check(repository.find(repository.criteria().uuid(uuid)).fetchAll().getUnchecked()).hasContentInAnyOrder(expected);
  check(repository.find(repository.criteria().uuid(UUID.randomUUID())).fetchAll().getUnchecked()).isEmpty();
}
 
Example 6
Source File: JacksonRepoTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Test
public void withDate() {
  final Date date = new Date();
  final ObjectId id = ObjectId.get();
  final Jackson expected = ImmutableJackson.builder()
          .id(id)
          .prop1("prop1")
          .prop2("22")
          .date(new Date(date.getTime()))
          .build();

  repository.insert(expected).getUnchecked();

  check(collection.count()).is(1L);

  final Jackson actual = repository.findAll().fetchAll().getUnchecked().get(0);
  check(expected).is(actual);

  final BsonDocument doc = collection.find().first();
  check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid");
  check(doc.get("date").asDateTime().getValue()).is(date.getTime());
  check(doc.get("_id").asObjectId().getValue()).is(id);
}
 
Example 7
Source File: JsonTypeAdapterExample.java    From javers with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeValueToJsonWithTypeAdapter() {
    //given
    Javers javers = JaversBuilder.javers()
            .registerValueTypeAdapter(new ObjectIdTypeAdapter())
            .build();

    //when
    ObjectId id = ObjectId.get();
    MongoStoredEntity entity1 = new MongoStoredEntity(id, "alg1", "1.0", "name");
    MongoStoredEntity entity2 = new MongoStoredEntity(id, "alg1", "1.0", "another");
    Diff diff = javers.compare(entity1, entity2);

    //then
    String json = javers.getJsonConverter().toJson(diff);
    Assertions.assertThat(json).contains(id.toString());

    System.out.println(json);
}
 
Example 8
Source File: TestUtils.java    From ExecDashboard with Apache License 2.0 5 votes vote down vote up
public static Portfolio makePortfolio(MetricLevel metricLevel,RoleRelationShipType roleRelationShipType){
    Portfolio portfolio = new Portfolio();
    ObjectId id = ObjectId.get();
    portfolio.setId(id);
    portfolio.setName("portfolio1");
    portfolio.setCommonName("PF");
    portfolio.setLob("LOB");
    portfolio.setThumbnail("thumbnailPhoto");
    portfolio.setDashboardDisplayName("PF dashboard");
    portfolio.setMetricLevel(metricLevel);
    portfolio.setMetricsId(ObjectId.get());
    portfolio.setOwners(makeOwners(roleRelationShipType));
    portfolio.setProducts(Stream.of(makeProduct()).collect(Collectors.toList()));
    return portfolio;
}
 
Example 9
Source File: NumbersTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * Serializing big numbers which can't be stored in {@link org.bson.types.Decimal128} format.
 * They should be serialized as strings (or binary) in mongo.
 */
@Test
public void larger_than_decimal128() {
  final AdvancedRepository repo = new AdvancedRepository(context.setup());
  final ObjectId id = ObjectId.get();
  final BigInteger integer = BigInteger.valueOf(Long.MAX_VALUE).pow(4);

  try {
    new Decimal128(new BigDecimal(integer));
    fail("Should fail for " + integer);
  } catch (NumberFormatException ignore) {
    // expected
  }

  final Advanced doc = ImmutableAdvanced.builder()
          .id(id)
          .bigDecimal(new BigDecimal(integer)) // make number big
          .bigInteger(integer) // make it also big
          .atomicBoolean(new AtomicBoolean(false))
          .atomicInteger(new AtomicInteger(1))
          .atomicLong(new AtomicLong(2))
          .build();

  repo.insert(doc).getUnchecked();

  final Advanced doc2 = repo.findById(id).fetchFirst().getUnchecked().get();
  check(doc2.bigDecimal().unscaledValue()).is(integer);
  check(doc2.bigInteger()).is(integer);

}
 
Example 10
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int _insert(Table table) {
	try {
		com.mongodb.client.MongoDatabase db = database.getMongoDB();
		ObjectId newOid = ObjectId.get();
		table.getParams().put("_id", newOid);
		db.getCollection(table.getTableName()).insertOne(Document.parse(JSON.serialize(table.getParams())));
		table.getParams().put("id", newOid.toString());
		LOG.debug("insert->collection:"+table.getTableName()+",script:"+JSON.serialize(table.getParams()));
	} catch (MongoException e) {
		LOG.error("mongo insert error", e);
	}
	return 1;
}
 
Example 11
Source File: MongoDAL.java    From uncode-dal-all with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int _insert(Table table) {
	try {
		DB db = database.getDB();
		Jongo jongo = new Jongo(db);
		ObjectId newOid = ObjectId.get();
		table.getParams().put("_id", newOid);
		jongo.getCollection(table.getTableName()).save(table.getParams());
		table.getParams().put("id", newOid.toString());
	} catch (MongoException e) {
		LOG.error("mongo insert error", e);
	}
	return 1;
}
 
Example 12
Source File: TemplateRepositoryTest.java    From hygieia-core with Apache License 2.0 5 votes vote down vote up
@Test
public void validate_delete() {
    ObjectId templateId = ObjectId.get();
    mockTemplate.setId(templateId);
    templateRepository.save(mockTemplate);
    templateRepository.delete(templateId);
    Template actual = templateRepository.findByTemplate("template1");
    assertNull(actual);
}
 
Example 13
Source File: RelatedCollectorItemRepositoryTest.java    From hygieia-core with Apache License 2.0 5 votes vote down vote up
@Test
public void saveRelatedItemsDuplicateMany() {
    relatedCollectorItemRepository.deleteAll();
    ObjectId left = ObjectId.get();
    ObjectId right = ObjectId.get();
    for (int i = 0; i < 5; i++) {
        RelatedCollectorItem rc = new RelatedCollectorItem();
        rc.setLeft(left);
        rc.setRight(right);
        rc.setSource("some source");
        rc.setReason("some reason");
        relatedCollectorItemRepository.save(rc);
    }

    RelatedCollectorItem saved = relatedCollectorItemRepository.findAll().iterator().next();

    RelatedCollectorItem savedAgain = relatedCollectorItemRepository.saveRelatedItems(left, right, "some source", "some reason");

    List<RelatedCollectorItem> relatedCollectorItemList = Lists.newArrayList(relatedCollectorItemRepository.findAll());
    assertTrue(!CollectionUtils.isEmpty(relatedCollectorItemList));
    assertEquals(1, relatedCollectorItemList.size());
    assertEquals(relatedCollectorItemList.get(0).getLeft(), left);
    assertEquals(relatedCollectorItemList.get(0).getRight(), right);
    assertTrue(relatedCollectorItemList.get(0).getReason().equalsIgnoreCase("some reason"));
    assertTrue(relatedCollectorItemList.get(0).getSource().equalsIgnoreCase("some source"));
    assertTrue(!Objects.equals(saved.getId(), savedAgain.getId()));


}
 
Example 14
Source File: RelatedCollectorItemRepositoryTest.java    From hygieia-core with Apache License 2.0 5 votes vote down vote up
@Test
public void saveRelatedItems() {
    relatedCollectorItemRepository.deleteAll();
    ObjectId left = ObjectId.get();
    ObjectId right = ObjectId.get();
    relatedCollectorItemRepository.saveRelatedItems(left, right, "some source", "some reason");
    List<RelatedCollectorItem> relatedCollectorItemList = Lists.newArrayList(relatedCollectorItemRepository.findAll());
    assertFalse(CollectionUtils.isEmpty(relatedCollectorItemList));
    assertEquals(1, relatedCollectorItemList.size());
    assertEquals(relatedCollectorItemList.get(0).getLeft(), left);
    assertEquals(relatedCollectorItemList.get(0).getRight(), right);
    assertTrue(relatedCollectorItemList.get(0).getReason().equalsIgnoreCase("some reason"));
    assertTrue(relatedCollectorItemList.get(0).getSource().equalsIgnoreCase("some source"));

}
 
Example 15
Source File: Book.java    From spring-data-dev-tools with Apache License 2.0 4 votes vote down vote up
public Book(String title, int pages) {
	
	this.id = ObjectId.get();
	this.title = title;
	this.pages = pages;
}
 
Example 16
Source File: BsonOidStrategy.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
public BsonValue generateId(SinkDocument doc, SinkRecord orig) {
    return new BsonObjectId(ObjectId.get());
}
 
Example 17
Source File: StandardWidgetTest.java    From hygieia-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testStandardwidget() {
    StandardWidget sw = new StandardWidget(CollectorType.Build, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("build"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "build0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("build"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "build0"));

    sw = new StandardWidget(CollectorType.SCM, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("repo"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "repo0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("repo"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "repo0"));

    sw = new StandardWidget(CollectorType.CodeQuality, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "codeanalysis0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "codeanalysis0"));

    sw = new StandardWidget(CollectorType.Test, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "codeanalysis0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "codeanalysis0"));

    sw = new StandardWidget(CollectorType.StaticSecurityScan, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "codeanalysis0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "codeanalysis0"));


    sw = new StandardWidget(CollectorType.LibraryPolicy, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "codeanalysis0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("codeanalysis"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "codeanalysis0"));

    sw = new StandardWidget(CollectorType.Deployment, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("deploy"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "deploy0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("deploy"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "deploy0"));

    sw = new StandardWidget(CollectorType.AgileTool, ObjectId.get());
    assertTrue(sw.getName().equalsIgnoreCase("feature"));
    assertTrue(Objects.equals(sw.getOptions().get("id"), "feature0"));
    assertTrue(sw.getWidget().getName().equalsIgnoreCase("feature"));
    assertTrue(Objects.equals(sw.getWidget().getOptions().get("id"), "feature0"));
}
 
Example 18
Source File: BsonOidStrategy.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  return new BsonObjectId(ObjectId.get());
}
 
Example 19
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Test
public void objectId() throws IOException {
  ObjectId id = ObjectId.get();
  check(Jsons.readerAt(new BsonObjectId(id)).peek()).is(JsonToken.STRING);
  check(Jsons.readerAt(new BsonObjectId(id)).nextString()).is(id.toHexString());
}
 
Example 20
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Test
void objectId() throws IOException {
  ObjectId id = ObjectId.get();
  check(Parsers.parserAt(new BsonObjectId(id)).getText()).is(id.toHexString());
}