90f5a44752a5c88fbd00fdc585d783564a7eb31a
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.adapters.appc.orchestrator.client;
22
23 import static org.hamcrest.CoreMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import org.camunda.bpm.client.task.ExternalTask;
26 import org.camunda.bpm.client.task.ExternalTaskService;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.so.adapters.appc.orchestrator.client.ApplicationControllerCallback;
34 import org.onap.so.adapters.appc.orchestrator.client.ApplicationControllerSupport;
35 import org.onap.appc.client.lcm.model.Status;
36 import org.onap.appc.client.lcm.exceptions.AppcClientException;
37 import org.onap.appc.client.lcm.model.ResumeTrafficOutput;
38
39 public class ApplicationControllerCallbackTest {
40
41     @InjectMocks
42     ApplicationControllerCallback appcTaskCallback;
43
44     @Mock
45     ApplicationControllerSupport applicationControllerSupport;
46
47     @Mock
48     ExternalTask mockExternalTask;
49
50     @Mock
51     ExternalTaskService mockExternalTaskService;
52
53     @Before
54     public void setup() {
55         MockitoAnnotations.initMocks(this);
56         appcTaskCallback = new ApplicationControllerCallback(mockExternalTask, mockExternalTaskService,
57                 applicationControllerSupport);
58     }
59
60     @Test
61     public void onResponse_appcCallback_success_Test() throws Exception {
62         Status status = new Status();
63         status.setCode(400);
64         ResumeTrafficOutput response = new ResumeTrafficOutput();
65         response.setStatus(status);
66         doReturn(status).when(applicationControllerSupport).getStatusFromGenericResponse(response);
67         doReturn(true).when(applicationControllerSupport).getFinalityOf(status);
68         doReturn(StatusCategory.NORMAL).when(applicationControllerSupport).getCategoryOf(status);
69         appcTaskCallback.onResponse(response);
70         Mockito.verify(mockExternalTaskService).complete(mockExternalTask);
71     }
72
73     @Test
74     public void onResponse_appcCallback_intermediateResponse_Test() throws Exception {
75         Status status = new Status();
76         status.setCode(100);
77         ResumeTrafficOutput response = new ResumeTrafficOutput();
78         response.setStatus(status);
79         doReturn(status).when(applicationControllerSupport).getStatusFromGenericResponse(response);
80         appcTaskCallback.onResponse(response);
81         Mockito.verifyZeroInteractions(mockExternalTaskService);
82     }
83
84     @Test
85     public void onResponse_appcCallback_failure_Test() throws Exception {
86         String testFailure = "test failure";
87         Status status = new Status();
88         status.setCode(200);
89         status.setMessage(testFailure);
90         ResumeTrafficOutput response = new ResumeTrafficOutput();
91         response.setStatus(status);
92         doReturn(status).when(applicationControllerSupport).getStatusFromGenericResponse(response);
93         doReturn(true).when(applicationControllerSupport).getFinalityOf(status);
94         doReturn(StatusCategory.ERROR).when(applicationControllerSupport).getCategoryOf(status);
95         appcTaskCallback.onResponse(response);
96         Mockito.verify(mockExternalTaskService).handleBpmnError(mockExternalTask, "MSOWorkflowException", testFailure);
97     }
98
99     @Test
100     public void onException_appcCallback_failure_Test() throws Exception {
101         String testFailure = "test failure";
102         AppcClientException appcException = new AppcClientException(testFailure);
103         Status status = new Status();
104         status.setCode(200);
105         String exceptionMessage = "Exception on APPC request: " + testFailure;
106         status.setMessage(exceptionMessage);
107         doReturn(status).when(applicationControllerSupport).buildStatusFromAppcException(appcException);
108         doReturn(StatusCategory.ERROR).when(applicationControllerSupport).getCategoryOf(status);
109         appcTaskCallback.onException(appcException);
110         Mockito.verify(mockExternalTaskService).handleBpmnError(mockExternalTask, "MSOWorkflowException",
111                 exceptionMessage);
112     }
113 }