Ejemplo n.º 1
0
  @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();
  }
Ejemplo n.º 2
0
  @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);
  }
Ejemplo n.º 3
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() + "\"}"));
  }
Ejemplo n.º 4
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);
    }
  }
Ejemplo n.º 5
0
  @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();
  }
Ejemplo n.º 6
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);
  }
Ejemplo n.º 7
0
 @Before
 public void setUp() throws Exception {
   collection = createEmptyCollection("marshalling");
   collection.save(new Friend("robert", "Wall Street", new Coordinate(2, 3)));
 }