Java Code Examples for com.mongodb.client.MongoDatabase

The following examples show how to use com.mongodb.client.MongoDatabase. These examples are extracted from open source projects. 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
private boolean takeOverTransactionInMongoDB(TransactionXid transactionXid, String source, String target) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId);
		Bson instIdFilter = Filters.eq("identifier", source);

		Document document = new Document("$set", new Document("identifier", target));

		UpdateResult result = collection.updateOne(Filters.and(globalFilter, instIdFilter), document);
		return result.getMatchedCount() == 1;
	} catch (RuntimeException rex) {
		logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex);
		return false;
	}
}
 
Example 2
Source Project: jpa-unit   Source File: MongoDbDecoratorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMongoClientIsClosedEvenIfExecuteAfterTestFails() throws Throwable {
    // GIVEN
    when(invocation.getException()).thenReturn(Optional.of(new Exception()));
    doThrow(RuntimeException.class).when(executor).executeAfterTest(any(MongoDatabase.class), anyBoolean());

    // WHEN
    try {
        decorator.afterTest(invocation);
        fail("Exception expected");
    } catch (final Exception e) {
        // expected
    }

    // THEN
    verifyZeroInteractions(mongoClient);
}
 
Example 3
Source Project: mongobee   Source File: ChangeEntryDaoTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReleaseLockFromLockDao() throws Exception {

  // given
  MongoClient mongoClient = mock(MongoClient.class);
  MongoDatabase db = new Fongo(TEST_SERVER).getDatabase(DB_NAME);
  when(mongoClient.getDatabase(anyString())).thenReturn(db);

  ChangeEntryDao dao = new ChangeEntryDao(CHANGELOG_COLLECTION_NAME, LOCK_COLLECTION_NAME, WAIT_FOR_LOCK,
      CHANGE_LOG_LOCK_WAIT_TIME, CHANGE_LOG_LOCK_POLL_RATE, THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);

  LockDao lockDao = mock(LockDao.class);
  dao.setLockDao(lockDao);

  dao.connectMongoDb(mongoClient, DB_NAME);

  // when
  dao.releaseProcessLock();

  // then
  verify(lockDao).releaseLock(any(MongoDatabase.class));
}
 
Example 4
Source Project: hazelcast-simulator   Source File: ReadWriteTest.java    License: Apache License 2.0 6 votes vote down vote up
@Setup
public void setUp() {
    if (itemCount <= 0) {
        throw new IllegalStateException("size must be larger than 0");
    }

    MongoDatabase database = client.getDatabase(databaseName);
    col = database.getCollection(collectionName);

    values = new Document[idArraySize][itemCount];
    for (int i = 0; i < idArraySize; i++) {
        for (int j = 0; j < itemCount; j++) {
            values[i][j] = createEntry(j);
        }
    }
}
 
Example 5
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (coll.find(eq("email", local_user.getEmail())).first() != null) {
            mongoClient.close();
            return 1; // Repeated email
        }
        Document document = local_user.getRegisterDocument();
        coll.insertOne(document);
        local_user.setId(document.getObjectId("_id").toString());
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example 6
private String getTransactionOwnerInMongoDB(TransactionXid transactionXid) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		FindIterable<Document> findIterable = collection.find(Filters.eq(CONSTANTS_FD_GLOBAL, instanceId));
		MongoCursor<Document> cursor = findIterable.iterator();
		if (cursor.hasNext()) {
			Document document = cursor.next();
			return document.getString("identifier");
		} else {
			return null;
		}
	} catch (RuntimeException rex) {
		logger.error("Error occurred while querying the lock-owner of transaction(gxid= {}).", instanceId, rex);
		return null;
	}
}
 
Example 7
Source Project: mongobee   Source File: ChangeEntryDaoTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = MongobeeLockException.class)
public void shouldThrowLockExceptionIfThrowExceptionIsTrue() throws Exception {
  // given
  MongoClient mongoClient = mock(MongoClient.class);
  MongoDatabase db = new Fongo(TEST_SERVER).getDatabase(DB_NAME);
  when(mongoClient.getDatabase(anyString())).thenReturn(db);

  ChangeEntryDao dao = new ChangeEntryDao(CHANGELOG_COLLECTION_NAME, LOCK_COLLECTION_NAME, WAIT_FOR_LOCK,
      CHANGE_LOG_LOCK_WAIT_TIME, CHANGE_LOG_LOCK_POLL_RATE, true);

  LockDao lockDao = mock(LockDao.class);
  when(lockDao.acquireLock(any(MongoDatabase.class))).thenReturn(false);
  dao.setLockDao(lockDao);

  dao.connectMongoDb(mongoClient, DB_NAME);

  // when
  boolean hasLock = dao.acquireProcessLock();

  // then
  assertFalse(hasLock);
}
 
