@Test
  @Ignore("Really slow on the delete, not a good unit tests atm")
  public void deleteBatchTest() throws Exception {

    DB db = getDb();

    int count = (int) (OpDelete.BATCH_SIZE * 1.5);

    List<DBObject> docs = new ArrayList<DBObject>(count);

    for (int i = 0; i < count; i++) {
      BasicDBObject doc = new BasicDBObject();

      doc.put("index", i);

      docs.add(doc);
    }

    WriteResult result = db.getCollection("deletebatchtests").insert(docs);

    assertNull(result.getLastError().getErrorMessage());

    // iterate over all the data to make sure it's been inserted

    DBCursor cursor = db.getCollection("deletebatchtests").find();

    for (int i = 0; i < count && cursor.hasNext(); i++) {
      int index = new BasicDBObject(cursor.next().toMap()).getInt("index");

      assertEquals(i, index);
    }

    BasicDBObject query = new BasicDBObject();
    query.put("index", new BasicDBObject("$lte", count));

    // now delete the objects
    db.getCollection("deletebatchtests").remove(query, WriteConcern.SAFE);

    // now  try and iterate, there should be no results
    cursor = db.getCollection("deletebatchtests").find();

    assertFalse(cursor.hasNext());

    // check it has been deleted
    UUID appId = emf.lookupApplication("test-organization/test-app");
    EntityManager em = emf.getEntityManager(appId);

    Results results =
        em.searchCollection(
            new SimpleEntityRef("application", appId), "deletebatchtests", new Query());

    assertEquals(0, results.size());
  }
  @Test
  public void deleteTest() throws Exception {

    DB db = getDb();

    BasicDBObject doc = new BasicDBObject();

    doc.put("name", "nico");
    doc.put("color", "tabby");

    WriteResult result = db.getCollection("deletetests").insert(doc);

    ObjectId savedOid = doc.getObjectId("_id");

    assertNull(result.getError());

    BasicDBObject query = new BasicDBObject();
    query.put("_id", savedOid);

    // now load by the mongo Id. Users will use this the most to read data.

    BasicDBObject returnedObject =
        new BasicDBObject(db.getCollection("deletetests").findOne(query).toMap());

    assertEquals("nico", returnedObject.get("name"));
    assertEquals("tabby", returnedObject.get("color"));

    // TODO uncomment me assertEquals(savedOid,
    // returnedObject.getObjectId("_id"));

    UUID id = UUID.fromString(returnedObject.get("uuid").toString());

    // now delete the object
    db.getCollection("deletetests").remove(returnedObject, WriteConcern.SAFE);

    DBObject searched = db.getCollection("deletetests").findOne(query);

    assertNull(searched);

    // check it has been deleted

    UUID appId = emf.lookupApplication("test-organization/test-app");
    EntityManager em = emf.getEntityManager(appId);

    Entity entity = em.get(id);

    assertNull(entity);
  }
