Search in sources :

Example 1 with Article

use of models.Article in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method testGetAndPostArticleViaJson.

@Test
public void testGetAndPostArticleViaJson() {
    // /////////////////////////////////////////////////////////////////////
    // Test initial data:
    // /////////////////////////////////////////////////////////////////////
    sayNextSection("Retrieving articles for a user (Json)");
    say("Retrieving all articles of a user is a GET request to " + GET_ARTICLES_URL);
    Response response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com"))));
    ArticlesDto articlesDto = getGsonWithLongToDateParsing().fromJson(response.payload, ArticlesDto.class);
    sayAndAssertThat("We get back all 3 articles of that user", articlesDto.articles.size(), CoreMatchers.is(3));
    // /////////////////////////////////////////////////////////////////////
    // Post new article:
    // /////////////////////////////////////////////////////////////////////
    sayNextSection("Posting new article (Json)");
    say("Posting a new article is a post request to " + POST_ARTICLE_URL);
    say("Please note that you have to be authenticated in order to be allowed to post.");
    ArticleDto articleDto = new ArticleDto();
    articleDto.content = "contentcontent";
    articleDto.title = "new title new title";
    response = sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_ARTICLE_URL.replace("{username}", USER))).payload(articleDto));
    sayAndAssertThat("You have to be authenticated in order to post articles", response.httpStatus, CoreMatchers.is(403));
    doLogin();
    say("Now we are authenticated and expect the post to succeed...");
    response = sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_ARTICLE_URL.replace("{username}", USER))).contentTypeApplicationJson().payload(articleDto));
    sayAndAssertThat("After successful login we are able to post articles", response.httpStatus, CoreMatchers.is(200));
    // /////////////////////////////////////////////////////////////////////
    // Fetch articles again => assert we got a new one ...
    // /////////////////////////////////////////////////////////////////////
    say("If we now fetch the articles again we are getting a new article (the one we have posted successfully");
    response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com"))));
    articlesDto = getGsonWithLongToDateParsing().fromJson(response.payload, ArticlesDto.class);
    // one new result:
    sayAndAssertThat("We are now getting 4 articles.", articlesDto.articles.size(), CoreMatchers.is(4));
    // /////////////////////////////////////////////////////////////////////
    // Fetch single article
    // /////////////////////////////////////////////////////////////////////
    say("We can also fetch an individual article via the Json Api.");
    say("That's a GET request to: " + GET_ARTICLE_URL);
    response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLE_URL.replace("{username}", "bob@gmail.com").replace("{id}", "1"))));
    Article article = getGsonWithLongToDateParsing().fromJson(response.payload, Article.class);
    // one new result:
    sayAndAssertThat("And we got back the first article", article.id, CoreMatchers.is(1L));
}
Also used : Response(org.doctester.testbrowser.Response) Article(models.Article) ArticlesDto(models.ArticlesDto) ArticleDto(models.ArticleDto) Test(org.junit.Test)

Example 2 with Article

use of models.Article in project ninja by ninjaframework.

the class ApplicationController method index.

public Result index() {
    Article frontPost = articleDao.getFirstArticleForFrontPage();
    List<Article> olderPosts = articleDao.getOlderArticlesForFrontPage();
    Map<String, Object> map = Maps.newHashMap();
    map.put("frontArticle", frontPost);
    map.put("olderArticles", olderPosts);
    return Results.html().render("frontArticle", frontPost).render("olderArticles", olderPosts);
}
Also used : Article(models.Article)

Example 3 with Article

use of models.Article in project ninja by ninjaframework.

the class ArticleDao method getAllArticles.

@UnitOfWork
public ArticlesDto getAllArticles() {
    EntityManager entityManager = entitiyManagerProvider.get();
    TypedQuery<Article> query = entityManager.createQuery("SELECT x FROM Article x", Article.class);
    List<Article> articles = query.getResultList();
    ArticlesDto articlesDto = new ArticlesDto();
    articlesDto.articles = articles;
    return articlesDto;
}
Also used : EntityManager(javax.persistence.EntityManager) Article(models.Article) ArticlesDto(models.ArticlesDto) UnitOfWork(ninja.jpa.UnitOfWork)

Example 4 with Article

use of models.Article in project ninja by ninjaframework.

the class ArticleDao method getArticle.

@UnitOfWork
public Article getArticle(Long id) {
    EntityManager entityManager = entitiyManagerProvider.get();
    Query q = entityManager.createQuery("SELECT x FROM Article x WHERE x.id = :idParam");
    Article article = (Article) q.setParameter("idParam", id).getSingleResult();
    return article;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Article(models.Article) UnitOfWork(ninja.jpa.UnitOfWork)

Example 5 with Article

use of models.Article in project ninja by ninjaframework.

the class ArticleDao method getOlderArticlesForFrontPage.

@UnitOfWork
public List<Article> getOlderArticlesForFrontPage() {
    EntityManager entityManager = entitiyManagerProvider.get();
    Query q = entityManager.createQuery("SELECT x FROM Article x ORDER BY x.postedAt DESC");
    List<Article> articles = (List<Article>) q.setFirstResult(1).setMaxResults(10).getResultList();
    return articles;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Article(models.Article) List(java.util.List) UnitOfWork(ninja.jpa.UnitOfWork)

Aggregations

Article (models.Article)8 EntityManager (javax.persistence.EntityManager)6 Query (javax.persistence.Query)5 TypedQuery (javax.persistence.TypedQuery)4 UnitOfWork (ninja.jpa.UnitOfWork)4 Transactional (com.google.inject.persist.Transactional)2 List (java.util.List)2 ArticlesDto (models.ArticlesDto)2 User (models.User)2 ArticleDto (models.ArticleDto)1 Response (org.doctester.testbrowser.Response)1 Test (org.junit.Test)1