Search in sources :

Example 16 with MongoCollection

use of com.mongodb.client.MongoCollection in project graylog2-server by Graylog2.

the class V20190127111728_MigrateWidgetFormatSettingsTest method testMigrationWithOneChartColorMapping.

@Test
@MongoDBFixtures("V20190127111728_MigrateWidgetFormatSettings.json")
public void testMigrationWithOneChartColorMapping() {
    final BasicDBObject dbQuery1 = new BasicDBObject();
    dbQuery1.put("_id", new ObjectId("5e2ee372b22d7970576b2eb3"));
    final MongoCollection<Document> collection = mongoDB.mongoConnection().getMongoDatabase().getCollection("views");
    migration.upgrade();
    final FindIterable<Document> views = collection.find(dbQuery1);
    final Document view1 = views.first();
    @SuppressWarnings("unchecked") final List<Document> widgets1 = (List) view1.get("state", Document.class).get("2c67cc0f-c62e-47c1-8b70-e3198925e6bc", Document.class).get("widgets");
    assertThat(widgets1.size()).isEqualTo(2);
    Set<Document> aggregationWidgets = widgets1.stream().filter(w -> w.getString("type").equals("aggregation")).collect(Collectors.toSet());
    assertThat(aggregationWidgets.size()).isEqualTo(1);
    final Document aggregationWidget = aggregationWidgets.iterator().next();
    final Document config = aggregationWidget.get("config", Document.class);
    final Document formattingSettings = config.get("formatting_settings", Document.class);
    @SuppressWarnings("unchecked") final List<Document> chartColors = (List) formattingSettings.get("chart_colors", List.class);
    assertThat(chartColors.size()).isEqualTo(1);
    final Document chartColor = chartColors.get(0);
    assertThat(chartColor.getString("field_name")).isEqualTo("count()");
    assertThat(chartColor.getString("chart_color")).isEqualTo("#e91e63");
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicDBObject(com.mongodb.BasicDBObject) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Rule(org.junit.Rule) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) ObjectId(org.bson.types.ObjectId) MockitoJUnit(org.mockito.junit.MockitoJUnit) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Before(org.junit.Before) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) List(java.util.List) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 17 with MongoCollection

use of com.mongodb.client.MongoCollection in project graylog2-server by Graylog2.

the class V20190304102700_MigrateMessageListStructureTest method testMigratingViewStructure.

@Test
@MongoDBFixtures("V20190304102700_MigrateMessageListStructureTest.json")
public void testMigratingViewStructure() {
    final BasicDBObject dbQuery1 = new BasicDBObject();
    dbQuery1.put("_id", new ObjectId("58458e442f857c314491344e"));
    final MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection("views");
    migration.upgrade();
    final FindIterable<Document> views = collection.find(dbQuery1);
    final Document view1 = views.first();
    @SuppressWarnings("unchecked") final List<Document> widgets1 = (List) view1.get("state", Document.class).get("a2a804b7-27cf-4cac-8015-58d9a9640d33", Document.class).get("widgets");
    assertThat(widgets1.size()).isEqualTo(2);
    assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isEqualTo(1);
    assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).allMatch((widget) -> {
        final Document config = widget.get("config", Document.class);
        @SuppressWarnings("unchecked") final List<String> fields = (List) config.get("fields");
        final boolean startWithTimestamp = fields.get(0).contains("timestamp");
        final boolean showMessageRow = config.getBoolean("show_message_row");
        return startWithTimestamp && showMessageRow;
    })).isTrue();
    final BasicDBObject dbQuery2 = new BasicDBObject();
    dbQuery2.put("_id", new ObjectId("58458e442f857c314491344f"));
    final FindIterable<Document> views2 = collection.find(dbQuery2);
    final Document view2 = views2.first();
    final Document states = view2.get("state", Document.class);
    assertThat(states.values().size()).isEqualTo(13);
    assertThat(states.keySet()).containsExactly("7c042319-530a-41b9-9dbb-9676fb1da1a4", "9e5144be-a445-4289-a4cc-0f55142524bc", "c13b2482-60e7-4b1e-98c9-0df8d6da8230", "5adc9297-dfc8-4fd9-b422-cbb097715a62", "ade8c853-503c-407f-b125-efbe2d368973", "cc2bf983-b398-4295-bf01-1c10ed1a97e1", "64feccae-9447-40ef-a401-79a7972078a2", "7c7e04c6-f9f0-495c-91cc-865f60687f8c", "eeaa8838-616f-40c0-88c0-1059ac64f37e", "91c6f8c9-024c-48ec-a869-90548fad218a", "955a71f2-673a-4e1c-a99f-ef97b1b4ae71", "343ff7b6-4554-49d4-bc0b-1339fdc5dac0", "7a84d053-e40a-48c1-a433-97521f7ce7ef");
    states.values().forEach(state -> {
        @SuppressWarnings("unchecked") final List<Document> widgets2 = (List) ((Document) state).get("widgets");
        assertThat(widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isGreaterThan(0);
        widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).forEach((widget) -> {
            final Document config = widget.get("config", Document.class);
            @SuppressWarnings("unchecked") final List<String> fields = (List) config.get("fields");
            final boolean startWithTimestamp = fields.get(0).contains("timestamp");
            final boolean showMessageRow = config.getBoolean("show_message_row");
            assertThat(startWithTimestamp).isTrue();
            assertThat(showMessageRow).isTrue();
        });
    });
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test) List(java.util.List) Rule(org.junit.Rule) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) ObjectId(org.bson.types.ObjectId) MockitoJUnit(org.mockito.junit.MockitoJUnit) Migration(org.graylog2.migrations.Migration) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Before(org.junit.Before) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) List(java.util.List) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 18 with MongoCollection

