@Test
  // https://github.com/bguerout/jongo/issues/60
  public void shouldBindPatterns() throws Exception {

    collection.save(new Friend("ab"));

    assertThat(collection.findOne("{name:#}", Pattern.compile("ab")).as(Friend.class)).isNotNull();
    assertThat(collection.findOne("{name:{$regex: 'ab'}}").as(Friend.class)).isNotNull();
  }
  @Test
  public void shouldBindListOfPrimitive() throws Exception {

    collection.insert("{index:1}");

    List<Integer> indexes = Lists.newArrayList(1, 2);

    long nb = collection.count("{index:{$in:#}}", indexes);

    assertThat(nb).isEqualTo(1);
  }
  @Test
  // https://groups.google.com/forum/?hl=fr&fromgroups#!topic/jongo-user/ga3n5_ybYm4
  public void shouldBindAListOfPojo() throws Exception {

    Buddies buddies = new Buddies();
    buddies.add(new Friend("john"));
    collection.save(buddies);

    collection.update("{}").with("{$push:{friends:#}}", new Friend("peter"));

    assertThat(collection.count("{ friends.name : 'peter'}")).isEqualTo(1);
  }
  @Test
  public void shouldBindEnumParameter() throws Exception {

    Friend friend = new Friend("John", new Coordinate(2, 31));
    friend.setGender(Gender.FEMALE);
    collection.save(friend);

    Iterator<Friend> results =
        collection.find("{'gender':#}", Gender.FEMALE).as(Friend.class).iterator();

    assertThat(results.next().getGender()).isEqualTo(Gender.FEMALE);
    assertThat(results.hasNext()).isFalse();
  }
  @Test
  public void shouldBindAFieldName() throws Exception {

    /* given */
    collection.insert("{name:{1:'John'}}");

    /* when */
    DBObject result = collection.findOne("{name.#:#}", 1, "John").map(new DBObjectResultMapper());

    /* then */
    assertThat(result).isNotNull();
    assertThat(result.get("name")).isInstanceOf(DBObject.class);
    assertThat(((DBObject) result.get("name")).get("1")).isEqualTo("John");
  }
Пример #6
0
  public static String generateUniqueId(String type) {

    MongoCollection idSeed = MongoDBController.getCollection(CollectionNames.idcollection);
    ID id = idSeed.findOne().as(ID.class);

    int uniqueNumber = id.val.intValue();
    id.val = new Integer(uniqueNumber + 1);
    idSeed.update("{val: #}", uniqueNumber).upsert().with("{val : #}", (uniqueNumber + 1));
    // idSeed.update().with("{val : " + id.val + "}");

    DateTime now = DateTime.now();
    String uniqueId =
        now.year().getAsString() + now.monthOfYear().getAsString() + type + uniqueNumber;

    return uniqueId;
  }
  @Test
  public void shouldBindAPojo() throws Exception {

    long nb = collection.count("#", new Friend("robert", "Wall Street", new Coordinate(2, 3)));

    assertThat(nb).isEqualTo(1);
  }
  @Test
  public void shouldBindOneParameter() throws Exception {

    long nb = collection.count("{name:#}", "robert");

    assertThat(nb).isEqualTo(1);
  }
  @Test
  public void shouldBindParametersOnNestedFields() throws Exception {

    long nb = collection.count("{coordinate.lat:#}", 2);

    assertThat(nb).isEqualTo(1);
  }
Пример #10
0
  @Test
  public void shouldBindManyParameter() throws Exception {

    long nb = collection.count("{name:#,address:#}", "robert", "Wall Street");

    assertThat(nb).isEqualTo(1);
  }
Пример #11
0
  @Test
  public void canFindWithOidNamed() throws Exception {
    /* given */
    ObjectId id = new ObjectId();
    LinkedFriend john = new LinkedFriend(id);
    collection.save(john);

    Iterator<LinkedFriend> friends =
        collection
            .find("{friendRelationId:{$oid:#}}", id.toString())
            .as(LinkedFriend.class)
            .iterator();

    /* then */
    assertThat(friends.hasNext()).isTrue();
    assertThat(friends.next().getRelationId()).isEqualTo(id);
  }
Пример #12
0
 @Test
 public void shouldThrowMarshallExceptionOnInvalidParameter() throws Exception {
   try {
     collection.findOne("{id:#}", new ErrorObject()).as(Friend.class);
   } catch (Exception e) {
     assertThat(e).isInstanceOf(IllegalArgumentException.class);
     assertThat(e.getCause()).isInstanceOf(MarshallingException.class);
     assertThat(e.getMessage()).contains("{id:#}");
   }
 }
Пример #13
0
  @Test
  public void shouldThrowArgumentExceptionOnInvalidQuery() throws Exception {

    try {
      collection.count("{invalid}");
      fail();
    } catch (Exception e) {
      assertThat(e).isInstanceOf(IllegalArgumentException.class);
      assertThat(e.getMessage()).contains("{invalid}");
    }
  }
