Пример #1
0
  public static void createVerses() {
    Session session =
        ((PersistenceManagerImpl) AppConfiguration.instance().getPersistenceManager())
            .openSession();
    Transaction transaction = session.beginTransaction();
    int verseId = 0;

    for (int i = 0; i < 50; i++) {
      ChapterBean chapter = (ChapterBean) session.load(ChapterBeanImpl.class, (i + 1));

      for (byte j = 1; j <= 5; j++) {
        VerseBean verse = prepareVerse(++verseId, String.valueOf(j), chapter);
        session.saveOrUpdate(verse);
      }
    }

    transaction.commit();
  }
public class ChapterResourceConsumerTest {

  private static final int MAX_ELEMENTS_BY_QUERY = AppConfiguration.instance().maxElementsByQuery();

  public ChapterResourceConsumerTest() {
    DataHelper.instance().initializeDataHelpers();
  }

  @Test
  public void testGetChaptersAsXml() throws JAXBException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(AppHelper.getServiceUrl("chapters"));

    String content = target.request(MediaType.APPLICATION_XML).get(String.class);
    client.close();

    Unmarshaller u = JAXBContext.newInstance(Chapters.class).createUnmarshaller();
    Chapters chapters = (Chapters) u.unmarshal(new StringReader(content));

    assertEquals(new Integer(50), chapters.getSize());
    assertEquals(MAX_ELEMENTS_BY_QUERY, chapters.getChapters().size());
  }

  @Test
  public void testGetChapterAsXml() throws JAXBException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(AppHelper.getServiceUrl("chapters/1"));

    String content = target.request(MediaType.APPLICATION_XML).get(String.class);
    client.close();

    Unmarshaller u = JAXBContext.newInstance(Chapters.class).createUnmarshaller();
    Chapter chapter = (Chapter) u.unmarshal(new StringReader(content));

    assertEquals(new Integer(1), chapter.getChapterId());
    assertEquals(new Integer(1), chapter.getBookId());
    assertEquals("1", chapter.getNumber());
  }

  @Test
  public void testGetChaptersAsJson() throws JsonParseException, JsonMappingException, IOException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(AppHelper.getServiceUrl("chapters"));

    String content = target.request(MediaType.APPLICATION_JSON).get(String.class);
    client.close();

    ObjectMapper mapper = new ObjectMapper();
    Chapters chapters = mapper.readValue(content, Chapters.class);

    assertEquals(new Integer(50), chapters.getSize());
    assertEquals(MAX_ELEMENTS_BY_QUERY, chapters.getChapters().size());
  }

  @Test
  public void testGetChapterAsJson() throws JsonParseException, JsonMappingException, IOException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(AppHelper.getServiceUrl("chapters/1"));

    String content = target.request(MediaType.APPLICATION_JSON).get(String.class);
    client.close();

    ObjectMapper mapper = new ObjectMapper();
    Chapter chapter = mapper.readValue(content, Chapter.class);

    assertEquals(new Integer(1), chapter.getChapterId());
    assertEquals(new Integer(1), chapter.getBookId());
    assertEquals("1", chapter.getNumber());
  }
}