1   /*
2    * Created by IntelliJ IDEA.
3    * User: owen
4    * Date: Nov 25, 2002
5    * Time: 10:44:50 AM
6    * CVS Revision: $Revision: 1.5 $
7    * Last CVS Commit: $Date: 2005/05/10 07:01:16 $
8    * Author of last CVS Commit: $Author: schang $
9    * To change this template use Options | File Templates.
10   */
11  package test.atlassian.mail.server.managers;
12  
13  import com.atlassian.core.ofbiz.CoreFactory;
14  import com.atlassian.core.ofbiz.test.AbstractOFBizTestCase;
15  import com.atlassian.core.ofbiz.test.AbstractOFBizTestCase;
16  import com.atlassian.core.ofbiz.CoreFactory;
17  import com.atlassian.mail.server.MailServer;
18  import com.atlassian.mail.server.PopMailServer;
19  import com.atlassian.mail.server.SMTPMailServer;
20  import com.atlassian.mail.server.impl.PopMailServerImpl;
21  import com.atlassian.mail.server.impl.SMTPMailServerImpl;
22  import com.atlassian.mail.server.managers.OFBizMailServerManager;
23  import org.ofbiz.core.entity.GenericValue;
24  import org.ofbiz.core.util.UtilMisc;
25  import test.mock.mail.server.MockMailServerManager;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  public class TestOFBizMailServerManager extends AbstractOFBizTestCase
31  {
32      private MockMailServerManager mmsm;
33  
34      public TestOFBizMailServerManager(String s)
35      {
36          super(s);
37      }
38  
39      protected void setUp() throws Exception
40      {
41          super.setUp();
42          mmsm = new MockMailServerManager();
43      }
44  
45      public void testCreateSMTPAndGet() throws Exception
46      {
47          MailServer localMailServer = new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows");
48          Long id = mmsm.create(localMailServer);
49          assertNotNull(id);
50          MailServer mailServer = mmsm.getMailServer(id);
51          assertEquals(localMailServer, mailServer);
52      }
53  
54      public void testUpdateSMTP() throws Exception
55      {
56          Long id = mmsm.create(new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows"));
57          MailServer mailServer = mmsm.getMailServer("Name");
58          if (OFBizMailServerManager.SERVER_TYPES[1].equals(mailServer.getType()))
59          {
60              SMTPMailServer smtp = (SMTPMailServer)mailServer;
61              smtp.setUsername(null);
62              smtp.setPassword(null);
63              smtp.setName("new name");
64              mmsm.update(smtp);
65              MailServer updatedMailServer = mmsm.getMailServer(id);
66              assertNull(updatedMailServer.getUsername());
67              assertNull(updatedMailServer.getPassword());
68              assertEquals("new name", updatedMailServer.getName());
69              assertEquals(mailServer, updatedMailServer);
70          }
71          else
72              fail ("Mail Server returned is not an SMTP server");
73      }
74  
75      public void testDeleteSMTP() throws Exception
76      {
77          Long id = mmsm.create(new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows"));
78          MailServer mailServer = mmsm.getMailServer(id);
79          mmsm.delete(mailServer.getId());
80          assertNull(mmsm.getMailServer(id));
81      }
82  
83      public void testGetMailServerGV() throws Exception
84      {
85          Map params = UtilMisc.toMap("id", new Long(1));
86          params.put("name","Name");
87          params.put("description","Description");
88          params.put("from","owen@atlassian.com");
89          params.put("prefix","[OWEN]");
90          params.put("servername","mail.atlassian.com");
91          params.put("smtpPort","25");
92          params.put("username","owen");
93          params.put("password","fellows");
94          params.put("type",OFBizMailServerManager.SERVER_TYPES[1]);
95          GenericValue gv = CoreFactory.getGenericDelegator().makeValue("MailServer", params);
96          Long id = mmsm.create(new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows"));
97          GenericValue mailServerGV = mmsm.getMailServerGV(id);
98          assertEquals(gv, mailServerGV);
99      }
100 
101     public void testConstructMailServer1()
102     {
103         MailServer oldMailServer = new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows");
104         Map params = UtilMisc.toMap("id", new Long(1));
105         params.put("name","Name");
106         params.put("description","Description");
107         params.put("from","owen@atlassian.com");
108         params.put("prefix","[OWEN]");
109         params.put("servername","mail.atlassian.com");
110         params.put("username","owen");
111         params.put("password","fellows");
112         params.put("type",OFBizMailServerManager.SERVER_TYPES[1]);
113         GenericValue gv = CoreFactory.getGenericDelegator().makeValue("MailServer", params);
114         MailServer newMailServer = mmsm.constructMailServer(gv);
115         assertEquals(oldMailServer, newMailServer);
116     }
117 
118     public void testConstructMailServer2()
119     {
120         MailServer oldMailServer = new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",true,"mail.atlassian.com",null,null);
121         Map params = UtilMisc.toMap("id", new Long(1));
122         params.put("name","Name");
123         params.put("description","Description");
124         params.put("from","owen@atlassian.com");
125         params.put("prefix","[OWEN]");
126         params.put("jndilocation","mail.atlassian.com");
127         params.put("username",null);
128         params.put("password",null);
129         params.put("type",OFBizMailServerManager.SERVER_TYPES[1]);
130         GenericValue gv = CoreFactory.getGenericDelegator().makeValue("MailServer", params);
131         MailServer newMailServer = mmsm.constructMailServer(gv);
132         assertEquals(oldMailServer, newMailServer);
133     }
134 
135     public void testConstructMailServer3()
136     {
137         Map params = UtilMisc.toMap("id", new Long(1));
138         params.put("name","Name");
139         params.put("description","Description");
140         params.put("servername","mail.atlassian.com");
141         params.put("username","owen");
142         params.put("password","fellows");
143         params.put("type",OFBizMailServerManager.SERVER_TYPES[0]);
144         GenericValue gv = CoreFactory.getGenericDelegator().makeValue("MailServer", params);
145         MailServer newMailServer = mmsm.constructMailServer(gv);
146         PopMailServer expectedResult = new PopMailServerImpl(new Long(1), "Name", "Description", "mail.atlassian.com", "owen", "fellows");
147         assertEquals(expectedResult, newMailServer);
148     }
149 
150     public void testConstructMailServer4()
151     {
152         Map params = UtilMisc.toMap("type","notype");
153         GenericValue gv = CoreFactory.getGenericDelegator().makeValue("MailServer", params);
154         MailServer newMailServer = mmsm.constructMailServer(gv);
155         assertEquals(null, newMailServer);
156     }
157 
158     public void testGetMapFromColumns1() throws Exception
159     {
160         MailServer oldMailServer = new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",false,"mail.atlassian.com","owen","fellows");
161         Map oldMap = mmsm.getMapFromColumns(oldMailServer);
162         Map params = UtilMisc.toMap("name","Name");
163         params.put("description","Description");
164         params.put("from","owen@atlassian.com");
165         params.put("prefix","[OWEN]");
166         params.put("servername","mail.atlassian.com");
167         params.put("username","owen");
168         params.put("password","fellows");
169         params.put("smtpPort","25");
170         params.put("type",OFBizMailServerManager.SERVER_TYPES[1]);
171         assertEquals(oldMap, params);
172     }
173 
174     public void testGetMapFromColumns2() throws Exception
175     {
176         MailServer oldMailServer = new SMTPMailServerImpl(new Long(1),"Name","Description","owen@atlassian.com","[OWEN]",true,"mail.atlassian.com",null,null);
177         Map oldMap = mmsm.getMapFromColumns(oldMailServer);
178         Map params = new HashMap();
179         params.put("name","Name");
180         params.put("description","Description");
181         params.put("from","owen@atlassian.com");
182         params.put("prefix","[OWEN]");
183         params.put("servername", null);
184         params.put("jndilocation", "mail.atlassian.com");
185         params.put("username",null);
186         params.put("password",null);
187         params.put("smtpPort","25");
188         params.put("type",OFBizMailServerManager.SERVER_TYPES[1]);
189         assertEquals(oldMap, params);
190     }
191 
192     public void testGetMapFromColumns3() throws Exception
193     {
194         MailServer oldMailServer = new PopMailServerImpl(new Long(1),"Name","Description","mail.atlassian.com","owen","fellows");
195         Map oldMap = mmsm.getMapFromColumns(oldMailServer);
196         Map params = UtilMisc.toMap("name","Name");
197         params.put("description","Description");
198         params.put("username","owen");
199         params.put("password","fellows");
200         params.put("type",OFBizMailServerManager.SERVER_TYPES[0]);
201         params.put("servername","mail.atlassian.com");
202         assertEquals(oldMap, params);
203     }
204 
205     public void testGetDefaultSMTPMailServer() throws Exception
206     {
207         SMTPMailServer server1 = new SMTPMailServerImpl(new Long(1), "Smtp Server Name1", "Description1", "test1@atlassian.com", "[OWEN]", false, "mail1.atlassian.com", "owen", "fellows");
208         SMTPMailServer server2 = new SMTPMailServerImpl(new Long(2), "Smtp Server Name", "Description2", "test2@atlassian.com", "[OWEN]", false, "mail2.atlassian.com", "owen", "fellows");
209         mmsm.create(server1);
210         mmsm.create(server2);
211 
212         // get it from the prefereneces-default.xml file
213         assertEquals(server2, mmsm.getDefaultSMTPMailServer());
214         assertEquals(server1, mmsm.getSmtpMailServers().get(0));
215 
216         // now get if from the first smtp server in servers
217         mmsm.delete(new Long(2));
218         assertEquals(server1, mmsm.getDefaultSMTPMailServer());
219         assertEquals(server1, mmsm.getSmtpMailServers().get(0));
220 
221         // cannot get any smtp servers, return null
222         mmsm.delete(new Long(1));
223         assertNull(mmsm.getDefaultSMTPMailServer());
224     }
225 
226     public void testGetDefaultPopMailServer() throws Exception
227     {
228         PopMailServer server1 = new PopMailServerImpl(new Long(1), "Pop Server Name1", "Description1", "mail1.atlassian.com", "ownen", "fellows");
229         PopMailServer server2 = new PopMailServerImpl(new Long(2), "Pop Server Name", "Description2", "mail2.atlassian.com", "ownen", "fellows");
230         mmsm.create(server1);
231         mmsm.create(server2);
232 
233         assertEquals(server2, mmsm.getDefaultPopMailServer());
234         assertEquals(server1, mmsm.getPopMailServers().get(0));
235 
236         mmsm.delete(new Long(2));
237         assertEquals(server1, mmsm.getDefaultPopMailServer());
238         assertEquals(server1, mmsm.getPopMailServers().get(0));
239 
240         mmsm.delete(new Long(1));
241         assertNull(mmsm.getDefaultPopMailServer());
242     }
243 }