示例#3
0
  /*
   * (non-Javadoc)
   *
   * @see
   * org.usergrid.tools.ToolBase#runTool(org.apache.commons.cli.CommandLine)
   */
  @Override
  public void runTool(CommandLine line) throws Exception {
    startSpring();

    EntityManager rootEm = emf.getEntityManager(CassandraService.MANAGEMENT_APPLICATION_ID);

    for (Entry<UUID, String> org : managementService.getOrganizations().entrySet()) {

      for (Entry<UUID, String> app :
          managementService.getApplicationsForOrganization(org.getKey()).entrySet()) {

        Application application = rootEm.get(app.getKey(), Application.class);

        if (application == null) {
          logger.error("Could not load app with id {}", app.getKey());
          continue;
        }

        String appName = application.getName();

        // nothing to do, it's right
        if (appName.contains("/")) {
          logger.info("Application name is correct: {}", appName);
          continue;
        }

        String correctAppName = org.getValue() + "/" + app.getValue();

        correctAppName = correctAppName.toLowerCase();

        application.setName(correctAppName);

        rootEm.setProperty(application, "name", correctAppName, true);

        Application changedApp = rootEm.get(app.getKey(), Application.class);

        if (correctAppName.equals(changedApp.getName())) {
          logger.info("Application name corrected.  {} : {}", appName, correctAppName);
        } else {
          logger.error(
              "Could not correct Application with id {} to {}", app.getKey(), correctAppName);
        }
      }
    }
  }
  @Override
  public void runTool(CommandLine line) throws Exception {
    logger.info("Starting test...");
    startSpring();

    UserInfo user =
        managementService.createAdminUser(
            "admin", "admin", "*****@*****.**", "none", false, false, false);

    logger.info("Creating organization: sample-organization");
    // management
    // .createOrganization("sample-organization", "*****@*****.**", "1234");
    OrganizationInfo organization =
        managementService.createOrganization("sample-organization", user);

    logger.info("creating application: testEntityManagerTest");
    UUID applicationId =
        managementService.createApplication(organization.getUuid(), "sample-application");

    ServiceManager sm = smf.getServiceManager(applicationId);

    EntityManager em = emf.getEntityManager(applicationId);

    // Create user
    Map<String, Object> properties = new LinkedHashMap<String, Object>();
    properties.put("username", "edanuff");
    properties.put("email", "*****@*****.**");
    properties.put("name", "Ed Anuff");

    Entity user1 = em.create("user", properties);

    // Create activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST, null, "I ate a sammich", null, user1, null, "tweet", null, null)
            .getProperties();

    @SuppressWarnings("unused")
    Entity activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user1.getUuid(), "activities")
            .getEntity();

    // Create another activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST, null, "cool pic dude", null, user1, null, "tweet", null, null)
            .getProperties();

    activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user1.getUuid(), "activities")
            .getEntity();

    // Create another user
    properties = new LinkedHashMap<String, Object>();
    properties.put("username", "justin");
    properties.put("email", "*****@*****.**");
    properties.put("name", "Justin Clark");

    Entity user2 = em.create("user", properties);

    // Create activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST,
                null,
                "ATT U-verse May payment",
                null,
                user2,
                null,
                "payment",
                null,
                null)
            .getProperties();

    activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user2.getUuid(), "activities")
            .getEntity();

    // Connections
    em.createConnection(user1, "workWith", user2);
  }
  @Test
  public void insertTest() throws Exception {

    DB db = getDb();

    BasicDBObject doc = new BasicDBObject();

    doc.put("name", "nico");
    doc.put("color", "tabby");

    WriteResult result = db.getCollection("inserttests").insert(doc);

    ObjectId savedOid = doc.getObjectId("_id");

    assertNull(result.getError());

    // check we've created the collection

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

    assertTrue(colls.contains("inserttests"));

    // iterate the collection to ensure we can retrieve the object
    DBCollection coll = db.getCollection("inserttests");
    DBCursor cur = coll.find();

    BasicDBObject returnedObject = null;

    assertTrue(cur.hasNext());

    returnedObject = (BasicDBObject) cur.next();

    assertFalse(cur.hasNext());

    UUID id = UUID.fromString(returnedObject.get("uuid").toString());

    // this should work.  Appears to be the type of ObjectId getting lost on column serialization
    ObjectId returnedOid = new ObjectId(returnedObject.getString("_id"));

    assertEquals("nico", returnedObject.get("name"));
    assertEquals("tabby", returnedObject.get("color"));
    assertEquals(savedOid, returnedOid);
    assertNotNull(id);

    BasicDBObject query = new BasicDBObject();
    query.put("_id", savedOid);

    // now load by the mongo Id. Users will use this the most to read data.

    returnedObject = new BasicDBObject(db.getCollection("inserttests").findOne(query).toMap());

    assertEquals("nico", returnedObject.get("name"));
    assertEquals("tabby", returnedObject.get("color"));

    assertEquals(savedOid, new ObjectId(returnedObject.getString("_id")));
    assertEquals(id.toString(), returnedObject.get("uuid"));

    // check we can find it when using the native entity manager

    UUID appId = emf.lookupApplication("test-organization/test-app");
    EntityManager em = emf.getEntityManager(appId);

    Entity entity = em.get(id);

    assertNotNull(entity);
    assertEquals("nico", entity.getProperty("name"));
    assertEquals("tabby", entity.getProperty("color"));
  }