bff193ffddfa3b233b62e854bfb3754b84de168d
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / WorkflowContextHolderTest.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
4  * ================================================================================ 
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
6  * ================================================================================ 
7  * Licensed under the Apache License, Version 2.0 (the "License"); 
8  * you may not use this file except in compliance with the License. 
9  * You may obtain a copy of the License at 
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0 
12  * 
13  * Unless required by applicable law or agreed to in writing, software 
14  * distributed under the License is distributed on an "AS IS" BASIS, 
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
16  * See the License for the specific language governing permissions and 
17  * limitations under the License. 
18  * ============LICENSE_END========================================================= 
19  */ 
20
21 package org.openecomp.mso.bpmn.common;
22
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
27 import java.util.UUID;
28
29 import javax.ws.rs.core.Response;
30
31 import org.jboss.resteasy.spi.AsynchronousResponse;
32 import org.junit.Assert;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowCallbackResponse;
36 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowContext;
37 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowContextHolder;
38 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
39
40 public class WorkflowContextHolderTest {
41
42         private WorkflowContext createContext(AsynchronousResponse asyncResponse) {
43                 WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();
44                 String requestId = UUID.randomUUID().toString();
45                 WorkflowContext workflowContext = new WorkflowContext("testAsyncProcess",
46                         requestId, asyncResponse, 1000L);
47                 contextHolder.put(workflowContext);
48                 return workflowContext;
49         }
50
51         @Test
52         @Ignore // BROKEN TEST (previously ignored)
53         public void testContextExpiry() throws InterruptedException {
54
55                 WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();
56                 AsynchronousResponse asyncResponse = mock(AsynchronousResponse.class);
57                 WorkflowContext workflowContext = createContext(asyncResponse);
58                 String requestId = workflowContext.getRequestId();
59                 WorkflowContext context1 = contextHolder.getWorkflowContext(requestId);
60
61                 Assert.assertNotNull(context1);
62                 Assert.assertEquals(requestId, context1.getRequestId());
63                 Assert.assertEquals(workflowContext.getProcessKey(), context1.getProcessKey());
64                 Assert.assertEquals(workflowContext.getStartTime(), context1.getStartTime());
65
66                 Thread.sleep(1000);
67                 //context should not be available after a second
68                 WorkflowContext context2 = contextHolder.getWorkflowContext(requestId);
69                 Assert.assertNull(context2);
70         }
71
72         @Test
73         public void testProcessCallback() {
74                 WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance();
75                 AsynchronousResponse asyncResponse = mock(AsynchronousResponse.class);
76                 WorkflowContext workflowContext = createContext(asyncResponse);
77
78                 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
79                 callbackResponse.setMessage("Success");
80                 callbackResponse.setResponse("Successfully processed request");
81                 callbackResponse.setStatusCode(200);
82
83                 Response response = contextHolder.processCallback("testAsyncProcess",
84                         "process-instance-id", workflowContext.getRequestId(),
85                         callbackResponse);
86                 WorkflowResponse response1 = (WorkflowResponse) response.getEntity();
87                 Assert.assertNotNull(response1.getMessage());
88                 Assert.assertEquals(200,response1.getMessageCode());
89                 Assert.assertEquals("Success", response1.getMessage());
90                 Assert.assertEquals("Successfully processed request", response1.getResponse());
91                 verify(asyncResponse).setResponse(any(Response.class));
92
93                 WorkflowContext context1 = contextHolder.getWorkflowContext(workflowContext.getRequestId());
94                 Assert.assertNull(context1);
95         }
96
97 }