@Test(groups = "dev")
  public void autoWrapTest() throws EventDeliveryException {
    ctx.put(MongoSink.AUTO_WRAP, Boolean.toString(true));
    ctx.put(MongoSink.DB_NAME, "test_wrap");

    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();
    String msg =
        "2012/10/26 11:23:08 [error] 7289#0: *6430831 open() \"/usr/local/nginx/html/50x.html\" failed (2: No such file or directory), client: 10.160.105.161, server: sg15.redatoms.com, request: \"POST /mojo/ajax/embed HTTP/1.0\", upstream: \"fastcgi://unix:/tmp/php-fpm.sock:\", host: \"sg15.redatoms.com\", referrer: \"http://sg15.redatoms.com/mojo/mobile/package\"";

    Event e = EventBuilder.withBody(msg.getBytes());
    channel.put(e);
    tx.commit();
    tx.close();

    sink.process();
    sink.stop();

    DB db = mongo.getDB("test_wrap");
    DBCollection collection = db.getCollection("test_log");
    DBCursor cursor = collection.find(new BasicDBObject(MongoSink.DEFAULT_WRAP_FIELD, msg));
    assertTrue(cursor.hasNext());
    DBObject dbObject = cursor.next();
    assertNotNull(dbObject);
    assertEquals(dbObject.get(MongoSink.DEFAULT_WRAP_FIELD), msg);
    mongo.dropDatabase("test_wrap");
  }
 @AfterMethod(groups = {"dev"})
 public static void tearDown() {
   mongo.dropDatabase(DBNAME);
   mongo.dropDatabase("test_events");
   mongo.dropDatabase("dynamic_db");
   mongo.close();
 }
Exemplo n.º 3
0
  private void saveEvents(Map<String, List<DBObject>> eventMap) {
    if (eventMap.isEmpty()) {
      logger.debug("eventMap is empty");
      return;
    }

    for (String eventCollection : eventMap.keySet()) {
      List<DBObject> docs = eventMap.get(eventCollection);
      if (logger.isDebugEnabled()) {
        logger.debug("collection: {}, length: {}", eventCollection, docs.size());
      }
      int separatorIndex = eventCollection.indexOf(NAMESPACE_SEPARATOR);
      String eventDb = eventCollection.substring(0, separatorIndex);
      String collectionName = eventCollection.substring(separatorIndex + 1);

      // Warning: please change the WriteConcern level if you need high datum consistence.
      CommandResult result =
          mongo
              .getDB(eventDb)
              .getCollection(collectionName)
              .insert(docs, WriteConcern.NORMAL)
              .getLastError();
      if (result.ok()) {
        String errorMessage = result.getErrorMessage();
        if (errorMessage != null) {
          logger.error("can't insert documents with error: {} ", errorMessage);
          logger.error("with exception", result.getException());

          throw new MongoException(errorMessage);
        }
      } else {
        logger.error("can't get last error");
      }
    }
  }
  @Test(groups = "dev")
  public void sinkSingleModelTest() throws EventDeliveryException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.single.name());

    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();
    JSONObject msg = new JSONObject();
    msg.put("name", "test");
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());

    Event e = EventBuilder.withBody(msg.toJSONString().getBytes());
    channel.put(e);
    tx.commit();
    tx.close();

    sink.process();
    sink.stop();

    DB db = mongo.getDB("test_events");
    DBCollection collection = db.getCollection("test_log");
    DBCursor cursor = collection.find(new BasicDBObject(msg));
    assertTrue(cursor.hasNext());
    DBObject dbObject = cursor.next();
    assertNotNull(dbObject);
    assertEquals(dbObject.get("name"), msg.get("name"));
    assertEquals(dbObject.get("age"), msg.get("age"));
    assertEquals(dbObject.get("birthday"), msg.get("birthday"));
  }
  @Test(groups = "dev")
  public void dbTest() {
    DB db = mongo.getDB(DBNAME);
    db.getCollectionNames();
    List<String> names = mongo.getDatabaseNames();

    assertNotNull(names);
    boolean hit = false;

    for (String name : names) {
      if (DBNAME.equals(name)) {
        hit = true;
        break;
      }
    }

    assertTrue(hit);
  }
  @Test(groups = "dev")
  public void timestampExistingFieldTest() throws EventDeliveryException, ParseException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.dynamic.name());
    String tsField = "createdOn";
    ctx.put(MongoSink.TIMESTAMP_FIELD, tsField);
    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    JSONObject msg = new JSONObject();
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());
    String dateText = "2013-02-19T14:20:53+08:00";
    msg.put(tsField, dateText);

    Transaction tx;

    for (int i = 0; i < 10; i++) {
      tx = channel.getTransaction();
      tx.begin();
      msg.put("name", "test" + i);
      JSONObject header = new JSONObject();
      header.put(MongoSink.COLLECTION, "my_events");
      header.put(MongoSink.DB_NAME, "dynamic_db");

      Event e = EventBuilder.withBody(msg.toJSONString().getBytes(), header);
      channel.put(e);
      tx.commit();
      tx.close();
    }
    sink.process();
    sink.stop();

    msg.put(tsField, MongoSink.dateTimeFormatter.parseDateTime(dateText).toDate());
    for (int i = 0; i < 10; i++) {
      msg.put("name", "test" + i);

      System.out.println("i = " + i);

      DB db = mongo.getDB("dynamic_db");
      DBCollection collection = db.getCollection("my_events");
      DBCursor cursor = collection.find(new BasicDBObject(msg));
      assertTrue(cursor.hasNext());
      DBObject dbObject = cursor.next();
      assertNotNull(dbObject);
      assertEquals(dbObject.get("name"), msg.get("name"));
      assertEquals(dbObject.get("age"), msg.get("age"));
      assertEquals(dbObject.get("birthday"), msg.get("birthday"));
      assertTrue(dbObject.get(tsField) instanceof Date);
      System.out.println("ts = " + dbObject.get(tsField));
    }
  }
