1 | package fr.mael.jiwigo.service; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import junit.framework.Assert; |
---|
7 | |
---|
8 | import org.junit.Before; |
---|
9 | import org.junit.Test; |
---|
10 | |
---|
11 | import fr.mael.jiwigo.dao.impl.CategoryDaoImpl; |
---|
12 | import fr.mael.jiwigo.dao.impl.ImageDaoImpl; |
---|
13 | import fr.mael.jiwigo.om.Category; |
---|
14 | import fr.mael.jiwigo.om.Image; |
---|
15 | import fr.mael.jiwigo.service.impl.CategoryServiceImpl; |
---|
16 | import fr.mael.jiwigo.service.impl.ImageServiceImpl; |
---|
17 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
18 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
19 | import fr.mael.jiwigo.transverse.session.impl.SessionManagerImpl; |
---|
20 | |
---|
21 | public class ServicesTest { |
---|
22 | |
---|
23 | private SessionManager sessionManager; |
---|
24 | |
---|
25 | @Before |
---|
26 | public void setUp() throws JiwigoException { |
---|
27 | sessionManager = new SessionManagerImpl("mael", "motdepasse", "http://mael.piwigo.com", "Unit Test"); |
---|
28 | sessionManager.processLogin(); |
---|
29 | } |
---|
30 | |
---|
31 | @Test |
---|
32 | public void testCreer() throws Exception { |
---|
33 | Category cat = null; |
---|
34 | CategoryServiceImpl categoryService = new CategoryServiceImpl(); |
---|
35 | CategoryDaoImpl categoryDao = new CategoryDaoImpl(); |
---|
36 | categoryDao.setSessionManager(sessionManager); |
---|
37 | categoryService.setDao(categoryDao); |
---|
38 | |
---|
39 | ImageServiceImpl imageService = new ImageServiceImpl(); |
---|
40 | ImageDaoImpl imageDao = new ImageDaoImpl(); |
---|
41 | imageDao.setSessionManager(sessionManager); |
---|
42 | imageService.setDao(imageDao); |
---|
43 | //we choose category number 3 |
---|
44 | for (Category category : categoryService.list(true)) { |
---|
45 | if (category.getIdentifier().equals(3)) { |
---|
46 | cat = category; |
---|
47 | break; |
---|
48 | } |
---|
49 | } |
---|
50 | //we look for the first picture of category 3 |
---|
51 | Image image = imageService.listByCategory(cat.getIdentifier(), true).get(0); |
---|
52 | Assert.assertEquals("test de test do not delete", cat.getName()); |
---|
53 | Assert.assertEquals("20110329194915-0c0b3f36.jpg", image.getFile()); |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | @Test |
---|
58 | public void addSimpleTest() { |
---|
59 | |
---|
60 | // we prepare the service implementing add-simple |
---|
61 | ImageServiceImpl imageService = new ImageServiceImpl(); |
---|
62 | ImageDaoImpl dao = new ImageDaoImpl(); |
---|
63 | dao.setSessionManager(sessionManager); |
---|
64 | imageService.setDao(dao); |
---|
65 | |
---|
66 | //we prepare the resource to send |
---|
67 | File imageFile = new File(this.getClass().getClassLoader().getResource("piwigo_org.png").getPath()); |
---|
68 | // File imageFile = new File("piwigo_org.png"); |
---|
69 | Integer categoryTest = 98; |
---|
70 | String title = "title" + System.currentTimeMillis(); |
---|
71 | try { |
---|
72 | imageService.addSimple(imageFile, categoryTest, title); |
---|
73 | } catch (JiwigoException e) { |
---|
74 | Assert.fail("An exception was thrown while trying to send the pictures" + e.getMessage()); |
---|
75 | } |
---|
76 | |
---|
77 | //the image should be sent, let's check if piwigo added it to the category. |
---|
78 | boolean found = false; |
---|
79 | List<Image> listByCategory; |
---|
80 | try { |
---|
81 | listByCategory = imageService.listByCategory(categoryTest, true); |
---|
82 | for (Image image : listByCategory) { |
---|
83 | if (image.getName().equals(title)) { |
---|
84 | found = true; |
---|
85 | break; |
---|
86 | } |
---|
87 | } |
---|
88 | } catch (Exception e) { |
---|
89 | Assert.fail("An exception was thrown while trying to fetch the pictures from category " + categoryTest |
---|
90 | + "exception was : " + e.getMessage()); |
---|
91 | } |
---|
92 | Assert.assertTrue("The picture just sent could not be found", found); |
---|
93 | |
---|
94 | } |
---|
95 | } |
---|