Search in sources :

Example 11 with Query

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

the class TagQueryService method getTopTags.

/**
     * Gets top (reference count descending) tags.
     *
     * @param fetchSize the specified fetch size
     * @return for example,      <pre>
     * [
     *     {"tagTitle": "", "tagReferenceCount": int, ....},
     *     ....
     * ]
     * </pre>, returns an empty list if not found
     *
     * @throws ServiceException service exception
     */
public List<JSONObject> getTopTags(final int fetchSize) throws ServiceException {
    try {
        final Query query = new Query().setPageCount(1).setPageSize(fetchSize).addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.DESCENDING);
        final JSONObject result = tagRepository.get(query);
        final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
        return CollectionUtils.jsonArrayToList(tagArray);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets top tags failed", e);
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 12 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 13 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 14 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 15 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)

Aggregations

Query (org.b3log.latke.repository.Query)54 JSONObject (org.json.JSONObject)54 JSONArray (org.json.JSONArray)47 PropertyFilter (org.b3log.latke.repository.PropertyFilter)28 RepositoryException (org.b3log.latke.repository.RepositoryException)9 ServiceException (org.b3log.latke.service.ServiceException)9 Test (org.testng.annotations.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Date (java.util.Date)5 URL (org.b3log.solo.model.sitemap.URL)4 JSONException (org.json.JSONException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2 Filter (org.b3log.latke.repository.Filter)2 Transaction (org.b3log.latke.repository.Transaction)2