public void performCRUDTest(String objectTypeName) throws Exception {
    try {
      s_log.info("CRUDTest on " + objectTypeName);
      DataObject object = SessionManager.getSession().create(objectTypeName);
      reportPropertyTypes(object);

      initializeObject(object, null);
      OID id = object.getOID();
      s_log.info("Initialized object with id: " + id);
      object.save();
      object = SessionManager.getSession().retrieve(id);
      Assert.assertNotNull(
          object, "Object of type: " + objectTypeName + "and id: " + id + " was not found!");
      checkDefaultValues(object);

      checkUpdates(object, null);
      deleteObject(id);

    } catch (Exception e) {
      s_log.info("END CRUDTest on " + objectTypeName + " With error!");
      s_log.info(e.getMessage());
      s_log.info("");
      s_log.info("");
      throw e;
    }

    s_log.info("END CRUDTest on " + objectTypeName);
    s_log.info("");
    s_log.info("");
  }
    /**
     * Iterates over all of the variantValues for the given Property and attempts to update the
     * property to that value. If an invalid failure to update occurs, an exception is thrown.
     *
     * @param p The property to update.
     * @param data The DataObject to save.
     * @pre p.equals(data.getObjectType.getProperty(p.getName()))
     */
    public void updateAllPropertyCombinations(Property p, DataObject data) throws Exception {
      OID id = data.getOID();
      s_log.info("Property " + p.getName() + " class is: " + p.getJavaClass());
      // Verify that nulls are handled correctly for all columns
      setProperty(p, data, null);
      data = SessionManager.getSession().retrieve(id);

      Iterator iter = getVariantValues().iterator();
      while (iter.hasNext()) {
        Object value = iter.next();
        setProperty(p, data, value);
        // It is neccessary to re-fetch the DataObject, since some 'failed' updates
        // will not cause the test to fail, but will leave the DataObject in an inconsistent state.
        // These are proper update failures, such as an oversized default String value being sent
        // to a constrained column.
        data = SessionManager.getSession().retrieve(id);
      }
    }
 private static final Column getColumn(Property p) {
   Root root = SessionManager.getSession().getMetadataRoot().getRoot();
   ObjectMap om = root.getObjectMap(root.getObjectType(p.getContainer().getQualifiedName()));
   Mapping m = om.getMapping(Path.get(p.getName()));
   if (m instanceof Value) {
     return ((Value) m).getColumn();
   } else {
     return null;
   }
 }
    /**
     * Attempts to update the Property to a given value. If an invalid failure to update occurs, an
     * exception is thrown. If the update appears successful, the DataObject is re-fetched from the
     * database, and the property value compared to what it should be. If they differ, an exception
     * is thrown.
     *
     * @param p The property to update.
     * @param data The DataObject to save.
     * @param value The value to update the property with.
     * @pre p.equals(data.getObjectType.getProperty(p.getName()))
     */
    void setProperty(Property p, DataObject data, Object value) throws Exception {
      final String propName = p.getName();
      OID id = data.getOID();
      s_log.debug(
          "setting property : "
              + data.getObjectType().getQualifiedName()
              + "."
              + data.getObjectType().getName()
              + "."
              + p.getName()); // + " to value: " + value);
      s_log.debug("Old value was: " + data.get(p.getName()));
      final boolean valueIsNull =
          (value == null || value instanceof String && ((String) value).length() == 0);
      boolean savedNullInRequiredField = false;
      try {
        data.set(p.getName(), value);
        data.save();
        // This is neccessary since fail cannot be called here.
        // Its exception would be caught. Need to check this
        // value after the catch block
        savedNullInRequiredField = (p.isRequired() && valueIsNull);
      } catch (Exception t) {
        if (p.isRequired() && valueIsNull) {
          return;
        }
        if (p.isNullable() && valueIsNull) {
          String msg = "Failed to save DataObject: " + data.getObjectType().getName();
          msg += "\nTried to update nullable property " + p.getName();
          msg += " with null value and failed!";
          msg += "\nException is: " + t.getMessage();
          s_log.debug(msg);
          throw new CheckedWrapperException(msg, t);
        } else {
          s_log.debug("Failed to set property " + p.getName());
          s_log.debug("Checking error");
          checkSetError(t, p, data, value);
          //                    if (DbHelper.getDatabase(getSession().getConnection()) ==
          // DbHelper.DB_POSTGRES) {
          throw new AbortMetaTestException();
          //                    } else {
          //                        return;
          //                    }
        }
      }

      if (savedNullInRequiredField) {
        String msg = "DataObject saved null value in a required Property: " + p.getName();
        s_log.debug(msg);
        throw new Exception(msg);
      }
      DataObject fromDatabase = SessionManager.getSession().retrieve(id);
      Object newValue = fromDatabase.get(p.getName());
      checkEquals(p.getName(), value, newValue);
    }
  private void deleteObject(OID id) throws Exception {

    // Manipulator for removing associations before delete
    PropertyManipulator.AssociationManipulator assocRemover =
        new PropertyManipulator.AssociationManipulator() {
          public void manipulate(Property p, DataObject data) throws Exception {
            s_log.info("Found association: " + p.getName());
            if (p.isCollection()) {
              DataAssociation assoc = (DataAssociation) data.get(p.getName());
              DataAssociationCursor cursor = assoc.cursor();
              while (cursor.next()) {
                s_log.info(
                    "Removing from association: "
                        + cursor.getDataObject().getObjectType().getName());
                cursor.remove();
                s_log.info("Removed!");
              }
            }
          }
        };

    DataObject data = SessionManager.getSession().retrieve(id);
    s_log.info("");
    String objectName = data.getObjectType().getName();
    s_log.info("Deleting object: " + objectName + " with OID: " + data.getOID());

    PropertyManipulator.manipulateProperties(data, assocRemover);

    s_log.info("daving data!");
    data.save();
    s_log.info("about to delete!");
    data.delete();
    Assert.truth(data.isDeleted());
    data = SessionManager.getSession().retrieve(id);
    Assert.truth(null == data);
    s_log.info("END Removing object: " + objectName);
    s_log.info("");
  }
  public void testReferenceLinkAttribute() {
    Session ssn = SessionManager.getSession();
    DataObject user = ssn.create(getModelName() + ".User");
    user.set("id", BigInteger.valueOf(0));
    user.set("email", "*****@*****.**");
    user.set("firstName", "foo");
    user.set("lastNames", "bar");
    user.save();

    DataObject[] images = new DataObject[2];
    for (int i = 0; i < images.length; i++) {
      images[i] = ssn.create(getModelName() + ".Image");
      images[i].set("id", BigInteger.valueOf(i));
      byte[] bytes = "This is the image.".getBytes();
      images[i].set("bytes", bytes);
      images[i].save();
    }

    // set image
    user.set("image", images[0]);
    user.save();

    // retrieve and then update caption
    DataAssociationCursor dac = ((DataAssociation) images[0].get("users")).cursor();
    dac.next();
    assertNull(dac.getLinkProperty("caption"));
    assertEquals(user, dac.getDataObject());
    DataObject link = dac.getLink();
    link.set("caption", "caption");
    link.save();

    dac = ((DataAssociation) images[0].get("users")).cursor();
    dac.next();
    assertEquals("caption", dac.getLinkProperty("caption"));
    assertEquals(1L, ((DataAssociation) images[0].get("users")).size());

    // set new image as image
    user.set("image", images[1]);
    user.save();

    // check that old image is no longer associated with user
    assertEquals(0L, ((DataAssociation) images[0].get("users")).size());

    // check that new image is associated with user and has no caption
    dac = ((DataAssociation) images[1].get("users")).cursor();
    dac.next();
    assertNull(dac.getLinkProperty("caption"));
    assertEquals(1L, ((DataAssociation) images[1].get("users")).size());
  }
