use of org.summerb.microservices.articles.api.dto.consuming.RenderedArticle in project summerb by skarpushin.
the class ArticleRendererImpl method buildRenderedArticleTemplate.
private RenderedArticle buildRenderedArticleTemplate(Article article) {
RenderedArticle renderedArticle = new RenderedArticle();
renderedArticle.setId(article.getId());
renderedArticle.setArticleKey(article.getArticleKey());
renderedArticle.setLang(article.getLang());
renderedArticle.setCreatedAt(article.getCreatedAt());
renderedArticle.setCreatedBy(article.getCreatedBy());
renderedArticle.setModifiedAt(article.getModifiedAt());
renderedArticle.setModifiedBy(article.getModifiedBy());
renderedArticle.setTitle(article.getTitle());
renderedArticle.setArticleGroup(article.getArticleGroup());
return renderedArticle;
}
use of org.summerb.microservices.articles.api.dto.consuming.RenderedArticle in project summerb by skarpushin.
the class ArticleRendererImpl method renderArticle.
@Override
public RenderedArticle renderArticle(String key, Locale locale) {
Preconditions.checkArgument(StringUtils.hasText(key));
Preconditions.checkArgument(locale != null);
try {
Article article = articleService.findArticleByKeyAndLocale(key, locale);
if (article == null) {
throw new RuntimeException("Article not found: " + key + ", locale: " + locale);
}
RenderedArticle renderedArticle = buildRenderedArticleTemplate(article);
ArticleRenderingContext articleRenderingContext = new ArticleRenderingContext(locale, renderedArticle, articleService, articleAbsoluteUrlBuilder);
StringTemplate annotationTemplate = stringTemplateCompiler.compile(article.getAnnotation());
renderedArticle.setAnnotation(annotationTemplate.applyTo(articleRenderingContext));
StringTemplate contentTemplate = stringTemplateCompiler.compile(article.getContent());
renderedArticle.setContent(contentTemplate.applyTo(articleRenderingContext));
return renderedArticle;
} catch (Throwable t) {
throw new RuntimeException("Failed to render article", t);
}
}
use of org.summerb.microservices.articles.api.dto.consuming.RenderedArticle in project summerb by skarpushin.
the class ArticleRendererCachingImplTest method testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange.
@Test
public void testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange() throws Exception {
ArticleRendererCachingImpl fixture = new ArticleRendererCachingImpl();
fixture.setEventBus(mock(EventBus.class));
ArticleRenderer renderer = mock(ArticleRenderer.class);
fixture.setArticleRenderer(renderer);
fixture.afterPropertiesSet();
Locale locale = new Locale("ru");
mockTest(renderer, locale);
// force cache
fixture.renderArticle("a1", locale);
fixture.renderArticle("a2", locale);
fixture.renderArticle("a3", locale);
RenderedArticle ra2v2 = new RenderedArticle();
ra2v2.setId(1L);
ra2v2.setLang("ru");
ra2v2.setTitle("t2v2");
ra2v2.setArticleKey("a2");
ra2v2.setContent("c");
ra2v2.setArticleReferences(Arrays.asList(3L));
when(renderer.renderArticle("a2", locale)).thenReturn(ra2v2);
Attachment att3 = new Attachment();
att3.setArticleId(3L);
fixture.onArticleAttachmentChanged(EntityChangedEvent.removedObject(att3));
ra2v2 = fixture.renderArticle("a2", locale);
assertEquals("t2v2", ra2v2.getTitle());
}
use of org.summerb.microservices.articles.api.dto.consuming.RenderedArticle in project summerb by skarpushin.
the class ArticleRendererCachingImplTest method mockTest.
private void mockTest(ArticleRenderer renderer, Locale locale) {
RenderedArticle ra1 = new RenderedArticle();
ra1.setId(1L);
ra1.setLang("ru");
ra1.setTitle("t1v1");
ra1.setArticleKey("a1");
ra1.setContent("c");
ra1.setArticleReferences(Arrays.asList(2L, 3L));
when(renderer.renderArticle("a1", locale)).thenReturn(ra1);
RenderedArticle ra2 = new RenderedArticle();
ra2.setId(2L);
ra2.setLang("ru");
ra2.setTitle("t2v1");
ra2.setArticleKey("a2");
ra2.setContent("c");
ra2.setArticleReferences(Arrays.asList(3L));
when(renderer.renderArticle("a2", locale)).thenReturn(ra2);
RenderedArticle ra3 = new RenderedArticle();
ra3.setId(3L);
ra3.setLang("ru");
ra3.setTitle("t3v1");
ra3.setArticleKey("a3");
ra3.setContent("c");
when(renderer.renderArticle("a3", locale)).thenReturn(ra3);
}
use of org.summerb.microservices.articles.api.dto.consuming.RenderedArticle in project summerb by skarpushin.
the class ArticleRenderingServiceImplTest method testRenderArticle_smokeTest.
@Test
public void testRenderArticle_smokeTest() throws Exception {
ArticleAbsoluteUrlBuilder articleAbsoluteUrlBuilder = new UrlBuilderTestImpl();
ArticleService articleService = Mockito.mock(ArticleService.class);
Article a1 = new Article();
a1.setId(1L);
a1.setArticleKey("a1");
a1.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
a1.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
when(articleService.findArticleByKeyAndLocale(eq("a1"), any(Locale.class))).thenReturn(a1);
Attachment att1 = new Attachment();
att1.setId(1L);
att1.setArticleId(1L);
att1.setName("screenshot1.jpg");
when(articleService.findArticleAttachments(1L)).thenReturn(new Attachment[] { att1 });
Article a2 = new Article();
a2.setId(1L);
a2.setArticleKey("a2");
a2.setTitle("Title of article 2");
a2.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
a2.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
when(articleService.findArticleByKeyAndLocale(eq("a2"), any(Locale.class))).thenReturn(a2);
ArticleRendererImpl fixture = new ArticleRendererImpl();
fixture.setArticleAbsoluteUrlBuilder(articleAbsoluteUrlBuilder);
fixture.setArticleService(articleService);
fixture.setStringTemplateCompiler(new StringTemplateCompilerlImpl());
RenderedArticle result = fixture.renderArticle("a1", Locale.ENGLISH);
assertNotNull(result);
String a2href = "<a href=\"url-article:a2\" title=\"Title of article 2\">Title of article 2</a>";
assertEquals("Article annotation. Read other article first: " + a2href, result.getAnnotation());
assertEquals("Article text. See: " + a2href + ". And now - here is the screenshot: <img class=\"article-image\" src=\"url-att:screenshot1.jpg\" alt=\"screenshot1.jpg\" />", result.getContent());
}
Aggregations