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));
}
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);
}
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;
}
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;
}
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;
}
Aggregations