Пример #7
0
  public void testArticleImageLink() {
    Session ssn = SessionManager.getSession();
    DataObject article = ssn.create(getModel() + ".Article");
    article.set("id", BigInteger.ZERO);
    String text = "This is the article text.";
    article.set("text", text);

    for (int i = 0; i < 10; i++) {
      DataObject image = ssn.create(getModel() + ".Image");
      image.set("id", new BigInteger(Integer.toString(i)));
      byte[] bytes = "This is the image.".getBytes();
      image.set("bytes", bytes);
      image.save();
    }

    DataAssociation links = (DataAssociation) article.get("images");
    DataCollection images = ssn.retrieve(getModel() + ".Image");
    while (images.next()) {
      DataObject image = images.getDataObject();
      DataObject link = ssn.create(getModel() + ".ArticleImageLink");
      link.set("article", article);
      link.set("image", image);
      link.set("caption", "The caption for: " + image.getOID());
      links.add(link);
    }

    article.save();

    DataAssociationCursor cursor = links.cursor();
    assertEquals(10, cursor.size());

    DataCollection aiLinks = ssn.retrieve(getModel() + ".ArticleImageLink");
    aiLinks.addEqualsFilter("image.id", new BigDecimal(5));
    if (aiLinks.next()) {
      DataObject linkArticle = (DataObject) aiLinks.get("article");
      DataObject linkImage = (DataObject) aiLinks.get("image");
      String caption = (String) aiLinks.get("caption");
      assertEquals(BigInteger.valueOf(0), linkArticle.get("id"));
      assertEquals(BigInteger.valueOf(5), linkImage.get("id"));

      if (aiLinks.next()) {
        fail("too many rows");
      }
    } else {
      fail("no rows returned");
    }

    article.delete();
  }
