View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.blog.business.portlet;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.portal.service.plugin.PluginService;
38  import fr.paris.lutece.test.LuteceTestCase;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  import java.sql.SQLException;
41  import java.util.Collection;
42  import java.util.Date;
43  import java.util.List;
44  import org.junit.Test;
45  
46  /**
47   * BlogPublicationDAO Test
48   */
49  public class BlogPublicationDAOTest extends LuteceTestCase
50  {
51  
52      private static final String PLUGIN_NAME = "blog";
53      private static final int BLOG_ID = 10000;
54      private static final int PORTLET_ID = 20000;
55      private static final int STATUS_ENABLED = 1;
56      private static final int ORDER = 1;
57      private static final int TESTCOUNT = 14;
58  
59      private static Plugin _plugin = PluginService.getPlugin( PLUGIN_NAME );
60      private static BlogPublicationDAO _dao = new BlogPublicationDAO( );
61      private static String _strDatabaseEngine;
62      private static int nTestCount = 0;
63  
64      /**
65       * {@inheritDoc }
66       * 
67       * @throws Exception
68       */
69      @Override
70      protected void setUp( ) throws Exception
71      {
72          super.setUp( );
73  
74          // Equivalent to @BeforeClass that is not compatible with LuteceTestCase
75          if ( nTestCount == 0 )
76          {
77              setUpDatabase( );
78          }
79          nTestCount++;
80      }
81  
82      /**
83       * {@inheritDoc }
84       * 
85       * @throws Exception
86       */
87      @Override
88      protected void tearDown( ) throws Exception
89      {
90          super.tearDown( );
91  
92          // Equivalent to @AfterClass that is not compatible with LuteceTestCase
93          if ( nTestCount == TESTCOUNT )
94          {
95              tearDownDatabase( );
96          }
97      }
98  
99      /**
100      * Test of insertBlogsId method, of class BlogPublicationDAO.
101      */
102     @Test
103     public void testInsertBlogsId( )
104     {
105         System.out.println( "insertBlogsId" );
106         insert( );
107     }
108 
109     /**
110      * Test of store method, of class BlogPublicationDAO.
111      */
112     @Test
113     public void testStore( )
114     {
115         System.out.println( "store" );
116         BlogPublication blogPublication = getBlogPublication( );
117         _dao.store( blogPublication, _plugin );
118     }
119 
120     /**
121      * Test of deleteBlogsId method, of class BlogPublicationDAO.
122      */
123     @Test
124     public void testDeleteBlogsId( )
125     {
126         System.out.println( "deleteBlogsId" );
127         insert( );
128         int nDocId = BLOG_ID;
129         _dao.deleteBlogsId( nDocId, _plugin );
130     }
131 
132     /**
133      * Test of deleteBlogByIdPortlet method, of class BlogPublicationDAO.
134      */
135     @Test
136     public void testDeleteBlogByIdPortlet( )
137     {
138         System.out.println( "deleteBlogByIdPortlet" );
139         insert( );
140         int nIdPortlet = PORTLET_ID;
141         _dao.deleteBlogByIdPortlet( nIdPortlet, _plugin );
142     }
143 
144     /**
145      * Test of remove method, of class BlogPublicationDAO.
146      */
147     @Test
148     public void testRemove( )
149     {
150         System.out.println( "remove" );
151         insert( );
152         int nDocId = BLOG_ID;
153         int nIdPortlet = PORTLET_ID;
154         _dao.remove( nDocId, nIdPortlet, _plugin );
155     }
156 
157     /**
158      * Test of loadBlogsId method, of class BlogPublicationDAO.
159      */
160     @Test
161     public void testLoadBlogsId( )
162     {
163         System.out.println( "loadBlogsId" );
164         insert( );
165         int nDocId = BLOG_ID;
166         List<BlogPublication> result = _dao.loadBlogsId( nDocId, _plugin );
167         assertTrue( !result.isEmpty( ) );
168     }
169 
170     /**
171      * Test of loadBlogsByPortlet method, of class BlogPublicationDAO.
172      */
173     @Test
174     public void testLoadBlogsByPortlet( )
175     {
176         System.out.println( "loadBlogsByPortlet" );
177         insert( );
178         int nIdPortlet = PORTLET_ID;
179         List<BlogPublication> result = _dao.loadBlogsByPortlet( nIdPortlet, _plugin );
180         assertTrue( !result.isEmpty( ) );
181     }
182 
183     /**
184      * Test of loadBlogsByPortletAndPublicationDate method, of class BlogPublicationDAO.
185      */
186     @Test
187     public void testLoadBlogsByPortletAndPublicationDate( )
188     {
189         System.out.println( "loadBlogsByPortletAndPublicationDate" );
190         insert( );
191         int nIdPortlet = PORTLET_ID;
192         Date datePublishing = new Date( getTime( -2 ) );
193         Date dateEndPublishing = new Date( getTime( 2 ) );
194         List<BlogPublication> result = _dao.loadBlogsByPortletAndPublicationDate( nIdPortlet, datePublishing, dateEndPublishing, _plugin );
195         assertTrue( !result.isEmpty( ) );
196     }
197 
198     /**
199      * Test of loadBlogsPublication method, of class BlogPublicationDAO.
200      */
201     @Test
202     public void testLoadBlogsPublication( )
203     {
204         System.out.println( "loadBlogsPublication" );
205         insert( );
206         int nPortletId = PORTLET_ID;
207         int nDocId = BLOG_ID;
208         BlogPublication result = _dao.loadBlogsPublication( nPortletId, nDocId, _plugin );
209         assertNotNull( result );
210     }
211 
212     /**
213      * Test of loadAllBlogsPublication method, of class BlogPublicationDAO.
214      */
215     @Test
216     public void testLoadAllBlogsPublication( )
217     {
218         System.out.println( "loadAllBlogsPublication" );
219         insert( );
220         List<BlogPublication> result = _dao.loadAllBlogsPublication( _plugin );
221         assertTrue( !result.isEmpty( ) );
222     }
223 
224     /**
225      * Test of selectSinceDatePublishingAndStatus method, of class BlogPublicationDAO.
226      */
227     @Test
228     public void testSelectSinceDatePublishingAndStatus( )
229     {
230         System.out.println( "selectSinceDatePublishingAndStatus" );
231         insert( );
232         Date datePublishing = new Date( getTime( -4 ) );
233         Date dateEndPublishing = new Date( getTime( 2 ) );
234         int nStatus = STATUS_ENABLED;
235         Collection<BlogPublication> result = _dao.selectSinceDatePublishingAndStatus( datePublishing, dateEndPublishing, nStatus, _plugin );
236         assertTrue( !result.isEmpty( ) );
237     }
238 
239     /**
240      * Test of getPublishedBlogsIdsListByPortletIds method, of class BlogPublicationDAO.
241      */
242     @Test
243     public void testGetPublishedBlogsIdsListByPortletIds( )
244     {
245         System.out.println( "getPublishedBlogsIdsListByPortletIds" );
246         insert( );
247         int [ ] nPortletsIds = {
248                 PORTLET_ID
249         };
250         Date datePublishing = new Date( getTime( -2 ) );
251         Date dateEndPublishing = new Date( getTime( 2 ) );
252         List<Integer> result = _dao.getPublishedBlogsIdsListByPortletIds( nPortletsIds, datePublishing, dateEndPublishing, _plugin );
253         assertTrue( !result.isEmpty( ) );
254     }
255 
256     /**
257      * Test of getLastPublishedBlogsIdsListByPortletIds method, of class BlogPublicationDAO.
258      */
259     @Test
260     public void testGetLastPublishedBlogsIdsListByPortletIds( )
261     {
262         System.out.println( "getLastPublishedBlogsIdsListByPortletIds" );
263         insert( );
264         int [ ] nPortletsIds = {
265                 PORTLET_ID
266         };
267         Date dateUpdated = new Date( getTime( 0 ) );
268         List<Integer> result = _dao.getLastPublishedBlogsIdsListByPortletIds( nPortletsIds, dateUpdated, _plugin );
269         // assertTrue( !result.isEmpty() ); // the request need a join with blog post, so with this test data set the list is empty
270     }
271 
272     /**
273      * Test of countPublicationByIdBlogAndDate method, of class BlogPublicationDAO.
274      */
275     @Test
276     public void testCountPublicationByIdBlogAndDate( )
277     {
278         System.out.println( "countPublicationByIdBlogAndDate" );
279         insert( );
280         int nIdBlog = BLOG_ID;
281         Date date = new Date( );
282         int result = _dao.countPublicationByIdBlogAndDate( nIdBlog, date, _plugin );
283         assertTrue( result > 0 );
284     }
285 
286     // private utils
287 
288     /**
289      * SetUp Database : disable referential integrity
290      */
291     private void setUpDatabase( )
292     {
293         try
294         {
295             _plugin = PluginService.getPlugin( PLUGIN_NAME );
296             _strDatabaseEngine = _plugin.getConnectionService( ).getConnection( ).getMetaData( ).getDatabaseProductName( );
297 
298             System.out.println( "Database server : " + _strDatabaseEngine );
299 
300             String strMessage = "Disable referential integrity for the server : " + _strDatabaseEngine;
301             if ( _strDatabaseEngine.equalsIgnoreCase( "MySQL" ) )
302             {
303                 execute( "SET FOREIGN_KEY_CHECKS=0;", strMessage );
304             }
305             else
306                 if ( _strDatabaseEngine.contains( "HSQL" ) )
307                 {
308                     execute( "SET DATABASE REFERENTIAL INTEGRITY FALSE", strMessage );
309                 }
310         }
311         catch( SQLException ex )
312         {
313             System.out.println( ex );
314         }
315     }
316 
317     /**
318      * Tear Down Database : enable referential integrity
319      */
320     private void tearDownDatabase( ) throws Exception
321     {
322         clean( );
323 
324         String strMessage = "Enable referential integrity for the server : " + _strDatabaseEngine;
325         if ( _strDatabaseEngine.equalsIgnoreCase( "MySQL" ) )
326         {
327             execute( "SET FOREIGN_KEY_CHECKS=1;", strMessage );
328         }
329         else
330             if ( _strDatabaseEngine.contains( "HSQL" ) )
331             {
332                 execute( "SET DATABASE REFERENTIAL INTEGRITY TRUE", strMessage );
333             }
334     }
335 
336     /**
337      * Clean test data
338      */
339     private void clean( )
340     {
341         _dao.deleteBlogsId( BLOG_ID, _plugin );
342     }
343 
344     /**
345      * Insert test data
346      */
347     private void insert( )
348     {
349         clean( );
350         BlogPublication blogPublication = getBlogPublication( );
351         _dao.insertBlogsId( blogPublication, _plugin );
352 
353     }
354 
355     /**
356      * Create a publication
357      * 
358      * @return The publication
359      */
360     private BlogPublication getBlogPublication( )
361     {
362         BlogPublication blogPublication = new BlogPublication( );
363         blogPublication.setIdBlog( BLOG_ID );
364         blogPublication.setIdPortlet( PORTLET_ID );
365         blogPublication.setStatus( STATUS_ENABLED );
366         blogPublication.setBlogOrder( ORDER );
367         blogPublication.setDateBeginPublishing( new java.sql.Date( getTime( -3 ) ) );
368         blogPublication.setDateEndPublishing( new java.sql.Date( getTime( 3 ) ) );
369 
370         return blogPublication;
371     }
372 
373     /**
374      * Give a time corresponding to a date of today with a delta in hour
375      * 
376      * @param lDeltaHour
377      *            The delta
378      * @return The time
379      */
380     private long getTime( long lDeltaHour )
381     {
382         return new Date( ).getTime( ) + ( lDeltaHour * 36000000L );
383     }
384 
385     /**
386      * Execute a SQL statement
387      * 
388      * @param strStatement
389      *            The statement
390      * @param strMessage
391      *            The message for the output
392      */
393     private void execute( String strStatement, String strMessage )
394     {
395         try ( DAOUtil dao = new DAOUtil( strStatement ) )
396         {
397             dao.executeUpdate( );
398             System.out.println( strMessage );
399         }
400 
401     }
402 }