Example 8
Source Project: mongo-kafka   Source File: ConnectorValidationTest.java    License: Apache License 2.0 6 votes vote down vote up
private void dropUserAndRoles() {
  if (isAuthEnabled()) {
    List<MongoDatabase> databases =
        asList(
            getMongoClient().getDatabase(getConnectionString().getCredential().getSource()),
            getMongoClient().getDatabase(CUSTOM_DATABASE));

    for (final MongoDatabase database : databases) {
      tryAndIgnore(
          () -> database.runCommand(Document.parse(format("{dropUser: '%s'}", CUSTOM_USER))));
      tryAndIgnore(
          () -> database.runCommand(Document.parse(format("{dropRole: '%s'}", CUSTOM_ROLE))));
      tryAndIgnore(() -> database.runCommand(Document.parse("{invalidateUserCache: 1}")));
    }
  }
}
 
Example 9
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (!local_user.getEmail().equals(original_email)) {
            Document user = coll.find(eq("email", local_user.getEmail())).first();
            if (user != null) {
                return 1; // Repeated email
            }
        }

        Document search = new Document("_id", new ObjectId(local_user.getId()));
        Document replacement = new Document("$set", local_user.getRegisterDocument());
        // We update some fields of the documents without affecting the rest
        coll.updateOne(search, replacement);
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example 10
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	try{
		Document result	= db.runCommand( new Document("collection",collection).append( "verbose", true ) );
		return tagUtils.convertToCfData((Map)result);	 	
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}

}
 
Example 11
Source Project: tutorials   Source File: BsonToJsonLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() {

    String json = null;
    try (MongoClient mongoClient = new MongoClient()) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
        Document bson = mongoDatabase.getCollection("Books").find().first();
        json = bson.toJson(JsonWriterSettings
            .builder()
            .dateTimeConverter(new JsonDateTimeConverter())
            .build());
    }

    String expectedJson = "{\"_id\": \"isbn\", " + 
        "\"className\": \"com.baeldung.bsontojson.Book\", " + 
        "\"title\": \"title\", " + 
        "\"author\": \"author\", " + 
        "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + 
        "\"name\": \"publisher\"}, " + 
        "\"price\": 3.95, " + 
        "\"publishDate\": \"2020-01-01T17:13:32Z\"}";

    assertEquals(expectedJson, json);

}
 
Example 12
Source Project: baleen   Source File: RealMongoFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MongoDatabase createDatabase() {

  int port =
      argumentsMap.containsKey(PORT) ? Integer.parseInt(argumentsMap.get(PORT)) : DEFAULT_PORT;
  String host = argumentsMap.getOrDefault(HOST, DEFAULT_HOST);
  String databaseName = argumentsMap.getOrDefault(DATABASE_NAME, DEFAULT_DATABASE_NAME);
  String username = argumentsMap.getOrDefault(USERNAME, DEFAULT_USERNAME);
  String password = argumentsMap.getOrDefault(PASSWORD, DEFAULT_PASSWORD);

  List<ServerAddress> seeds = new ArrayList<>();
  List<MongoCredential> credentials = new ArrayList<>();
  seeds.add(new ServerAddress(host, port));
  credentials.add(
      MongoCredential.createScramSha1Credential(username, databaseName, password.toCharArray()));

  Boolean useAuthentication =
      Boolean.valueOf(argumentsMap.getOrDefault(USE_AUTHENTICATION, FALSE));

  client = useAuthentication ? new MongoClient(seeds, credentials) : new MongoClient(seeds);

  String dbName = argumentsMap.getOrDefault(DATABASE_NAME, DEFAULT_DATABASE_NAME);
  return client.getDatabase(dbName);
}
 
Example 13
Source Project: DBus   Source File: MongoWrapperDefaultHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 根据oid去数据库回查数据
 *
 * @param oid
 * @return
 */
private Document fetchData(String schemaName, String tableName, String oid) {
    Document result = null;
    DbusDatasource datasource = GlobalCache.getDatasource();
    MongoClientURI uri = new MongoClientURI(datasource.getMasterUrl());
    MongoClient client = new MongoClient(uri);
    MongoDatabase database = client.getDatabase(schemaName);
    MongoCollection<Document> collection = database.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(new BasicDBObject().append("_id", new ObjectId(oid))).iterator();
    if (cursor.hasNext()) {
        result = cursor.next();
    } else {
        logger.error("get source data error. schemaName:{}, tableName:{}, oid:{}", schemaName, tableName, oid);
    }
    client.close();
    return result;

}
 
