PageRenderTime 3978ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ServiceTest.java

https://github.com/xblaster/wadManager
Java | 200 lines | 139 code | 56 blank | 5 comment | 0 complexity | aeb348fe2f5adbbe315ef6fb22f3ccb5 MD5 | raw file
  1. import injection.WadGuiceModule;
  2. import java.math.BigDecimal;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.List;
  6. import models.BankAccount;
  7. import models.Operation;
  8. import models.Tag;
  9. import models.User;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import play.test.UnitTest;
  13. import services.BankAccountService;
  14. import services.TagService;
  15. import services.UserService;
  16. import com.google.inject.Guice;
  17. import com.google.inject.Injector;
  18. public class ServiceTest extends UnitTest{
  19. private static Injector injector = Guice.createInjector(new WadGuiceModule());
  20. private BankAccountService bService;
  21. private UserService uService;
  22. private TagService tService;
  23. /**
  24. * create a user on the fly
  25. * @return
  26. */
  27. private User fetchOnTheFlyCreatedUser() {
  28. User u = new User();
  29. u.firstname = "bob";
  30. u.lastname = "morane";
  31. uService.save(u);
  32. return u;
  33. }
  34. @Before
  35. public void setUp() {
  36. bService = injector.getInstance(BankAccountService.class);
  37. uService = injector.getInstance(UserService.class);
  38. tService = injector.getInstance(TagService.class);
  39. }
  40. @Test
  41. public void userCreateTest() {
  42. User u = fetchOnTheFlyCreatedUser();
  43. assertNotNull(u.getId());
  44. User u2 = uService.getById(u.getId());
  45. assertEquals(u.firstname, u2.firstname);
  46. assertEquals(u.lastname, u2.lastname);
  47. }
  48. @Test
  49. public void bankCreateTest() {
  50. User u = fetchOnTheFlyCreatedUser();
  51. BankAccount bAccount = new BankAccount();
  52. bAccount.user = u;
  53. bAccount.name = "bank1";
  54. bService.save(bAccount);
  55. BankAccount bAccount2 = new BankAccount();
  56. bAccount2.user = u;
  57. bAccount2.name = "bank2";
  58. bService.save(bAccount2);
  59. List<BankAccount> blist = uService.getAllBankAccountsForUser(u);
  60. assertEquals(2, blist.size());
  61. }
  62. @Test
  63. public void operationExistsTest() {
  64. User u = fetchOnTheFlyCreatedUser();
  65. BankAccount bAccount = new BankAccount();
  66. bAccount.user = u;
  67. bAccount.name = "bank1";
  68. bService.save(bAccount);
  69. Operation op = new Operation();
  70. op.name = "test";
  71. op.amount = 234;
  72. op.date = new Date();
  73. op.bankAccount = bAccount;
  74. bService.saveOperation(op);
  75. assertEquals(true, bService.operationExists(op));
  76. Operation op2 = new Operation();
  77. op2.name = "test";
  78. op2.amount = 7879;
  79. op2.date = new Date();
  80. op2.bankAccount = bAccount;
  81. //we don't save it
  82. assertEquals(false, bService.operationExists(op2));
  83. }
  84. @Test
  85. public void tagAddTest() {
  86. User u = fetchOnTheFlyCreatedUser();
  87. BankAccount bAccount = new BankAccount();
  88. bAccount.user = u;
  89. bAccount.name = "bankAddTest";
  90. bService.save(bAccount);
  91. Operation op = new Operation();
  92. op.name = "test";
  93. op.amount = 234;
  94. op.date = new Date();
  95. op.bankAccount = bAccount;
  96. bService.saveOperation(op);
  97. Tag t = tService.getOrCreateByName("test1");
  98. System.out.println("**** TAG ***"+t.getId());
  99. op.tags.add(t);
  100. op.save();
  101. }
  102. @Test
  103. public void amountAtTest() {
  104. User u = fetchOnTheFlyCreatedUser();
  105. BankAccount bAccount = new BankAccount();
  106. bAccount.user = u;
  107. bAccount.name = "bankAddTest";
  108. bService.save(bAccount);
  109. Calendar cal = Calendar.getInstance();
  110. cal.add(Calendar.MONTH, -1);
  111. Operation op = new Operation();
  112. op.name = "test";
  113. op.amount = 234;
  114. op.date = cal.getTime();
  115. op.bankAccount = bAccount;
  116. bService.saveOperation(op);
  117. assertEquals(BigDecimal.valueOf(234.0),bService.getAmountAt(bAccount, new Date()));
  118. cal.add(Calendar.MONTH, -2);
  119. assertEquals(BigDecimal.valueOf(0.0),bService.getAmountAt(bAccount, cal.getTime()));
  120. }
  121. @Test
  122. public void amountForTagTest() {
  123. User u = fetchOnTheFlyCreatedUser();
  124. BankAccount bAccount = new BankAccount();
  125. bAccount.user = u;
  126. bAccount.name = "bankAddTest";
  127. bService.save(bAccount);
  128. Calendar cal = Calendar.getInstance();
  129. cal.add(Calendar.MONTH, -1);
  130. Operation op = new Operation();
  131. op.name = "test";
  132. op.amount = 2348;
  133. op.date = cal.getTime();
  134. op.bankAccount = bAccount;
  135. Tag t = tService.getOrCreateByName("test1");
  136. op.tags.add(t);
  137. bService.saveOperation(op);
  138. cal.add(Calendar.MONTH, -2);
  139. assertEquals(BigDecimal.valueOf(2348.0),bService.calculateBudgetForTag(bAccount,
  140. cal.getTime(),
  141. new Date(),
  142. t));
  143. }
  144. }