Search in sources :

Example 1 with ArticlesDto

use of models.ArticlesDto 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 ArticlesDto

use of models.ArticlesDto in project ninja by ninjaframework.

the class ApiControllerTest method testGetAndPostArticleViaJson.

@Test
public void testGetAndPostArticleViaJson() throws Exception {
    // /////////////////////////////////////////////////////////////////////
    // Test initial data:
    // /////////////////////////////////////////////////////////////////////
    String response = ninjaTestBrowser.makeJsonRequest(getServerAddress() + "api/bob@gmail.com/articles.json");
    System.out.println("response: " + response);
    ArticlesDto articlesDto = getGsonWithLongToDateParsing().fromJson(response, ArticlesDto.class);
    assertEquals(3, articlesDto.articles.size());
    // /////////////////////////////////////////////////////////////////////
    // Post new article:
    // /////////////////////////////////////////////////////////////////////
    ArticleDto articleDto = new ArticleDto();
    articleDto.content = "contentcontent";
    articleDto.title = "new title new title";
    response = ninjaTestBrowser.postJson(getServerAddress() + "api/bob@gmail.com/article.json", articleDto);
    assertTrue(response.contains("Error. Forbidden."));
    doLogin();
    response = ninjaTestBrowser.postJson(getServerAddress() + "api/bob@gmail.com/article.json", articleDto);
    assertFalse(response.contains("Error. Forbidden."));
    // /////////////////////////////////////////////////////////////////////
    // Fetch articles again => assert we got a new one ...
    // /////////////////////////////////////////////////////////////////////
    response = ninjaTestBrowser.makeJsonRequest(getServerAddress() + "api/bob@gmail.com/articles.json");
    articlesDto = getGsonWithLongToDateParsing().fromJson(response, ArticlesDto.class);
    // one new result:
    assertEquals(4, articlesDto.articles.size());
}
Also used : ArticlesDto(models.ArticlesDto) ArticleDto(models.ArticleDto) Test(org.junit.Test) NinjaTest(ninja.NinjaTest)

Example 3 with ArticlesDto

use of models.ArticlesDto 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 ArticlesDto

use of models.ArticlesDto in project ninja by ninjaframework.

the class ApiControllerDocTest method testGetAndPostArticleViaJson.

@Test
public void testGetAndPostArticleViaJson() throws Exception {
    // /////////////////////////////////////////////////////////////////////
    // 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);
    ApiResponse apiResponse = makeGetRequest(buildUri(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com")));
    ArticlesDto articlesDto = getGsonWithLongToDateParsing().fromJson(apiResponse.payload, ArticlesDto.class);
    assertEqualsAndSay(3, articlesDto.articles.size(), "We get back all 3 articles of that user");
    // /////////////////////////////////////////////////////////////////////
    // 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";
    apiResponse = makePostRequest(buildUri(POST_ARTICLE_URL.replace("{username}", USER)), articleDto);
    assertEqualsAndSay(403, apiResponse.httpStatus, "You have to be authenticated in order to post articles");
    doLogin();
    say("Now we are authenticated and expect the post to succeed...");
    apiResponse = makePostRequest(buildUri(POST_ARTICLE_URL.replace("{username}", USER)), articleDto);
    assertEqualsAndSay(200, apiResponse.httpStatus, "After successful login we are able to post articles");
    // /////////////////////////////////////////////////////////////////////
    // 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");
    apiResponse = makeGetRequest(buildUri(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com")));
    articlesDto = getGsonWithLongToDateParsing().fromJson(apiResponse.payload, ArticlesDto.class);
    // one new result:
    assertEqualsAndSay(4, articlesDto.articles.size(), "We are now getting 4 articles.");
}
Also used : ApiResponse(de.devbliss.apitester.ApiResponse) ArticlesDto(models.ArticlesDto) ArticleDto(models.ArticleDto) NinjaApiDocTest(ninja.NinjaApiDocTest) Test(org.junit.Test)

Example 5 with ArticlesDto

use of models.ArticlesDto in project ninja by ninjaframework.

the class ApiControllerTest method testGetAndPostArticleViaXml.

@Test
public void testGetAndPostArticleViaXml() throws Exception {
    // /////////////////////////////////////////////////////////////////////
    // Test initial data:
    // /////////////////////////////////////////////////////////////////////
    String response = ninjaTestBrowser.makeXmlRequest(getServerAddress() + "api/bob@gmail.com/articles.xml");
    System.out.println("response xml: " + response);
    JacksonXmlModule module = new JacksonXmlModule();
    // and then configure, for example:
    module.setDefaultUseWrapper(false);
    XmlMapper xmlMapper = new XmlMapper(module);
    ArticlesDto articlesDto = xmlMapper.readValue(response, ArticlesDto.class);
    assertEquals(3, articlesDto.articles.size());
    // /////////////////////////////////////////////////////////////////////
    // Post new article:
    // /////////////////////////////////////////////////////////////////////
    ArticleDto articleDto = new ArticleDto();
    articleDto.content = "contentcontent";
    articleDto.title = "new title new title";
    response = ninjaTestBrowser.postXml(getServerAddress() + "api/bob@gmail.com/article.xml", articleDto);
    assertTrue(response.contains("Error. Forbidden."));
    doLogin();
    response = ninjaTestBrowser.postXml(getServerAddress() + "api/bob@gmail.com/article.xml", articleDto);
    assertFalse(response.contains("Error. Forbidden."));
    // /////////////////////////////////////////////////////////////////////
    // Fetch articles again => assert we got a new one ...
    // /////////////////////////////////////////////////////////////////////
    response = ninjaTestBrowser.makeXmlRequest(getServerAddress() + "api/bob@gmail.com/articles.xml");
    articlesDto = xmlMapper.readValue(response, ArticlesDto.class);
    // one new result:
    assertEquals(4, articlesDto.articles.size());
}
Also used : JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) ArticlesDto(models.ArticlesDto) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) ArticleDto(models.ArticleDto) Test(org.junit.Test) NinjaTest(ninja.NinjaTest)

Aggregations

ArticlesDto (models.ArticlesDto)5 ArticleDto (models.ArticleDto)4 Test (org.junit.Test)4 Article (models.Article)2 NinjaTest (ninja.NinjaTest)2 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 ApiResponse (de.devbliss.apitester.ApiResponse)1 EntityManager (javax.persistence.EntityManager)1 NinjaApiDocTest (ninja.NinjaApiDocTest)1 UnitOfWork (ninja.jpa.UnitOfWork)1 Response (org.doctester.testbrowser.Response)1