use of com.mongodb.client.MongoCollection in project spring-data-mongodb by spring-projects.

the class DefaultDbRefResolver method bulkFetch.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
	 */
@Override
public List<Document> bulkFetch(List<DBRef> refs) {
    Assert.notNull(mongoDbFactory, "Factory must not be null!");
    Assert.notNull(refs, "DBRef to fetch must not be null!");
    if (refs.isEmpty()) {
        return Collections.emptyList();
    }
    String collection = refs.iterator().next().getCollectionName();
    List<Object> ids = new ArrayList<>(refs.size());
    for (DBRef ref : refs) {
        if (!collection.equals(ref.getCollectionName())) {
            throw new InvalidDataAccessApiUsageException("DBRefs must all target the same collection for bulk fetch operation.");
        }
        ids.add(ref.getId());
    }
    DBRef databaseSource = refs.iterator().next();
    MongoCollection<Document> mongoCollection = getCollection(databaseSource);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(String.format("Bulk fetching DBRefs %s from %s.%s.", ids, StringUtils.hasText(databaseSource.getDatabaseName()) ? databaseSource.getDatabaseName() : mongoCollection.getNamespace().getDatabaseName(), databaseSource.getCollectionName()));
    }
    List<Document> result = // 
    mongoCollection.find(// 
    new Document(BasicMongoPersistentProperty.ID_FIELD_NAME, new Document("$in", ids))).into(new ArrayList<>());
    return // 
    ids.stream().flatMap(// 
    id -> documentWithId(id, result)).collect(Collectors.toList());
}
Also used : Document(org.bson.Document) MongoDatabaseUtils(org.springframework.data.mongodb.MongoDatabaseUtils) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MongoCollection(com.mongodb.client.MongoCollection) Collection(java.util.Collection) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) DocumentReferenceQuery(org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) List(java.util.List) Stream(java.util.stream.Stream) DBRef(com.mongodb.DBRef) BasicMongoPersistentProperty(org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty) Log(org.apache.commons.logging.Log) Nullable(org.springframework.lang.Nullable) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) Document(org.bson.Document)

Example 19 with MongoCollection

use of com.mongodb.client.MongoCollection in project spring-data-mongodb by spring-projects.

the class SessionAwareMethodInterceptorUnitTests method proxiesNewCollectionInstanceReturnedByMethod.

// DATAMONGO-1880
@Test
public void proxiesNewCollectionInstanceReturnedByMethod() {
    MongoCollection otherCollection = mock(MongoCollection.class);
    when(targetCollection.withCodecRegistry(any())).thenReturn(otherCollection);
    MongoCollection target = collection.withCodecRegistry(MongoClientSettings.getDefaultCodecRegistry());
    assertThat(target).isInstanceOf(Proxy.class).isNotSameAs(collection).isNotSameAs(targetCollection);
    target.drop();
    verify(otherCollection).drop(eq(session));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Test(org.junit.jupiter.api.Test)

Example 20 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method updateToV2.

private void updateToV2() {
    MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
    petCollection.createIndex(new BasicDBObject("uuid", 1));
    petCollection.createIndex(new BasicDBObject("owner_uuid", 1));
    MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    playerCollection.createIndex(new BasicDBObject("internal_uuid", 1));
    playerCollection.createIndex(new BasicDBObject("offline_uuid", 1));
    playerCollection.createIndex(new BasicDBObject("mojang_uuid", 1));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection)

Aggregations

MongoCollection (com.mongodb.client.MongoCollection)53 Document (org.bson.Document)39 MongoDatabase (com.mongodb.client.MongoDatabase)21 List (java.util.List)16 FindIterable (com.mongodb.client.FindIterable)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 Set (java.util.Set)9 ObjectId (org.bson.types.ObjectId)9 MongoCursor (com.mongodb.client.MongoCursor)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Bson (org.bson.conversions.Bson)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 BsonDocument (org.bson.BsonDocument)6 Block (com.mongodb.Block)5 UpdateOptions (com.mongodb.client.model.UpdateOptions)5