Пример #14
0
  @BodyParser.Of(BodyParser.Json.class)
  public static Result newBeer() {
    GridFSInputFile gfsImg = null;
    ObjectMapper mapper = new ObjectMapper();
    Beer beer = null;
    try {
      beer = mapper.readValue(request().body().asJson(), Beer.class);
      beers.save(beer);
    } catch (JsonParseException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (JsonMappingException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {
      BufferedImage img =
          ImageIO.read(new URL("http://wwwimages.harpoonbrewery.com/SummerBeer-2013-Modal.jpg"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(img, "jpg", baos);
      baos.flush();
      gfsImg = gfs.createFile(baos.toByteArray());
      gfsImg.setFilename("bestbeer.jpg");
      gfsImg.save();
      beer.imgId = gfsImg.getId().toString();
      beers.save(beer);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return ok(Json.parse("{\"beerId\":\"" + beer.getId() + "\"}"));
  }
Пример #15
0
  @Test
  public void canFindWithTwoOid() throws Exception {
    /* given */
    ObjectId id1 = new ObjectId();
    Friend john = new Friend(id1, "John");
    ObjectId id2 = new ObjectId();
    Friend peter = new Friend(id2, "Peter");

    collection.save(john);
    collection.save(peter);

    Iterable<Friend> friends =
        collection
            .find("{$or :[{_id:{$oid:#}},{_id:{$oid:#}}]}", id1.toString(), id2.toString())
            .as(Friend.class);

    /* then */
    assertThat(friends.iterator().hasNext()).isTrue();
    for (Friend friend : friends) {
      assertThat(friend.getId()).isIn(id1, id2);
    }
  }
Пример #16
0
  @Override
  public void start(Stage stage) throws Exception {

    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();

    try {
      DB db = new MongoClient("localhost", 27017).getDB("UchCentre");

      Jongo jongo = new Jongo(db);

      MongoCollection studentss = jongo.getCollection("students");
      System.out.println("Найдено объектов: " + studentss.count());

      MongoCursor<Student> all = studentss.find("{}").as(Student.class);
      Student one = studentss.findOne("{name: 'Nurbek'}").as(Student.class);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #17
0
  @Override
  public Object execute(StatementRuntime runtime) {
    Method m = runtime.getMethod();
    Update update = m.getAnnotation(Update.class);
    String doc = update.coll();
    String cnd = update.cnd();
    String with = update.with();
    boolean upsert = update.upsert();
    boolean multi = update.multi();

    int queryParasNum = StringUtils.countMatches(cnd, "#");
    int withParasNum = StringUtils.countMatches(with, "#");

    List<Object> paras = runtime.getParameters();
    if (paras.size() < queryParasNum + withParasNum) {
      return null;
    }

    List<Object> queryParas = paras.subList(0, queryParasNum);
    List<Object> withParas = paras.subList(queryParasNum, queryParasNum + withParasNum);

    MongoCollection mc = this.jongo.getCollection(doc);
    org.jongo.Update upder = null;
    if (cnd.equals("")) {
      upder = mc.update(cnd);
    } else if (cnd.equals("#oid")) {
      upder = mc.update(new ObjectId(paras.get(0).toString()));
    } else if (cnd.equals("#id")) {
      upder = mc.update("{_id : #}", paras.get(0));
    } else if (queryParasNum > 0) {
      upder = mc.update(cnd, queryParas.toArray());
    } else {
      upder = mc.update(cnd);
    }

    if (upsert) {
      upder = upder.upsert();
    }

    if (multi) {
      upder = upder.multi();
    }
    WriteResult wr = null;
    if (with.equals("")) {
      wr = upder.with("");
    } else if (withParasNum > 0) {
      wr = upder.with(with, withParas.toArray());
    } else {
      wr = upder.with(with);
    }

    return wr;
  }
Пример #18
0
  public static Result listBeers() {

    Iterable<Beer> all = beers.find().as(Beer.class);
    return ok(Json.toJson(all));
  }
Пример #19
0
 @Before
 public void setUp() throws Exception {
   collection = createEmptyCollection("marshalling");
   collection.save(new Friend("robert", "Wall Street", new Coordinate(2, 3)));
 }
Пример #20
0
  public static Result deleteBeer(String id) {

    Logger.info("Deleting beer ID: " + new ObjectId(id).toString());
    beers.remove(new ObjectId(id));
    return ok();
  }
Пример #21
0
 @BodyParser.Of(BodyParser.Json.class)
 public static Result updateBeer(String id) {
   // TODO: Fix imgId so we can updated image w/ this as well.
   beers.update(new ObjectId(id)).with(request().body().asJson().toString());
   return ok();
 }