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