Advertisement
JFLORES99

Controller

Dec 27th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.controller;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.lang.reflect.Modifier;
  14. import java.sql.Timestamp;
  15. import java.util.ArrayList;
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Properties;
  19. import lapr.project.model.Bicycle;
  20. import lapr.project.model.Journey;
  21. import lapr.project.model.Location;
  22. import lapr.project.model.Route;
  23. import lapr.project.model.User;
  24. import org.junit.After;
  25. import org.junit.AfterClass;
  26. import org.junit.Before;
  27. import org.junit.BeforeClass;
  28. import static org.junit.Assert.*;
  29. import static org.junit.jupiter.api.Assertions.assertEquals;
  30. import org.junit.jupiter.api.Test;
  31.  
  32. /**
  33. *
  34. * @author morei
  35. */
  36. public class SendEmailControllerTest {
  37.  
  38. List<Route> routes = new ArrayList<>();
  39. Route r1 = new Route(new Location(1, 0, 0, 0), 1, new Timestamp(10), 1.0f);
  40. Route r2 = new Route(new Location(1, 0, 0, 0), 1, new Timestamp(10), 1.0f);
  41.  
  42. public SendEmailControllerTest() {
  43. }
  44.  
  45. @BeforeClass
  46. public static void setUpClass() {
  47.  
  48. }
  49.  
  50. @AfterClass
  51. public static void tearDownClass() {
  52. }
  53.  
  54. @Before
  55. public void setUp() throws FileNotFoundException, IOException {
  56.  
  57. }
  58.  
  59. @After
  60. public void tearDown() {
  61. }
  62.  
  63. @Test
  64. public void testConstructorIsPrivate() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  65. Constructor<SendEmailController> constructor = SendEmailController.class.getDeclaredConstructor();
  66. assertEquals(true, Modifier.isPrivate(constructor.getModifiers()));
  67. constructor.setAccessible(true);
  68. constructor.newInstance();
  69. }
  70.  
  71. /**
  72. * Test of send method, of class SendEmailController.
  73. *
  74. * @throws java.lang.Exception
  75. */
  76. @Test
  77. public void testSend() throws Exception {
  78. System.out.println("send");
  79. Properties props1 = new Properties();
  80. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  81. User u = new User("adminbikeshare@isep.ipp.pt", "12345678912", 180.4, 85.6, 0);
  82. Bicycle b = new Bicycle(1, 1, "y", 12, null);
  83. routes.add(r1);
  84. routes.add(r2);
  85. Journey journey = new Journey(1, u, new Timestamp(2), b, 12, new Date(12), routes);
  86. boolean send = SendEmailController.send(props1, journey);
  87. assertEquals(true, send);
  88. }
  89.  
  90. @Test
  91. public void testSendNull() throws Exception {
  92. System.out.println("send");
  93. Properties props1 = new Properties();
  94. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  95. User u = new User("", "12345678912", 180.4, 85.6, 0);
  96. Bicycle b = new Bicycle(1, 1, "y", 12, null);
  97. routes.add(r1);
  98. routes.add(r2);
  99. Journey journey = new Journey(1, u, new Timestamp(2), b, 12, new Date(12), routes);
  100. boolean send = SendEmailController.send(props1, journey);
  101. assertEquals(false, send);
  102. }
  103.  
  104. /**
  105. * Test of getHost method, of class SendEmailController.
  106. */
  107. @Test
  108. public void testGetHost() throws FileNotFoundException, IOException {
  109. System.out.println("getHost");
  110. Properties props1 = new Properties();
  111. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  112. String expResult = "smtp.gmail.com";
  113. String result = SendEmailController.getHost(props1);
  114. assertEquals(expResult, result);
  115. }
  116.  
  117. /**
  118. * Test of getFrom method, of class SendEmailController.
  119. */
  120. @Test
  121. public void testGetFrom() throws FileNotFoundException, IOException {
  122. System.out.println("getFrom");
  123. Properties props1 = new Properties();
  124. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  125. String expResult = "bikesharingg44@gmail.com";
  126. String result = SendEmailController.getFrom(props1);
  127. assertEquals(expResult, result);
  128. }
  129.  
  130. /**
  131. * Test of getPass method, of class SendEmailController.
  132. */
  133. @Test
  134. public void testGetPass() throws FileNotFoundException, IOException {
  135. System.out.println("getPass");
  136. Properties props1 = new Properties();
  137. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  138. String expResult = "12345678=a";
  139. String result = SendEmailController.getPass(props1);
  140. assertEquals(expResult, result);
  141. }
  142.  
  143. /**
  144. * Test of getSubject method, of class SendEmailController.
  145. */
  146. @Test
  147. public void testGetSubject() throws FileNotFoundException, IOException {
  148. System.out.println("getSubject");
  149. Properties props1 = new Properties();
  150. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  151. String expResult = "Spend hours at journey";
  152. String result = SendEmailController.getSubject(props1);
  153. assertEquals(expResult, result);
  154. }
  155.  
  156. /**
  157. * Test of getMessageText method, of class SendEmailController.
  158. *
  159. * @throws java.io.FileNotFoundException
  160. */
  161. @Test
  162. public void testGetMessageText() throws FileNotFoundException, IOException {
  163. System.out.println("getMessageText");
  164. Properties props1 = new Properties();
  165. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  166. String expResult = "Number of spend hours";
  167. String result = SendEmailController.getMessageText(props1);
  168. assertEquals(expResult, result);
  169. }
  170.  
  171. /**
  172. * Test of getProperties method, of class SendEmailController.
  173. *
  174. * @throws java.io.FileNotFoundException
  175. */
  176. @Test
  177. public void testGetProperties() throws FileNotFoundException, IOException {
  178. System.out.println("getProperties");
  179. Properties props1 = new Properties();
  180. props1.load(new FileInputStream("src/main/resources/mail.properties"));
  181. String host = "smtp.gmail.com";
  182. String from = "bikesharingg44@gmail.com";
  183. String pass = "12345678=a";
  184. Properties expResult = System.getProperties();
  185. expResult.put("mail.smtp.starttls.enable", "true");
  186. expResult.put("mail.smtp.host", host);
  187. expResult.put("mail.smtp.user", from);
  188. expResult.put("mail.smtp.password", pass);
  189. expResult.put("mail.smtp.port", "587");
  190. expResult.put("mail.smtp.auth", "true");
  191. Properties result = SendEmailController.getProperties(host, from, pass);
  192. assertEquals(expResult, result);
  193. }
  194.  
  195. @Test
  196. public void testGetTo() {
  197. System.out.println("getMessageText");
  198. User u = new User("adminbikeshare@isep.ipp.pt", "12345678912", 180.4, 85.6, 0);
  199. Bicycle b = new Bicycle(1, 1, "y", 12, null);
  200. routes.add(r1);
  201. routes.add(r2);
  202. Journey journey = new Journey(1, u, new Timestamp(2), b, 12, new Date(12), routes);
  203. String exp = "adminbikeshare@isep.ipp.pt";
  204. assertEquals(exp, SendEmailController.getTo(journey));
  205. }
  206.  
  207. /**
  208. * Test of getHours method, of class SendEmailController.
  209. */
  210. @org.junit.Test
  211. public void testGetHours() {
  212. System.out.println("getHours");
  213. User u = new User("adminbikeshare@isep.ipp.pt", "12345678912", 180.4, 85.6, 0);
  214. Bicycle b = new Bicycle(1, 1, "y", 12, null);
  215. routes.add(r1);
  216. routes.add(r2);
  217. Journey journey = new Journey(1, u, new Timestamp(2), b, 12, new Date(12), routes);
  218. int expResult = 1;
  219. int result = SendEmailController.getHours(journey);
  220. assertEquals(expResult, result);
  221. }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement