Search in sources :

Example 1 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class ArticleProcessorTestCase method getRelevantArticles.

/**
     * getRelevantArticles.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void getRelevantArticles() throws Exception {
    final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    final String articleId = article.optString(Keys.OBJECT_ID);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/article/id/" + articleId + "/relevant/articles");
    when(request.getMethod()).thenReturn("GET");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "{\"relevantArticles\""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 2 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class FeedProcessorTestCase method tagArticlesAtom.

/**
     * tagArticlesAtom.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void tagArticlesAtom() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/tag-articles-feed.do");
    when(request.getMethod()).thenReturn("GET");
    final JSONObject tag = getTagRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    when(request.getQueryString()).thenReturn("tag=" + tag.optString(Keys.OBJECT_ID));
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 3 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class UserRepositoryImplTestCase method test.

/**
     * Tests.
     * 
     * @throws Exception exception
     */
@Test
public void test() throws Exception {
    final UserRepository userRepository = getUserRepository();
    final JSONObject another = new JSONObject();
    another.put(User.USER_NAME, "test1");
    another.put(User.USER_EMAIL, "test1@gmail.com");
    another.put(User.USER_PASSWORD, "pass1");
    another.put(User.USER_URL, "http://b3log.org");
    another.put(User.USER_ROLE, Role.DEFAULT_ROLE);
    another.put(UserExt.USER_ARTICLE_COUNT, 0);
    another.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
    another.put(UserExt.USER_AVATAR, "");
    Transaction transaction = userRepository.beginTransaction();
    userRepository.add(another);
    transaction.commit();
    Assert.assertNull(userRepository.getAdmin());
    JSONObject admin = new JSONObject();
    admin.put(User.USER_NAME, "test");
    admin.put(User.USER_EMAIL, "test@gmail.com");
    admin.put(User.USER_PASSWORD, "pass");
    admin.put(User.USER_URL, "http://b3log.org");
    admin.put(User.USER_ROLE, Role.ADMIN_ROLE);
    admin.put(UserExt.USER_ARTICLE_COUNT, 0);
    admin.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
    admin.put(UserExt.USER_AVATAR, "");
    transaction = userRepository.beginTransaction();
    userRepository.add(admin);
    transaction.commit();
    Assert.assertTrue(userRepository.isAdminEmail("test@gmail.com"));
    Assert.assertFalse(userRepository.isAdminEmail("notFound@gmail.com"));
    admin = userRepository.getAdmin();
    Assert.assertNotNull(admin);
    Assert.assertEquals("test", admin.optString(User.USER_NAME));
    final JSONObject result = userRepository.get(new Query().setFilter(new PropertyFilter(User.USER_NAME, FilterOperator.EQUAL, "test1")));
    final JSONArray users = result.getJSONArray(Keys.RESULTS);
    Assert.assertEquals(users.length(), 1);
    Assert.assertEquals(users.getJSONObject(0).getString(User.USER_EMAIL), "test1@gmail.com");
    final JSONObject notFound = userRepository.getByEmail("not.found@gmail.com");
    Assert.assertNull(notFound);
    final JSONObject found = userRepository.getByEmail("test1@gmail.com");
    Assert.assertNotNull(found);
    Assert.assertEquals(found.getString(User.USER_PASSWORD), "pass1");
}
Also used : UserRepository(org.b3log.solo.repository.UserRepository) JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) Test(org.testng.annotations.Test)

Example 4 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class ArticleRepositoryImplTestCase method isPublished.

/**
     * Is Published.
     * 
     * @throws Exception exception
     */
@Test(dependsOnMethods = { "add", "getMostViewCountArticles" })
public void isPublished() throws Exception {
    final ArticleRepository articleRepository = getArticleRepository();
    final JSONArray all = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
    Assert.assertNotNull(all);
    final JSONObject article = all.getJSONObject(0);
    Assert.assertTrue(articleRepository.isPublished(article.getString(Keys.OBJECT_ID)));
    final JSONObject notPublished = articleRepository.getByPermalink("article permalink4");
    Assert.assertNotNull(notPublished);
    Assert.assertFalse(notPublished.getBoolean(Article.ARTICLE_IS_PUBLISHED));
    Assert.assertFalse(articleRepository.isPublished("not found"));
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Test(org.testng.annotations.Test)

Example 5 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class PageRepositoryImpl method getMaxOrder.

@Override
public int getMaxOrder() throws RepositoryException {
    final Query query = new Query().addSort(Page.PAGE_ORDER, SortDirection.DESCENDING).setPageCount(1);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (0 == array.length()) {
        return -1;
    }
    return array.optJSONObject(0).optInt(Page.PAGE_ORDER);
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Aggregations

Query (org.b3log.latke.repository.Query)64 JSONObject (org.json.JSONObject)63 JSONArray (org.json.JSONArray)55 PropertyFilter (org.b3log.latke.repository.PropertyFilter)35 RepositoryException (org.b3log.latke.repository.RepositoryException)15 ServiceException (org.b3log.latke.service.ServiceException)10 Test (org.testng.annotations.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 JSONException (org.json.JSONException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 URL (org.b3log.solo.model.sitemap.URL)4 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)3 Matcher (java.util.regex.Matcher)2 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2