PageRenderTime 33ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/alea_bpm_prototype/src/test/java/ch/bbv/alea/bpm/TestAleaGameCheck.java

https://github.com/PoliM/playground
Java | 228 lines | 162 code | 45 blank | 21 comment | 6 complexity | 944c699ad501fbc0be153443233da395 MD5 | raw file
  1. package ch.bbv.alea.bpm;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNotNull;
  4. import static org.junit.Assert.assertTrue;
  5. import static org.mockito.Matchers.anyLong;
  6. import static org.mockito.Mockito.never;
  7. import static org.mockito.Mockito.verify;
  8. import static org.mockito.Mockito.when;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import org.drools.KnowledgeBase;
  12. import org.drools.builder.KnowledgeBuilder;
  13. import org.drools.builder.KnowledgeBuilderFactory;
  14. import org.drools.builder.ResourceType;
  15. import org.drools.event.DefaultProcessEventListener;
  16. import org.drools.event.process.ProcessNodeTriggeredEvent;
  17. import org.drools.event.process.ProcessVariableChangedEvent;
  18. import org.drools.io.ResourceFactory;
  19. import org.drools.runtime.StatefulKnowledgeSession;
  20. import org.drools.runtime.process.ProcessInstance;
  21. import org.drools.runtime.process.WorkItem;
  22. import org.jbpm.workflow.instance.WorkflowProcessInstance;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.mockito.Mock;
  26. import org.mockito.MockitoAnnotations;
  27. import com.google.inject.AbstractModule;
  28. import com.google.inject.Guice;
  29. public class TestAleaGameCheck {
  30. @Mock
  31. private WorkflowService workflowService;
  32. private StatefulKnowledgeSession ksession;
  33. private TestWorkItemHandler workItemHandler;
  34. @Before
  35. public void setUp() throws Exception {
  36. MockitoAnnotations.initMocks(this);
  37. WorkflowServiceFactory.setInjector(Guice.createInjector(new AbstractModule() {
  38. @Override
  39. protected void configure() {
  40. bind(WorkflowService.class).toInstance(workflowService);
  41. }
  42. }));
  43. KnowledgeBase kbase = createKnowledgeBase();
  44. ksession = createKnowledgeSession(kbase);
  45. ksession.addEventListener(new DefaultProcessEventListener() {
  46. @Override
  47. public void beforeVariableChanged(ProcessVariableChangedEvent event) {
  48. System.out.println(event);
  49. }
  50. @Override
  51. public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
  52. System.out.print(event);
  53. System.out.println(event.getNodeInstance().getNodeName());
  54. }
  55. });
  56. workItemHandler = new TestWorkItemHandler();
  57. ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
  58. }
  59. @Test
  60. public void whenBadGameThenSendReject() throws Exception {
  61. // Arrange
  62. Map<String, Object> params = createProcessParameters(12345);
  63. when(workflowService.checkUploadedGame(anyLong())).thenReturn(false);
  64. // Act
  65. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  66. // Assert
  67. verify(workflowService).checkUploadedGame(12345);
  68. verify(workflowService).sendBadUploadEMail(12345);
  69. verify(workflowService, never()).publishGame(anyLong());
  70. verify(workflowService, never()).sendGamePublishedEMail(anyLong());
  71. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  72. }
  73. @Test
  74. public void whenVipThenDirectPublish() throws Exception {
  75. // Arrange
  76. Map<String, Object> params = createProcessParameters(12345);
  77. when(workflowService.checkUploadedGame(12345)).thenReturn(true);
  78. when(workflowService.checkCustomerVip(12345)).thenReturn(true);
  79. // Act
  80. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  81. // Assert
  82. verify(workflowService).checkUploadedGame(12345);
  83. verify(workflowService).publishGame(12345);
  84. verify(workflowService).sendGamePublishedEMail(12345);
  85. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  86. }
  87. @Test
  88. public void whenNonVipAndGoodGameThenPublish() throws Exception {
  89. // Arrange
  90. Map<String, Object> params = createProcessParameters(12345);
  91. when(workflowService.checkUploadedGame(12345)).thenReturn(true);
  92. when(workflowService.checkCustomerVip(12345)).thenReturn(false);
  93. when(workflowService.checkGameUploadState(12345)).thenReturn(GameUploadState.OK);
  94. // Act
  95. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  96. // Assert
  97. verify(workflowService).checkUploadedGame(12345);
  98. verify(workflowService).checkGameUploadState(12345);
  99. verify(workflowService).publishGame(12345);
  100. verify(workflowService).sendGamePublishedEMail(12345);
  101. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  102. }
  103. @Test
  104. public void whenNonVipAndBadGameThenReject() throws Exception {
  105. // Arrange
  106. Map<String, Object> params = createProcessParameters(12345);
  107. when(workflowService.checkUploadedGame(12345)).thenReturn(true);
  108. when(workflowService.checkCustomerVip(12345)).thenReturn(false);
  109. when(workflowService.checkGameUploadState(12345)).thenReturn(GameUploadState.REJECT);
  110. // Act
  111. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  112. // Assert
  113. verify(workflowService).checkUploadedGame(12345);
  114. verify(workflowService, never()).publishGame(anyLong());
  115. verify(workflowService, never()).sendGamePublishedEMail(anyLong());
  116. verify(workflowService).sendGameRejectedEMail(12345, "Code contains bad API calls");
  117. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  118. }
  119. @Test
  120. public void whenNonVipAndManualGoodGameThenPublish() throws Exception {
  121. // Arrange
  122. Map<String, Object> params = createProcessParameters(12345);
  123. when(workflowService.checkUploadedGame(12345)).thenReturn(true);
  124. when(workflowService.checkCustomerVip(12345)).thenReturn(false);
  125. when(workflowService.checkGameUploadState(12345)).thenReturn(GameUploadState.MANUAL);
  126. // Act
  127. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  128. // Human Task step
  129. validateAndCompleteHumanTask_ManualGameCheck(Boolean.FALSE, "");
  130. // Assert
  131. verify(workflowService).checkUploadedGame(12345);
  132. verify(workflowService).publishGame(12345);
  133. verify(workflowService).sendGamePublishedEMail(12345);
  134. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  135. }
  136. @Test
  137. public void whenNonVipAndManualBadGameThenReject() throws Exception {
  138. // Arrange
  139. Map<String, Object> params = createProcessParameters(12345);
  140. when(workflowService.checkUploadedGame(12345)).thenReturn(true);
  141. when(workflowService.checkCustomerVip(12345)).thenReturn(false);
  142. when(workflowService.checkGameUploadState(12345)).thenReturn(GameUploadState.MANUAL);
  143. // Act
  144. WorkflowProcessInstance processInstance = startProcess(ksession, params);
  145. // Human Task step
  146. validateAndCompleteHumanTask_ManualGameCheck(Boolean.TRUE, "Contains commercials");
  147. // Assert
  148. verify(workflowService).checkUploadedGame(12345);
  149. verify(workflowService, never()).publishGame(anyLong());
  150. verify(workflowService, never()).sendGamePublishedEMail(anyLong());
  151. verify(workflowService).sendGameRejectedEMail(12345, "Contains commercials");
  152. assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
  153. }
  154. private void validateAndCompleteHumanTask_ManualGameCheck(Boolean reject, String reason) {
  155. WorkItem workItem = workItemHandler.getWorkItem();
  156. assertNotNull(workItem);
  157. assertEquals("GameValidators", workItem.getParameter("GroupId"));
  158. assertEquals(Long.valueOf(12345), workItem.getParameter("gameId"));
  159. // Complete step
  160. Map<String, Object> taskResult = new HashMap<String, Object>();
  161. taskResult.put("rejected", reject);
  162. taskResult.put("reason", reason);
  163. ksession.getWorkItemManager().completeWorkItem(workItem.getId(), taskResult);
  164. }
  165. private WorkflowProcessInstance startProcess(StatefulKnowledgeSession ksession, Map<String, Object> params) {
  166. System.out.println("Starting process with parameters: " + params);
  167. return (WorkflowProcessInstance) ksession.startProcess("ch.bbv.alea.bpm.uploadValidation", params);
  168. }
  169. private Map<String, Object> createProcessParameters(int gameId) {
  170. Map<String, Object> params = new HashMap<String, Object>();
  171. params.put("gameId", Long.valueOf(gameId));
  172. return params;
  173. }
  174. private KnowledgeBase createKnowledgeBase() throws Exception {
  175. System.out.println("Loading process... ");
  176. KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
  177. kbuilder.add(ResourceFactory.newClassPathResource("alea.bpmn"), ResourceType.BPMN2);
  178. return kbuilder.newKnowledgeBase();
  179. }
  180. private StatefulKnowledgeSession createKnowledgeSession(KnowledgeBase kbase) {
  181. return kbase.newStatefulKnowledgeSession();
  182. }
  183. }