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