b4ec68050e367d09c4e899e5191a1b83556f51b2
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionUnitTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 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.infrastructure.workflow.tasks;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertThat;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyBoolean;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.List;
42
43 import org.camunda.bpm.engine.delegate.DelegateExecution;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.Spy;
49 import org.mockito.runners.MockitoJUnitRunner;
50 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
51 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
52 import org.onap.so.client.exception.ExceptionBuilder;
53 import org.onap.so.client.orchestration.AAIConfigurationResources;
54 import org.onap.so.db.catalog.beans.ConfigurationResource;
55 import org.onap.so.db.catalog.beans.CvnfcCustomization;
56 import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
57 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
58 import org.onap.so.db.catalog.client.CatalogDbClient;
59
60 @RunWith(MockitoJUnitRunner.class)
61 public class WorkflowActionUnitTest {
62
63         private final static String JSON_FILE_LOCATION = "src/test/resources/__files/Macro/";
64         
65         @Mock
66         private CatalogDbClient catalogDbClient;
67         @Mock
68         private BBInputSetup bbInputSetup;
69         @Mock
70         private BBInputSetupUtils bbInputSetupUtils;
71         @Mock
72         private ExceptionBuilder exceptionBuilder;
73         @Mock
74         private AAIConfigurationResources aaiConfigurationResources;
75         
76         @InjectMocks
77         @Spy
78         private WorkflowAction workflowAction;
79         
80         @Test
81         public void filterOrchFlowsHasFabricTest() {
82                 
83                 List<OrchestrationFlow> flows = createFlowList(
84                                 "DeactivateFabricConfigurationBB",
85                                 "flow x",
86                                 "flow y",
87                                 "ActivateFabricConfigurationBB",
88                                 "flow z");
89                 doReturn(Arrays.asList("yes", "yes")).when(workflowAction).traverseCatalogDbForConfiguration(any(String.class), any(String.class));
90                 
91                 List<OrchestrationFlow> result = workflowAction.filterOrchFlows(flows, WorkflowType.VFMODULE, mock(DelegateExecution.class));
92                 
93                 assertThat(result, is(flows));
94         }
95         
96         @Test
97         public void filterOrchFlowNoFabricTest() {
98                 List<OrchestrationFlow> flows = createFlowList(
99                                 "DeactivateFabricConfigurationBB",
100                                 "flow x",
101                                 "flow y",
102                                 "ActivateFabricConfigurationBB",
103                                 "flow z");
104                 doReturn(Arrays.asList()).when(workflowAction).traverseCatalogDbForConfiguration(any(String.class), any(String.class));
105                 
106                 List<OrchestrationFlow> result = workflowAction.filterOrchFlows(flows, WorkflowType.VFMODULE, mock(DelegateExecution.class));
107                 List<OrchestrationFlow> expected = createFlowList(
108                                 "flow x",
109                                 "flow y",
110                                 "flow z");
111                 
112                 assertThat(result, is(expected));
113         }
114         
115         @Test
116         public void traverseCatalogDbForConfigurationTest() {
117                 
118                 CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
119                 VnfVfmoduleCvnfcConfigurationCustomization vfModuleCustomization = new VnfVfmoduleCvnfcConfigurationCustomization();
120                 ConfigurationResource configuration = new ConfigurationResource();
121                 configuration.setToscaNodeType("FabricConfiguration");
122                 configuration.setModelUUID("my-uuid");
123                 vfModuleCustomization.setConfigurationResource(configuration);
124                 cvnfcCustomization.setVnfVfmoduleCvnfcConfigurationCustomization(Collections.singleton(vfModuleCustomization));
125                 List<CvnfcCustomization> cvnfcCustomizations = Arrays.asList(cvnfcCustomization);
126                 when(catalogDbClient.getCvnfcCustomizationByVnfCustomizationUUIDAndVfModuleCustomizationUUID(any(String.class), any(String.class)))
127                         .thenReturn(cvnfcCustomizations);
128                 
129                 List<String> results = workflowAction.traverseCatalogDbForConfiguration("myVnfCustomizationId", "myVfModuleCustomizationId");
130                 
131                 assertThat(results, is(Arrays.asList("my-uuid")));
132                 
133         }
134         
135         @Test
136         public void verifyFilterOrchInvocation() throws Exception {
137                 DelegateExecution execution = mock(DelegateExecution.class);
138                 
139                 when(execution.getVariable(eq("aLaCarte"))).thenReturn(true);
140                 when(execution.getVariable(eq("bpmnRequest"))).thenReturn(getJson("ServiceMacroAssign.json"));
141                 when(execution.getVariable(eq("requestUri"))).thenReturn("/v6/serviceInstances/123/vnfs/1234");
142                 
143                 OrchestrationFlow flow = new OrchestrationFlow();
144                 flow.setFlowName("flow x");
145                 
146                 List<OrchestrationFlow> flows = Arrays.asList(flow);
147                 doReturn(Arrays.asList(flow)).when(workflowAction).queryNorthBoundRequestCatalogDb(any(), any(), any(), anyBoolean());
148                 workflowAction.selectExecutionList(execution);
149                 
150                 verify(workflowAction, times(1)).filterOrchFlows(eq(flows), any(), any());
151                 
152                 flow = new OrchestrationFlow();
153                 flow.setFlowName("flow y");
154                 flows = Arrays.asList(flow);
155                 when(execution.getVariable(eq("aLaCarte"))).thenReturn(false);
156                 workflowAction.selectExecutionList(execution);
157                 
158                 verify(workflowAction, never()).filterOrchFlows(eq(flows), any(), any());
159
160         }
161         
162         private String getJson(String filename) throws IOException {
163                  return new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + filename)));
164         }
165         
166         private List<OrchestrationFlow> createFlowList(String... myList) {
167                 
168                 List<OrchestrationFlow> result = new ArrayList<>();
169                 for (String name : myList) {
170                         OrchestrationFlow flow = new OrchestrationFlow();
171                         flow.setFlowName(name);
172                         result.add(flow);
173                 }
174                 
175                 return result;
176                 
177         }
178 }