946c5dfe804abd6e8f882cecd0ec16687432b9f7
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / common / aai / tasks / AAIFlagTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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 package org.onap.so.bpmn.common.aai.tasks;
21
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Matchers.isA;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.so.bpmn.common.BuildingBlockExecution;
40 import org.onap.so.bpmn.common.InjectionHelper;
41 import org.onap.so.bpmn.common.data.TestDataSetup;
42 import org.onap.so.bpmn.infrastructure.aai.tasks.AAIFlagTasks;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
44 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
45 import org.onap.so.client.aai.AAIResourcesClient;
46 import org.onap.so.client.aai.mapper.AAIObjectMapper;
47 import org.onap.so.client.exception.BBObjectNotFoundException;
48 import org.onap.so.client.exception.ExceptionBuilder;
49 import org.onap.so.client.orchestration.AAIVnfResources;
50 import org.springframework.beans.factory.annotation.Autowired;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class AAIFlagTasksTest extends TestDataSetup {
54
55         @InjectMocks
56         private AAIFlagTasks aaiFlagTasks = new AAIFlagTasks();
57
58         @Mock
59         private AAIVnfResources aaiVnfResources;
60
61         @Mock
62         protected AAIObjectMapper MOCK_aaiObjectMapper;
63
64         @Mock
65         protected InjectionHelper MOCK_injectionHelper;
66         
67         @Mock
68         protected AAIResourcesClient MOCK_aaiResourcesClient;
69         
70
71         @Mock
72         private ExtractPojosForBB extractPojosForBB;
73
74         @Mock
75         private ExceptionBuilder exceptionUtil;
76
77         private GenericVnf genericVnf;
78
79         @Before
80         public void before() throws BBObjectNotFoundException {
81                 genericVnf = setGenericVnf();
82                 doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
83                 when(extractPojosForBB.extractByKey(any(),any(), any())).thenReturn(genericVnf);
84         }
85
86         @Test
87         public void checkVnfInMaintTestTrue() throws Exception {
88                 doThrow(new BpmnError("VNF is in maintenance in A&AI")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
89                 doReturn(false).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
90                 doReturn(true).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
91                 try {
92                         aaiFlagTasks.checkVnfInMaintFlag(execution);
93                 } catch (Exception e) {
94                         verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
95                         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), eq("VNF is in maintenance in A&AI"));
96                 }
97         }
98
99         @Test
100         public void checkVnfInMaintTestFalse() throws Exception {
101                 doReturn(false).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
102                 aaiFlagTasks.checkVnfInMaintFlag(execution);
103                 verify(exceptionUtil, times(0)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), any(int.class), any(String.class));    
104         }
105
106         @Test
107         public void checkVnfInMaintFlagExceptionTest() {
108
109                 doThrow(new BpmnError("Unknown Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
110                 doThrow(Exception.class).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
111                 try {
112                         aaiFlagTasks.checkVnfInMaintFlag(execution);
113                 } catch (Exception e) {
114                         verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
115                         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
116                 }
117                 
118         }
119
120         @Test
121         public void modifyVnfInMaintFlagTest() throws Exception {
122                 doNothing().when(aaiVnfResources).updateObjectVnf(isA(GenericVnf.class));
123                 aaiFlagTasks.modifyVnfInMaintFlag(execution, true);
124                 verify(aaiVnfResources, times(1)).updateObjectVnf(genericVnf);
125         }
126
127         @Test
128         public void modifyVnfInMaintFlagExceptionTest() {
129                 
130                 doThrow(new BpmnError("Unknown Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
131                 doThrow(Exception.class).when(aaiVnfResources).updateObjectVnf(isA(GenericVnf.class));
132                 try {
133                         aaiFlagTasks.modifyVnfInMaintFlag(execution, true);
134                 } catch (Exception e) {
135                         verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
136                         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
137                 }
138         }
139 }