View Javadoc
1   /*
2    *
3    *  * Copyright (C) 2003-2015 eXo Platform SAS.
4    *  *
5    *  * This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Affero General Public License
7    *  as published by the Free Software Foundation; either version 3
8    *  of the License, or (at your option) any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, see<http://www.gnu.org/licenses/>.
17   *
18   */
19  
20  package org.exoplatform.wiki.jpa.dao;
21  
22  
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.List;
26  
27  import org.junit.Test;
28  
29  import org.exoplatform.wiki.jpa.BaseWikiJPAIntegrationTest;
30  import org.exoplatform.wiki.jpa.entity.TemplateEntity;
31  import org.exoplatform.wiki.jpa.entity.WikiEntity;
32  
33  /**
34   * Created by The eXo Platform SAS Author : eXoPlatform exo@exoplatform.com
35   * 7/31/15
36   */
37  public class TemplateDAOTest extends BaseWikiJPAIntegrationTest {
38  
39    @Test
40    public void testGetTemplateOfWikiByName() {
41      //Given
42      WikiEntity wiki = new WikiEntity();
43      wiki.setType("portal");
44      wiki.setOwner("wiki1");
45      wiki = wikiDAO.create(wiki);
46  
47      TemplateEntity template = new TemplateEntity();
48      template.setWiki(wiki);
49      template.setName("template1");
50      template.setTitle("Template 1");
51      template.setContent("Template 1 Content");
52      Date now = Calendar.getInstance().getTime();
53      template.setCreatedDate(now);
54      template.setUpdatedDate(now);
55      templateDAO.create(template);
56  
57      //When
58      TemplateEntity fetchedTemplate1 = templateDAO.getTemplateOfWikiByName("portal", "wiki1", "template1");
59      TemplateEntity fetchedTemplate2 = templateDAO.getTemplateOfWikiByName("portal", "wiki1", "template2");
60      TemplateEntity fetchedTemplate1OfWiki2 = templateDAO.getTemplateOfWikiByName("portal", "wiki2", "template1");
61  
62      //Then
63      assertEquals(1, templateDAO.findAll().size());
64      assertNotNull(fetchedTemplate1);
65      assertEquals("portal", fetchedTemplate1.getWiki().getType());
66      assertEquals("wiki1", fetchedTemplate1.getWiki().getOwner());
67      assertEquals("template1", fetchedTemplate1.getName());
68      assertEquals("Template 1", fetchedTemplate1.getTitle());
69      assertEquals("Template 1 Content", fetchedTemplate1.getContent());
70      assertNull(fetchedTemplate2);
71      assertNull(fetchedTemplate1OfWiki2);
72    }
73  
74  
75    @Test
76    public void testGetTemplatesOfWiki() {
77      //Given
78      WikiEntity wiki = new WikiEntity();
79      wiki.setType("portal");
80      wiki.setOwner("wiki1");
81      wiki = wikiDAO.create(wiki);
82  
83      Date now = Calendar.getInstance().getTime();
84  
85      TemplateEntity template1 = new TemplateEntity();
86      template1.setWiki(wiki);
87      template1.setName("template1");
88      template1.setTitle("Template 1");
89      template1.setContent("Template 1 Content");
90      template1.setCreatedDate(now);
91      template1.setUpdatedDate(now);
92      templateDAO.create(template1);
93  
94      TemplateEntity template2 = new TemplateEntity();
95      template2.setWiki(wiki);
96      template2.setName("template2");
97      template2.setTitle("Template 2");
98      template2.setContent("Template 2 Content");
99      template2.setCreatedDate(now);
100     template2.setUpdatedDate(now);
101     templateDAO.create(template2);
102 
103     //When
104     List<TemplateEntity> templatesWiki1 = templateDAO.getTemplatesOfWiki("portal", "wiki1");
105     List<TemplateEntity> templatesWiki2 = templateDAO.getTemplatesOfWiki("portal", "wiki2");
106 
107     //Then
108     assertEquals(2, templateDAO.findAll().size());
109     assertNotNull(templatesWiki1);
110     assertEquals(2, templatesWiki1.size());
111     assertNotNull(templatesWiki2);
112     assertEquals(0, templatesWiki2.size());
113   }
114 
115   @Test
116   public void testSearchTemplates() {
117     //Given
118     WikiEntity wiki = new WikiEntity();
119     wiki.setType("portal");
120     wiki.setOwner("wiki1");
121     wiki = wikiDAO.create(wiki);
122 
123     Date now = Calendar.getInstance().getTime();
124 
125     TemplateEntity template1 = new TemplateEntity();
126     template1.setWiki(wiki);
127     template1.setName("template1");
128     template1.setTitle("Template with Title 1");
129     template1.setContent("Template 1 Content");
130     template1.setCreatedDate(now);
131     template1.setUpdatedDate(now);
132     templateDAO.create(template1);
133 
134     TemplateEntity template2 = new TemplateEntity();
135     template2.setWiki(wiki);
136     template2.setName("template2");
137     template2.setTitle("Template with Title 2");
138     template2.setContent("Template 2 Content");
139     template2.setCreatedDate(now);
140     template2.setUpdatedDate(now);
141     templateDAO.create(template2);
142 
143     //When
144     List<TemplateEntity> templatesWiki1 = templateDAO.searchTemplatesByTitle("portal", "wiki1", "with Title");
145     List<TemplateEntity> templatesWiki2 = templateDAO.searchTemplatesByTitle("portal", "wiki1", "Title 1");
146     List<TemplateEntity> templatesWiki3 = templateDAO.searchTemplatesByTitle("portal", "wiki1", "No Result");
147 
148     //Then
149     assertEquals(2, templateDAO.findAll().size());
150     assertNotNull(templatesWiki1);
151     assertEquals(2, templatesWiki1.size());
152     assertNotNull(templatesWiki2);
153     assertEquals(1, templatesWiki2.size());
154     assertNotNull(templatesWiki3);
155     assertEquals(0, templatesWiki3.size());
156   }
157 
158 }