b6052097390f29b1d69287d535fc45ecff4e436a
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / onap / so / bpmn / core / ResponseBuilderTest.java
1 /*
2 * ============LICENSE_START=======================================================
3  * ONAP : SO
4  * ================================================================================
5  * Copyright (C) 2018 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.onap.so.bpmn.core;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import org.camunda.bpm.engine.ProcessEngineServices;
28 import org.camunda.bpm.engine.RepositoryService;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.repository.ProcessDefinition;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class ResponseBuilderTest {
35
36         private String prefix = "PRE_";
37         private String processKey = "AnyProcessKey";
38         private String definitionId = "100";
39         private int errorCode_200 = 200;
40         private int errorCode_404 = 404;        
41         private String errorMessage = "any error message!";
42         private String errorMessageXML = "<ErrorMessage>any error message!</ErrorMessage><ErrorCode>200</ErrorCode>";   
43         private String response = "<WorkflowResponse>bad</WorkflowResponse>";
44         
45         private DelegateExecution mockExecution;
46         private ResponseBuilder responseBuilder;
47         private WorkflowException workflowException;
48         private Object obj;
49         
50         @Before
51         public void before() throws Exception {
52                 responseBuilder = new ResponseBuilder();
53                 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class);
54                 when(mockProcessDefinition.getKey()).thenReturn(processKey);
55                 RepositoryService mockRepositoryService = mock(RepositoryService.class);
56                 when(mockRepositoryService.getProcessDefinition(definitionId)).thenReturn(mockProcessDefinition);
57                 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class);
58                 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService);
59                 mockExecution = mock(DelegateExecution.class);
60                 when(mockExecution.getId()).thenReturn(definitionId);
61                 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices);
62                 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition);
63                 when(mockExecution.getVariable("prefix")).thenReturn(prefix);
64                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true");
65         }       
66         
67         @Test
68         public void buildWorkflowException_WorkflowException_2000_Test() {
69                 when(mockExecution.getVariable(prefix+"ErrorResponse")).thenReturn(errorMessage);
70                 when(mockExecution.getVariable(prefix+"ResponseCode")).thenReturn(errorCode_200);
71                 workflowException = responseBuilder.buildWorkflowException(mockExecution);
72                 assertEquals(2000, workflowException.getErrorCode());
73                 assertEquals("any error message!", workflowException.getErrorMessage());
74         }
75
76         @Test
77         public void buildWorkflowException_WorkflowException_XML_2000_Test() {
78                 when(mockExecution.getVariable(prefix+"ErrorResponse")).thenReturn(errorMessageXML);
79                 when(mockExecution.getVariable(prefix+"ResponseCode")).thenReturn(errorCode_200);
80                 workflowException = responseBuilder.buildWorkflowException(mockExecution);
81                 assertEquals(2000, workflowException.getErrorCode());
82                 assertEquals("any error message!", workflowException.getErrorMessage());
83         }       
84         
85         @Test
86         public void buildWorkflowException_WorkflowException_NULL_Test() {
87                 when(mockExecution.getVariable(prefix+"ErrorResponse")).thenReturn(null);               
88                 when(mockExecution.getVariable(prefix+"ResponseCode")).thenReturn(null);
89                 workflowException = responseBuilder.buildWorkflowException(mockExecution);
90                 assertEquals(null, workflowException);
91         }
92         
93         @Test
94         public void buildWorkflowException_Response_1002_Test() {
95                 when(mockExecution.getVariable(processKey+"Response")).thenReturn(response);            
96                 when(mockExecution.getVariable(prefix+"ResponseCode")).thenReturn(errorCode_404);
97                 workflowException = responseBuilder.buildWorkflowException(mockExecution);
98                 assertEquals(response, workflowException.getErrorMessage());
99                 assertEquals(1002, workflowException.getErrorCode());
100         }
101         
102         @Test
103         public void buildWorkflowResponse_Object_Test() {
104                 String workflowResponse = "<WorkflowResponse>good</WorkflowResponse>";
105                 when(mockExecution.getVariable("WorkflowResponse")).thenReturn(workflowResponse);
106                 obj = responseBuilder.buildWorkflowResponse(mockExecution);
107                 assertEquals(workflowResponse, obj);  
108         }       
109         
110 }