Пример #8
0
  @Override
  public ResponseMessage execute() {
    SessionManager sm = sessionManager;
    Session session = sm.getSession(sessionHandler);

    if (session == null) {
      return ResponseMessage.ErrorMessage("Bad session handler.");
    }

    Signer signer = session.getSigner();
    try {
      signer.init(this.mechanism, this.privateKeyHandler);
      return ResponseMessage.OKMessage();
    } catch (Exception e) {
      return ResponseMessage.ErrorMessage(e.getLocalizedMessage());
    }
  }
Пример #9
0
  public void testImage() {
    Session ssn = SessionManager.getSession();
    DataObject image = ssn.create(getModel() + ".Image");
    image.set("id", BigInteger.ZERO);
    byte[] bytes = "This is the image.".getBytes();
    image.set("bytes", bytes);
    image.save();

    OID oid = new OID(getModel() + ".Image", BigInteger.ZERO);

    image = ssn.retrieve(oid);
    assertEquals("incorrect id", BigInteger.ZERO, image.get("id"));
    assertTrue("incorrect image", Arrays.equals(bytes, (byte[]) image.get("bytes")));

    image.delete();

    assertEquals("image not deleted properly", null, ssn.retrieve(oid));
  }
Пример #10
0
  public void testArticle() {
    Session ssn = SessionManager.getSession();
    DataObject article = ssn.create(getModel() + ".Article");
    article.set("id", BigInteger.ZERO);
    String text = "This is the article text.";
    article.set("text", text);
    article.save();

    OID oid = new OID(getModel() + ".Article", BigInteger.ZERO);

    article = ssn.retrieve(oid);
    assertEquals("incorrect id", BigInteger.ZERO, article.get("id"));
    assertEquals("incorrect text", text, article.get("text"));

    article.delete();

    assertEquals("article not deleted properly", null, ssn.retrieve(oid));
  }
  private void makeChild(Property p, final DataObject parent) throws Exception {

    final String fullTypeName = p.getType().getQualifiedName();
    s_log.info(
        "Making child object: "
            + fullTypeName
            + " for ObjectType: "
            + parent.getObjectType().getQualifiedName());

    DataObject child = SessionManager.getSession().create(fullTypeName);
    reportPropertyTypes(child);

    initializeObject(child, parent);
    PropertyManipulator.AssociationManipulator manip =
        new PropertyManipulator.AssociationManipulator() {
          public boolean obeys(Property pInner) {
            final boolean isParentRef =
                super.obeys(pInner)
                    && !pInner.isCollection()
                    && pInner.getType().equals(parent.getObjectType());
            return isParentRef;
          }

          public void manipulate(Property pInner, DataObject data) throws Exception {
            s_log.info(
                "Setting parent role reference for: "
                    + fullTypeName
                    + " Property: "
                    + pInner.getName());
            data.set(pInner.getName(), parent);
          }
        };
    PropertyManipulator.manipulateProperties(child, manip);
    if (p.isCollection()) {
      DataAssociation children = (DataAssociation) parent.get(p.getName());
      children.add(child);

    } else {
      parent.set(p.getName(), child);
    }
  }
  private void makeAssociation(Property p, DataObject data) throws Exception {

    String fullTypeName = p.getType().getQualifiedName();
    s_log.info(
        "Making associated object: "
            + fullTypeName
            + " for ObjectType: "
            + data.getObjectType().getQualifiedName());

    DataObject associatedObject = SessionManager.getSession().create(fullTypeName);
    reportPropertyTypes(associatedObject);
    initializeObject(associatedObject, data);
    associatedObject.save();
    reportPropertyTypes(associatedObject);
    s_log.info("Getting association:  " + p.getName());
    if (p.isCollection()) {
      DataAssociation assoc = (DataAssociation) data.get(p.getName());
      assoc.add(associatedObject);
    } else {
      data.set(p.getName(), associatedObject);
    }
  }
