Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / MsoGroovyTest.groovy
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
21 package org.onap.so.bpmn.common.scripts
22
23 import static org.mockito.Mockito.*
24
25 import org.camunda.bpm.engine.ProcessEngineServices
26 import org.camunda.bpm.engine.RepositoryService
27 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
28 import org.camunda.bpm.engine.repository.ProcessDefinition
29 import org.junit.Rule
30 import org.junit.rules.ExpectedException
31 import org.junit.runner.RunWith
32 import org.mockito.junit.MockitoJUnitRunner
33 import org.onap.aai.domain.yang.GenericVnf
34 import org.onap.so.bpmn.mock.FileUtil
35 import org.onap.so.client.aai.AAIObjectPlurals
36 import org.onap.so.client.aai.AAIObjectType
37 import org.onap.so.client.aai.AAIResourcesClient
38 import org.onap.so.client.aai.entities.AAIResultWrapper
39 import org.onap.so.client.aai.entities.uri.AAIResourceUri
40 import org.onap.so.client.aai.entities.uri.AAIUriFactory
41 import org.onap.so.client.graphinventory.entities.uri.Depth
42 import org.onap.so.constants.Defaults
43
44 @RunWith(MockitoJUnitRunner.Silent.class)
45 abstract class MsoGroovyTest {
46
47     @Rule
48     public ExpectedException thrown = ExpectedException.none()
49
50         protected ExecutionEntity mockExecution
51         protected AAIResourcesClient client
52     protected AllottedResourceUtils allottedResourceUtils_MOCK
53         protected final String SEARCH_RESULT_AAI_WITH_RESULTDATA =
54                         FileUtil.readResourceFile("__files/aai/searchResults.json")
55         protected static final String CLOUD_OWNER = Defaults.CLOUD_OWNER.toString();
56
57         protected void init(String procName){
58                 mockExecution = setupMock(procName)
59                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
60                 client = mock(AAIResourcesClient.class)
61         }
62
63         protected ExecutionEntity setupMock(String procName) {
64                 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
65                 when(mockProcessDefinition.getKey()).thenReturn(procName)
66                 
67                 RepositoryService mockRepositoryService = mock(RepositoryService.class)
68                 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
69                 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn(procName)
70                 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
71                 
72                 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
73                 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
74                 
75                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
76                 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
77                 
78                 return mockExecution
79         }
80         
81         protected ExecutionEntity setupMockWithPrefix(String procName, String prefix) {
82                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
83
84                 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
85
86                 ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class)
87                 RepositoryService repositoryService = mock(RepositoryService.class)
88                 ProcessDefinition processDefinition = mock(ProcessDefinition.class)
89
90                 when(mockExecution.getProcessEngineServices()).thenReturn(processEngineServices)
91                 when(processEngineServices.getRepositoryService()).thenReturn(repositoryService)
92                 when(repositoryService.getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(processDefinition)
93                 when(processDefinition.getKey()).thenReturn(procName)
94                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
95                 return mockExecution
96         }
97         
98     protected <T> Optional<T> getAAIObjectFromJson(Class<T> clazz , String file){
99         String json = FileUtil.readResourceFile(file)
100         AAIResultWrapper resultWrapper = new AAIResultWrapper(json)
101         return resultWrapper.asBean(clazz)
102     }
103
104     protected Optional<GenericVnf> mockAAIGenericVnf(String vnfId){
105         return mockAAIGenericVnf(vnfId,"__files/aai/GenericVnf.json")
106     }
107         
108     protected Optional<GenericVnf> mockAAIGenericVnf(String vnfId,String file){
109         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
110         AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
111         Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,file);
112         when(client.get(GenericVnf.class, resourceUri)).thenReturn(genericVnf)
113         when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(genericVnf)
114         return genericVnf
115     }
116
117     protected Optional<GenericVnf> mockAAIGenericVnfByName(String vnfName){
118         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName)
119         AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName).depth(Depth.ONE)
120         Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnf.json");
121         when(client.get(GenericVnf.class, resourceUri)).thenReturn(genericVnf)
122         when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(genericVnf)
123         return genericVnf
124     }
125
126     protected void mockAAIGenericVnfNotFound(String vnfId){
127         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
128         AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE)
129         when(client.get(GenericVnf.class, resourceUri)).thenReturn(Optional.empty())
130         when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(Optional.empty())
131     }
132
133     protected void mockAAIGenericVnfByNameNotFound(String vnfName){
134         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName)
135         AAIResourceUri resourceUriDepthOne = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName).depth(Depth.ONE)
136         when(client.get(GenericVnf.class, resourceUri)).thenReturn(Optional.empty())
137         when(client.get(GenericVnf.class, resourceUriDepthOne)).thenReturn(Optional.empty())
138     }
139
140     protected AAIResultWrapper mockVolumeGroupWrapper(String region, String volumeGroupId, String file){
141         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, region,volumeGroupId)
142         String json = FileUtil.readResourceFile(file)
143         AAIResultWrapper resultWrapper = new AAIResultWrapper(json)
144         when(client.get(resourceUri)).thenReturn(resultWrapper)
145         return resultWrapper
146     }
147
148     void initAR(String procName){
149         init(procName)
150         allottedResourceUtils_MOCK = spy(new AllottedResourceUtils(mock(AbstractServiceTaskProcessor.class)))
151         when(allottedResourceUtils_MOCK.getAAIClient()).thenReturn(client)
152     }
153 }