Example 14
Source Project: heimdall   Source File: MongoDBAppender.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
	log.info("Initializing Mongodb Appender");
	if (this.uri != null) {
		this.mongoClient = new MongoClient(new MongoClientURI(this.uri));
	} else {
		MongoClientOptions options = new MongoClientOptions.Builder().build();
		ServerAddress address = new ServerAddress(this.url, this.port.intValue());
		this.mongoClient = new MongoClient(address, options);
	}

	MongoDatabase database = this.mongoClient.getDatabase(this.dataBase);
	this.collection = database.getCollection(this.collectionName);
	log.info("Starting connection with url: {} - port: {}", this.url, this.port);
	log.info("Database used: {} - Collection: {}", this.dataBase, this.collectionName);
	super.start();
}
 
Example 15
Source Project: logging-log4j2   Source File: MongoDb4Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final Logger logger = LogManager.getLogger();
    logger.info("Hello log");
    try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) {
        final MongoDatabase database = mongoClient.getDatabase("testDb");
        Assert.assertNotNull(database);
        final MongoCollection<Document> collection = database.getCollection("testCollection");
        Assert.assertNotNull(collection);
        final Document first = collection.find().first();
        Assert.assertNotNull(first);
        Assert.assertEquals(first.toJson(), "Hello log", first.getString("message"));
        Assert.assertEquals(first.toJson(), "INFO", first.getString("level"));
    }
}
 
Example 16
@BeforeEach
public void setUp() {
    mongoClient = mongoDBService.getClient();

    MongoDatabase database = mongoClient.getDatabase("testDatabase");

    /*
     The consume operation needs taliable cursors which require capped
     collections
     */
    CreateCollectionOptions options = new CreateCollectionOptions();
    options.capped(true);
    options.sizeInBytes(1024 * 1024);

    database.createCollection("testCollection", options);

    MongoCollection<Document> collection = database.getCollection("testCollection");

    List<Document> documents = new ArrayList<>(expect);
    for (int i = 0; i < expect; i++) {
        Document doc = new Document();

        doc.append("name", "test");
        doc.append("value", "value " + i);

        documents.add(doc);
    }

    collection.insertMany(documents);
}
 
Example 17
Source Project: lumongo   Source File: MongoDocumentStorage.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deleteSourceDocument(String uniqueId) throws Exception {
	MongoDatabase db = mongoClient.getDatabase(database);
	MongoCollection<Document> coll = db.getCollection(rawCollectionName);
	Document search = new Document(MongoConstants.StandardFields._ID, uniqueId);
	coll.deleteOne(search);
}
 
Example 18
Source Project: glowroot   Source File: MongoDbPluginIT.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void transactionMarker() {
    MongoDatabase database = mongoClient.getDatabase("testdb");
    MongoCollection<Document> collection = database.getCollection("test");
    Document document = new Document("test1", "test2")
            .append("test3", "test4");
    collection.insertOne(document);
}
 
Example 19
Source Project: vividus   Source File: MongoDbStepsTests.java    License: Apache License 2.0 5 votes vote down vote up
@PrepareForTest(MongoClients.class)
@Test
public void testExecuteCommand()
{
    MongoDatabase database = mockDatabase();
    when(database.runCommand(COMMAND)).thenReturn(DOCUMENT);

    MongoDbSteps steps = new MongoDbSteps(Map.of(LOCAL_KEY, CONNECTION_KEY), jsonUtils, context);
    steps.executeCommand(COMMAND, LOCAL_KEY, LOCAL_KEY, Set.of(VariableScope.STORY), VARIABLE_KEY);

    verify(context).putVariable(Set.of(VariableScope.STORY), VARIABLE_KEY, Map.of("id", "1"));
}
 
Example 20
Source Project: spring-data-dev-tools   Source File: CallbacksBenchmark.java    License: Apache License 2.0 5 votes vote down vote up
@Setup
public void setUp() {

	MongoClient client = Mockito.mock(MongoClient.class);
	MongoDatabase db = Mockito.mock(MongoDatabase.class);
	MongoCollection<Document> collection = Mockito.mock(MongoCollection.class);

	Mockito.when(client.getDatabase(Mockito.anyString())).thenReturn(db);
	Mockito.when(db.getCollection(Mockito.anyString(), Mockito.eq(Document.class))).thenReturn(collection);

	MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "mock-database");

	templateWithoutContext = new MongoTemplate(factory);

	templateWithEmptyContext = new MongoTemplate(factory);
	templateWithEmptyContext.setApplicationContext(new AnnotationConfigApplicationContext(EmptyConfig.class));

	templateWithContext = new MongoTemplate(factory);
	templateWithContext.setApplicationContext(new AnnotationConfigApplicationContext(EntityCallbackConfig.class));

	source = new Person();
	source.id = "luke-skywalker";
	source.firstname = "luke";
	source.lastname = "skywalker";

	source.address = new Address();
	source.address.street = "melenium falcon 1";
	source.address.city = "deathstar";
}
 
Example 21
Source Project: presto   Source File: MongoSession.java    License: Apache License 2.0 5 votes vote down vote up
private Document getTableMetadata(SchemaTableName schemaTableName)
        throws TableNotFoundException
{
    String schemaName = toRemoteSchemaName(schemaTableName.getSchemaName());
    String tableName = toRemoteTableName(schemaName, schemaTableName.getTableName());

    MongoDatabase db = client.getDatabase(schemaName);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);

    Document doc = schema
            .find(new Document(TABLE_NAME_KEY, tableName)).first();

    if (doc == null) {
        if (!collectionExists(db, tableName)) {
            throw new TableNotFoundException(schemaTableName);
        }
        else {
            Document metadata = new Document(TABLE_NAME_KEY, tableName);
            metadata.append(FIELDS_KEY, guessTableFields(schemaName, tableName));

            schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
            schema.insertOne(metadata);

            return metadata;
        }
    }

    return doc;
}
 
Example 22
/**
 * Inserts a new document in the given raw collection within the given database (row-like mode).
 * @param dbName
 * @param collectionName
 * @param aggregation
 * @throws Exception
 */
@Override
public void insertContextDataRaw(String dbName, String collectionName, ArrayList<Document> aggregation)
    throws Exception {
    MongoDatabase db = getDatabase(dbName);
    MongoCollection collection = db.getCollection(collectionName);
    collection.insertMany(aggregation);
}
 
Example 23
Source Project: presto   Source File: MongoSession.java    License: Apache License 2.0 5 votes vote down vote up
private List<Document> guessTableFields(String schemaName, String tableName)
{
    MongoDatabase db = client.getDatabase(schemaName);
    Document doc = db.getCollection(tableName).find().first();
    if (doc == null) {
        // no records at the collection
        return ImmutableList.of();
    }

    ImmutableList.Builder<Document> builder = ImmutableList.builder();

    for (String key : doc.keySet()) {
        Object value = doc.get(key);
        Optional<TypeSignature> fieldType = guessFieldType(value);
        if (fieldType.isPresent()) {
            Document metadata = new Document();
            metadata.append(FIELDS_NAME_KEY, key);
            metadata.append(FIELDS_TYPE_KEY, fieldType.get().toString());
            metadata.append(FIELDS_HIDDEN_KEY,
                    key.equals("_id") && fieldType.get().equals(OBJECT_ID.getTypeSignature()));

            builder.add(metadata);
        }
        else {
            log.debug("Unable to guess field type from %s : %s", value == null ? "null" : value.getClass().getName(), value);
        }
    }

    return builder.build();
}
 
Example 24
Source Project: baleen   Source File: MaxEntClassifierTrainer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
  super.initialize(context);

  MongoDatabase db = mongo.getDB();
  documentsCollection = db.getCollection(documentCollectionName);
  labelsAndFeatures = readLabelsAndFeaturesFromFile(labelsFile);
  stopwords = stopwordResource.getStopwords(stoplist);
}
 
Example 25
Source Project: logging-log4j2   Source File: MongoDb4AuthFailureTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final Logger logger = LogManager.getLogger();
    logger.info("Hello log");
    try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) {
        final MongoDatabase database = mongoClient.getDatabase("testDb");
        Assert.assertNotNull(database);
        final MongoCollection<Document> collection = database.getCollection("testCollection");
        Assert.assertNotNull(collection);
        final Document first = collection.find().first();
        Assert.assertNull(first);
    }
}
 
Example 26
Source Project: render   Source File: MongoUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static boolean exists(final MongoDatabase database,
                             final String collectionName) {
    for (final String name : database.listCollectionNames()) {
        if (name.equals(collectionName)) {
            return true;
        }
    }
    return false;
}
 