Пример #13
0
  /**
   * Performs the actual packet routing.
   *
   * <p>You routing is considered 'quick' and implementations may not take excessive amounts of time
   * to complete the routing. If routing will take a long amount of time, the actual routing should
   * be done in another thread so this method returns quickly.
   *
   * <h2>Warning</h2>
   *
   * <p>Be careful to enforce concurrency DbC of concurrent by synchronizing any accesses to class
   * resources.
   *
   * @param packet The packet to route
   * @throws NullPointerException If the packet is null
   */
  public void route(Message packet) {
    if (packet == null) {
      throw new NullPointerException();
    }
    Session session = sessionManager.getSession(packet.getFrom());
    if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
      JID recipientJID = packet.getTo();

      // If the message was sent to the server hostname then forward the message to
      // a configurable set of JID's (probably admin users)
      if (recipientJID.getNode() == null
          && recipientJID.getResource() == null
          && serverName.equals(recipientJID.getDomain())) {
        sendMessageToAdmins(packet);
        return;
      }

      try {
        routingTable.getBestRoute(recipientJID).process(packet);
      } catch (Exception e) {
        try {
          messageStrategy.storeOffline(packet);
        } catch (Exception e1) {
          Log.error(e1);
        }
      }

    } else {
      packet.setTo(session.getAddress());
      packet.setFrom((JID) null);
      packet.setError(PacketError.Condition.not_authorized);
      try {
        session.process(packet);
      } catch (UnauthorizedException ue) {
        Log.error(ue);
      }
    }
  }
  public void testDeepLink() {
    Session ssn = SessionManager.getSession();

    DataObject[] users = new DataObject[4];

    DataObject group = getSession().create(getModelName() + ".Group");
    group.set("id", BigInteger.valueOf(users.length));
    group.set("email", "*****@*****.**");
    group.set("name", "SIPB");
    group.save();
    DataAssociation members = (DataAssociation) group.get("members");

    for (int i = 0; i < users.length; i++) {
      users[i] = ssn.create(getModelName() + ".User");
      users[i].set("id", BigInteger.valueOf(i));
      users[i].set("email", "*****@*****.**");
      users[i].set("firstName", "foo");
      users[i].set("lastNames", "bar");
      users[i].save();
      members.add(users[i]);
    }
    group.save();

    DataObject[] images = new DataObject[users.length / 2];
    for (int i = 0; i < images.length; i++) {
      images[i] = ssn.create(getModelName() + ".Image");
      images[i].set("id", BigInteger.valueOf(i));
      byte[] bytes = "This is the image.".getBytes();
      images[i].set("bytes", bytes);
      images[i].save();
    }

    // create link between user i and image i/2 with caption i
    for (int i = 0; i < users.length; i++) {
      // set image
      DataAssociation imageUsers = (DataAssociation) images[i / 2].get("users");
      DataObject link = imageUsers.add(users[i]);
      link.set("caption", String.valueOf(i));
      link.save();
    }

    DataCollection dc = ssn.retrieve(getModelName() + ".Group");
    dc.addEqualsFilter("members.image.link.caption", "0");
    assertEquals(1, dc.size());

    dc = ssn.retrieve(getModelName() + ".User");
    dc.addPath("image.link.caption");
    assertEquals(users.length, dc.size());
    while (dc.next()) {
      assertEquals(dc.get("id").toString(), dc.get("image.link.caption"));
    }

    dc = ssn.retrieve(getModelName() + ".User");
    dc.addPath("image.id");
    assertEquals(users.length, dc.size());
    while (dc.next()) {
      int id = ((BigInteger) dc.get("id")).intValue();
      assertEquals(BigInteger.valueOf(id / 2), dc.get("image.id"));
    }

    DataCollection dcUp = ssn.retrieve(getModelName() + ".User");
    DataCollection dcDown = ssn.retrieve(getModelName() + ".User");
    dcUp.addOrder("image.link.caption asc");
    dcDown.addOrder("image.link.caption desc");
    dcUp.next();
    dcDown.next();
    assertEquals(BigInteger.valueOf(0), dcUp.get("id"));
    assertEquals(BigInteger.valueOf(users.length - 1), dcDown.get("id"));
    dcUp.close();
    dcDown.close();

    dcUp = ssn.retrieve(getModelName() + ".Image");
    dcDown = ssn.retrieve(getModelName() + ".Image");
    dcUp.addOrder("users.link.caption asc");
    dcDown.addOrder("users.link.caption desc");
    dcUp.next();
    dcDown.next();
    assertEquals(BigInteger.valueOf(0), dcUp.get("id"));
    assertEquals(BigInteger.valueOf(images.length - 1), dcDown.get("id"));
    dcUp.close();
    dcDown.close();

    dc = ssn.retrieve(getModelName() + ".Group");
    dc.addFilter("members.image.id = 0");
    assertEquals(2, dc.size());

    dc = ssn.retrieve(getModelName() + ".Image");
    dc.addFilter("users.id = 0 and users.link.caption = '1'");
    assertEquals(0, dc.size());

    dc = ssn.retrieve(getModelName() + ".Group");
    dc.addPath("members.id");
    dc.addFilter("members.image.id = 0 and members.image.link.caption = '1'");
    assertTrue(dc.next());
    assertEquals(BigInteger.valueOf(1), dc.get("members.id"));
    assertFalse(dc.next());
  }
 public void save(Validacion validacion) {
   SessionManager.getSession().saveOrUpdate(validacion);
 }
 public Validacion get(int id) {
   return (Validacion) SessionManager.getSession().get(Validacion.class, id);
 }