Exemplo n.º 7
0
  @Override
  public synchronized void start() {
    logger.info("Starting {}...", getName());

    try {
      mongo = new Mongo(host, port);
      db = mongo.getDB(dbName);
    } catch (UnknownHostException e) {
      logger.error("Can't connect to mongoDB", e);
    }

    super.start();
    logger.info("Started {}.", getName());
  }
  @Test(groups = "dev")
  public void sinkDynamicDbTest() throws EventDeliveryException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.dynamic.name());
    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    JSONObject msg = new JSONObject();
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());

    Transaction tx;

    for (int i = 0; i < 10; i++) {
      tx = channel.getTransaction();
      tx.begin();
      msg.put("name", "test" + i);
      JSONObject header = new JSONObject();
      header.put(MongoSink.COLLECTION, "my_events");
      header.put(MongoSink.DB_NAME, "dynamic_db");

      Event e = EventBuilder.withBody(msg.toJSONString().getBytes(), header);
      channel.put(e);
      tx.commit();
      tx.close();
    }
    sink.process();
    sink.stop();

    for (int i = 0; i < 10; i++) {
      msg.put("name", "test" + i);

      System.out.println("i = " + i);

      DB db = mongo.getDB("dynamic_db");
      DBCollection collection = db.getCollection("my_events");
      DBCursor cursor = collection.find(new BasicDBObject(msg));
      assertTrue(cursor.hasNext());
      DBObject dbObject = cursor.next();
      assertNotNull(dbObject);
      assertEquals(dbObject.get("name"), msg.get("name"));
      assertEquals(dbObject.get("age"), msg.get("age"));
      assertEquals(dbObject.get("birthday"), msg.get("birthday"));
    }
  }
  @Test(groups = "dev")
  public void collectionTest() {
    DB db = mongo.getDB(DBNAME);
    DBCollection myCollection = db.getCollection("myCollection");
    myCollection.save(
        new BasicDBObject(
            MapUtils.putAll(new HashMap(), new Object[] {"name", "leon", "age", 33})));
    myCollection.findOne();

    Set<String> names = db.getCollectionNames();

    assertNotNull(names);
    boolean hit = false;

    for (String name : names) {
      if ("myCollection".equals(name)) {
        hit = true;
        break;
      }
    }

    assertTrue(hit);
  }