Example 27
Source Project: mongo-kafka   Source File: MongoSourceTask.java    License: Apache License 2.0 5 votes vote down vote up
private ChangeStreamIterable<Document> getChangeStreamIterable(
    final MongoSourceConfig sourceConfig, final MongoClient mongoClient) {
  String database = sourceConfig.getString(DATABASE_CONFIG);
  String collection = sourceConfig.getString(COLLECTION_CONFIG);

  Optional<List<Document>> pipeline = sourceConfig.getPipeline();
  ChangeStreamIterable<Document> changeStream;
  if (database.isEmpty()) {
    LOGGER.info("Watching all changes on the cluster");
    changeStream = pipeline.map(mongoClient::watch).orElse(mongoClient.watch());
  } else if (collection.isEmpty()) {
    LOGGER.info("Watching for database changes on '{}'", database);
    MongoDatabase db = mongoClient.getDatabase(database);
    changeStream = pipeline.map(db::watch).orElse(db.watch());
  } else {
    LOGGER.info("Watching for collection changes on '{}.{}'", database, collection);
    MongoCollection<Document> coll = mongoClient.getDatabase(database).getCollection(collection);
    changeStream = pipeline.map(coll::watch).orElse(coll.watch());
  }

  int batchSize = sourceConfig.getInt(BATCH_SIZE_CONFIG);
  if (batchSize > 0) {
    changeStream.batchSize(batchSize);
  }
  sourceConfig.getFullDocument().ifPresent(changeStream::fullDocument);
  sourceConfig.getCollation().ifPresent(changeStream::collation);
  return changeStream;
}
 
Example 28
private void insertContextDataAggregatedForResoultion(String dbName, String collectionName,
        GregorianCalendar calendar, String entityId, String entityType, String attrName, String attrType,
        HashMap<String, Integer> counts, Resolution resolution) {
    // Get database and collection
    MongoDatabase db = getDatabase(dbName);
    MongoCollection collection = db.getCollection(collectionName);

    // Build the query
    BasicDBObject query = buildQueryForInsertAggregated(calendar, entityId, entityType, attrName, resolution);

    // Prepopulate if needed
    BasicDBObject insert = buildInsertForPrepopulate(attrType, resolution, false);
    UpdateResult res = collection.updateOne(query, insert, new UpdateOptions().upsert(true));

    if (res.getMatchedCount() == 0) {
        LOGGER.debug("Prepopulating data, database=" + dbName + ", collection=" + collectionName + ", query="
                + query.toString() + ", insert=" + insert.toString());
    } // if

    // Do the update
    for (String key : counts.keySet()) {
        int count = counts.get(key);
        BasicDBObject update = buildUpdateForUpdate(attrType, resolution, calendar, key, count);
        LOGGER.debug("Updating data, database=" + dbName + ", collection=" + collectionName + ", query="
                + query.toString() + ", update=" + update.toString());
        collection.updateOne(query, update);
    } // for
}
 
Example 29
Source Project: repairnator   Source File: CleanProjectList.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    String projectPath = args[0];
    String dbUrl = args[1];
    String dbName = args[2];
    String collectionName = args[3];
    String destList = args[4];

    List<String> allProjects = Files.readAllLines(new File(projectPath).toPath());
    MongoConnection mongoConnection = new MongoConnection(dbUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);

    List<String> selectedProjects = new ArrayList<>();
    for (String project : allProjects) {
        Repository repo = RepositoryHelper.getRepositoryFromSlug(project);
        if (repo != null) {
            Build b = repo.getLastBuild(false);
            if (b != null) {
                if (b.getBuildTool() == BuildTool.MAVEN) {
                    long results = collection.count(and(
                            eq("repositoryName", project),
                            ne("typeOfFailures", null)
                    ));

                    if (results > 0) {
                        selectedProjects.add(project);
                    }
                }
            }
        }
    }

    File outputFile = new File(destList);
    BufferedWriter buffer = new BufferedWriter(new FileWriter(outputFile));
    buffer.write(StringUtils.join(selectedProjects,"\n"));
    buffer.close();

    System.out.println("Read projects: "+allProjects.size()+" | Selected projects : "+selectedProjects.size());
    System.out.println(StringUtils.join(selectedProjects, "\n"));
}
 
Example 30
Source Project: obevo   Source File: MongoDbChangeAuditDaoIT.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    this.mongoClient = MongoClientFactory.getInstance().getMongoClient(MongoDbTestHelper.HOST, MongoDbTestHelper.PORT);
    MongoDatabase mydb = mongoClient.getDatabase("mydb");
    mydb.getCollection("ARTIFACTDEPLOYMENT").drop();
    mydb.getCollection("ARTIFACTEXECUTION").drop();
}