/net.violet.my-nabaztag/src/test/java/net/violet/mynabaztag/action/MySrvTwitterActionTest.java

https://github.com/nguillaumin/nabaztag-server · Java · 221 lines · 106 code · 34 blank · 81 comment · 0 complexity · 46be4a7cfe89f7a03ffcbee99858805c MD5 · raw file

  1. package net.violet.mynabaztag.action;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import javax.servlet.http.HttpSession;
  5. import net.violet.mynabaztag.form.MySrvTwitterForm;
  6. import net.violet.platform.datamodel.Application;
  7. import net.violet.platform.datamodel.User;
  8. import net.violet.platform.datamodel.VObject;
  9. import net.violet.platform.datamodel.factories.Factories;
  10. import net.violet.platform.web.ServletTestBase;
  11. import org.junit.Test;
  12. public class MySrvTwitterActionTest extends ActionTestBase {
  13. /**
  14. * Simulate the subscription of the service.
  15. *
  16. * @param myUser
  17. * the user.
  18. * @param myObject
  19. * the object.
  20. * @param login
  21. * the login of the Twitter account.
  22. * @param password
  23. * the password associated.
  24. * @return the <code>MySrvTwitterForm</code> created.s
  25. */
  26. private MySrvTwitterForm activateTwitterlService(User myUser, VObject myObject, String login, String password) {
  27. final MySrvTwitterAction myAction = new MySrvTwitterAction();
  28. final MySrvTwitterForm myForm = new MySrvTwitterForm();
  29. final HttpSession session = ActionTestBase.createUserRabbitSession(myUser, myObject);
  30. final HttpServletRequest myRequest = ServletTestBase.createRequest(session);
  31. final HttpServletResponse myResponse = ServletTestBase.createResponse();
  32. myForm.setLogin(login);
  33. myForm.setPassword(password);
  34. myForm.setTwitterLang(myUser.getLang().getId().intValue());
  35. myAction.activate(ActionTestBase.createMapping(), myForm, myRequest, myResponse);
  36. return myForm;
  37. }
  38. /**
  39. * Simulate the update of the settings of Twitter service.
  40. *
  41. * @param myUser
  42. * the user.
  43. * @param myObject
  44. * the object.
  45. * @param login
  46. * the login of the Twitter account.
  47. * @param password
  48. * the password associated.
  49. * @return the <code>MySrvTwitterForm</code> created.s
  50. */
  51. private MySrvTwitterForm updateTwitterService(User myUser, VObject myObject, String login, String password) {
  52. final MySrvTwitterAction myAction = new MySrvTwitterAction();
  53. final MySrvTwitterForm myForm = new MySrvTwitterForm();
  54. final HttpSession session = ActionTestBase.createUserRabbitSession(myUser, myObject);
  55. final HttpServletRequest myRequest = ServletTestBase.createRequest(session);
  56. final HttpServletResponse myResponse = ServletTestBase.createResponse();
  57. myForm.setLogin(login);
  58. myForm.setPassword(password);
  59. myForm.setTwitterLang(myUser.getLang().getId().intValue());
  60. myAction.update(ActionTestBase.createMapping(), myForm, myRequest, myResponse);
  61. return myForm;
  62. }
  63. /**
  64. * Simulate the termination of the Twitter service.
  65. *
  66. * @param myUser
  67. * the user.
  68. * @param myObject
  69. * the object.
  70. * @param login
  71. * the login of the Twitter account.
  72. * @param password
  73. * the password associated.
  74. * @return the <code>MySrvTwitterForm</code> created.s
  75. */
  76. private MySrvTwitterForm deleteTwitterlService(User myUser, VObject myObject) {
  77. final MySrvTwitterAction myAction = new MySrvTwitterAction();
  78. final MySrvTwitterForm myForm = new MySrvTwitterForm();
  79. final HttpSession session = ActionTestBase.createUserRabbitSession(myUser, myObject);
  80. final HttpServletRequest myRequest = ServletTestBase.createRequest(session);
  81. final HttpServletResponse myResponse = ServletTestBase.createResponse();
  82. // myForm.setLogin(login);
  83. // myForm.setPassword(password);
  84. // myForm.setTwitterLang((int)myUser.getLang().getId());
  85. myAction.delete(ActionTestBase.createMapping(), myForm, myRequest, myResponse);
  86. return myForm;
  87. }
  88. private void initFileForAnnonce() {
  89. final String filePath = "broadcast/broad/config/Twitter/Twitter-annonce.mp3";
  90. createFileById(Application.NativeApplication.TWITTER.getApplication().getProfile().getAnnounceFile().getId(), filePath);
  91. }
  92. @Test
  93. public void testEmptySessionRedirect() {
  94. ActionTestBase.testRedirectEmptySession(MySrvTwitterAction.class, MySrvTwitterForm.class, "load", ActionTestBase.FORWARD_LOGIN);
  95. ActionTestBase.testRedirectEmptySession(MySrvTwitterAction.class, MySrvTwitterForm.class, "activate", ActionTestBase.FORWARD_LOGIN);
  96. ActionTestBase.testRedirectEmptySession(MySrvTwitterAction.class, MySrvTwitterForm.class, "update", ActionTestBase.FORWARD_LOGIN);
  97. ActionTestBase.testRedirectEmptySession(MySrvTwitterAction.class, MySrvTwitterForm.class, "delete", ActionTestBase.FORWARD_LOGIN);
  98. }
  99. @Test
  100. public void testNoRabbitRedirect() {
  101. testRedirectNoRabbit(MySrvTwitterAction.class, MySrvTwitterForm.class, "load", ActionTestBase.FORWARD_LOGIN);
  102. testRedirectNoRabbit(MySrvTwitterAction.class, MySrvTwitterForm.class, "activate", ActionTestBase.FORWARD_LOGIN);
  103. testRedirectNoRabbit(MySrvTwitterAction.class, MySrvTwitterForm.class, "update", ActionTestBase.FORWARD_LOGIN);
  104. testRedirectNoRabbit(MySrvTwitterAction.class, MySrvTwitterForm.class, "delete", ActionTestBase.FORWARD_LOGIN);
  105. }
  106. // TODO : appel BD via constructeur
  107. // @Test TODO FIXME test
  108. public void checkAll() {
  109. final User inUser = getActionTestUser();
  110. // We work with the name because of the getScenarios method which
  111. // doesn't retrieve information from the database at each time
  112. final String V1Name = "ActionTestObject";
  113. final String V2Name = "ActionTestObjectV1";
  114. initFileForAnnonce();
  115. // Test pour V1
  116. updateWithoutActivate(inUser, V1Name);
  117. activateWithWrongLogin(inUser, V1Name);
  118. activateWithGoodLogin(inUser, V1Name);
  119. updateWithWrongLogin(inUser, V1Name);
  120. updateWithGoodLogin(inUser, V1Name);
  121. deleteTwitterService(inUser, V1Name);
  122. // Test pour V2
  123. updateWithoutActivate(inUser, V1Name);
  124. activateWithWrongLogin(inUser, V2Name);
  125. activateWithGoodLogin(inUser, V2Name);
  126. updateWithWrongLogin(inUser, V2Name);
  127. updateWithGoodLogin(inUser, V2Name);
  128. deleteTwitterService(inUser, V2Name);
  129. }
  130. /**
  131. * Test if the user can't subscribe to this service because of a wrong
  132. * identification
  133. *
  134. * @param inUser
  135. * @param nameObject
  136. */
  137. private void activateWithWrongLogin(User inUser, String nameObject) {
  138. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  139. activateTwitterlService(inUser, myObject, "TheAmazingVioletCorporation", "violet");
  140. }
  141. /**
  142. * Test if the user can subscribe to this service
  143. *
  144. * @param inUser
  145. * @param nameObject
  146. */
  147. private void activateWithGoodLogin(User inUser, String nameObject) {
  148. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  149. activateTwitterlService(inUser, myObject, "tnabaztag", "violet");
  150. }
  151. /**
  152. * Test if the user can't update the service settings because of a wrong
  153. * identification
  154. *
  155. * @param inUser
  156. * @param nameObject
  157. */
  158. private void updateWithWrongLogin(User inUser, String nameObject) {
  159. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  160. updateTwitterService(inUser, myObject, "TheAmazingVioletCorporation", "violet");
  161. }
  162. /**
  163. * Test if the user can't update the service settings if this one wasn't
  164. * activate before
  165. *
  166. * @param inUser
  167. * @param nameObject
  168. */
  169. private void updateWithoutActivate(User inUser, String nameObject) {
  170. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  171. updateTwitterService(inUser, myObject, "tnabaztag", "violet");
  172. }
  173. /**
  174. * Test if the user can update the service settings
  175. *
  176. * @param inUser
  177. * @param nameObject
  178. */
  179. private void updateWithGoodLogin(User inUser, String nameObject) {
  180. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  181. updateTwitterService(inUser, myObject, "tnabaztag", "violet");
  182. }
  183. private void deleteTwitterService(User inUser, String nameObject) {
  184. final VObject myObject = Factories.VOBJECT.findByName(nameObject);
  185. deleteTwitterlService(inUser, myObject);
  186. }
  187. }